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>
48 const Int_t AliMpExMap::fgkDefaultSize = 300;
49 const Bool_t AliMpExMap::fgkDefaultOwnership = true;
51 const Int_t AliMpExMap::fgkSeparator1 = 10000;
52 const Int_t AliMpExMap::fgkSeparator2 = 100;
58 //_____________________________________________________________________________
59 const TString& AliMpExMap::GetCharacterMap()
61 /// Return the string mapping characters to integers
62 static const TString kCharacterMap
63 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
67 //_____________________________________________________________________________
68 Long_t AliMpExMap::GetIndex(const TString& s)
70 /// Convert the TString to integer.
73 AliErrorClass("String too long.");
78 for (Int_t i=s.Length()-1; i>=0; --i)
79 index = index*fgkSeparator2 + GetCharacterMap().First(s(i));
84 //_____________________________________________________________________________
85 TString AliMpExMap::GetString(Long_t index)
87 /// Convert the integer index to the string.
91 Char_t c = GetCharacterMap()(index%fgkSeparator2);
93 index = index/fgkSeparator2;
99 // constructors/destructor
102 //_____________________________________________________________________________
103 AliMpExMap::AliMpExMap()
105 fMap(fgkDefaultSize),
106 fObjects(fgkDefaultSize),
107 fKeys(fgkDefaultSize)
109 /// Default constructor
111 fObjects.SetOwner(fgkDefaultOwnership);
114 //_____________________________________________________________________________
115 AliMpExMap::AliMpExMap(TRootIOCtor*)
121 /// "Root - I/O" constructor
125 //_____________________________________________________________________________
126 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
137 //_____________________________________________________________________________
139 AliMpExMap::operator=(const AliMpExMap& rhs)
141 /// Assignment operator
143 // check assignment to self
144 if (this == &rhs) return *this;
150 //_____________________________________________________________________________
151 AliMpExMap::~AliMpExMap()
157 // private static methods
160 //______________________________________________________________________________
161 Long_t AliMpExMap::GetIndex(Int_t first, Int_t second)
163 /// Convert the pair of integers to integer.
165 if ( first >= 0xFFFF || second >= 0xFFFF )
167 AliFatalClass("Index out of limit");
171 return 1 + ( first | ( second << 16 ) );
173 // if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
174 // AliFatalClass("Index out of limit.");
178 // return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
181 //______________________________________________________________________________
182 Int_t AliMpExMap::GetPairFirst(Long_t index)
184 /// Return first integer from index (encoded pair)
186 return (index-1) & 0xFFFF ;
189 //______________________________________________________________________________
190 Int_t AliMpExMap::GetPairSecond(Long_t index)
192 /// Return second integer from index (encoded pair)
194 return ( (index-1) & 0xFFFF0000 ) >> 16 ;
201 //_____________________________________________________________________________
202 void AliMpExMap::FillMap()
204 /// Fill transient map from the arrays of objects and keys
206 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
207 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
210 //_____________________________________________________________________________
211 void AliMpExMap::AddKey(Long_t key)
213 /// Add key in array with checking size
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;
221 fKeys.AddAt(key, fObjects.GetEntriesFast());
224 //_____________________________________________________________________________
226 AliMpExMap::Copy(TObject& dest) const
228 /// Copy this to dest
229 /// Copy implies that dest will become owner of its objects, whatever
230 /// the ownership of (*this) is.
235 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
240 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
242 TObject* o = fObjects.At(i)->Clone();
245 AliError("Object was not cloned properly ! Please investigate...");
247 m.fObjects.AddLast(o);
250 m.fObjects.SetOwner(kTRUE);
257 //_____________________________________________________________________________
258 void AliMpExMap::Clear(Option_t* option)
263 fObjects.Clear(option);
267 //_____________________________________________________________________________
268 void AliMpExMap::Print(Option_t* opt) const
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;
279 if ( sopt.Contains("FULL") )
281 TIter next(CreateIterator());
283 while ( ( o = next() ) )
290 //_____________________________________________________________________________
291 void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
293 /// Add object with its key to the map and arrays
295 fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
296 AddKey(GetIndex(keyFirst, keySecond));
297 fObjects.Add(object);
300 //_____________________________________________________________________________
301 void AliMpExMap::Add(const TString& key, TObject* object)
303 /// Add object with its key to the map and arrays
305 fMap.Add(GetIndex(key), (Long_t)object);
306 AddKey(GetIndex(key));
307 fObjects.Add(object);
310 //_____________________________________________________________________________
311 void AliMpExMap::Add(Int_t key, TObject* object)
313 /// Add object with its key to the map and arrays
315 fMap.Add(key, (Long_t)object);
317 fObjects.Add(object);
320 //_____________________________________________________________________________
321 void AliMpExMap::SetSize(Int_t size)
323 /// Set given size to the key array
326 // fObjects.Set(size);
330 //_____________________________________________________________________________
331 void AliMpExMap::SetOwner(Bool_t owner)
333 /// Set given ownership to object array
335 fObjects.SetOwner(owner);
338 //_____________________________________________________________________________
339 Int_t AliMpExMap::GetSize() const
341 /// Return the map size
343 return fObjects.GetEntriesFast();
346 //_____________________________________________________________________________
347 Int_t AliMpExMap::GetCapacity() const
349 /// Return the map capacity
351 return fObjects.GetSize();
354 //_____________________________________________________________________________
356 AliMpExMap::CreateIterator() const
358 /// Return iterator set to the beginning of the map
360 return new AliMpExMapIterator(*this);
363 //_____________________________________________________________________________
364 TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
366 /// Return the object associated with the given key if found,
367 /// otherwise return 0
369 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
372 //_____________________________________________________________________________
373 TObject* AliMpExMap::GetValue(const TString& key) const
375 /// Return the object associated with the given key if found,
376 /// otherwise return 0
378 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
381 //_____________________________________________________________________________
382 TObject* AliMpExMap::GetValue(Int_t key) const
384 /// Return the object associated with the given key if found,
385 /// otherwise return 0
387 return reinterpret_cast<TObject*>(fMap.GetValue(key));
390 //_____________________________________________________________________________
391 void AliMpExMap::Streamer(TBuffer &R__b)
393 // Customized streamer \n
394 // After the arrays are read, fill the transient map
396 if (R__b.IsReading()) {
397 AliMpExMap::Class()->ReadBuffer(R__b, this);
401 AliMpExMap::Class()->WriteBuffer(R__b, this);