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