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