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