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
156 // check assignment to self
157 if (this == &rhs) return *this;
163 //_____________________________________________________________________________
165 AliMpExMap::Copy(TObject& dest) const
167 /// Copy this to dest
168 /// Copy implies that dest will become owner of its objects, whatever
169 /// the ownership of (*this) is.
174 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
179 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
181 TObject* o = fObjects.At(i)->Clone();
184 AliError("Object was not cloned properly ! Please investigate...");
186 m.fObjects.AddLast(o);
189 m.fObjects.SetOwner(kTRUE);
192 //_____________________________________________________________________________
193 AliMpExMap::~AliMpExMap()
202 //_____________________________________________________________________________
203 void AliMpExMap::FillMap()
205 /// Fill transient map from the arrays of objects and keys
207 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
208 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
211 //_____________________________________________________________________________
212 void AliMpExMap::AddKey(Long_t key)
214 /// Add key in array with checking size
216 // Resize array if needed
217 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
218 fKeys.Set(2*fKeys.GetSize());
219 AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl;
222 fKeys.AddAt(key, fObjects.GetEntriesFast());
229 //_____________________________________________________________________________
230 void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
232 /// Add object with its key to the map and arrays
234 fMap.Add(GetIndex(key), (Long_t)object);
235 AddKey(GetIndex(key));
236 fObjects.Add(object);
239 //_____________________________________________________________________________
240 void AliMpExMap::Add(const TString& key, TObject* object)
242 /// Add object with its key to the map and arrays
244 fMap.Add(GetIndex(key), (Long_t)object);
245 AddKey(GetIndex(key));
246 fObjects.Add(object);
249 //_____________________________________________________________________________
250 void AliMpExMap::Add(Int_t key, TObject* object)
252 /// Add object with its key to the map and arrays
254 fMap.Add(key, (Long_t)object);
256 fObjects.Add(object);
259 //_____________________________________________________________________________
260 void AliMpExMap::SetSize(Int_t size)
262 /// Set given size to the key array
265 // fObjects.Set(size);
269 //_____________________________________________________________________________
270 void AliMpExMap::SetOwner(Bool_t owner)
272 /// Set given ownership to object array
274 fObjects.SetOwner(owner);
277 //_____________________________________________________________________________
278 Int_t AliMpExMap::GetSize() const
280 /// Return the map size
282 return fObjects.GetEntriesFast();
285 //_____________________________________________________________________________
286 TExMapIter AliMpExMap::GetIterator() const
288 /// Return TExMap iterator set to the beginning of the map
290 return TExMapIter(&fMap);
293 //_____________________________________________________________________________
294 TObject* AliMpExMap::GetObject(Int_t index) const
296 /// Return the object via its index in the array
297 /// (This function makes possible looping over map as over an array)
299 if ( index < 0 || index >= fObjects.GetEntriesFast() ) {
300 AliErrorStream() << "Index outside limits" << endl;
304 return fObjects.UncheckedAt(index);
307 //_____________________________________________________________________________
308 TObject* AliMpExMap::GetObjectFast(Int_t index) const
310 /// Return the object via its index in the array
311 /// (This function makes possible looping over map as over an array)
312 /// without bound checking.
314 return fObjects.UncheckedAt(index);
317 //_____________________________________________________________________________
318 TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
320 /// Return the object associated with the given key if found,
321 /// otherwise return 0
323 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
326 //_____________________________________________________________________________
327 TObject* AliMpExMap::GetValue(const TString& key) const
329 /// Return the object associated with the given key if found,
330 /// otherwise return 0
332 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
335 //_____________________________________________________________________________
336 TObject* AliMpExMap::GetValue(Int_t key) const
338 /// Return the object associated with the given key if found,
339 /// otherwise return 0
341 return reinterpret_cast<TObject*>(fMap.GetValue(key));
344 //_____________________________________________________________________________
345 void AliMpExMap::Streamer(TBuffer &R__b)
347 // Customized streamer \n
348 // After the arrays are read, fill the transient map
350 if (R__b.IsReading()) {
351 AliMpExMap::Class()->ReadBuffer(R__b, this);
355 AliMpExMap::Class()->WriteBuffer(R__b, this);