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