AliRoot Core  a565103 (a565103)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliMpStringObjMap.cxx
Go to the documentation of this file.
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;
40 
41 //
42 // static private methods
43 //
44 
45 //______________________________________________________________________________
46 const TString& AliMpStringObjMap::GetUndefinedKey()
47 {
49  static const TString kUndefinedKey = "Undefined";
50  return kUndefinedKey;
51 }
52 
53 //
54 // ctors, dtor
55 //
56 
57 //______________________________________________________________________________
59  : TObject(),
60  fNofItems(0),
61  fFirstArray(),
62  fSecondArray(),
63  fCurrentIndex(0)
64 {
66 
67  fFirstArray.SetOwner(true);
68  fSecondArray.SetOwner(isOwner);
69 }
70 
71 //______________________________________________________________________________
73 {
75 
76  fFirstArray.Delete();
77 }
78 
79 //
80 // public methods
81 //
82 
83 //______________________________________________________________________________
84 Bool_t AliMpStringObjMap::Add(const TString& first, TObject* second)
85 {
87 
88  TObject* second2 = Get(first);
89  if ( second2 ) {
90  AliError(Form("%s is already present in the map", first.Data()));
91  return false;
92  }
93 
94  fFirstArray.Add(new TObjString(first));
95  fSecondArray.Add(second);
96  fNofItems++;
97 
98  return true;
99 }
100 
101 //______________________________________________________________________________
102 TObject* AliMpStringObjMap::Get(const TString& first) const
103 {
105 
106  for (Int_t i=0; i<fNofItems; i++) {
107  if ( ((TObjString*)fFirstArray.At(i))->GetString() == first )
108  return fSecondArray.At(i);
109  }
110 
111  return 0;
112 }
113 
114 //______________________________________________________________________________
116 {
118 
119  return fNofItems;
120 }
121 
122 //______________________________________________________________________________
123 void AliMpStringObjMap::Clear(Option_t* /*option*/)
124 {
126 
127  fNofItems = 0;
128  fFirstArray.Delete();
129  fSecondArray.Delete();
130 }
131 
132 //______________________________________________________________________________
133 void AliMpStringObjMap::Print(const char* /*option*/) const
134 {
136 
137  for (Int_t i=0; i<fNofItems; i++) {
138  cout << setw(4)
139  << i << " "
140  << ((TObjString*)fFirstArray.At(i))->GetString()
141  << " "
142  << setw(5)
143  << fSecondArray.At(i)
144  << endl;
145  }
146 }
147 
148 //______________________________________________________________________________
149 void AliMpStringObjMap::Print(const TString& key, ofstream& out) const
150 {
152 
153  for (Int_t i=0; i<fNofItems; i++) {
154  out << key << " "
155  << ((TObjString*)fFirstArray.At(i))->GetString()
156  << " "
157  << setw(5)
158  << fSecondArray.At(i)
159  << endl;
160  }
161 }
162 
163 //______________________________________________________________________________
165 {
167 
168  fCurrentIndex = 0;
169 }
170 
171 
172 //______________________________________________________________________________
174 {
177 
178  ++fCurrentIndex;
179 }
180 
181 
182 //______________________________________________________________________________
184 {
186 
187  if ( fCurrentIndex >= fNofItems ) return 0;
188 
189  return fSecondArray.At(fCurrentIndex);
190 }
191 
192 
193 //______________________________________________________________________________
195 {
197 
198  if ( fCurrentIndex >= fNofItems ) return GetUndefinedKey();
199 
200  return ((TObjString*)fFirstArray.At(fCurrentIndex))->GetString();
201 }
202 
203 
204 //______________________________________________________________________________
206 {
208 
209  return fCurrentIndex >= fNofItems;
210 }
211 
AliMpStringObjMap(Bool_t isOwner=false)
TObjArray fFirstArray
first item array
static const TString & GetUndefinedKey()
Bool_t Add(const TString &first, TObject *second)
Int_t GetNofItems() const
Int_t fCurrentIndex
current item index (for iteration)
ClassImp(TPCGenInfo)
Definition: AliTPCCmpNG.C:254
Substitutes map <string, TObject> which ALICE does not allow to use.
Bool_t IsDone() const
Int_t fNofItems
number of items
TObject * Get(const TString &first) const
TObjArray fSecondArray
second item array
virtual void Clear(Option_t *="")
virtual void Print(const char *="") const