AliRoot Core  3dc7879 (3dc7879)
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 "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;
42 ClassImp(AliMpExMap)
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 {
63  static const TString kCharacterMap
64  = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
65  return kCharacterMap;
66 }
67 
68 //_____________________________________________________________________________
69 Long_t AliMpExMap::GetIndex(const TString& s)
70 {
72 
73  if (s.Length() > 5) {
74  AliErrorClass("String too long.");
75  return -1;
76  }
77 
78  Long_t index = 0;
79  for (Int_t i=s.Length()-1; i>=0; --i)
80  index = index*fgkSeparator2 + GetCharacterMap().First(s(i));
81 
82  return index;
83 }
84 
85 //_____________________________________________________________________________
86 TString AliMpExMap::GetString(Long_t index)
87 {
89 
90  TString s;
91  while (index >0) {
92  Char_t c = GetCharacterMap()(index%fgkSeparator2);
93  s += c;
94  index = index/fgkSeparator2;
95  }
96  return s;
97 }
98 
99 //
100 // constructors/destructor
101 //
102 
103 //_____________________________________________________________________________
105  : TObject(),
109 {
111 
112  fObjects.SetOwner(fgkDefaultOwnership);
113 }
114 
115 //_____________________________________________________________________________
117  : TObject(),
118  fMap(),
119  fObjects(),
120  fKeys()
121 {
123 }
124 
125 
126 //_____________________________________________________________________________
128  : TObject(),
129  fMap(),
130  fObjects(),
131  fKeys()
132 
133 {
135  rhs.Copy(*this);
136 }
137 
138 //_____________________________________________________________________________
139 AliMpExMap&
141 {
143 
144  // check assignment to self
145  if (this == &rhs) return *this;
146 
147  rhs.Copy(*this);
148  return *this;
149 }
150 
151 //_____________________________________________________________________________
153 {
155 }
156 
157 //
158 // private static methods
159 //
160 
161 //______________________________________________________________________________
162 Long_t AliMpExMap::GetIndex(Int_t first, Int_t second)
163 {
165 
166  if ( first >= 0xFFFF || second >= 0xFFFF )
167  {
168  AliFatalClass("Index out of limit");
169  return 0;
170  }
171 
172  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 }
181 
182 //______________________________________________________________________________
183 Int_t AliMpExMap::GetPairFirst(Long_t index)
184 {
186 
187  return (index-1) & 0xFFFF ;
188 }
189 
190 //______________________________________________________________________________
191 Int_t AliMpExMap::GetPairSecond(Long_t index)
192 {
194 
195  return ( (index-1) & 0xFFFF0000 ) >> 16 ;
196 }
197 
198 //
199 // private methods
200 //
201 
202 //_____________________________________________________________________________
204 {
206 
207  for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
208  fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
209 }
210 
211 //_____________________________________________________________________________
212 void AliMpExMap::AddKey(Long_t key)
213 {
215 
216  // Resize array if needed
217  if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
218  fKeys.Set(2*fKeys.GetSize());
219  AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
220  }
221 
222  fKeys.AddAt(key, fObjects.GetEntriesFast());
223 }
224 
225 //_____________________________________________________________________________
226 void
227 AliMpExMap::Copy(TObject& dest) const
228 {
232 
233  AliDebug(1,"");
234 
235  TObject::Copy(dest);
236  AliMpExMap& m = static_cast<AliMpExMap&>(dest);
237  m.fKeys = fKeys;
238  m.fMap.Delete();
239  m.fObjects.Clear();
240 
241  for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
242  {
243  TObject* o = fObjects.At(i)->Clone();
244  if (!o)
245  {
246  AliError("Object was not cloned properly ! Please investigate...");
247  }
248  m.fObjects.AddLast(o);
249  }
250  m.FillMap();
251  m.fObjects.SetOwner(kTRUE);
252 }
253 
254 //
255 // public methods
256 //
257 
258 //_____________________________________________________________________________
259 void AliMpExMap::Clear(Option_t* option)
260 {
262 
263  fMap.Delete();
264  fObjects.Clear(option);
265  fKeys.Reset();
266 }
267 
268 //_____________________________________________________________________________
269 void AliMpExMap::Print(Option_t* opt) const
270 {
272 
273  cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity())
274  << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries())
275  << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
276 
277  TString sopt(opt);
278  sopt.ToUpper();
279 
280  if ( sopt.Contains("FULL") )
281  {
282  TIter next(CreateIterator());
283  TObject* o;
284  while ( ( o = next() ) )
285  {
286  o->Print();
287  }
288  }
289 }
290 
291 //_____________________________________________________________________________
292 void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
293 {
295 
296  fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
297  AddKey(GetIndex(keyFirst, keySecond));
298  fObjects.Add(object);
299 }
300 
301 //_____________________________________________________________________________
302 void AliMpExMap::Add(const TString& key, TObject* object)
303 {
305 
306  fMap.Add(GetIndex(key), (Long_t)object);
307  AddKey(GetIndex(key));
308  fObjects.Add(object);
309 }
310 
311 //_____________________________________________________________________________
312 void AliMpExMap::Add(Int_t key, TObject* object)
313 {
315 
316  fMap.Add(key, (Long_t)object);
317  AddKey(key);
318  fObjects.Add(object);
319 }
320 
321 //_____________________________________________________________________________
322 void AliMpExMap::SetSize(Int_t size)
323 {
325 
326  // fMap.Set(size);
327  // fObjects.Set(size);
328  fKeys.Set(size);
329 }
330 
331 //_____________________________________________________________________________
332 void AliMpExMap::SetOwner(Bool_t owner)
333 {
335 
336  fObjects.SetOwner(owner);
337 }
338 
339 //_____________________________________________________________________________
340 Int_t AliMpExMap::GetSize() const
341 {
343 
344  return fObjects.GetEntriesFast();
345 }
346 
347 //_____________________________________________________________________________
349 {
351 
352  return fObjects.GetSize();
353 }
354 
355 //_____________________________________________________________________________
358 {
360 
361  return new AliMpExMapIterator(*this);
362 }
363 
364 //_____________________________________________________________________________
365 TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
366 {
369 
370  return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
371 }
372 
373 //_____________________________________________________________________________
374 TObject* AliMpExMap::GetValue(const TString& key) const
375 {
378 
379  return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
380 }
381 
382 //_____________________________________________________________________________
383 TObject* AliMpExMap::GetValue(Int_t key) const
384 {
387 
388  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  if (R__b.IsReading()) {
398  AliMpExMap::Class()->ReadBuffer(R__b, this);
399  FillMap();
400  }
401  else {
402  AliMpExMap::Class()->WriteBuffer(R__b, this);
403  }
404 }
virtual void Print(Option_t *opt="") const
Definition: AliMpExMap.cxx:269
void SetSize(Int_t size)
Definition: AliMpExMap.cxx:322
static const TString & GetCharacterMap()
Definition: AliMpExMap.cxx:60
#define AliFatalClass(message)
Definition: AliLog.h:645
void AddKey(Long_t key)
Definition: AliMpExMap.cxx:212
Int_t GetSize() const
Definition: AliMpExMap.cxx:340
AliMpExMap & operator=(const AliMpExMap &rhs)
Definition: AliMpExMap.cxx:140
#define AliErrorClass(message)
Definition: AliLog.h:596
static const Int_t fgkDefaultSize
Default initial size.
Definition: AliMpExMap.h:83
virtual ~AliMpExMap()
Definition: AliMpExMap.cxx:152
void Copy(TObject &dest) const
Definition: AliMpExMap.cxx:227
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:365
TExMap fMap
! Transient map class
Definition: AliMpExMap.h:92
void Add(Int_t keyFirst, Int_t keySecond, TObject *object)
Definition: AliMpExMap.cxx:292
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:332
#define AliDebug(logLevel, message)
Definition: AliLog.h:300
#define AliDebugStream(level)
Definition: AliLog.h:388
static Int_t GetPairSecond(Long_t index)
Definition: AliMpExMap.cxx:191
TArrayL fKeys
Array of keys.
Definition: AliMpExMap.h:94
void FillMap()
Definition: AliMpExMap.cxx:203
#define AliError(message)
Definition: AliLog.h:591
static Long_t GetIndex(const TString &s)
Definition: AliMpExMap.cxx:69
static TString GetString(Long_t index)
Definition: AliMpExMap.cxx:86
Implementation of TIterator for AliMpExMap.
virtual void Clear(Option_t *opt="")
Definition: AliMpExMap.cxx:259
Int_t GetCapacity() const
Definition: AliMpExMap.cxx:348
Helper class making Root persistent TExMap.
Definition: AliMpExMap.h:28
AliMpExMapIterator * CreateIterator() const
Definition: AliMpExMap.cxx:357
TObjArray fObjects
Array of objects.
Definition: AliMpExMap.h:93
static Int_t GetPairFirst(Long_t index)
Definition: AliMpExMap.cxx:183