1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // $MpId: AliMpExMap.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
20 //-----------------------------------------------------------------------------
22 // ------------------------
23 // Helper class making Root persistent TExMap
24 // Author:Ivana Hrivnacova; IPN Orsay
25 //-----------------------------------------------------------------------------
27 #include "AliMpExMap.h"
28 #include "AliMpExMapIterator.h"
34 #include <Riostream.h>
46 const Int_t AliMpExMap::fgkDefaultSize = 300;
47 const Bool_t AliMpExMap::fgkDefaultOwnership = true;
49 const Int_t AliMpExMap::fgkSeparator1 = 10000;
50 const Int_t AliMpExMap::fgkSeparator2 = 100;
56 //_____________________________________________________________________________
57 const TString& AliMpExMap::GetCharacterMap()
59 /// Return the string mapping characters to integers
60 static const TString kCharacterMap
61 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
65 //_____________________________________________________________________________
66 Long_t AliMpExMap::GetIndex(const TString& s)
68 /// Convert the TString to integer.
71 AliErrorClass("String too long.");
76 for (Int_t i=s.Length()-1; i>=0; --i)
77 index = index*fgkSeparator2 + GetCharacterMap().First(s(i));
82 //_____________________________________________________________________________
83 TString AliMpExMap::GetString(Long_t index)
85 /// Convert the integer index to the string.
89 Char_t c = GetCharacterMap()(index%fgkSeparator2);
91 index = index/fgkSeparator2;
97 // constructors/destructor
100 //_____________________________________________________________________________
101 AliMpExMap::AliMpExMap()
103 fMap(fgkDefaultSize),
104 fObjects(fgkDefaultSize),
105 fKeys(fgkDefaultSize)
107 /// Default constructor
109 fObjects.SetOwner(fgkDefaultOwnership);
112 //_____________________________________________________________________________
113 AliMpExMap::AliMpExMap(TRootIOCtor*)
119 /// "Root - I/O" constructor
123 //_____________________________________________________________________________
124 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
135 //_____________________________________________________________________________
137 AliMpExMap::operator=(const AliMpExMap& rhs)
139 /// Assignment operator
141 // check assignment to self
142 if (this == &rhs) return *this;
148 //_____________________________________________________________________________
149 AliMpExMap::~AliMpExMap()
155 // private static methods
158 //______________________________________________________________________________
159 Long_t AliMpExMap::GetIndex(Int_t first, Int_t second)
161 /// Convert the pair of integers to integer.
163 if ( first >= 0xFFFF || second >= 0xFFFF )
165 AliFatalClass("Index out of limit");
169 return 1 + ( first | ( second << 16 ) );
171 // if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
172 // AliFatalClass("Index out of limit.");
176 // return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
179 //______________________________________________________________________________
180 Int_t AliMpExMap::GetPairFirst(Long_t index)
182 /// Return first integer from index (encoded pair)
184 return (index-1) & 0xFFFF ;
187 //______________________________________________________________________________
188 Int_t AliMpExMap::GetPairSecond(Long_t index)
190 /// Return second integer from index (encoded pair)
192 return ( (index-1) & 0xFFFF0000 ) >> 16 ;
199 //_____________________________________________________________________________
200 void AliMpExMap::FillMap()
202 /// Fill transient map from the arrays of objects and keys
204 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
205 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
208 //_____________________________________________________________________________
209 void AliMpExMap::AddKey(Long_t key)
211 /// Add key in array with checking size
213 // Resize array if needed
214 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
215 fKeys.Set(2*fKeys.GetSize());
216 AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
219 fKeys.AddAt(key, fObjects.GetEntriesFast());
222 //_____________________________________________________________________________
224 AliMpExMap::Copy(TObject& dest) const
226 /// Copy this to dest
227 /// Copy implies that dest will become owner of its objects, whatever
228 /// the ownership of (*this) is.
233 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
238 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
240 TObject* o = fObjects.At(i)->Clone();
243 AliError("Object was not cloned properly ! Please investigate...");
245 m.fObjects.AddLast(o);
248 m.fObjects.SetOwner(kTRUE);
255 //_____________________________________________________________________________
256 void AliMpExMap::Clear(Option_t* option)
261 fObjects.Clear(option);
265 //_____________________________________________________________________________
266 void AliMpExMap::Print(Option_t* opt) const
270 cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity())
271 << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries())
272 << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
277 if ( sopt.Contains("FULL") )
279 TIter next(CreateIterator());
281 while ( ( o = next() ) )
288 //_____________________________________________________________________________
289 void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
291 /// Add object with its key to the map and arrays
293 fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
294 AddKey(GetIndex(keyFirst, keySecond));
295 fObjects.Add(object);
298 //_____________________________________________________________________________
299 void AliMpExMap::Add(const TString& key, TObject* object)
301 /// Add object with its key to the map and arrays
303 fMap.Add(GetIndex(key), (Long_t)object);
304 AddKey(GetIndex(key));
305 fObjects.Add(object);
308 //_____________________________________________________________________________
309 void AliMpExMap::Add(Int_t key, TObject* object)
311 /// Add object with its key to the map and arrays
313 fMap.Add(key, (Long_t)object);
315 fObjects.Add(object);
318 //_____________________________________________________________________________
319 void AliMpExMap::SetSize(Int_t size)
321 /// Set given size to the key array
324 // fObjects.Set(size);
328 //_____________________________________________________________________________
329 void AliMpExMap::SetOwner(Bool_t owner)
331 /// Set given ownership to object array
333 fObjects.SetOwner(owner);
336 //_____________________________________________________________________________
337 Int_t AliMpExMap::GetSize() const
339 /// Return the map size
341 return fObjects.GetEntriesFast();
344 //_____________________________________________________________________________
345 Int_t AliMpExMap::GetCapacity() const
347 /// Return the map capacity
349 return fObjects.GetSize();
352 //_____________________________________________________________________________
354 AliMpExMap::CreateIterator() const
356 /// Return iterator set to the beginning of the map
358 return new AliMpExMapIterator(*this);
361 //_____________________________________________________________________________
362 TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
364 /// Return the object associated with the given key if found,
365 /// otherwise return 0
367 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
370 //_____________________________________________________________________________
371 TObject* AliMpExMap::GetValue(const TString& key) const
373 /// Return the object associated with the given key if found,
374 /// otherwise return 0
376 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
379 //_____________________________________________________________________________
380 TObject* AliMpExMap::GetValue(Int_t key) const
382 /// Return the object associated with the given key if found,
383 /// otherwise return 0
385 return reinterpret_cast<TObject*>(fMap.GetValue(key));
388 //_____________________________________________________________________________
389 void AliMpExMap::Streamer(TBuffer &R__b)
391 // Customized streamer \n
392 // After the arrays are read, fill the transient map
394 if (R__b.IsReading()) {
395 AliMpExMap::Class()->ReadBuffer(R__b, this);
399 AliMpExMap::Class()->WriteBuffer(R__b, this);