]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/mapping/AliMpExMap.cxx
In AliMpPad:
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpExMap.cxx
CommitLineData
5006ec94 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$
13985652 17// $MpId: AliMpExMap.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
5006ec94 18// Category: basic
3d1463c8 19
20//-----------------------------------------------------------------------------
5006ec94 21// Class AliMpExMap
22// ------------------------
23// Helper class making Root persistent TExMap
24// Author:Ivana Hrivnacova; IPN Orsay
3d1463c8 25//-----------------------------------------------------------------------------
5006ec94 26
617853db 27#include "AliMpExMap.h"
2c605e66 28#include "AliMpIntPair.h"
617853db 29
2c605e66 30#include "AliLog.h"
5006ec94 31
32#include <TClass.h>
5006ec94 33#include <TString.h>
2c605e66 34#include <Riostream.h>
5006ec94 35
2c605e66 36#include <stdlib.h>
5006ec94 37
13985652 38/// \cond CLASSIMP
39ClassImp(AliMpExMap)
40/// \endcond
41
5006ec94 42//
43// static members
44//
45
46const Int_t AliMpExMap::fgkDefaultSize = 300;
47const Bool_t AliMpExMap::fgkDefaultOwnership = true;
48
49const Int_t AliMpExMap::fgkSeparator1 = 10000;
50const Int_t AliMpExMap::fgkSeparator2 = 100;
51
52const TString AliMpExMap::fgkCharacterMap
6a1e6897 53 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
5006ec94 54
55//
56// static methods
57//
58
59//______________________________________________________________________________
60Long_t AliMpExMap::GetIndex(const AliMpIntPair& pair)
61{
62/// Convert the pair of integers to integer.
63
64 if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
2c605e66 65 AliFatalClass("Index out of limit.");
5006ec94 66 exit(1);
67 }
68
69 return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
70}
71
72//_____________________________________________________________________________
73Long_t AliMpExMap::GetIndex(const TString& s)
74{
75/// Convert the TString to integer.
76
77 if (s.Length() > 5) {
79072b9c 78 AliErrorClass("String too long.");
79 return -1;
5006ec94 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//______________________________________________________________________________
90AliMpIntPair 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//_____________________________________________________________________________
98TString 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//_____________________________________________________________________________
116AliMpExMap::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//_____________________________________________________________________________
128AliMpExMap::AliMpExMap()
129 : TObject(),
130 fMap(),
131 fObjects(),
132 fKeys()
133{
134/// Default constructor
135}
136
f8919723 137
138//_____________________________________________________________________________
139AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
140 : TObject(),
141 fMap(),
142 fObjects(),
143 fKeys()
144
145{
146 /// Copy ctor
147 rhs.Copy(*this);
148}
149
150//_____________________________________________________________________________
151AliMpExMap&
152AliMpExMap::operator=(const AliMpExMap& rhs)
153{
154 /// Assignment operator
e7796c12 155
156 // check assignment to self
157 if (this == &rhs) return *this;
158
52147223 159 rhs.Copy(*this);
f8919723 160 return *this;
161}
162
163//_____________________________________________________________________________
164void
165AliMpExMap::Copy(TObject& dest) const
166{
167 /// Copy this to dest
168 /// Copy implies that dest will become owner of its objects, whatever
169 /// the ownership of (*this) is.
170
171 AliDebug(1,"");
172
173 TObject::Copy(dest);
174 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
175 m.fKeys = fKeys;
176 m.fMap.Delete();
7332f213 177 m.fObjects.Clear();
f8919723 178
179 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
180 {
181 TObject* o = fObjects.At(i)->Clone();
182 if (!o)
183 {
184 AliError("Object was not cloned properly ! Please investigate...");
185 }
186 m.fObjects.AddLast(o);
187 }
188 m.FillMap();
189 m.fObjects.SetOwner(kTRUE);
190}
191
5006ec94 192//_____________________________________________________________________________
193AliMpExMap::~AliMpExMap()
194{
195/// Destructor
196}
197
198//
199// private methods
200//
201
202//_____________________________________________________________________________
203void AliMpExMap::FillMap()
204{
205/// Fill transient map from the arrays of objects and keys
206
207 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
208 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
209}
210
211//_____________________________________________________________________________
212void AliMpExMap::AddKey(Long_t key)
213{
214/// Add key in array with checking size
215
216 // Resize array if needed
217 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
218 fKeys.Set(2*fKeys.GetSize());
2c605e66 219 AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl;
5006ec94 220 }
221
222 fKeys.AddAt(key, fObjects.GetEntriesFast());
223}
224
225//
226// public methods
227//
228
7332f213 229//_____________________________________________________________________________
df165da6 230void AliMpExMap::Clear(Option_t* option)
7332f213 231{
232/// Clear memory
233
234 fMap.Delete();
df165da6 235 fObjects.Clear(option);
7332f213 236 fKeys.Reset();
237}
238
5006ec94 239//_____________________________________________________________________________
240void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
241{
242/// Add object with its key to the map and arrays
243
244 fMap.Add(GetIndex(key), (Long_t)object);
245 AddKey(GetIndex(key));
246 fObjects.Add(object);
247}
248
249//_____________________________________________________________________________
250void AliMpExMap::Add(const TString& key, TObject* object)
251{
252/// Add object with its key to the map and arrays
253
254 fMap.Add(GetIndex(key), (Long_t)object);
255 AddKey(GetIndex(key));
256 fObjects.Add(object);
257}
258
259//_____________________________________________________________________________
260void AliMpExMap::Add(Int_t key, TObject* object)
261{
262/// Add object with its key to the map and arrays
263
264 fMap.Add(key, (Long_t)object);
265 AddKey(key);
266 fObjects.Add(object);
267}
268
269//_____________________________________________________________________________
270void AliMpExMap::SetSize(Int_t size)
271{
272/// Set given size to the key array
273
274 // fMap.Set(size);
275 // fObjects.Set(size);
276 fKeys.Set(size);
277}
278
279//_____________________________________________________________________________
280void AliMpExMap::SetOwner(Bool_t owner)
281{
282/// Set given ownership to object array
283
284 fObjects.SetOwner(owner);
285}
286
287//_____________________________________________________________________________
288Int_t AliMpExMap::GetSize() const
289{
6b431a48 290/// Return the map size
5006ec94 291
292 return fObjects.GetEntriesFast();
293}
294
295//_____________________________________________________________________________
296TExMapIter AliMpExMap::GetIterator() const
297{
298/// Return TExMap iterator set to the beginning of the map
299
300 return TExMapIter(&fMap);
301}
302
6b431a48 303//_____________________________________________________________________________
304TObject* AliMpExMap::GetObject(Int_t index) const
305{
306/// Return the object via its index in the array
307/// (This function makes possible looping over map as over an array)
308
309 if ( index < 0 || index >= fObjects.GetEntriesFast() ) {
310 AliErrorStream() << "Index outside limits" << endl;
311 return 0;
312 }
313
4c7ce41d 314 return fObjects.UncheckedAt(index);
315}
316
317//_____________________________________________________________________________
318TObject* AliMpExMap::GetObjectFast(Int_t index) const
319{
320 /// Return the object via its index in the array
321 /// (This function makes possible looping over map as over an array)
322 /// without bound checking.
323
324 return fObjects.UncheckedAt(index);
6b431a48 325}
326
5006ec94 327//_____________________________________________________________________________
617853db 328TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
5006ec94 329{
330/// Return the object associated with the given key if found,
331/// otherwise return 0
332
617853db 333 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 334}
335
336//_____________________________________________________________________________
337TObject* AliMpExMap::GetValue(const TString& key) const
338{
339/// Return the object associated with the given key if found,
340/// otherwise return 0
341
617853db 342 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 343}
344
345//_____________________________________________________________________________
346TObject* AliMpExMap::GetValue(Int_t key) const
347{
348/// Return the object associated with the given key if found,
349/// otherwise return 0
350
617853db 351 return reinterpret_cast<TObject*>(fMap.GetValue(key));
5006ec94 352}
353
354//_____________________________________________________________________________
355void AliMpExMap::Streamer(TBuffer &R__b)
356{
78649106 357// Customized streamer \n
358// After the arrays are read, fill the transient map
5006ec94 359
360 if (R__b.IsReading()) {
361 AliMpExMap::Class()->ReadBuffer(R__b, this);
362 FillMap();
363 }
364 else {
365 AliMpExMap::Class()->WriteBuffer(R__b, this);
366 }
367}