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