]>
Commit | Line | Data |
---|---|---|
5006ec94 | 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: AliMpExMap.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $ |
5006ec94 | 18 | // Category: basic |
f7006443 | 19 | // ------------------------ |
5006ec94 | 20 | // Class AliMpExMap |
21 | // ------------------------ | |
22 | // Helper class making Root persistent TExMap | |
23 | // Author:Ivana Hrivnacova; IPN Orsay | |
24 | ||
617853db | 25 | #include "AliMpExMap.h" |
2c605e66 | 26 | #include "AliMpIntPair.h" |
617853db | 27 | |
2c605e66 | 28 | #include "AliLog.h" |
5006ec94 | 29 | |
30 | #include <TClass.h> | |
5006ec94 | 31 | #include <TString.h> |
2c605e66 | 32 | #include <Riostream.h> |
5006ec94 | 33 | |
2c605e66 | 34 | #include <stdlib.h> |
5006ec94 | 35 | |
13985652 | 36 | /// \cond CLASSIMP |
37 | ClassImp(AliMpExMap) | |
38 | /// \endcond | |
39 | ||
5006ec94 | 40 | // |
41 | // static members | |
42 | // | |
43 | ||
44 | const Int_t AliMpExMap::fgkDefaultSize = 300; | |
45 | const Bool_t AliMpExMap::fgkDefaultOwnership = true; | |
46 | ||
47 | const Int_t AliMpExMap::fgkSeparator1 = 10000; | |
48 | const Int_t AliMpExMap::fgkSeparator2 = 100; | |
49 | ||
50 | const TString AliMpExMap::fgkCharacterMap | |
51 | = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; | |
52 | ||
53 | // | |
54 | // static methods | |
55 | // | |
56 | ||
57 | //______________________________________________________________________________ | |
58 | Long_t AliMpExMap::GetIndex(const AliMpIntPair& pair) | |
59 | { | |
60 | /// Convert the pair of integers to integer. | |
61 | ||
62 | if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) { | |
2c605e66 | 63 | AliFatalClass("Index out of limit."); |
5006ec94 | 64 | exit(1); |
65 | } | |
66 | ||
67 | return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1; | |
68 | } | |
69 | ||
70 | //_____________________________________________________________________________ | |
71 | Long_t AliMpExMap::GetIndex(const TString& s) | |
72 | { | |
73 | /// Convert the TString to integer. | |
74 | ||
75 | if (s.Length() > 5) { | |
2c605e66 | 76 | AliFatalClass("String too long."); |
5006ec94 | 77 | return 0; |
78 | } | |
79 | ||
80 | Long_t index = 0; | |
81 | for (Int_t i=s.Length()-1; i>=0; --i) | |
82 | index = index*fgkSeparator2 + fgkCharacterMap.First(s(i)); | |
83 | ||
84 | return index; | |
85 | } | |
86 | ||
87 | //______________________________________________________________________________ | |
88 | AliMpIntPair AliMpExMap::GetPair(Long_t index) | |
89 | { | |
90 | /// Convert the integer index to the pair of integers. | |
91 | ||
92 | return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1); | |
93 | } | |
94 | ||
95 | //_____________________________________________________________________________ | |
96 | TString AliMpExMap::GetString(Long_t index) | |
97 | { | |
98 | /// Convert the integer index to the string. | |
99 | ||
100 | TString s; | |
101 | while (index >0) { | |
102 | Char_t c = fgkCharacterMap(index%fgkSeparator2); | |
103 | s += c; | |
104 | index = index/fgkSeparator2; | |
105 | } | |
106 | return s; | |
107 | } | |
108 | ||
109 | // | |
110 | // constructors/destructor | |
111 | // | |
112 | ||
113 | //_____________________________________________________________________________ | |
114 | AliMpExMap::AliMpExMap(Bool_t /*standardConstructor*/) | |
115 | : TObject(), | |
116 | fMap(fgkDefaultSize), | |
117 | fObjects(fgkDefaultSize), | |
118 | fKeys(fgkDefaultSize) | |
119 | { | |
120 | /// Standard constructor | |
121 | ||
122 | fObjects.SetOwner(fgkDefaultOwnership); | |
123 | } | |
124 | ||
125 | //_____________________________________________________________________________ | |
126 | AliMpExMap::AliMpExMap() | |
127 | : TObject(), | |
128 | fMap(), | |
129 | fObjects(), | |
130 | fKeys() | |
131 | { | |
132 | /// Default constructor | |
133 | } | |
134 | ||
f8919723 | 135 | |
136 | //_____________________________________________________________________________ | |
137 | AliMpExMap::AliMpExMap(const AliMpExMap& rhs) | |
138 | : TObject(), | |
139 | fMap(), | |
140 | fObjects(), | |
141 | fKeys() | |
142 | ||
143 | { | |
144 | /// Copy ctor | |
145 | rhs.Copy(*this); | |
146 | } | |
147 | ||
148 | //_____________________________________________________________________________ | |
149 | AliMpExMap& | |
150 | AliMpExMap::operator=(const AliMpExMap& rhs) | |
151 | { | |
152 | /// Assignment operator | |
153 | AliMpExMap tmp(rhs); | |
154 | tmp.Copy(*this); | |
155 | return *this; | |
156 | } | |
157 | ||
158 | //_____________________________________________________________________________ | |
159 | void | |
160 | AliMpExMap::Copy(TObject& dest) const | |
161 | { | |
162 | /// Copy this to dest | |
163 | /// Copy implies that dest will become owner of its objects, whatever | |
164 | /// the ownership of (*this) is. | |
165 | ||
166 | AliDebug(1,""); | |
167 | ||
168 | TObject::Copy(dest); | |
169 | AliMpExMap& m = static_cast<AliMpExMap&>(dest); | |
170 | m.fKeys = fKeys; | |
171 | m.fMap.Delete(); | |
172 | m.fObjects.Delete(); | |
173 | ||
174 | for ( Int_t i = 0; i <= fObjects.GetLast(); ++i ) | |
175 | { | |
176 | TObject* o = fObjects.At(i)->Clone(); | |
177 | if (!o) | |
178 | { | |
179 | AliError("Object was not cloned properly ! Please investigate..."); | |
180 | } | |
181 | m.fObjects.AddLast(o); | |
182 | } | |
183 | m.FillMap(); | |
184 | m.fObjects.SetOwner(kTRUE); | |
185 | } | |
186 | ||
5006ec94 | 187 | //_____________________________________________________________________________ |
188 | AliMpExMap::~AliMpExMap() | |
189 | { | |
190 | /// Destructor | |
191 | } | |
192 | ||
193 | // | |
194 | // private methods | |
195 | // | |
196 | ||
197 | //_____________________________________________________________________________ | |
198 | void AliMpExMap::FillMap() | |
199 | { | |
200 | /// Fill transient map from the arrays of objects and keys | |
201 | ||
202 | for (Int_t i=0; i<fObjects.GetEntriesFast(); i++) | |
203 | fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i)); | |
204 | } | |
205 | ||
206 | //_____________________________________________________________________________ | |
207 | void AliMpExMap::AddKey(Long_t key) | |
208 | { | |
209 | /// Add key in array with checking size | |
210 | ||
211 | // Resize array if needed | |
212 | if (fObjects.GetEntriesFast() == fKeys.GetSize()) { | |
213 | fKeys.Set(2*fKeys.GetSize()); | |
2c605e66 | 214 | AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl; |
5006ec94 | 215 | } |
216 | ||
217 | fKeys.AddAt(key, fObjects.GetEntriesFast()); | |
218 | } | |
219 | ||
220 | // | |
221 | // public methods | |
222 | // | |
223 | ||
224 | //_____________________________________________________________________________ | |
225 | void AliMpExMap::Add(const AliMpIntPair& key, TObject* object) | |
226 | { | |
227 | /// Add object with its key to the map and arrays | |
228 | ||
229 | fMap.Add(GetIndex(key), (Long_t)object); | |
230 | AddKey(GetIndex(key)); | |
231 | fObjects.Add(object); | |
232 | } | |
233 | ||
234 | //_____________________________________________________________________________ | |
235 | void AliMpExMap::Add(const TString& key, TObject* object) | |
236 | { | |
237 | /// Add object with its key to the map and arrays | |
238 | ||
239 | fMap.Add(GetIndex(key), (Long_t)object); | |
240 | AddKey(GetIndex(key)); | |
241 | fObjects.Add(object); | |
242 | } | |
243 | ||
244 | //_____________________________________________________________________________ | |
245 | void AliMpExMap::Add(Int_t key, TObject* object) | |
246 | { | |
247 | /// Add object with its key to the map and arrays | |
248 | ||
249 | fMap.Add(key, (Long_t)object); | |
250 | AddKey(key); | |
251 | fObjects.Add(object); | |
252 | } | |
253 | ||
254 | //_____________________________________________________________________________ | |
255 | void AliMpExMap::SetSize(Int_t size) | |
256 | { | |
257 | /// Set given size to the key array | |
258 | ||
259 | // fMap.Set(size); | |
260 | // fObjects.Set(size); | |
261 | fKeys.Set(size); | |
262 | } | |
263 | ||
264 | //_____________________________________________________________________________ | |
265 | void AliMpExMap::SetOwner(Bool_t owner) | |
266 | { | |
267 | /// Set given ownership to object array | |
268 | ||
269 | fObjects.SetOwner(owner); | |
270 | } | |
271 | ||
272 | //_____________________________________________________________________________ | |
273 | Int_t AliMpExMap::GetSize() const | |
274 | { | |
6b431a48 | 275 | /// Return the map size |
5006ec94 | 276 | |
277 | return fObjects.GetEntriesFast(); | |
278 | } | |
279 | ||
280 | //_____________________________________________________________________________ | |
281 | TExMapIter AliMpExMap::GetIterator() const | |
282 | { | |
283 | /// Return TExMap iterator set to the beginning of the map | |
284 | ||
285 | return TExMapIter(&fMap); | |
286 | } | |
287 | ||
6b431a48 | 288 | //_____________________________________________________________________________ |
289 | TObject* AliMpExMap::GetObject(Int_t index) const | |
290 | { | |
291 | /// Return the object via its index in the array | |
292 | /// (This function makes possible looping over map as over an array) | |
293 | ||
294 | if ( index < 0 || index >= fObjects.GetEntriesFast() ) { | |
295 | AliErrorStream() << "Index outside limits" << endl; | |
296 | return 0; | |
297 | } | |
298 | ||
299 | return fObjects.At(index); | |
300 | } | |
301 | ||
5006ec94 | 302 | //_____________________________________________________________________________ |
617853db | 303 | TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const |
5006ec94 | 304 | { |
305 | /// Return the object associated with the given key if found, | |
306 | /// otherwise return 0 | |
307 | ||
617853db | 308 | return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key))); |
5006ec94 | 309 | } |
310 | ||
311 | //_____________________________________________________________________________ | |
312 | TObject* AliMpExMap::GetValue(const TString& key) const | |
313 | { | |
314 | /// Return the object associated with the given key if found, | |
315 | /// otherwise return 0 | |
316 | ||
617853db | 317 | return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key))); |
5006ec94 | 318 | } |
319 | ||
320 | //_____________________________________________________________________________ | |
321 | TObject* AliMpExMap::GetValue(Int_t key) const | |
322 | { | |
323 | /// Return the object associated with the given key if found, | |
324 | /// otherwise return 0 | |
325 | ||
617853db | 326 | return reinterpret_cast<TObject*>(fMap.GetValue(key)); |
5006ec94 | 327 | } |
328 | ||
329 | //_____________________________________________________________________________ | |
330 | void AliMpExMap::Streamer(TBuffer &R__b) | |
331 | { | |
78649106 | 332 | // Customized streamer \n |
333 | // After the arrays are read, fill the transient map | |
5006ec94 | 334 | |
335 | if (R__b.IsReading()) { | |
336 | AliMpExMap::Class()->ReadBuffer(R__b, this); | |
337 | FillMap(); | |
338 | } | |
339 | else { | |
340 | AliMpExMap::Class()->WriteBuffer(R__b, this); | |
341 | } | |
342 | } |