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