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