]>
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 |
3d1463c8 | 19 | |
20 | //----------------------------------------------------------------------------- | |
5006ec94 | 21 | // Class AliMpExMap |
22 | // ------------------------ | |
23 | // Helper class making Root persistent TExMap | |
24 | // Author:Ivana Hrivnacova; IPN Orsay | |
3d1463c8 | 25 | //----------------------------------------------------------------------------- |
5006ec94 | 26 | |
617853db | 27 | #include "AliMpExMap.h" |
630711ed | 28 | #include "AliMpExMapIterator.h" |
617853db | 29 | |
2c605e66 | 30 | #include "AliLog.h" |
5006ec94 | 31 | |
32 | #include <TClass.h> | |
5006ec94 | 33 | #include <TString.h> |
2c605e66 | 34 | #include <Riostream.h> |
5006ec94 | 35 | |
2c605e66 | 36 | #include <stdlib.h> |
5006ec94 | 37 | |
13985652 | 38 | /// \cond CLASSIMP |
39 | ClassImp(AliMpExMap) | |
40 | /// \endcond | |
41 | ||
5006ec94 | 42 | // |
43 | // static members | |
44 | // | |
45 | ||
46 | const Int_t AliMpExMap::fgkDefaultSize = 300; | |
47 | const Bool_t AliMpExMap::fgkDefaultOwnership = true; | |
48 | ||
49 | const Int_t AliMpExMap::fgkSeparator1 = 10000; | |
50 | const Int_t AliMpExMap::fgkSeparator2 = 100; | |
51 | ||
52 | const TString AliMpExMap::fgkCharacterMap | |
6a1e6897 | 53 | = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-"; |
5006ec94 | 54 | |
55 | // | |
56 | // static methods | |
57 | // | |
58 | ||
5006ec94 | 59 | //_____________________________________________________________________________ |
60 | Long_t AliMpExMap::GetIndex(const TString& s) | |
61 | { | |
62 | /// Convert the TString to integer. | |
63 | ||
64 | if (s.Length() > 5) { | |
79072b9c | 65 | AliErrorClass("String too long."); |
66 | return -1; | |
5006ec94 | 67 | } |
68 | ||
69 | Long_t index = 0; | |
70 | for (Int_t i=s.Length()-1; i>=0; --i) | |
71 | index = index*fgkSeparator2 + fgkCharacterMap.First(s(i)); | |
72 | ||
73 | return index; | |
74 | } | |
75 | ||
5006ec94 | 76 | //_____________________________________________________________________________ |
77 | TString AliMpExMap::GetString(Long_t index) | |
78 | { | |
79 | /// Convert the integer index to the string. | |
80 | ||
81 | TString s; | |
82 | while (index >0) { | |
83 | Char_t c = fgkCharacterMap(index%fgkSeparator2); | |
84 | s += c; | |
85 | index = index/fgkSeparator2; | |
86 | } | |
87 | return s; | |
88 | } | |
89 | ||
90 | // | |
91 | // constructors/destructor | |
92 | // | |
93 | ||
94 | //_____________________________________________________________________________ | |
630711ed | 95 | AliMpExMap::AliMpExMap() |
5006ec94 | 96 | : TObject(), |
97 | fMap(fgkDefaultSize), | |
98 | fObjects(fgkDefaultSize), | |
99 | fKeys(fgkDefaultSize) | |
100 | { | |
630711ed | 101 | /// Default constructor |
5006ec94 | 102 | |
103 | fObjects.SetOwner(fgkDefaultOwnership); | |
104 | } | |
105 | ||
106 | //_____________________________________________________________________________ | |
630711ed | 107 | AliMpExMap::AliMpExMap(TRootIOCtor*) |
5006ec94 | 108 | : TObject(), |
109 | fMap(), | |
110 | fObjects(), | |
111 | fKeys() | |
112 | { | |
630711ed | 113 | /// "Root - I/O" constructor |
5006ec94 | 114 | } |
115 | ||
f8919723 | 116 | |
117 | //_____________________________________________________________________________ | |
118 | AliMpExMap::AliMpExMap(const AliMpExMap& rhs) | |
119 | : TObject(), | |
120 | fMap(), | |
121 | fObjects(), | |
122 | fKeys() | |
123 | ||
124 | { | |
125 | /// Copy ctor | |
126 | rhs.Copy(*this); | |
127 | } | |
128 | ||
129 | //_____________________________________________________________________________ | |
130 | AliMpExMap& | |
131 | AliMpExMap::operator=(const AliMpExMap& rhs) | |
132 | { | |
133 | /// Assignment operator | |
e7796c12 | 134 | |
135 | // check assignment to self | |
136 | if (this == &rhs) return *this; | |
137 | ||
52147223 | 138 | rhs.Copy(*this); |
f8919723 | 139 | return *this; |
140 | } | |
141 | ||
5006ec94 | 142 | //_____________________________________________________________________________ |
143 | AliMpExMap::~AliMpExMap() | |
144 | { | |
145 | /// Destructor | |
146 | } | |
147 | ||
168e9c4d | 148 | // |
149 | // private static methods | |
150 | // | |
151 | ||
152 | //______________________________________________________________________________ | |
153 | Long_t AliMpExMap::GetIndex(Int_t first, Int_t second) | |
154 | { | |
155 | /// Convert the pair of integers to integer. | |
156 | ||
157 | if ( first >= 0xFFFF || second >= 0xFFFF ) | |
158 | { | |
159 | AliFatalClass("Index out of limit"); | |
160 | return 0; | |
161 | } | |
162 | ||
163 | return 1 + ( first | ( second << 16 ) ); | |
164 | ||
165 | // if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) { | |
166 | // AliFatalClass("Index out of limit."); | |
167 | // exit(1); | |
168 | // } | |
169 | // | |
170 | // return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1; | |
171 | } | |
172 | ||
173 | //______________________________________________________________________________ | |
174 | Int_t AliMpExMap::GetPairFirst(Long_t index) | |
175 | { | |
176 | /// Return first integer from index (encoded pair) | |
177 | ||
178 | return (index-1) & 0xFFFF ; | |
179 | } | |
180 | ||
181 | //______________________________________________________________________________ | |
182 | Int_t AliMpExMap::GetPairSecond(Long_t index) | |
183 | { | |
184 | /// Return second integer from index (encoded pair) | |
185 | ||
186 | return ( (index-1) & 0xFFFF0000 ) >> 16 ; | |
187 | } | |
188 | ||
5006ec94 | 189 | // |
190 | // private methods | |
191 | // | |
192 | ||
193 | //_____________________________________________________________________________ | |
194 | void AliMpExMap::FillMap() | |
195 | { | |
196 | /// Fill transient map from the arrays of objects and keys | |
197 | ||
198 | for (Int_t i=0; i<fObjects.GetEntriesFast(); i++) | |
199 | fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i)); | |
200 | } | |
201 | ||
202 | //_____________________________________________________________________________ | |
203 | void AliMpExMap::AddKey(Long_t key) | |
204 | { | |
205 | /// Add key in array with checking size | |
206 | ||
207 | // Resize array if needed | |
208 | if (fObjects.GetEntriesFast() == fKeys.GetSize()) { | |
209 | fKeys.Set(2*fKeys.GetSize()); | |
630711ed | 210 | AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl; |
5006ec94 | 211 | } |
212 | ||
213 | fKeys.AddAt(key, fObjects.GetEntriesFast()); | |
214 | } | |
215 | ||
630711ed | 216 | //_____________________________________________________________________________ |
217 | void | |
218 | AliMpExMap::Copy(TObject& dest) const | |
219 | { | |
220 | /// Copy this to dest | |
221 | /// Copy implies that dest will become owner of its objects, whatever | |
222 | /// the ownership of (*this) is. | |
223 | ||
224 | AliDebug(1,""); | |
225 | ||
226 | TObject::Copy(dest); | |
227 | AliMpExMap& m = static_cast<AliMpExMap&>(dest); | |
228 | m.fKeys = fKeys; | |
229 | m.fMap.Delete(); | |
230 | m.fObjects.Clear(); | |
231 | ||
232 | for ( Int_t i = 0; i <= fObjects.GetLast(); ++i ) | |
233 | { | |
234 | TObject* o = fObjects.At(i)->Clone(); | |
235 | if (!o) | |
236 | { | |
237 | AliError("Object was not cloned properly ! Please investigate..."); | |
238 | } | |
239 | m.fObjects.AddLast(o); | |
240 | } | |
241 | m.FillMap(); | |
242 | m.fObjects.SetOwner(kTRUE); | |
243 | } | |
244 | ||
5006ec94 | 245 | // |
246 | // public methods | |
247 | // | |
248 | ||
7332f213 | 249 | //_____________________________________________________________________________ |
df165da6 | 250 | void AliMpExMap::Clear(Option_t* option) |
7332f213 | 251 | { |
252 | /// Clear memory | |
253 | ||
254 | fMap.Delete(); | |
df165da6 | 255 | fObjects.Clear(option); |
7332f213 | 256 | fKeys.Reset(); |
257 | } | |
258 | ||
630711ed | 259 | //_____________________________________________________________________________ |
260 | void AliMpExMap::Print(Option_t* opt) const | |
261 | { | |
262 | /// Print out | |
263 | ||
264 | cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity()) | |
265 | << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries()) | |
266 | << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl; | |
267 | ||
268 | TString sopt(opt); | |
269 | sopt.ToUpper(); | |
270 | ||
271 | if ( sopt.Contains("FULL") ) | |
272 | { | |
273 | TIter next(CreateIterator()); | |
274 | TObject* o; | |
275 | while ( ( o = next() ) ) | |
276 | { | |
277 | o->Print(); | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
5006ec94 | 282 | //_____________________________________________________________________________ |
168e9c4d | 283 | void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object) |
5006ec94 | 284 | { |
285 | /// Add object with its key to the map and arrays | |
286 | ||
168e9c4d | 287 | fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object); |
288 | AddKey(GetIndex(keyFirst, keySecond)); | |
5006ec94 | 289 | fObjects.Add(object); |
290 | } | |
291 | ||
292 | //_____________________________________________________________________________ | |
293 | void AliMpExMap::Add(const TString& key, TObject* object) | |
294 | { | |
295 | /// Add object with its key to the map and arrays | |
296 | ||
297 | fMap.Add(GetIndex(key), (Long_t)object); | |
298 | AddKey(GetIndex(key)); | |
299 | fObjects.Add(object); | |
300 | } | |
301 | ||
302 | //_____________________________________________________________________________ | |
303 | void AliMpExMap::Add(Int_t key, TObject* object) | |
304 | { | |
305 | /// Add object with its key to the map and arrays | |
306 | ||
307 | fMap.Add(key, (Long_t)object); | |
308 | AddKey(key); | |
309 | fObjects.Add(object); | |
310 | } | |
311 | ||
312 | //_____________________________________________________________________________ | |
313 | void AliMpExMap::SetSize(Int_t size) | |
314 | { | |
315 | /// Set given size to the key array | |
316 | ||
317 | // fMap.Set(size); | |
318 | // fObjects.Set(size); | |
319 | fKeys.Set(size); | |
320 | } | |
321 | ||
322 | //_____________________________________________________________________________ | |
323 | void AliMpExMap::SetOwner(Bool_t owner) | |
324 | { | |
325 | /// Set given ownership to object array | |
326 | ||
327 | fObjects.SetOwner(owner); | |
328 | } | |
329 | ||
330 | //_____________________________________________________________________________ | |
331 | Int_t AliMpExMap::GetSize() const | |
332 | { | |
6b431a48 | 333 | /// Return the map size |
5006ec94 | 334 | |
335 | return fObjects.GetEntriesFast(); | |
336 | } | |
337 | ||
338 | //_____________________________________________________________________________ | |
630711ed | 339 | Int_t AliMpExMap::GetCapacity() const |
5006ec94 | 340 | { |
630711ed | 341 | /// Return the map capacity |
342 | ||
343 | return fObjects.GetSize(); | |
5006ec94 | 344 | } |
345 | ||
6b431a48 | 346 | //_____________________________________________________________________________ |
630711ed | 347 | AliMpExMapIterator* |
348 | AliMpExMap::CreateIterator() const | |
6b431a48 | 349 | { |
630711ed | 350 | /// Return iterator set to the beginning of the map |
6b431a48 | 351 | |
630711ed | 352 | return new AliMpExMapIterator(*this); |
353 | } | |
6b431a48 | 354 | |
5006ec94 | 355 | //_____________________________________________________________________________ |
168e9c4d | 356 | TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const |
5006ec94 | 357 | { |
358 | /// Return the object associated with the given key if found, | |
359 | /// otherwise return 0 | |
360 | ||
168e9c4d | 361 | return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond))); |
5006ec94 | 362 | } |
363 | ||
364 | //_____________________________________________________________________________ | |
365 | TObject* AliMpExMap::GetValue(const TString& key) const | |
366 | { | |
367 | /// Return the object associated with the given key if found, | |
368 | /// otherwise return 0 | |
369 | ||
617853db | 370 | return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key))); |
5006ec94 | 371 | } |
372 | ||
373 | //_____________________________________________________________________________ | |
374 | TObject* AliMpExMap::GetValue(Int_t key) const | |
375 | { | |
376 | /// Return the object associated with the given key if found, | |
377 | /// otherwise return 0 | |
378 | ||
617853db | 379 | return reinterpret_cast<TObject*>(fMap.GetValue(key)); |
5006ec94 | 380 | } |
381 | ||
382 | //_____________________________________________________________________________ | |
383 | void AliMpExMap::Streamer(TBuffer &R__b) | |
384 | { | |
78649106 | 385 | // Customized streamer \n |
386 | // After the arrays are read, fill the transient map | |
5006ec94 | 387 | |
388 | if (R__b.IsReading()) { | |
389 | AliMpExMap::Class()->ReadBuffer(R__b, this); | |
390 | FillMap(); | |
391 | } | |
392 | else { | |
393 | AliMpExMap::Class()->WriteBuffer(R__b, this); | |
394 | } | |
395 | } |