]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONStringIntMap.cxx
from Y. Schutz
[u/mrichter/AliRoot.git] / MUON / AliMUONStringIntMap.cxx
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
18 //-----------------------------------------------------------------------------
19 // Class AliMUONStringIntMap
20 // ------------------------------------ 
21 // Helper class that substitutes map <string, int> 
22 // which ALICE does not allow to use 
23 // Author: Ivana Hrivnacova, IPN Orsay
24 //-----------------------------------------------------------------------------
25
26 #include <Riostream.h>
27 #include <TObjString.h>
28
29 #include "AliMUONStringIntMap.h"
30 #include "AliLog.h"
31
32 /// \cond CLASSIMP
33 ClassImp(AliMUONStringIntMap)
34 /// \endcond
35
36 //______________________________________________________________________________
37 AliMUONStringIntMap::AliMUONStringIntMap()
38  : TObject(),
39    fNofItems(0),
40    fFirstArray(100),
41    fSecondArray(100)
42 {
43 /// Standard constructor
44
45   fFirstArray.SetOwner(true);
46 }
47
48 //______________________________________________________________________________
49 AliMUONStringIntMap::~AliMUONStringIntMap()
50 {
51 /// Destructor
52
53   fFirstArray.Delete();
54 }
55
56 //
57 // public methods
58 //
59
60 //______________________________________________________________________________
61 Bool_t  AliMUONStringIntMap::Add(const TString& first, Int_t second)
62 {
63 /// Add map element if first not yet present
64   
65   Int_t second2 = Get(first);
66   if ( second2 > 0 ) {
67     AliError(Form("%s is already present in the map", first.Data()));
68     return false;
69   }
70   
71   // Resize TArrayI if needed
72   if (fSecondArray.GetSize() == fNofItems) fSecondArray.Set(2*fNofItems);
73   
74   fFirstArray.Add(new TObjString(first)); 
75   fSecondArray.AddAt(second, fNofItems);
76   fNofItems++;
77    
78   return true;
79 }  
80
81 //______________________________________________________________________________
82 Bool_t  AliMUONStringIntMap::Set(const TString& first, Int_t second)
83 {
84   /// Set map element
85
86   Int_t index = Contains(first);
87   if ( index < 0 )
88   {
89     return Add(first,second);
90   }
91     
92   fSecondArray.AddAt(second, index);
93   
94   return true;
95 }  
96
97 //______________________________________________________________________________
98 Int_t 
99 AliMUONStringIntMap::Contains(const TString& first) const
100 {
101   /// Whether this map contains the string 'first' or not
102   
103   for (Int_t i=0; i<fNofItems; i++) 
104   {
105     if ( ((TObjString*)fFirstArray.At(i))->GetString() == first )
106     {
107       return i;
108     }
109   }
110   
111   return -1;
112 }      
113
114 //______________________________________________________________________________
115 Int_t  AliMUONStringIntMap::Get(const TString& first) const
116 {
117 /// Find the element with specified key (first)
118   
119   for (Int_t i=0; i<fNofItems; i++) {
120     if ( ((TObjString*)fFirstArray.At(i))->GetString() == first )
121       return fSecondArray.At(i);
122   }
123   
124   return 0;
125 }      
126
127 //______________________________________________________________________________
128 Int_t  AliMUONStringIntMap::GetNofItems() const
129 {
130 /// Return the number of elements
131
132   return fNofItems;
133 }  
134
135 //______________________________________________________________________________
136 void  AliMUONStringIntMap::Clear(Option_t* /*option*/)
137 {
138 /// Delete the elements
139
140   fNofItems = 0;
141   fFirstArray.Delete();
142   fSecondArray.Reset();
143 }  
144     
145 //______________________________________________________________________________
146 void AliMUONStringIntMap::Print(const char* /*option*/) const
147 {
148 /// Print the map elements
149
150   for (Int_t i=0; i<fNofItems; i++) {
151     cout << setw(4)
152          << i << "  "
153          << ((TObjString*)fFirstArray.At(i))->GetString()
154          << "  "
155          << setw(5)
156          << fSecondArray.At(i)
157          << endl;
158   }
159 }        
160
161 //______________________________________________________________________________
162 void AliMUONStringIntMap::Print(const TString& key, ofstream& out) const
163 {
164 /// Print the map elements preceded by a key word
165
166   for (Int_t i=0; i<fNofItems; i++) {
167     out  << key << "  "
168          << ((TObjString*)fFirstArray.At(i))->GetString()
169          << "  "
170          << setw(5)
171          << fSecondArray.At(i)
172          << endl;
173   }
174 }