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