]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/mapping/AliMpExMap.cxx
Coding conventions
[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"
630711ed 29#include "AliMpExMapIterator.h"
617853db 30
2c605e66 31#include "AliLog.h"
5006ec94 32
33#include <TClass.h>
5006ec94 34#include <TString.h>
2c605e66 35#include <Riostream.h>
5006ec94 36
2c605e66 37#include <stdlib.h>
5006ec94 38
13985652 39/// \cond CLASSIMP
40ClassImp(AliMpExMap)
41/// \endcond
42
5006ec94 43//
44// static members
45//
46
47const Int_t AliMpExMap::fgkDefaultSize = 300;
48const Bool_t AliMpExMap::fgkDefaultOwnership = true;
49
50const Int_t AliMpExMap::fgkSeparator1 = 10000;
51const Int_t AliMpExMap::fgkSeparator2 = 100;
52
53const TString AliMpExMap::fgkCharacterMap
6a1e6897 54 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
5006ec94 55
56//
57// static methods
58//
59
60//______________________________________________________________________________
61Long_t AliMpExMap::GetIndex(const AliMpIntPair& pair)
62{
63/// Convert the pair of integers to integer.
64
630711ed 65 if ( pair.GetFirst() >= 0xFFFF || pair.GetSecond() >= 0xFFFF )
66 {
67 AliFatalClass("Index out of limit");
68 return 0;
69 }
70
71 return 1 + ( pair.GetFirst() | ( pair.GetSecond() << 16 ) );
72
73// if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
74// AliFatalClass("Index out of limit.");
75// exit(1);
76// }
77//
78// return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
5006ec94 79}
80
81//_____________________________________________________________________________
82Long_t AliMpExMap::GetIndex(const TString& s)
83{
84/// Convert the TString to integer.
85
86 if (s.Length() > 5) {
79072b9c 87 AliErrorClass("String too long.");
88 return -1;
5006ec94 89 }
90
91 Long_t index = 0;
92 for (Int_t i=s.Length()-1; i>=0; --i)
93 index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
94
95 return index;
96}
97
98//______________________________________________________________________________
99AliMpIntPair AliMpExMap::GetPair(Long_t index)
100{
ea49e931 101/// Convert the integer index to the pair of integers.
5006ec94 102
630711ed 103// return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
104 return AliMpIntPair(
105 ( (index-1) & 0xFFFF ) ,
106 ( (index-1) & 0xFFFF0000 ) >> 16 );
5006ec94 107}
108
109//_____________________________________________________________________________
110TString AliMpExMap::GetString(Long_t index)
111{
112/// Convert the integer index to the string.
113
114 TString s;
115 while (index >0) {
116 Char_t c = fgkCharacterMap(index%fgkSeparator2);
117 s += c;
118 index = index/fgkSeparator2;
119 }
120 return s;
121}
122
123//
124// constructors/destructor
125//
126
127//_____________________________________________________________________________
630711ed 128AliMpExMap::AliMpExMap()
5006ec94 129 : TObject(),
130 fMap(fgkDefaultSize),
131 fObjects(fgkDefaultSize),
132 fKeys(fgkDefaultSize)
133{
630711ed 134 /// Default constructor
5006ec94 135
136 fObjects.SetOwner(fgkDefaultOwnership);
137}
138
139//_____________________________________________________________________________
630711ed 140AliMpExMap::AliMpExMap(TRootIOCtor*)
5006ec94 141 : TObject(),
142 fMap(),
143 fObjects(),
144 fKeys()
145{
630711ed 146 /// "Root - I/O" constructor
5006ec94 147}
148
f8919723 149
150//_____________________________________________________________________________
151AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
152 : TObject(),
153 fMap(),
154 fObjects(),
155 fKeys()
156
157{
158 /// Copy ctor
159 rhs.Copy(*this);
160}
161
162//_____________________________________________________________________________
163AliMpExMap&
164AliMpExMap::operator=(const AliMpExMap& rhs)
165{
166 /// Assignment operator
e7796c12 167
168 // check assignment to self
169 if (this == &rhs) return *this;
170
52147223 171 rhs.Copy(*this);
f8919723 172 return *this;
173}
174
5006ec94 175//_____________________________________________________________________________
176AliMpExMap::~AliMpExMap()
177{
178/// Destructor
179}
180
181//
182// private methods
183//
184
185//_____________________________________________________________________________
186void AliMpExMap::FillMap()
187{
188/// Fill transient map from the arrays of objects and keys
189
190 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
191 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
192}
193
194//_____________________________________________________________________________
195void AliMpExMap::AddKey(Long_t key)
196{
197/// Add key in array with checking size
198
199 // Resize array if needed
200 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
201 fKeys.Set(2*fKeys.GetSize());
630711ed 202 AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
5006ec94 203 }
204
205 fKeys.AddAt(key, fObjects.GetEntriesFast());
206}
207
630711ed 208//_____________________________________________________________________________
209void
210AliMpExMap::Copy(TObject& dest) const
211{
212 /// Copy this to dest
213 /// Copy implies that dest will become owner of its objects, whatever
214 /// the ownership of (*this) is.
215
216 AliDebug(1,"");
217
218 TObject::Copy(dest);
219 AliMpExMap& m = static_cast<AliMpExMap&>(dest);
220 m.fKeys = fKeys;
221 m.fMap.Delete();
222 m.fObjects.Clear();
223
224 for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
225 {
226 TObject* o = fObjects.At(i)->Clone();
227 if (!o)
228 {
229 AliError("Object was not cloned properly ! Please investigate...");
230 }
231 m.fObjects.AddLast(o);
232 }
233 m.FillMap();
234 m.fObjects.SetOwner(kTRUE);
235}
236
5006ec94 237//
238// public methods
239//
240
7332f213 241//_____________________________________________________________________________
df165da6 242void AliMpExMap::Clear(Option_t* option)
7332f213 243{
244/// Clear memory
245
246 fMap.Delete();
df165da6 247 fObjects.Clear(option);
7332f213 248 fKeys.Reset();
249}
250
630711ed 251//_____________________________________________________________________________
252void AliMpExMap::Print(Option_t* opt) const
253{
254/// Print out
255
256 cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity())
257 << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries())
258 << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
259
260 TString sopt(opt);
261 sopt.ToUpper();
262
263 if ( sopt.Contains("FULL") )
264 {
265 TIter next(CreateIterator());
266 TObject* o;
267 while ( ( o = next() ) )
268 {
269 o->Print();
270 }
271 }
272}
273
5006ec94 274//_____________________________________________________________________________
275void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
276{
277/// Add object with its key to the map and arrays
278
279 fMap.Add(GetIndex(key), (Long_t)object);
280 AddKey(GetIndex(key));
281 fObjects.Add(object);
282}
283
284//_____________________________________________________________________________
285void AliMpExMap::Add(const TString& key, TObject* object)
286{
287/// Add object with its key to the map and arrays
288
289 fMap.Add(GetIndex(key), (Long_t)object);
290 AddKey(GetIndex(key));
291 fObjects.Add(object);
292}
293
294//_____________________________________________________________________________
295void AliMpExMap::Add(Int_t key, TObject* object)
296{
297/// Add object with its key to the map and arrays
298
299 fMap.Add(key, (Long_t)object);
300 AddKey(key);
301 fObjects.Add(object);
302}
303
304//_____________________________________________________________________________
305void AliMpExMap::SetSize(Int_t size)
306{
307/// Set given size to the key array
308
309 // fMap.Set(size);
310 // fObjects.Set(size);
311 fKeys.Set(size);
312}
313
314//_____________________________________________________________________________
315void AliMpExMap::SetOwner(Bool_t owner)
316{
317/// Set given ownership to object array
318
319 fObjects.SetOwner(owner);
320}
321
322//_____________________________________________________________________________
323Int_t AliMpExMap::GetSize() const
324{
6b431a48 325/// Return the map size
5006ec94 326
327 return fObjects.GetEntriesFast();
328}
329
330//_____________________________________________________________________________
630711ed 331Int_t AliMpExMap::GetCapacity() const
5006ec94 332{
630711ed 333 /// Return the map capacity
334
335 return fObjects.GetSize();
5006ec94 336}
337
6b431a48 338//_____________________________________________________________________________
630711ed 339AliMpExMapIterator*
340AliMpExMap::CreateIterator() const
6b431a48 341{
630711ed 342/// Return iterator set to the beginning of the map
6b431a48 343
630711ed 344 return new AliMpExMapIterator(*this);
345}
6b431a48 346
5006ec94 347//_____________________________________________________________________________
617853db 348TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
5006ec94 349{
350/// Return the object associated with the given key if found,
351/// otherwise return 0
352
617853db 353 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 354}
355
356//_____________________________________________________________________________
357TObject* AliMpExMap::GetValue(const TString& key) const
358{
359/// Return the object associated with the given key if found,
360/// otherwise return 0
361
617853db 362 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 363}
364
365//_____________________________________________________________________________
366TObject* AliMpExMap::GetValue(Int_t key) const
367{
368/// Return the object associated with the given key if found,
369/// otherwise return 0
370
617853db 371 return reinterpret_cast<TObject*>(fMap.GetValue(key));
5006ec94 372}
373
374//_____________________________________________________________________________
375void AliMpExMap::Streamer(TBuffer &R__b)
376{
78649106 377// Customized streamer \n
378// After the arrays are read, fill the transient map
5006ec94 379
380 if (R__b.IsReading()) {
381 AliMpExMap::Class()->ReadBuffer(R__b, this);
382 FillMap();
383 }
384 else {
385 AliMpExMap::Class()->WriteBuffer(R__b, this);
386 }
387}