Replacement of AliMpIntPair object with algoritmic
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpArrayI.cxx
CommitLineData
0de80ab2 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: AliMpArrayI.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
18// Category: basic
3d1463c8 19
20//-----------------------------------------------------------------------------
0de80ab2 21// Class AliMpArrayI
22// ------------------------
23// Helper class for sorted integer array
24// Author:Ivana Hrivnacova; IPN Orsay
3d1463c8 25//-----------------------------------------------------------------------------
0de80ab2 26
27#include "AliMpArrayI.h"
0de80ab2 28
29#include "AliLog.h"
30
31#include <TClass.h>
32#include <TString.h>
33#include <Riostream.h>
34
35#include <stdlib.h>
3e54ea60 36#include <limits.h>
0de80ab2 37
38/// \cond CLASSIMP
39ClassImp(AliMpArrayI)
40/// \endcond
41
42const Int_t AliMpArrayI::fgkDefaultSize = 100;
43
44//_____________________________________________________________________________
3e54ea60 45AliMpArrayI::AliMpArrayI(Bool_t sort)
0de80ab2 46 : TObject(),
3e54ea60 47 fSort(sort),
0de80ab2 48 fNofValues(0),
3e54ea60 49 fValues(fgkDefaultSize),
168e9c4d 50 fMinValue(INT_MAX),
51 fMaxValue(INT_MIN)
0de80ab2 52{
53/// Standard & default constructor
54
55}
56
57//_____________________________________________________________________________
58AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/)
59 : TObject(),
3e54ea60 60 fSort(),
0de80ab2 61 fNofValues(),
3e54ea60 62 fValues(),
168e9c4d 63 fMinValue(),
64 fMaxValue()
0de80ab2 65{
66/// IO constructor
67}
68
69//_____________________________________________________________________________
70AliMpArrayI::~AliMpArrayI()
71{
72/// Destructor
73}
74
75//
76// private methods
77//
78
79//_____________________________________________________________________________
80Int_t AliMpArrayI::GetPosition(Int_t value) const
81{
82/// Return the new positon where the value should be put
83
84 for ( Int_t i=0; i<fNofValues; i++ ) {
85 if ( fValues.At(i) > value ) return i;
86 }
87
88 return fNofValues;
89}
90
91//
92// public methods
93//
94
95//_____________________________________________________________________________
c3882ed0 96Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
0de80ab2 97{
98/// Add object with its key to the map and arrays
99
100 // Resize array if needed
c3882ed0 101 if ( fValues.GetSize() == fNofValues )
102 {
103 fValues.Set(2*fValues.GetSize());
104 if ( warn )
105 {
106 AliWarningStream() << "Resized array." << endl;
107 }
0de80ab2 108 }
109
3e54ea60 110 // The position for the new value
111 Int_t pos;
112 if ( fSort ) {
113 pos = GetPosition(value);
114
115 // Move elements
116 for ( Int_t i=fNofValues; i>=pos; i-- )
117 fValues.AddAt(fValues.At(i), i+1);
118 }
119 else
120 pos = fNofValues;
121
0de80ab2 122 // Add the new value in freed space
123 fValues.AddAt(value, pos);
124 ++fNofValues;
125
3e54ea60 126 // Update linits
168e9c4d 127 if ( value < fMinValue ) fMinValue = value;
128 if ( value > fMaxValue ) fMaxValue = value;;
3e54ea60 129
0de80ab2 130 return true;
131}
132
133//_____________________________________________________________________________
134Bool_t AliMpArrayI::Remove(Int_t value)
135{
3e54ea60 136/// Remove value from the array
0de80ab2 137
138 // Find the position for the new value
139 Int_t pos = GetPosition(value);
140
141 // Return if value is not present
142 if ( pos == fNofValues ) return false;
143
144 // Move elements
145 for ( Int_t i=pos; i<fNofValues-1; i++ )
146 fValues.AddAt(fValues.At(i+1), i);
147
148 // Decrement number of values
149 --fNofValues;
150
151 return true;
152}
153
154//_____________________________________________________________________________
ae649dcb 155Bool_t AliMpArrayI::Revert()
156{
157/// Revert the order of elements
158
159 if ( fSort ) {
160 AliErrorStream() << "Cannot revert sorted array." << endl;
161 return false;
162 }
163
164 Int_t size = GetSize();
165 TArrayI newArray(size);
166 Int_t idx = 0 ;
167 for ( Int_t i = size-1 ; i >= 0 ; i--) {
168 Int_t value = GetValue(i);
169 newArray.AddAt(value,idx++);
170 }
171
172 for (Int_t i = 0; i < size ; i++) {
173 fValues[i]=newArray.At(i);
174 }
175 return true;
176}
177
178//_____________________________________________________________________________
179void AliMpArrayI::Reset()
180{
181/// Reset the array
182
183 fValues.Set(fgkDefaultSize);
184 fNofValues = 0;
168e9c4d 185 fMinValue = INT_MAX;
186 fMaxValue = INT_MIN;
ae649dcb 187}
188
189//_____________________________________________________________________________
0de80ab2 190void AliMpArrayI::SetSize(Int_t size)
191{
192/// Set given size to the array
193
194 fValues.Set(size);
195}
196
197//_____________________________________________________________________________
198Int_t AliMpArrayI::GetSize() const
199{
200/// Return the map size
201
202 return fNofValues;
203}
204
205//_____________________________________________________________________________
206Int_t AliMpArrayI::GetValue(Int_t index) const
207{
208/// Return the index-th value
209
210 if ( index < 0 || index >= fNofValues ) {
211 AliErrorStream() << "Index outside limits" << endl;
212 return 0;
213 }
214
215 return fValues.At(index);
216}
217
218//_____________________________________________________________________________
219Bool_t AliMpArrayI::HasValue(Int_t value) const
220{
221/// Return true if contains the given value
222
223 if ( ! fNofValues ) return false;
224
168e9c4d 225 if ( value < fMinValue || value > fMaxValue )
0de80ab2 226 return false;
227
228 for ( Int_t i=0; i<fNofValues; i++ )
229 if ( fValues.At(i) == value ) return true;
230
231 return false;
232}
233