AliRoot Core  v5-06-30 (35d6c57)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliMpExMap.cxx
Go to the documentation of this file.
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 <TClass.h>
33 #include <TString.h>
34 #include <Riostream.h>
35 
36 #include <stdlib.h>
37 
38 using std::cout;
39 using std::endl;
43 
44 //
45 // static members
46 //
47 
48 const Int_t AliMpExMap::fgkDefaultSize = 300;
49 const Bool_t AliMpExMap::fgkDefaultOwnership = true;
50 
51 const Int_t AliMpExMap::fgkSeparator1 = 10000;
52 const Int_t AliMpExMap::fgkSeparator2 = 100;
53 
54 //
55 // static methods
56 //
57 
58 //_____________________________________________________________________________
59 const TString& AliMpExMap::GetCharacterMap()
60 {
62  static const TString kCharacterMap
63  = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
64  return kCharacterMap;
65 }
66 
67 //_____________________________________________________________________________
68 Long_t AliMpExMap::GetIndex(const TString& s)
69 {
71 
72  if (s.Length() > 5) {
73  AliErrorClass("String too long.");
74  return -1;
75  }
76 
77  Long_t index = 0;
78  for (Int_t i=s.Length()-1; i>=0; --i)
79  index = index*fgkSeparator2 + GetCharacterMap().First(s(i));
80 
81  return index;
82 }
83 
84 //_____________________________________________________________________________
85 TString AliMpExMap::GetString(Long_t index)
86 {
88 
89  TString s;
90  while (index >0) {
91  Char_t c = GetCharacterMap()(index%fgkSeparator2);
92  s += c;
93  index = index/fgkSeparator2;
94  }
95  return s;
96 }
97 
98 //
99 // constructors/destructor
100 //
101 
102 //_____________________________________________________________________________
104  : TObject(),
105  fMap(fgkDefaultSize),
106  fObjects(fgkDefaultSize),
107  fKeys(fgkDefaultSize)
108 {
110 
111  fObjects.SetOwner(fgkDefaultOwnership);
112 }
113 
114 //_____________________________________________________________________________
116  : TObject(),
117  fMap(),
118  fObjects(),
119  fKeys()
120 {
122 }
123 
124 
125 //_____________________________________________________________________________
127  : TObject(),
128  fMap(),
129  fObjects(),
130  fKeys()
131 
132 {
134  rhs.Copy(*this);
135 }
136 
137 //_____________________________________________________________________________
138 AliMpExMap&
140 {
142 
143  // check assignment to self
144  if (this == &rhs) return *this;
145 
146  rhs.Copy(*this);
147  return *this;
148 }
149 
150 //_____________________________________________________________________________
152 {
154 }
155 
156 //
157 // private static methods
158 //
159 
160 //______________________________________________________________________________
161 Long_t AliMpExMap::GetIndex(Int_t first, Int_t second)
162 {
164 
165  if ( first >= 0xFFFF || second >= 0xFFFF )
166  {
167  AliFatalClass("Index out of limit");
168  return 0;
169  }
170 
171  return 1 + ( first | ( second << 16 ) );
172 
173 // if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
174 // AliFatalClass("Index out of limit.");
175 // exit(1);
176 // }
177 //
178 // return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
179 }
180 
181 //______________________________________________________________________________
182 Int_t AliMpExMap::GetPairFirst(Long_t index)
183 {
185 
186  return (index-1) & 0xFFFF ;
187 }
188 
189 //______________________________________________________________________________
190 Int_t AliMpExMap::GetPairSecond(Long_t index)
191 {
193 
194  return ( (index-1) & 0xFFFF0000 ) >> 16 ;
195 }
196 
197 //
198 // private methods
199 //
200 
201 //_____________________________________________________________________________
203 {
205 
206  for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
207  fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
208 }
209 
210 //_____________________________________________________________________________
211 void AliMpExMap::AddKey(Long_t key)
212 {
214 
215  // Resize array if needed
216  if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
217  fKeys.Set(2*fKeys.GetSize());
218  AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
219  }
220 
221  fKeys.AddAt(key, fObjects.GetEntriesFast());
222 }
223 
224 //_____________________________________________________________________________
225 void
226 AliMpExMap::Copy(TObject& dest) const
227 {
231 
232  AliDebug(1,"");
233 
234  TObject::Copy(dest);
235  AliMpExMap& m = static_cast<AliMpExMap&>(dest);
236  m.fKeys = fKeys;
237  m.fMap.Delete();
238  m.fObjects.Clear();
239 
240  for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
241  {
242  TObject* o = fObjects.At(i)->Clone();
243  if (!o)
244  {
245  AliError("Object was not cloned properly ! Please investigate...");
246  }
247  m.fObjects.AddLast(o);
248  }
249  m.FillMap();
250  m.fObjects.SetOwner(kTRUE);
251 }
252 
253 //
254 // public methods
255 //
256 
257 //_____________________________________________________________________________
258 void AliMpExMap::Clear(Option_t* option)
259 {
261 
262  fMap.Delete();
263  fObjects.Clear(option);
264  fKeys.Reset();
265 }
266 
267 //_____________________________________________________________________________
268 void AliMpExMap::Print(Option_t* opt) const
269 {
271 
272  cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity())
273  << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries())
274  << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
275 
276  TString sopt(opt);
277  sopt.ToUpper();
278 
279  if ( sopt.Contains("FULL") )
280  {
281  TIter next(CreateIterator());
282  TObject* o;
283  while ( ( o = next() ) )
284  {
285  o->Print();
286  }
287  }
288 }
289 
290 //_____________________________________________________________________________
291 void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
292 {
294 
295  fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
296  AddKey(GetIndex(keyFirst, keySecond));
297  fObjects.Add(object);
298 }
299 
300 //_____________________________________________________________________________
301 void AliMpExMap::Add(const TString& key, TObject* object)
302 {
304 
305  fMap.Add(GetIndex(key), (Long_t)object);
306  AddKey(GetIndex(key));
307  fObjects.Add(object);
308 }
309 
310 //_____________________________________________________________________________
311 void AliMpExMap::Add(Int_t key, TObject* object)
312 {
314 
315  fMap.Add(key, (Long_t)object);
316  AddKey(key);
317  fObjects.Add(object);
318 }
319 
320 //_____________________________________________________________________________
321 void AliMpExMap::SetSize(Int_t size)
322 {
324 
325  // fMap.Set(size);
326  // fObjects.Set(size);
327  fKeys.Set(size);
328 }
329 
330 //_____________________________________________________________________________
331 void AliMpExMap::SetOwner(Bool_t owner)
332 {
334 
335  fObjects.SetOwner(owner);
336 }
337 
338 //_____________________________________________________________________________
339 Int_t AliMpExMap::GetSize() const
340 {
342 
343  return fObjects.GetEntriesFast();
344 }
345 
346 //_____________________________________________________________________________
348 {
350 
351  return fObjects.GetSize();
352 }
353 
354 //_____________________________________________________________________________
357 {
359 
360  return new AliMpExMapIterator(*this);
361 }
362 
363 //_____________________________________________________________________________
364 TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
365 {
368 
369  return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
370 }
371 
372 //_____________________________________________________________________________
373 TObject* AliMpExMap::GetValue(const TString& key) const
374 {
377 
378  return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
379 }
380 
381 //_____________________________________________________________________________
382 TObject* AliMpExMap::GetValue(Int_t key) const
383 {
386 
387  return reinterpret_cast<TObject*>(fMap.GetValue(key));
388 }
389 
390 //_____________________________________________________________________________
391 void AliMpExMap::Streamer(TBuffer &R__b)
392 {
393 // Customized streamer \n
394 // After the arrays are read, fill the transient map
395 
396  if (R__b.IsReading()) {
397  AliMpExMap::Class()->ReadBuffer(R__b, this);
398  FillMap();
399  }
400  else {
401  AliMpExMap::Class()->WriteBuffer(R__b, this);
402  }
403 }
virtual void Print(Option_t *opt="") const
Definition: AliMpExMap.cxx:268
void SetSize(Int_t size)
Definition: AliMpExMap.cxx:321
static const TString & GetCharacterMap()
Definition: AliMpExMap.cxx:59
void AddKey(Long_t key)
Definition: AliMpExMap.cxx:211
ClassImp(TPCGenInfo)
Definition: AliTPCCmpNG.C:254
Int_t GetSize() const
Definition: AliMpExMap.cxx:339
AliMpExMap & operator=(const AliMpExMap &rhs)
Definition: AliMpExMap.cxx:139
virtual ~AliMpExMap()
Definition: AliMpExMap.cxx:151
void Copy(TObject &dest) const
Definition: AliMpExMap.cxx:226
friend class AliMpExMapIterator
Definition: AliMpExMap.h:30
static const Bool_t fgkDefaultOwnership
Default ownership.
Definition: AliMpExMap.h:84
TObject * GetValue(Int_t keyFirst, Int_t keySecond) const
Definition: AliMpExMap.cxx:364
TExMap fMap
! Transient map class
Definition: AliMpExMap.h:92
void Add(Int_t keyFirst, Int_t keySecond, TObject *object)
Definition: AliMpExMap.cxx:291
static const Int_t fgkSeparator2
the separator used for conversion of TString to Int_t
Definition: AliMpExMap.h:88
void SetOwner(Bool_t owner)
Definition: AliMpExMap.cxx:331
static Int_t GetPairSecond(Long_t index)
Definition: AliMpExMap.cxx:190
TArrayL fKeys
Array of keys.
Definition: AliMpExMap.h:94
void FillMap()
Definition: AliMpExMap.cxx:202
static Long_t GetIndex(const TString &s)
Definition: AliMpExMap.cxx:68
static TString GetString(Long_t index)
Definition: AliMpExMap.cxx:85
Implementation of TIterator for AliMpExMap.
virtual void Clear(Option_t *opt="")
Definition: AliMpExMap.cxx:258
Int_t GetCapacity() const
Definition: AliMpExMap.cxx:347
Helper class making Root persistent TExMap.
Definition: AliMpExMap.h:28
AliMpExMapIterator * CreateIterator() const
Definition: AliMpExMap.cxx:356
TObjArray fObjects
Array of objects.
Definition: AliMpExMap.h:93
static Int_t GetPairFirst(Long_t index)
Definition: AliMpExMap.cxx:182