]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONStringIntMap.cxx
Coverity fix
[u/mrichter/AliRoot.git] / MUON / AliMUONStringIntMap.cxx
CommitLineData
d635d68e 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16// $Id$
3d1463c8 17
18//-----------------------------------------------------------------------------
d635d68e 19// Class AliMUONStringIntMap
20// ------------------------------------
21// Helper class that substitutes map <string, int>
22// which ALICE does not allow to use
d635d68e 23// Author: Ivana Hrivnacova, IPN Orsay
3d1463c8 24//-----------------------------------------------------------------------------
d635d68e 25
26#include <Riostream.h>
27#include <TObjString.h>
28
29#include "AliMUONStringIntMap.h"
30#include "AliLog.h"
31
a9aad96e 32/// \cond CLASSIMP
d635d68e 33ClassImp(AliMUONStringIntMap)
a9aad96e 34/// \endcond
d635d68e 35
36//______________________________________________________________________________
37AliMUONStringIntMap::AliMUONStringIntMap()
38 : TObject(),
39 fNofItems(0),
40 fFirstArray(100),
3d775853 41 fSecondArray(100),
42 fCurrentIndex(0)
d635d68e 43{
44/// Standard constructor
45
46 fFirstArray.SetOwner(true);
47}
48
d635d68e 49//______________________________________________________________________________
50AliMUONStringIntMap::~AliMUONStringIntMap()
51{
52/// Destructor
53
54 fFirstArray.Delete();
55}
56
d635d68e 57//
58// public methods
59//
60
61//______________________________________________________________________________
62Bool_t AliMUONStringIntMap::Add(const TString& first, Int_t second)
63{
64/// Add map element if first not yet present
abca6eb2 65
d635d68e 66 Int_t second2 = Get(first);
67 if ( second2 > 0 ) {
68 AliError(Form("%s is already present in the map", first.Data()));
69 return false;
70 }
71
72 // Resize TArrayI if needed
73 if (fSecondArray.GetSize() == fNofItems) fSecondArray.Set(2*fNofItems);
74
75 fFirstArray.Add(new TObjString(first));
76 fSecondArray.AddAt(second, fNofItems);
77 fNofItems++;
78
79 return true;
80}
81
abca6eb2 82//______________________________________________________________________________
83Bool_t AliMUONStringIntMap::Set(const TString& first, Int_t second)
84{
85 /// Set map element
86
87 Int_t index = Contains(first);
88 if ( index < 0 )
89 {
90 return Add(first,second);
91 }
92
93 fSecondArray.AddAt(second, index);
94
95 return true;
96}
97
98//______________________________________________________________________________
99Int_t
100AliMUONStringIntMap::Contains(const TString& first) const
101{
102 /// Whether this map contains the string 'first' or not
103
104 for (Int_t i=0; i<fNofItems; i++)
105 {
106 if ( ((TObjString*)fFirstArray.At(i))->GetString() == first )
107 {
108 return i;
109 }
110 }
111
112 return -1;
113}
114
d635d68e 115//______________________________________________________________________________
116Int_t AliMUONStringIntMap::Get(const TString& first) const
117{
118/// Find the element with specified key (first)
119
120 for (Int_t i=0; i<fNofItems; i++) {
121 if ( ((TObjString*)fFirstArray.At(i))->GetString() == first )
122 return fSecondArray.At(i);
123 }
124
125 return 0;
126}
127
128//______________________________________________________________________________
129Int_t AliMUONStringIntMap::GetNofItems() const
130{
131/// Return the number of elements
132
133 return fNofItems;
134}
135
136//______________________________________________________________________________
137void AliMUONStringIntMap::Clear(Option_t* /*option*/)
138{
139/// Delete the elements
140
141 fNofItems = 0;
142 fFirstArray.Delete();
143 fSecondArray.Reset();
144}
145
146//______________________________________________________________________________
147void AliMUONStringIntMap::Print(const char* /*option*/) const
148{
149/// Print the map elements
150
151 for (Int_t i=0; i<fNofItems; i++) {
152 cout << setw(4)
153 << i << " "
154 << ((TObjString*)fFirstArray.At(i))->GetString()
155 << " "
156 << setw(5)
157 << fSecondArray.At(i)
158 << endl;
159 }
160}
161
162//______________________________________________________________________________
163void AliMUONStringIntMap::Print(const TString& key, ofstream& out) const
164{
a9aad96e 165/// Print the map elements preceded by a key word
d635d68e 166
167 for (Int_t i=0; i<fNofItems; i++) {
168 out << key << " "
169 << ((TObjString*)fFirstArray.At(i))->GetString()
170 << " "
171 << setw(5)
172 << fSecondArray.At(i)
173 << endl;
174 }
175}
3d775853 176
177//______________________________________________________________________________
178Bool_t AliMUONStringIntMap::Next(TString& first, Int_t& second)
179{
180/// Iterator: next method.
181/// Returns false if the iterator reached the end.
182
183
184 if ( fCurrentIndex >= fNofItems ) return false;
185
186 TObjString* objString = (TObjString*)fFirstArray.At(fCurrentIndex);
187 first = objString->GetString();
188
189 second = fSecondArray.At(fCurrentIndex);
190
191 ++fCurrentIndex;
192
193 return true;
194}
195
196//______________________________________________________________________________
197void AliMUONStringIntMap::ResetItr()
198{
199/// Reset iterator
200
201 fCurrentIndex = 0;
202}