]>
Commit | Line | Data |
---|---|---|
c623aa64 | 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$ | |
13985652 | 17 | // $MpId: AliMpStringObjMap.cxx,v 1.4 2006/05/24 13:58:29 ivana Exp $ |
f7006443 | 18 | |
3d1463c8 | 19 | //----------------------------------------------------------------------------- |
c623aa64 | 20 | // Class AliMpStringObjMap |
21 | // ------------------------------------ | |
22 | // Helper class that substitutes map <string, int> | |
23 | // which ALICE does not allow to use | |
c623aa64 | 24 | // Author: Ivana Hrivnacova, IPN Orsay |
3d1463c8 | 25 | //----------------------------------------------------------------------------- |
c623aa64 | 26 | |
c623aa64 | 27 | #include "AliMpStringObjMap.h" |
2c605e66 | 28 | |
c623aa64 | 29 | #include "AliLog.h" |
30 | ||
2c605e66 | 31 | #include <TObjString.h> |
32 | #include <Riostream.h> | |
33 | ||
13985652 | 34 | /// \cond CLASSIMP |
c623aa64 | 35 | ClassImp(AliMpStringObjMap) |
13985652 | 36 | /// \endcond |
c623aa64 | 37 | |
2a116780 | 38 | const TString AliMpStringObjMap::fgkUndefinedKey = "Undefined"; |
39 | ||
c623aa64 | 40 | //______________________________________________________________________________ |
973e9d18 | 41 | AliMpStringObjMap::AliMpStringObjMap(Bool_t isOwner) |
c623aa64 | 42 | : TObject(), |
43 | fNofItems(0), | |
44 | fFirstArray(), | |
9993e7da | 45 | fSecondArray(), |
46 | fCurrentIndex(0) | |
c623aa64 | 47 | { |
48 | /// Standard constructor | |
49 | ||
50 | fFirstArray.SetOwner(true); | |
973e9d18 | 51 | fSecondArray.SetOwner(isOwner); |
c623aa64 | 52 | } |
53 | ||
877f7c6e | 54 | //______________________________________________________________________________ |
55 | AliMpStringObjMap::AliMpStringObjMap(Bool_t /*minSize*/, Bool_t isOwner) | |
56 | : TObject(), | |
57 | fNofItems(0), | |
58 | fFirstArray(0), | |
59 | fSecondArray(0), | |
60 | fCurrentIndex(0) | |
61 | { | |
62 | /// Special constructor to create an object of minimum size. | |
63 | /// The argument minSize is used only to distinguish this ctor | |
64 | /// from the standard one, its value is not used. | |
65 | /// When setting the initial size to 0, Root will adapt it to a minimal | |
66 | /// allowed size: | |
67 | /// - TCollection::kInitCapacity for TObjArray (fObjects) | |
68 | ||
69 | fFirstArray.SetOwner(true); | |
70 | fSecondArray.SetOwner(isOwner); | |
71 | } | |
72 | ||
c623aa64 | 73 | //______________________________________________________________________________ |
74 | AliMpStringObjMap::~AliMpStringObjMap() | |
75 | { | |
76 | /// Destructor | |
77 | ||
78 | fFirstArray.Delete(); | |
79 | } | |
80 | ||
c623aa64 | 81 | // |
82 | // public methods | |
83 | // | |
84 | ||
85 | //______________________________________________________________________________ | |
86 | Bool_t AliMpStringObjMap::Add(const TString& first, TObject* second) | |
87 | { | |
88 | /// Add map element if first not yet present | |
89 | ||
90 | TObject* second2 = Get(first); | |
91 | if ( second2 ) { | |
92 | AliError(Form("%s is already present in the map", first.Data())); | |
93 | return false; | |
94 | } | |
95 | ||
96 | fFirstArray.Add(new TObjString(first)); | |
97 | fSecondArray.Add(second); | |
98 | fNofItems++; | |
99 | ||
100 | return true; | |
101 | } | |
102 | ||
103 | //______________________________________________________________________________ | |
104 | TObject* AliMpStringObjMap::Get(const TString& first) const | |
105 | { | |
106 | /// Find the element with specified key (first) | |
107 | ||
108 | for (Int_t i=0; i<fNofItems; i++) { | |
109 | if ( ((TObjString*)fFirstArray.At(i))->GetString() == first ) | |
110 | return fSecondArray.At(i); | |
111 | } | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | //______________________________________________________________________________ | |
117 | Int_t AliMpStringObjMap::GetNofItems() const | |
118 | { | |
119 | /// Return the number of elements | |
120 | ||
121 | return fNofItems; | |
122 | } | |
123 | ||
124 | //______________________________________________________________________________ | |
125 | void AliMpStringObjMap::Clear(Option_t* /*option*/) | |
126 | { | |
127 | /// Delete the elements | |
128 | ||
129 | fNofItems = 0; | |
130 | fFirstArray.Delete(); | |
131 | fSecondArray.Delete(); | |
132 | } | |
133 | ||
134 | //______________________________________________________________________________ | |
135 | void AliMpStringObjMap::Print(const char* /*option*/) const | |
136 | { | |
137 | /// Print the map elements | |
138 | ||
139 | for (Int_t i=0; i<fNofItems; i++) { | |
140 | cout << setw(4) | |
141 | << i << " " | |
142 | << ((TObjString*)fFirstArray.At(i))->GetString() | |
143 | << " " | |
144 | << setw(5) | |
145 | << fSecondArray.At(i) | |
146 | << endl; | |
147 | } | |
148 | } | |
149 | ||
150 | //______________________________________________________________________________ | |
151 | void AliMpStringObjMap::Print(const TString& key, ofstream& out) const | |
152 | { | |
f5671fc3 | 153 | /// Special printing |
c623aa64 | 154 | |
155 | for (Int_t i=0; i<fNofItems; i++) { | |
156 | out << key << " " | |
157 | << ((TObjString*)fFirstArray.At(i))->GetString() | |
158 | << " " | |
159 | << setw(5) | |
160 | << fSecondArray.At(i) | |
161 | << endl; | |
162 | } | |
163 | } | |
2a116780 | 164 | |
165 | //______________________________________________________________________________ | |
166 | void AliMpStringObjMap::First() | |
167 | { | |
168 | /// Set iterator to the first item and return its object | |
169 | ||
170 | fCurrentIndex = 0; | |
171 | } | |
172 | ||
173 | ||
174 | //______________________________________________________________________________ | |
175 | void AliMpStringObjMap::Next() | |
176 | { | |
177 | /// Set iterator to the next item and return its object | |
178 | /// Return 0 if there are no more items | |
179 | ||
180 | ++fCurrentIndex; | |
181 | } | |
182 | ||
183 | ||
184 | //______________________________________________________________________________ | |
185 | TObject* AliMpStringObjMap::CurrentItem() | |
186 | { | |
187 | /// Set iterator to the first item and return its object | |
188 | ||
189 | if ( fCurrentIndex >= fNofItems ) return 0; | |
190 | ||
191 | return fSecondArray.At(fCurrentIndex); | |
192 | } | |
193 | ||
194 | ||
195 | //______________________________________________________________________________ | |
196 | TString AliMpStringObjMap::CurrentKey() | |
197 | { | |
198 | /// Set iterator to the first item and return its object | |
199 | ||
200 | if ( fCurrentIndex >= fNofItems ) return fgkUndefinedKey; | |
201 | ||
202 | return ((TObjString*)fFirstArray.At(fCurrentIndex))->GetString(); | |
203 | } | |
204 | ||
205 | ||
206 | //______________________________________________________________________________ | |
207 | Bool_t AliMpStringObjMap::IsDone() const | |
208 | { | |
209 | /// Return true if the iterator reached the end of map | |
210 | ||
211 | return fCurrentIndex >= fNofItems; | |
212 | } | |
213 |