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.2 2006/03/02 16:28:23 ivana Exp $
19 // ------------------------
21 // ------------------------
22 // Helper class making Root persistent TExMap
23 // Author:Ivana Hrivnacova; IPN Orsay
25 #include "AliMpExMap.h"
30 #include <Riostream.h>
34 //#include "AliMpConstants.h"
35 #include "AliMpIntPair.h"
42 const Int_t AliMpExMap::fgkDefaultSize = 300;
43 const Bool_t AliMpExMap::fgkDefaultOwnership = true;
45 const Int_t AliMpExMap::fgkSeparator1 = 10000;
46 const Int_t AliMpExMap::fgkSeparator2 = 100;
48 const TString AliMpExMap::fgkCharacterMap
49 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
57 //______________________________________________________________________________
58 Long_t AliMpExMap::GetIndex(const AliMpIntPair& pair)
60 /// Convert the pair of integers to integer.
62 if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
63 // to do - use AliLog, is it possible for static function
64 // AliFatal("Index out of limit.");
65 cerr << "AliMpExMap::GetIndex(const AliMpIntPair&): Index out of limit." << endl;
69 return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
72 //_____________________________________________________________________________
73 Long_t AliMpExMap::GetIndex(const TString& s)
75 /// Convert the TString to integer.
78 // to do - use AliLog, is it possible for static function
79 // AliFatal("String too long.");
80 cerr << "AliMpExMap::GetIndex(const TString&): String too long." << endl;
85 for (Int_t i=s.Length()-1; i>=0; --i)
86 index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
91 //______________________________________________________________________________
92 AliMpIntPair AliMpExMap::GetPair(Long_t index)
94 /// Convert the integer index to the pair of integers.
96 return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
99 //_____________________________________________________________________________
100 TString AliMpExMap::GetString(Long_t index)
102 /// Convert the integer index to the string.
106 Char_t c = fgkCharacterMap(index%fgkSeparator2);
108 index = index/fgkSeparator2;
114 // constructors/destructor
117 //_____________________________________________________________________________
118 AliMpExMap::AliMpExMap(Bool_t /*standardConstructor*/)
120 fMap(fgkDefaultSize),
121 fObjects(fgkDefaultSize),
122 fKeys(fgkDefaultSize)
124 /// Standard constructor
126 fObjects.SetOwner(fgkDefaultOwnership);
129 //_____________________________________________________________________________
130 AliMpExMap::AliMpExMap()
136 /// Default constructor
139 //_____________________________________________________________________________
140 AliMpExMap::~AliMpExMap()
149 //_____________________________________________________________________________
150 void AliMpExMap::FillMap()
152 /// Fill transient map from the arrays of objects and keys
154 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
155 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
158 //_____________________________________________________________________________
159 void AliMpExMap::AddKey(Long_t key)
161 /// Add key in array with checking size
163 // Resize array if needed
164 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
165 fKeys.Set(2*fKeys.GetSize());
166 cout << "AliMpExMap::AddKey: resized Key array " << endl;
169 fKeys.AddAt(key, fObjects.GetEntriesFast());
176 //_____________________________________________________________________________
177 void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
179 /// Add object with its key to the map and arrays
181 fMap.Add(GetIndex(key), (Long_t)object);
182 AddKey(GetIndex(key));
183 fObjects.Add(object);
186 //_____________________________________________________________________________
187 void AliMpExMap::Add(const TString& key, TObject* object)
189 /// Add object with its key to the map and arrays
191 fMap.Add(GetIndex(key), (Long_t)object);
192 AddKey(GetIndex(key));
193 fObjects.Add(object);
196 //_____________________________________________________________________________
197 void AliMpExMap::Add(Int_t key, TObject* object)
199 /// Add object with its key to the map and arrays
201 fMap.Add(key, (Long_t)object);
203 fObjects.Add(object);
206 //_____________________________________________________________________________
207 void AliMpExMap::SetSize(Int_t size)
209 /// Set given size to the key array
212 // fObjects.Set(size);
216 //_____________________________________________________________________________
217 void AliMpExMap::SetOwner(Bool_t owner)
219 /// Set given ownership to object array
221 fObjects.SetOwner(owner);
224 //_____________________________________________________________________________
225 Int_t AliMpExMap::GetSize() const
227 /// Return TExMap iterator set to the beginning of the map
229 return fObjects.GetEntriesFast();
232 //_____________________________________________________________________________
233 TExMapIter AliMpExMap::GetIterator() const
235 /// Return TExMap iterator set to the beginning of the map
237 return TExMapIter(&fMap);
240 //_____________________________________________________________________________
241 TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
243 /// Return the object associated with the given key if found,
244 /// otherwise return 0
246 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
249 //_____________________________________________________________________________
250 TObject* AliMpExMap::GetValue(const TString& key) const
252 /// Return the object associated with the given key if found,
253 /// otherwise return 0
255 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
258 //_____________________________________________________________________________
259 TObject* AliMpExMap::GetValue(Int_t key) const
261 /// Return the object associated with the given key if found,
262 /// otherwise return 0
264 return reinterpret_cast<TObject*>(fMap.GetValue(key));
267 //_____________________________________________________________________________
268 void AliMpExMap::Streamer(TBuffer &R__b)
270 /// Customized streamer \n
271 /// After the arrays are read, fill the transient map
273 if (R__b.IsReading()) {
274 AliMpExMap::Class()->ReadBuffer(R__b, this);
278 AliMpExMap::Class()->WriteBuffer(R__b, this);