]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpExMap.cxx
4660f7817d6a9b26d05e05743ec8755630727774
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpExMap.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 // $Id$
17 // $MpId: AliMpExMap.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
18 // Category: basic
19
20 //-----------------------------------------------------------------------------
21 // Class AliMpExMap
22 // ------------------------
23 // Helper class making Root persistent TExMap
24 // Author:Ivana Hrivnacova; IPN Orsay
25 //-----------------------------------------------------------------------------
26
27 #include "AliMpExMap.h"
28 #include "AliMpIntPair.h"
29
30 #include "AliLog.h"
31
32 #include <TClass.h>
33 #include <TString.h>
34 #include <Riostream.h>
35
36 #include <stdlib.h>
37
38 /// \cond CLASSIMP
39 ClassImp(AliMpExMap)
40 /// \endcond
41
42 //
43 // static members
44 //
45
46 const Int_t   AliMpExMap::fgkDefaultSize = 300;
47 const Bool_t  AliMpExMap::fgkDefaultOwnership = true;
48
49 const Int_t AliMpExMap::fgkSeparator1 = 10000;
50 const Int_t AliMpExMap::fgkSeparator2 = 100;
51
52 const TString  AliMpExMap::fgkCharacterMap 
53   = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
54
55 //
56 // static methods
57 //
58
59 //______________________________________________________________________________
60 Long_t  AliMpExMap::GetIndex(const AliMpIntPair& pair)
61 {
62 /// Convert the pair of integers to integer.
63
64   if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
65     AliFatalClass("Index out of limit.");
66     exit(1); 
67   }  
68       
69   return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
70 }  
71
72 //_____________________________________________________________________________
73 Long_t  AliMpExMap::GetIndex(const TString& s)
74 {
75 /// Convert the TString to integer.
76
77   if (s.Length() > 5) {
78     AliFatalClass("String too long.");
79     return 0;
80   }  
81
82   Long_t index = 0;
83   for (Int_t i=s.Length()-1; i>=0; --i)  
84     index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
85   
86   return index;
87 }
88
89 //______________________________________________________________________________
90 AliMpIntPair  AliMpExMap::GetPair(Long_t index)
91 {
92 /// Convert the integer index to the pair of integers.
93
94   return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
95 }  
96
97 //_____________________________________________________________________________
98 TString  AliMpExMap::GetString(Long_t index)
99 {
100 /// Convert the integer index to the string.
101
102   TString s;
103   while (index >0) {
104     Char_t c = fgkCharacterMap(index%fgkSeparator2);
105     s += c;
106     index = index/fgkSeparator2;
107   }
108   return s;
109 }
110
111 //
112 // constructors/destructor
113 //
114
115 //_____________________________________________________________________________
116 AliMpExMap::AliMpExMap(Bool_t /*standardConstructor*/) 
117   : TObject(),
118     fMap(fgkDefaultSize),
119     fObjects(fgkDefaultSize),
120     fKeys(fgkDefaultSize)
121 {
122 /// Standard constructor
123
124   fObjects.SetOwner(fgkDefaultOwnership);
125 }
126
127 //_____________________________________________________________________________
128 AliMpExMap::AliMpExMap() 
129   : TObject(),
130     fMap(),
131     fObjects(),
132     fKeys()
133 {
134 /// Default constructor
135 }
136
137
138 //_____________________________________________________________________________
139 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
140   : TObject(),
141     fMap(),
142     fObjects(),
143     fKeys()
144
145 {
146   /// Copy ctor
147   rhs.Copy(*this);
148 }
149
150 //_____________________________________________________________________________
151 AliMpExMap&
152 AliMpExMap::operator=(const AliMpExMap& rhs)
153 {
154   /// Assignment operator
155   AliMpExMap tmp(rhs);
156   tmp.Copy(*this);
157   return *this;
158 }
159
160 //_____________________________________________________________________________
161 void
162 AliMpExMap::Copy(TObject& dest) const
163 {
164   /// Copy this to dest
165   /// Copy implies that dest will become owner of its objects, whatever
166   /// the ownership of (*this) is.
167   
168   AliDebug(1,"");
169   
170   TObject::Copy(dest);
171   AliMpExMap& m = static_cast<AliMpExMap&>(dest);
172   m.fKeys = fKeys;
173   m.fMap.Delete();
174   m.fObjects.Delete();
175   
176   for ( Int_t i = 0; i <= fObjects.GetLast(); ++i ) 
177   {
178     TObject* o = fObjects.At(i)->Clone();
179     if (!o)
180     {
181       AliError("Object was not cloned properly ! Please investigate...");
182     }
183     m.fObjects.AddLast(o);
184   }
185   m.FillMap();
186   m.fObjects.SetOwner(kTRUE);
187 }
188
189 //_____________________________________________________________________________
190 AliMpExMap::~AliMpExMap() 
191 {
192 /// Destructor 
193 }
194
195 //
196 // private methods
197 //
198
199 //_____________________________________________________________________________
200 void AliMpExMap::FillMap()
201 {
202 /// Fill transient map from the arrays of objects and keys
203
204   for (Int_t i=0; i<fObjects.GetEntriesFast(); i++) 
205     fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i)); 
206 }
207
208 //_____________________________________________________________________________
209 void AliMpExMap::AddKey(Long_t key)
210 {
211 /// Add key in array with checking size
212
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;
217   } 
218    
219   fKeys.AddAt(key, fObjects.GetEntriesFast());      
220 }
221
222 //
223 // public methods
224 //
225
226 //_____________________________________________________________________________
227 void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
228 {
229 /// Add object with its key to the map and arrays
230   
231   fMap.Add(GetIndex(key), (Long_t)object);
232   AddKey(GetIndex(key));
233   fObjects.Add(object);
234 }
235
236 //_____________________________________________________________________________
237 void AliMpExMap::Add(const TString& key, TObject* object)
238 {
239 /// Add object with its key to the map and arrays
240   
241   fMap.Add(GetIndex(key), (Long_t)object);
242   AddKey(GetIndex(key));
243   fObjects.Add(object);
244 }
245
246 //_____________________________________________________________________________
247 void AliMpExMap::Add(Int_t key, TObject* object)
248 {
249 /// Add object with its key to the map and arrays
250   
251   fMap.Add(key, (Long_t)object);
252   AddKey(key);
253   fObjects.Add(object);
254 }
255
256 //_____________________________________________________________________________
257 void AliMpExMap::SetSize(Int_t size)
258 {
259 /// Set given size to the key array
260
261   // fMap.Set(size);
262   // fObjects.Set(size);
263   fKeys.Set(size);
264
265
266 //_____________________________________________________________________________
267 void AliMpExMap::SetOwner(Bool_t owner)
268 {
269 /// Set given ownership to object array
270
271   fObjects.SetOwner(owner);
272 }  
273
274 //_____________________________________________________________________________
275 Int_t AliMpExMap::GetSize() const
276 {
277 /// Return the map size
278
279   return fObjects.GetEntriesFast();
280 }
281
282 //_____________________________________________________________________________
283 TExMapIter AliMpExMap::GetIterator() const
284 {
285 /// Return TExMap iterator set to the beginning of the map
286
287   return TExMapIter(&fMap);
288 }
289
290 //_____________________________________________________________________________
291 TObject* AliMpExMap::GetObject(Int_t index) const
292 {
293 /// Return the object via its index in the array
294 /// (This function makes possible looping over map as over an array)
295
296   if ( index < 0 || index >= fObjects.GetEntriesFast() ) {
297     AliErrorStream() << "Index outside limits" << endl;
298     return 0;
299   }
300   
301   return fObjects.UncheckedAt(index);
302 }      
303
304 //_____________________________________________________________________________
305 TObject* AliMpExMap::GetObjectFast(Int_t index) const
306 {
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.
310   
311   return fObjects.UncheckedAt(index);
312 }      
313
314 //_____________________________________________________________________________
315 TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
316 {
317 /// Return the object associated with the given key if found,
318 /// otherwise return 0
319
320   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
321 }
322
323 //_____________________________________________________________________________
324 TObject*  AliMpExMap::GetValue(const TString& key) const
325 {
326 /// Return the object associated with the given key if found,
327 /// otherwise return 0
328
329   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
330 }
331
332 //_____________________________________________________________________________
333 TObject*  AliMpExMap::GetValue(Int_t key) const
334 {
335 /// Return the object associated with the given key if found,
336 /// otherwise return 0
337
338   return reinterpret_cast<TObject*>(fMap.GetValue(key));
339 }
340
341 //_____________________________________________________________________________
342 void AliMpExMap::Streamer(TBuffer &R__b)
343 {
344 // Customized streamer                                                     \n
345 // After the arrays are read, fill the transient map
346
347   if (R__b.IsReading()) {
348     AliMpExMap::Class()->ReadBuffer(R__b, this);
349     FillMap();
350   } 
351   else {
352     AliMpExMap::Class()->WriteBuffer(R__b, this);
353   }
354 }