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;
52 const TString AliMpExMap::fgkCharacterMap
53 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
59 //_____________________________________________________________________________
60 Long_t AliMpExMap::GetIndex(const TString& s)
62 /// Convert the TString to integer.
65 AliErrorClass("String too long.");
70 for (Int_t i=s.Length()-1; i>=0; --i)
71 index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
76 //_____________________________________________________________________________
77 TString AliMpExMap::GetString(Long_t index)
79 /// Convert the integer index to the string.
83 Char_t c = fgkCharacterMap(index%fgkSeparator2);
85 index = index/fgkSeparator2;
91 // constructors/destructor
94 //_____________________________________________________________________________
95 AliMpExMap::AliMpExMap()
98 fObjects(fgkDefaultSize),
101 /// Default constructor
103 fObjects.SetOwner(fgkDefaultOwnership);
106 //_____________________________________________________________________________
107 AliMpExMap::AliMpExMap(TRootIOCtor*)
113 /// "Root - I/O" constructor
117 //_____________________________________________________________________________
118 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
129 //_____________________________________________________________________________
131 AliMpExMap::operator=(const AliMpExMap& rhs)
133 /// Assignment operator
135 // check assignment to self
136 if (this == &rhs) return *this;
142 //_____________________________________________________________________________
143 AliMpExMap::~AliMpExMap()
149 // private static methods
152 //______________________________________________________________________________
153 Long_t AliMpExMap::GetIndex(Int_t first, Int_t second)
155 /// Convert the pair of integers to integer.
157 if ( first >= 0xFFFF || second >= 0xFFFF )
159 AliFatalClass("Index out of limit");
163 return 1 + ( first | ( second << 16 ) );
165 // if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
166 // AliFatalClass("Index out of limit.");
170 // return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
173 //______________________________________________________________________________
174 Int_t AliMpExMap::GetPairFirst(Long_t index)
176 /// Return first integer from index (encoded pair)
178 return (index-1) & 0xFFFF ;
181 //______________________________________________________________________________
182 Int_t AliMpExMap::GetPairSecond(Long_t index)
184 /// Return second integer from index (encoded pair)
186 return ( (index-1) & 0xFFFF0000 ) >> 16 ;
193 //_____________________________________________________________________________
194 void AliMpExMap::FillMap()
196 /// Fill transient map from the arrays of objects and keys
198 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
199 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
202 //_____________________________________________________________________________
203 void AliMpExMap::AddKey(Long_t key)
205 /// Add key in array with checking size
207 // Resize array if needed
208 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
209 fKeys.Set(2*fKeys.GetSize());
210 AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
213 fKeys.AddAt(key, fObjects.GetEntriesFast());
216 //_____________________________________________________________________________
218 AliMpExMap::Copy(TObject& dest) const
220 /// Copy this to dest
221 /// Copy implies that dest will become owner of its objects, whatever
222 /// the ownership of (*this) is.
227 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
232 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
234 TObject* o = fObjects.At(i)->Clone();
237 AliError("Object was not cloned properly ! Please investigate...");
239 m.fObjects.AddLast(o);
242 m.fObjects.SetOwner(kTRUE);
249 //_____________________________________________________________________________
250 void AliMpExMap::Clear(Option_t* option)
255 fObjects.Clear(option);
259 //_____________________________________________________________________________
260 void AliMpExMap::Print(Option_t* opt) const
264 cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity())
265 << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries())
266 << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
271 if ( sopt.Contains("FULL") )
273 TIter next(CreateIterator());
275 while ( ( o = next() ) )
282 //_____________________________________________________________________________
283 void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
285 /// Add object with its key to the map and arrays
287 fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
288 AddKey(GetIndex(keyFirst, keySecond));
289 fObjects.Add(object);
292 //_____________________________________________________________________________
293 void AliMpExMap::Add(const TString& key, TObject* object)
295 /// Add object with its key to the map and arrays
297 fMap.Add(GetIndex(key), (Long_t)object);
298 AddKey(GetIndex(key));
299 fObjects.Add(object);
302 //_____________________________________________________________________________
303 void AliMpExMap::Add(Int_t key, TObject* object)
305 /// Add object with its key to the map and arrays
307 fMap.Add(key, (Long_t)object);
309 fObjects.Add(object);
312 //_____________________________________________________________________________
313 void AliMpExMap::SetSize(Int_t size)
315 /// Set given size to the key array
318 // fObjects.Set(size);
322 //_____________________________________________________________________________
323 void AliMpExMap::SetOwner(Bool_t owner)
325 /// Set given ownership to object array
327 fObjects.SetOwner(owner);
330 //_____________________________________________________________________________
331 Int_t AliMpExMap::GetSize() const
333 /// Return the map size
335 return fObjects.GetEntriesFast();
338 //_____________________________________________________________________________
339 Int_t AliMpExMap::GetCapacity() const
341 /// Return the map capacity
343 return fObjects.GetSize();
346 //_____________________________________________________________________________
348 AliMpExMap::CreateIterator() const
350 /// Return iterator set to the beginning of the map
352 return new AliMpExMapIterator(*this);
355 //_____________________________________________________________________________
356 TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
358 /// Return the object associated with the given key if found,
359 /// otherwise return 0
361 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
364 //_____________________________________________________________________________
365 TObject* AliMpExMap::GetValue(const TString& key) const
367 /// Return the object associated with the given key if found,
368 /// otherwise return 0
370 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
373 //_____________________________________________________________________________
374 TObject* AliMpExMap::GetValue(Int_t key) const
376 /// Return the object associated with the given key if found,
377 /// otherwise return 0
379 return reinterpret_cast<TObject*>(fMap.GetValue(key));
382 //_____________________________________________________________________________
383 void AliMpExMap::Streamer(TBuffer &R__b)
385 // Customized streamer \n
386 // After the arrays are read, fill the transient map
388 if (R__b.IsReading()) {
389 AliMpExMap::Class()->ReadBuffer(R__b, this);
393 AliMpExMap::Class()->WriteBuffer(R__b, this);