]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/mapping/AliMpExMap.cxx
In assignment operator: call Copy function only once
[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
52147223 155 rhs.Copy(*this);
f8919723 156 return *this;
157}
158
159//_____________________________________________________________________________
160void
161AliMpExMap::Copy(TObject& dest) const
162{
163 /// Copy this to dest
164 /// Copy implies that dest will become owner of its objects, whatever
165 /// the ownership of (*this) is.
166
167 AliDebug(1,"");
168
169 TObject::Copy(dest);
170 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
171 m.fKeys = fKeys;
172 m.fMap.Delete();
173 m.fObjects.Delete();
174
175 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
176 {
177 TObject* o = fObjects.At(i)->Clone();
178 if (!o)
179 {
180 AliError("Object was not cloned properly ! Please investigate...");
181 }
182 m.fObjects.AddLast(o);
183 }
184 m.FillMap();
185 m.fObjects.SetOwner(kTRUE);
186}
187
5006ec94 188//_____________________________________________________________________________
189AliMpExMap::~AliMpExMap()
190{
191/// Destructor
192}
193
194//
195// private methods
196//
197
198//_____________________________________________________________________________
199void AliMpExMap::FillMap()
200{
201/// Fill transient map from the arrays of objects and keys
202
203 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
204 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
205}
206
207//_____________________________________________________________________________
208void AliMpExMap::AddKey(Long_t key)
209{
210/// Add key in array with checking size
211
212 // Resize array if needed
213 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
214 fKeys.Set(2*fKeys.GetSize());
2c605e66 215 AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl;
5006ec94 216 }
217
218 fKeys.AddAt(key, fObjects.GetEntriesFast());
219}
220
221//
222// public methods
223//
224
225//_____________________________________________________________________________
226void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
227{
228/// Add object with its key to the map and arrays
229
230 fMap.Add(GetIndex(key), (Long_t)object);
231 AddKey(GetIndex(key));
232 fObjects.Add(object);
233}
234
235//_____________________________________________________________________________
236void AliMpExMap::Add(const TString& key, TObject* object)
237{
238/// Add object with its key to the map and arrays
239
240 fMap.Add(GetIndex(key), (Long_t)object);
241 AddKey(GetIndex(key));
242 fObjects.Add(object);
243}
244
245//_____________________________________________________________________________
246void AliMpExMap::Add(Int_t key, TObject* object)
247{
248/// Add object with its key to the map and arrays
249
250 fMap.Add(key, (Long_t)object);
251 AddKey(key);
252 fObjects.Add(object);
253}
254
255//_____________________________________________________________________________
256void AliMpExMap::SetSize(Int_t size)
257{
258/// Set given size to the key array
259
260 // fMap.Set(size);
261 // fObjects.Set(size);
262 fKeys.Set(size);
263}
264
265//_____________________________________________________________________________
266void AliMpExMap::SetOwner(Bool_t owner)
267{
268/// Set given ownership to object array
269
270 fObjects.SetOwner(owner);
271}
272
273//_____________________________________________________________________________
274Int_t AliMpExMap::GetSize() const
275{
6b431a48 276/// Return the map size
5006ec94 277
278 return fObjects.GetEntriesFast();
279}
280
281//_____________________________________________________________________________
282TExMapIter AliMpExMap::GetIterator() const
283{
284/// Return TExMap iterator set to the beginning of the map
285
286 return TExMapIter(&fMap);
287}
288
6b431a48 289//_____________________________________________________________________________
290TObject* AliMpExMap::GetObject(Int_t index) const
291{
292/// Return the object via its index in the array
293/// (This function makes possible looping over map as over an array)
294
295 if ( index < 0 || index >= fObjects.GetEntriesFast() ) {
296 AliErrorStream() << "Index outside limits" << endl;
297 return 0;
298 }
299
4c7ce41d 300 return fObjects.UncheckedAt(index);
301}
302
303//_____________________________________________________________________________
304TObject* AliMpExMap::GetObjectFast(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 /// without bound checking.
309
310 return fObjects.UncheckedAt(index);
6b431a48 311}
312
5006ec94 313//_____________________________________________________________________________
617853db 314TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
5006ec94 315{
316/// Return the object associated with the given key if found,
317/// otherwise return 0
318
617853db 319 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 320}
321
322//_____________________________________________________________________________
323TObject* AliMpExMap::GetValue(const TString& key) const
324{
325/// Return the object associated with the given key if found,
326/// otherwise return 0
327
617853db 328 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 329}
330
331//_____________________________________________________________________________
332TObject* AliMpExMap::GetValue(Int_t key) const
333{
334/// Return the object associated with the given key if found,
335/// otherwise return 0
336
617853db 337 return reinterpret_cast<TObject*>(fMap.GetValue(key));
5006ec94 338}
339
340//_____________________________________________________________________________
341void AliMpExMap::Streamer(TBuffer &R__b)
342{
78649106 343// Customized streamer \n
344// After the arrays are read, fill the transient map
5006ec94 345
346 if (R__b.IsReading()) {
347 AliMpExMap::Class()->ReadBuffer(R__b, this);
348 FillMap();
349 }
350 else {
351 AliMpExMap::Class()->WriteBuffer(R__b, this);
352 }
353}