]>
Commit | Line | Data |
---|---|---|
c623aa64 | 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: AliMpStringObjMap.cxx,v 1.4 2006/05/24 13:58:29 ivana Exp $ |
f7006443 | 18 | |
3d1463c8 | 19 | //----------------------------------------------------------------------------- |
c623aa64 | 20 | // Class AliMpStringObjMap |
21 | // ------------------------------------ | |
22 | // Helper class that substitutes map <string, int> | |
23 | // which ALICE does not allow to use | |
c623aa64 | 24 | // Author: Ivana Hrivnacova, IPN Orsay |
3d1463c8 | 25 | //----------------------------------------------------------------------------- |
c623aa64 | 26 | |
c623aa64 | 27 | #include "AliMpStringObjMap.h" |
2c605e66 | 28 | |
c623aa64 | 29 | #include "AliLog.h" |
30 | ||
2c605e66 | 31 | #include <TObjString.h> |
32 | #include <Riostream.h> | |
33 | ||
13985652 | 34 | /// \cond CLASSIMP |
c623aa64 | 35 | ClassImp(AliMpStringObjMap) |
13985652 | 36 | /// \endcond |
c623aa64 | 37 | |
2a116780 | 38 | const TString AliMpStringObjMap::fgkUndefinedKey = "Undefined"; |
39 | ||
c623aa64 | 40 | //______________________________________________________________________________ |
973e9d18 | 41 | AliMpStringObjMap::AliMpStringObjMap(Bool_t isOwner) |
c623aa64 | 42 | : TObject(), |
43 | fNofItems(0), | |
44 | fFirstArray(), | |
9993e7da | 45 | fSecondArray(), |
46 | fCurrentIndex(0) | |
c623aa64 | 47 | { |
48 | /// Standard constructor | |
49 | ||
50 | fFirstArray.SetOwner(true); | |
973e9d18 | 51 | fSecondArray.SetOwner(isOwner); |
c623aa64 | 52 | } |
53 | ||
c623aa64 | 54 | //______________________________________________________________________________ |
55 | AliMpStringObjMap::~AliMpStringObjMap() | |
56 | { | |
57 | /// Destructor | |
58 | ||
59 | fFirstArray.Delete(); | |
60 | } | |
61 | ||
c623aa64 | 62 | // |
63 | // public methods | |
64 | // | |
65 | ||
66 | //______________________________________________________________________________ | |
67 | Bool_t AliMpStringObjMap::Add(const TString& first, TObject* second) | |
68 | { | |
69 | /// Add map element if first not yet present | |
70 | ||
71 | TObject* second2 = Get(first); | |
72 | if ( second2 ) { | |
73 | AliError(Form("%s is already present in the map", first.Data())); | |
74 | return false; | |
75 | } | |
76 | ||
77 | fFirstArray.Add(new TObjString(first)); | |
78 | fSecondArray.Add(second); | |
79 | fNofItems++; | |
80 | ||
81 | return true; | |
82 | } | |
83 | ||
84 | //______________________________________________________________________________ | |
85 | TObject* AliMpStringObjMap::Get(const TString& first) const | |
86 | { | |
87 | /// Find the element with specified key (first) | |
88 | ||
89 | for (Int_t i=0; i<fNofItems; i++) { | |
90 | if ( ((TObjString*)fFirstArray.At(i))->GetString() == first ) | |
91 | return fSecondArray.At(i); | |
92 | } | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
97 | //______________________________________________________________________________ | |
98 | Int_t AliMpStringObjMap::GetNofItems() const | |
99 | { | |
100 | /// Return the number of elements | |
101 | ||
102 | return fNofItems; | |
103 | } | |
104 | ||
105 | //______________________________________________________________________________ | |
106 | void AliMpStringObjMap::Clear(Option_t* /*option*/) | |
107 | { | |
108 | /// Delete the elements | |
109 | ||
110 | fNofItems = 0; | |
111 | fFirstArray.Delete(); | |
112 | fSecondArray.Delete(); | |
113 | } | |
114 | ||
115 | //______________________________________________________________________________ | |
116 | void AliMpStringObjMap::Print(const char* /*option*/) const | |
117 | { | |
118 | /// Print the map elements | |
119 | ||
120 | for (Int_t i=0; i<fNofItems; i++) { | |
121 | cout << setw(4) | |
122 | << i << " " | |
123 | << ((TObjString*)fFirstArray.At(i))->GetString() | |
124 | << " " | |
125 | << setw(5) | |
126 | << fSecondArray.At(i) | |
127 | << endl; | |
128 | } | |
129 | } | |
130 | ||
131 | //______________________________________________________________________________ | |
132 | void AliMpStringObjMap::Print(const TString& key, ofstream& out) const | |
133 | { | |
f5671fc3 | 134 | /// Special printing |
c623aa64 | 135 | |
136 | for (Int_t i=0; i<fNofItems; i++) { | |
137 | out << key << " " | |
138 | << ((TObjString*)fFirstArray.At(i))->GetString() | |
139 | << " " | |
140 | << setw(5) | |
141 | << fSecondArray.At(i) | |
142 | << endl; | |
143 | } | |
144 | } | |
2a116780 | 145 | |
146 | //______________________________________________________________________________ | |
147 | void AliMpStringObjMap::First() | |
148 | { | |
149 | /// Set iterator to the first item and return its object | |
150 | ||
151 | fCurrentIndex = 0; | |
152 | } | |
153 | ||
154 | ||
155 | //______________________________________________________________________________ | |
156 | void AliMpStringObjMap::Next() | |
157 | { | |
158 | /// Set iterator to the next item and return its object | |
159 | /// Return 0 if there are no more items | |
160 | ||
161 | ++fCurrentIndex; | |
162 | } | |
163 | ||
164 | ||
165 | //______________________________________________________________________________ | |
166 | TObject* AliMpStringObjMap::CurrentItem() | |
167 | { | |
168 | /// Set iterator to the first item and return its object | |
169 | ||
170 | if ( fCurrentIndex >= fNofItems ) return 0; | |
171 | ||
172 | return fSecondArray.At(fCurrentIndex); | |
173 | } | |
174 | ||
175 | ||
176 | //______________________________________________________________________________ | |
177 | TString AliMpStringObjMap::CurrentKey() | |
178 | { | |
179 | /// Set iterator to the first item and return its object | |
180 | ||
181 | if ( fCurrentIndex >= fNofItems ) return fgkUndefinedKey; | |
182 | ||
183 | return ((TObjString*)fFirstArray.At(fCurrentIndex))->GetString(); | |
184 | } | |
185 | ||
186 | ||
187 | //______________________________________________________________________________ | |
188 | Bool_t AliMpStringObjMap::IsDone() const | |
189 | { | |
190 | /// Return true if the iterator reached the end of map | |
191 | ||
192 | return fCurrentIndex >= fNofItems; | |
193 | } | |
194 |