]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/mapping/AliMpExMap.cxx
- Reordering includes from most specific to more general ones
[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$
2c605e66 17// $MpId: AliMpExMap.cxx,v 1.4 2006/03/17 11:34:46 ivana Exp $
5006ec94 18// Category: basic
f7006443 19// ------------------------
5006ec94 20// Class AliMpExMap
21// ------------------------
22// Helper class making Root persistent TExMap
23// Author:Ivana Hrivnacova; IPN Orsay
24
617853db 25#include "AliMpExMap.h"
2c605e66 26#include "AliMpIntPair.h"
617853db 27
2c605e66 28#include "AliLog.h"
5006ec94 29
30#include <TClass.h>
5006ec94 31#include <TString.h>
2c605e66 32#include <Riostream.h>
5006ec94 33
2c605e66 34#include <stdlib.h>
5006ec94 35
5006ec94 36//
37// static members
38//
39
40const Int_t AliMpExMap::fgkDefaultSize = 300;
41const Bool_t AliMpExMap::fgkDefaultOwnership = true;
42
43const Int_t AliMpExMap::fgkSeparator1 = 10000;
44const Int_t AliMpExMap::fgkSeparator2 = 100;
45
46const TString AliMpExMap::fgkCharacterMap
47 = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
48
617853db 49ClassImp(AliMpExMap)
50
5006ec94 51//
52// static methods
53//
54
55//______________________________________________________________________________
56Long_t AliMpExMap::GetIndex(const AliMpIntPair& pair)
57{
58/// Convert the pair of integers to integer.
59
60 if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
2c605e66 61 AliFatalClass("Index out of limit.");
5006ec94 62 exit(1);
63 }
64
65 return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
66}
67
68//_____________________________________________________________________________
69Long_t AliMpExMap::GetIndex(const TString& s)
70{
71/// Convert the TString to integer.
72
73 if (s.Length() > 5) {
2c605e66 74 AliFatalClass("String too long.");
5006ec94 75 return 0;
76 }
77
78 Long_t index = 0;
79 for (Int_t i=s.Length()-1; i>=0; --i)
80 index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
81
82 return index;
83}
84
85//______________________________________________________________________________
86AliMpIntPair AliMpExMap::GetPair(Long_t index)
87{
88/// Convert the integer index to the pair of integers.
89
90 return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
91}
92
93//_____________________________________________________________________________
94TString AliMpExMap::GetString(Long_t index)
95{
96/// Convert the integer index to the string.
97
98 TString s;
99 while (index >0) {
100 Char_t c = fgkCharacterMap(index%fgkSeparator2);
101 s += c;
102 index = index/fgkSeparator2;
103 }
104 return s;
105}
106
107//
108// constructors/destructor
109//
110
111//_____________________________________________________________________________
112AliMpExMap::AliMpExMap(Bool_t /*standardConstructor*/)
113 : TObject(),
114 fMap(fgkDefaultSize),
115 fObjects(fgkDefaultSize),
116 fKeys(fgkDefaultSize)
117{
118/// Standard constructor
119
120 fObjects.SetOwner(fgkDefaultOwnership);
121}
122
123//_____________________________________________________________________________
124AliMpExMap::AliMpExMap()
125 : TObject(),
126 fMap(),
127 fObjects(),
128 fKeys()
129{
130/// Default constructor
131}
132
133//_____________________________________________________________________________
134AliMpExMap::~AliMpExMap()
135{
136/// Destructor
137}
138
139//
140// private methods
141//
142
143//_____________________________________________________________________________
144void AliMpExMap::FillMap()
145{
146/// Fill transient map from the arrays of objects and keys
147
148 for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
149 fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
150}
151
152//_____________________________________________________________________________
153void AliMpExMap::AddKey(Long_t key)
154{
155/// Add key in array with checking size
156
157 // Resize array if needed
158 if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
159 fKeys.Set(2*fKeys.GetSize());
2c605e66 160 AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl;
5006ec94 161 }
162
163 fKeys.AddAt(key, fObjects.GetEntriesFast());
164}
165
166//
167// public methods
168//
169
170//_____________________________________________________________________________
171void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
172{
173/// Add object with its key to the map and arrays
174
175 fMap.Add(GetIndex(key), (Long_t)object);
176 AddKey(GetIndex(key));
177 fObjects.Add(object);
178}
179
180//_____________________________________________________________________________
181void AliMpExMap::Add(const TString& key, TObject* object)
182{
183/// Add object with its key to the map and arrays
184
185 fMap.Add(GetIndex(key), (Long_t)object);
186 AddKey(GetIndex(key));
187 fObjects.Add(object);
188}
189
190//_____________________________________________________________________________
191void AliMpExMap::Add(Int_t key, TObject* object)
192{
193/// Add object with its key to the map and arrays
194
195 fMap.Add(key, (Long_t)object);
196 AddKey(key);
197 fObjects.Add(object);
198}
199
200//_____________________________________________________________________________
201void AliMpExMap::SetSize(Int_t size)
202{
203/// Set given size to the key array
204
205 // fMap.Set(size);
206 // fObjects.Set(size);
207 fKeys.Set(size);
208}
209
210//_____________________________________________________________________________
211void AliMpExMap::SetOwner(Bool_t owner)
212{
213/// Set given ownership to object array
214
215 fObjects.SetOwner(owner);
216}
217
218//_____________________________________________________________________________
219Int_t AliMpExMap::GetSize() const
220{
221/// Return TExMap iterator set to the beginning of the map
222
223 return fObjects.GetEntriesFast();
224}
225
226//_____________________________________________________________________________
227TExMapIter AliMpExMap::GetIterator() const
228{
229/// Return TExMap iterator set to the beginning of the map
230
231 return TExMapIter(&fMap);
232}
233
234//_____________________________________________________________________________
617853db 235TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
5006ec94 236{
237/// Return the object associated with the given key if found,
238/// otherwise return 0
239
617853db 240 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 241}
242
243//_____________________________________________________________________________
244TObject* AliMpExMap::GetValue(const TString& key) const
245{
246/// Return the object associated with the given key if found,
247/// otherwise return 0
248
617853db 249 return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
5006ec94 250}
251
252//_____________________________________________________________________________
253TObject* AliMpExMap::GetValue(Int_t key) const
254{
255/// Return the object associated with the given key if found,
256/// otherwise return 0
257
617853db 258 return reinterpret_cast<TObject*>(fMap.GetValue(key));
5006ec94 259}
260
261//_____________________________________________________________________________
262void AliMpExMap::Streamer(TBuffer &R__b)
263{
264/// Customized streamer \n
265/// After the arrays are read, fill the transient map
266
267 if (R__b.IsReading()) {
268 AliMpExMap::Class()->ReadBuffer(R__b, this);
269 FillMap();
270 }
271 else {
272 AliMpExMap::Class()->WriteBuffer(R__b, this);
273 }
274}