Line data Source code
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$
17 :
18 : //-----------------------------------------------------------------------------
19 : // Class AliMUONStringIntMap
20 : // ------------------------------------
21 : // Helper class that substitutes map <string, int>
22 : // which ALICE does not allow to use
23 : // Author: Ivana Hrivnacova, IPN Orsay
24 : //-----------------------------------------------------------------------------
25 :
26 : #include <Riostream.h>
27 : #include <TObjString.h>
28 :
29 : #include "AliMUONStringIntMap.h"
30 : #include "AliLog.h"
31 :
32 : using std::cout;
33 : using std::setw;
34 : using std::endl;
35 : /// \cond CLASSIMP
36 18 : ClassImp(AliMUONStringIntMap)
37 : /// \endcond
38 :
39 : //______________________________________________________________________________
40 : AliMUONStringIntMap::AliMUONStringIntMap()
41 274 : : TObject(),
42 274 : fNofItems(0),
43 274 : fFirstArray(100),
44 274 : fSecondArray(100),
45 274 : fCurrentIndex(0)
46 1370 : {
47 : /// Standard constructor
48 :
49 274 : fFirstArray.SetOwner(true);
50 548 : }
51 :
52 : //______________________________________________________________________________
53 : AliMUONStringIntMap::~AliMUONStringIntMap()
54 1620 : {
55 : /// Destructor
56 :
57 270 : fFirstArray.Delete();
58 810 : }
59 :
60 : //
61 : // public methods
62 : //
63 :
64 : //______________________________________________________________________________
65 : Bool_t AliMUONStringIntMap::Add(const TString& first, Int_t second)
66 : {
67 : /// Add map element if first not yet present
68 :
69 20644 : Int_t second2 = Get(first);
70 10322 : if ( second2 > 0 ) {
71 0 : AliError(Form("%s is already present in the map", first.Data()));
72 0 : return false;
73 : }
74 :
75 : // Resize TArrayI if needed
76 10337 : if (fSecondArray.GetSize() == fNofItems) fSecondArray.Set(2*fNofItems);
77 :
78 30966 : fFirstArray.Add(new TObjString(first));
79 10322 : fSecondArray.AddAt(second, fNofItems);
80 10322 : fNofItems++;
81 :
82 10322 : return true;
83 10322 : }
84 :
85 : //______________________________________________________________________________
86 : Bool_t AliMUONStringIntMap::Set(const TString& first, Int_t second)
87 : {
88 : /// Set map element
89 :
90 41232 : Int_t index = Contains(first);
91 20616 : if ( index < 0 )
92 : {
93 9602 : return Add(first,second);
94 : }
95 :
96 11014 : fSecondArray.AddAt(second, index);
97 :
98 11014 : return true;
99 20616 : }
100 :
101 : //______________________________________________________________________________
102 : Int_t
103 : AliMUONStringIntMap::Contains(const TString& first) const
104 : {
105 : /// Whether this map contains the string 'first' or not
106 :
107 61492250 : for (Int_t i=0; i<fNofItems; i++)
108 : {
109 30721414 : if ( ((TObjString*)fFirstArray.At(i))->String() == first )
110 : {
111 11014 : return i;
112 : }
113 : }
114 :
115 9602 : return -1;
116 20616 : }
117 :
118 : //______________________________________________________________________________
119 : Int_t AliMUONStringIntMap::Get(const TString& first) const
120 : {
121 : /// Find the element with specified key (first)
122 :
123 92322529 : for (Int_t i=0; i<fNofItems; i++) {
124 46115077 : if ( ((TObjString*)fFirstArray.At(i))->String() == first )
125 11759 : return fSecondArray.At(i);
126 : }
127 :
128 20154 : return 0;
129 31913 : }
130 :
131 : //______________________________________________________________________________
132 : Int_t AliMUONStringIntMap::GetNofItems() const
133 : {
134 : /// Return the number of elements
135 :
136 41238 : return fNofItems;
137 : }
138 :
139 : //______________________________________________________________________________
140 : void AliMUONStringIntMap::Clear(Option_t* /*option*/)
141 : {
142 : /// Delete the elements
143 :
144 0 : fNofItems = 0;
145 0 : fFirstArray.Delete();
146 0 : fSecondArray.Reset();
147 0 : }
148 :
149 : //______________________________________________________________________________
150 : void AliMUONStringIntMap::Print(const char* /*option*/) const
151 : {
152 : /// Print the map elements
153 :
154 10 : for (Int_t i=0; i<fNofItems; i++) {
155 4 : cout << setw(4)
156 2 : << i << " "
157 2 : << ((TObjString*)fFirstArray.At(i))->GetString()
158 2 : << " "
159 4 : << setw(5)
160 4 : << fSecondArray.At(i)
161 2 : << endl;
162 : }
163 2 : }
164 :
165 : //______________________________________________________________________________
166 : void AliMUONStringIntMap::Print(const TString& key, ofstream& out) const
167 : {
168 : /// Print the map elements preceded by a key word
169 :
170 0 : for (Int_t i=0; i<fNofItems; i++) {
171 0 : out << key << " "
172 0 : << ((TObjString*)fFirstArray.At(i))->GetString()
173 0 : << " "
174 0 : << setw(5)
175 0 : << fSecondArray.At(i)
176 0 : << endl;
177 : }
178 0 : }
179 :
180 : //______________________________________________________________________________
181 : Bool_t AliMUONStringIntMap::Next(TString& first, Int_t& second)
182 : {
183 : /// Iterator: next method.
184 : /// Returns false if the iterator reached the end.
185 :
186 :
187 6 : if ( fCurrentIndex >= fNofItems ) return false;
188 :
189 0 : TObjString* objString = (TObjString*)fFirstArray.At(fCurrentIndex);
190 0 : first = objString->GetString();
191 :
192 0 : second = fSecondArray.At(fCurrentIndex);
193 :
194 0 : ++fCurrentIndex;
195 :
196 : return true;
197 2 : }
198 :
199 : //______________________________________________________________________________
200 : void AliMUONStringIntMap::ResetItr()
201 : {
202 : /// Reset iterator
203 :
204 0 : fCurrentIndex = 0;
205 0 : }
|