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"
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 AliMpIntPair& pair)
62 /// Convert the pair of integers to integer.
64 if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
65 AliFatalClass("Index out of limit.");
69 return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
72 //_____________________________________________________________________________
73 Long_t AliMpExMap::GetIndex(const TString& s)
75 /// Convert the TString to integer.
78 AliErrorClass("String too long.");
83 for (Int_t i=s.Length()-1; i>=0; --i)
84 index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
89 //______________________________________________________________________________
90 AliMpIntPair AliMpExMap::GetPair(Long_t index)
92 /// Convert the integer index to the pair of integers.
94 return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
97 //_____________________________________________________________________________
98 TString AliMpExMap::GetString(Long_t index)
100 /// Convert the integer index to the string.
104 Char_t c = fgkCharacterMap(index%fgkSeparator2);
106 index = index/fgkSeparator2;
112 // constructors/destructor
115 //_____________________________________________________________________________
116 AliMpExMap::AliMpExMap(Bool_t /*standardConstructor*/)
118 fMap(fgkDefaultSize),
119 fObjects(fgkDefaultSize),
120 fKeys(fgkDefaultSize)
122 /// Standard constructor
124 fObjects.SetOwner(fgkDefaultOwnership);
127 //_____________________________________________________________________________
128 AliMpExMap::AliMpExMap()
134 /// Default constructor
138 //_____________________________________________________________________________
139 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
150 //_____________________________________________________________________________
152 AliMpExMap::operator=(const AliMpExMap& rhs)
154 /// Assignment operator
160 //_____________________________________________________________________________
162 AliMpExMap::Copy(TObject& dest) const
164 /// Copy this to dest
165 /// Copy implies that dest will become owner of its objects, whatever
166 /// the ownership of (*this) is.
171 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
176 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
178 TObject* o = fObjects.At(i)->Clone();
181 AliError("Object was not cloned properly ! Please investigate...");
183 m.fObjects.AddLast(o);
186 m.fObjects.SetOwner(kTRUE);
189 //_____________________________________________________________________________
190 AliMpExMap::~AliMpExMap()
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 AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl;
219 fKeys.AddAt(key, fObjects.GetEntriesFast());
226 //_____________________________________________________________________________
227 void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
229 /// Add object with its key to the map and arrays
231 fMap.Add(GetIndex(key), (Long_t)object);
232 AddKey(GetIndex(key));
233 fObjects.Add(object);
236 //_____________________________________________________________________________
237 void AliMpExMap::Add(const TString& key, TObject* object)
239 /// Add object with its key to the map and arrays
241 fMap.Add(GetIndex(key), (Long_t)object);
242 AddKey(GetIndex(key));
243 fObjects.Add(object);
246 //_____________________________________________________________________________
247 void AliMpExMap::Add(Int_t key, TObject* object)
249 /// Add object with its key to the map and arrays
251 fMap.Add(key, (Long_t)object);
253 fObjects.Add(object);
256 //_____________________________________________________________________________
257 void AliMpExMap::SetSize(Int_t size)
259 /// Set given size to the key array
262 // fObjects.Set(size);
266 //_____________________________________________________________________________
267 void AliMpExMap::SetOwner(Bool_t owner)
269 /// Set given ownership to object array
271 fObjects.SetOwner(owner);
274 //_____________________________________________________________________________
275 Int_t AliMpExMap::GetSize() const
277 /// Return the map size
279 return fObjects.GetEntriesFast();
282 //_____________________________________________________________________________
283 TExMapIter AliMpExMap::GetIterator() const
285 /// Return TExMap iterator set to the beginning of the map
287 return TExMapIter(&fMap);
290 //_____________________________________________________________________________
291 TObject* AliMpExMap::GetObject(Int_t index) const
293 /// Return the object via its index in the array
294 /// (This function makes possible looping over map as over an array)
296 if ( index < 0 || index >= fObjects.GetEntriesFast() ) {
297 AliErrorStream() << "Index outside limits" << endl;
301 return fObjects.UncheckedAt(index);
304 //_____________________________________________________________________________
305 TObject* AliMpExMap::GetObjectFast(Int_t index) const
307 /// Return the object via its index in the array
308 /// (This function makes possible looping over map as over an array)
309 /// without bound checking.
311 return fObjects.UncheckedAt(index);
314 //_____________________________________________________________________________
315 TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
317 /// Return the object associated with the given key if found,
318 /// otherwise return 0
320 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
323 //_____________________________________________________________________________
324 TObject* AliMpExMap::GetValue(const TString& key) const
326 /// Return the object associated with the given key if found,
327 /// otherwise return 0
329 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
332 //_____________________________________________________________________________
333 TObject* AliMpExMap::GetValue(Int_t key) const
335 /// Return the object associated with the given key if found,
336 /// otherwise return 0
338 return reinterpret_cast<TObject*>(fMap.GetValue(key));
341 //_____________________________________________________________________________
342 void AliMpExMap::Streamer(TBuffer &R__b)
344 // Customized streamer \n
345 // After the arrays are read, fill the transient map
347 if (R__b.IsReading()) {
348 AliMpExMap::Class()->ReadBuffer(R__b, this);
352 AliMpExMap::Class()->WriteBuffer(R__b, this);