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 "AliMpIntPair.h"
29 #include "AliMpExMapIterator.h"
35 #include <Riostream.h>
47 const Int_t AliMpExMap::fgkDefaultSize = 300;
48 const Bool_t AliMpExMap::fgkDefaultOwnership = true;
50 const Int_t AliMpExMap::fgkSeparator1 = 10000;
51 const Int_t AliMpExMap::fgkSeparator2 = 100;
53 const TString AliMpExMap::fgkCharacterMap
54 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
60 //______________________________________________________________________________
61 Long_t AliMpExMap::GetIndex(const AliMpIntPair& pair)
63 /// Convert the pair of integers to integer.
65 if ( pair.GetFirst() >= 0xFFFF || pair.GetSecond() >= 0xFFFF )
67 AliFatalClass("Index out of limit");
71 return 1 + ( pair.GetFirst() | ( pair.GetSecond() << 16 ) );
73 // if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
74 // AliFatalClass("Index out of limit.");
78 // return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
81 //_____________________________________________________________________________
82 Long_t AliMpExMap::GetIndex(const TString& s)
84 /// Convert the TString to integer.
87 AliErrorClass("String too long.");
92 for (Int_t i=s.Length()-1; i>=0; --i)
93 index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
98 //______________________________________________________________________________
99 AliMpIntPair AliMpExMap::GetPair(Long_t index)
101 // Convert the integer index to the pair of integers.
103 // return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
105 ( (index-1) & 0xFFFF ) ,
106 ( (index-1) & 0xFFFF0000 ) >> 16 );
109 //_____________________________________________________________________________
110 TString AliMpExMap::GetString(Long_t index)
112 /// Convert the integer index to the string.
116 Char_t c = fgkCharacterMap(index%fgkSeparator2);
118 index = index/fgkSeparator2;
124 // constructors/destructor
127 //_____________________________________________________________________________
128 AliMpExMap::AliMpExMap()
130 fMap(fgkDefaultSize),
131 fObjects(fgkDefaultSize),
132 fKeys(fgkDefaultSize)
134 /// Default constructor
136 fObjects.SetOwner(fgkDefaultOwnership);
139 //_____________________________________________________________________________
140 AliMpExMap::AliMpExMap(TRootIOCtor*)
146 /// "Root - I/O" constructor
150 //_____________________________________________________________________________
151 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
162 //_____________________________________________________________________________
164 AliMpExMap::operator=(const AliMpExMap& rhs)
166 /// Assignment operator
168 // check assignment to self
169 if (this == &rhs) return *this;
175 //_____________________________________________________________________________
176 AliMpExMap::~AliMpExMap()
185 //_____________________________________________________________________________
186 void AliMpExMap::FillMap()
188 /// Fill transient map from the arrays of objects and keys
190 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
191 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
194 //_____________________________________________________________________________
195 void AliMpExMap::AddKey(Long_t key)
197 /// Add key in array with checking size
199 // Resize array if needed
200 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
201 fKeys.Set(2*fKeys.GetSize());
202 AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
205 fKeys.AddAt(key, fObjects.GetEntriesFast());
208 //_____________________________________________________________________________
210 AliMpExMap::Copy(TObject& dest) const
212 /// Copy this to dest
213 /// Copy implies that dest will become owner of its objects, whatever
214 /// the ownership of (*this) is.
219 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
224 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
226 TObject* o = fObjects.At(i)->Clone();
229 AliError("Object was not cloned properly ! Please investigate...");
231 m.fObjects.AddLast(o);
234 m.fObjects.SetOwner(kTRUE);
241 //_____________________________________________________________________________
242 void AliMpExMap::Clear(Option_t* option)
247 fObjects.Clear(option);
251 //_____________________________________________________________________________
252 void AliMpExMap::Print(Option_t* opt) const
256 cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity())
257 << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries())
258 << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
263 if ( sopt.Contains("FULL") )
265 TIter next(CreateIterator());
267 while ( ( o = next() ) )
274 //_____________________________________________________________________________
275 void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
277 /// Add object with its key to the map and arrays
279 fMap.Add(GetIndex(key), (Long_t)object);
280 AddKey(GetIndex(key));
281 fObjects.Add(object);
284 //_____________________________________________________________________________
285 void AliMpExMap::Add(const TString& key, TObject* object)
287 /// Add object with its key to the map and arrays
289 fMap.Add(GetIndex(key), (Long_t)object);
290 AddKey(GetIndex(key));
291 fObjects.Add(object);
294 //_____________________________________________________________________________
295 void AliMpExMap::Add(Int_t key, TObject* object)
297 /// Add object with its key to the map and arrays
299 fMap.Add(key, (Long_t)object);
301 fObjects.Add(object);
304 //_____________________________________________________________________________
305 void AliMpExMap::SetSize(Int_t size)
307 /// Set given size to the key array
310 // fObjects.Set(size);
314 //_____________________________________________________________________________
315 void AliMpExMap::SetOwner(Bool_t owner)
317 /// Set given ownership to object array
319 fObjects.SetOwner(owner);
322 //_____________________________________________________________________________
323 Int_t AliMpExMap::GetSize() const
325 /// Return the map size
327 return fObjects.GetEntriesFast();
330 //_____________________________________________________________________________
331 Int_t AliMpExMap::GetCapacity() const
333 /// Return the map capacity
335 return fObjects.GetSize();
338 //_____________________________________________________________________________
340 AliMpExMap::CreateIterator() const
342 /// Return iterator set to the beginning of the map
344 return new AliMpExMapIterator(*this);
347 //_____________________________________________________________________________
348 TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
350 /// Return the object associated with the given key if found,
351 /// otherwise return 0
353 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
356 //_____________________________________________________________________________
357 TObject* AliMpExMap::GetValue(const TString& key) const
359 /// Return the object associated with the given key if found,
360 /// otherwise return 0
362 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
365 //_____________________________________________________________________________
366 TObject* AliMpExMap::GetValue(Int_t key) const
368 /// Return the object associated with the given key if found,
369 /// otherwise return 0
371 return reinterpret_cast<TObject*>(fMap.GetValue(key));
374 //_____________________________________________________________________________
375 void AliMpExMap::Streamer(TBuffer &R__b)
377 // Customized streamer \n
378 // After the arrays are read, fill the transient map
380 if (R__b.IsReading()) {
381 AliMpExMap::Class()->ReadBuffer(R__b, this);
385 AliMpExMap::Class()->WriteBuffer(R__b, this);