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