AliRoot Core  a565103 (a565103)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliMpArrayI.cxx
Go to the documentation of this file.
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;
42 
43 const Int_t AliMpArrayI::fgkDefaultSize = 100;
44 
45 //_____________________________________________________________________________
46 AliMpArrayI::AliMpArrayI(Bool_t sort)
47  : TObject(),
48  fSort(sort),
49  fNofValues(0),
50  fValues(fgkDefaultSize),
51  fMinValue(INT_MAX),
52  fMaxValue(INT_MIN)
53 {
55 
56 }
57 
58 //_____________________________________________________________________________
59 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/)
60  : TObject(),
61  fSort(),
62  fNofValues(),
63  fValues(),
64  fMinValue(),
65  fMaxValue()
66 {
68 }
69 
70 //_____________________________________________________________________________
72 {
74 }
75 
76 //
77 // private methods
78 //
79 
80 //_____________________________________________________________________________
81 Int_t AliMpArrayI::GetPosition(Int_t value) const
82 {
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 //_____________________________________________________________________________
97 Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
98 {
100 
101  // Resize array if needed
102  if ( fValues.GetSize() == fNofValues )
103  {
104  fValues.Set(2*fValues.GetSize());
105  if ( warn )
106  {
107  AliWarningStream() << "Resized array." << endl;
108  }
109  }
110 
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 
123  // Add the new value in freed space
124  fValues.AddAt(value, pos);
125  ++fNofValues;
126 
127  // Update linits
128  if ( value < fMinValue ) fMinValue = value;
129  if ( value > fMaxValue ) fMaxValue = value;;
130 
131  return true;
132 }
133 
134 //_____________________________________________________________________________
135 Bool_t AliMpArrayI::Remove(Int_t value)
136 {
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 //_____________________________________________________________________________
157 {
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 //_____________________________________________________________________________
181 {
183 
184  fValues.Set(fgkDefaultSize);
185  fNofValues = 0;
186  fMinValue = INT_MAX;
187  fMaxValue = INT_MIN;
188 }
189 
190 //_____________________________________________________________________________
191 void AliMpArrayI::SetSize(Int_t size)
192 {
194 
195  fValues.Set(size);
196 }
197 
198 //_____________________________________________________________________________
199 Int_t AliMpArrayI::GetSize() const
200 {
202 
203  return fNofValues;
204 }
205 
206 //_____________________________________________________________________________
207 Int_t AliMpArrayI::GetValue(Int_t index) const
208 {
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 {
223 
224  if ( ! fNofValues ) return false;
225 
226  if ( value < fMinValue || value > fMaxValue )
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 
virtual ~AliMpArrayI()
Definition: AliMpArrayI.cxx:71
Int_t GetSize() const
Int_t fMinValue
The minimum value in the array.
Definition: AliMpArrayI.h:53
Bool_t HasValue(Int_t value) const
Int_t fNofValues
Number of values in the array.
Definition: AliMpArrayI.h:51
Bool_t Add(Int_t value, Bool_t warn=kTRUE)
Definition: AliMpArrayI.cxx:97
AliMpArrayI(Bool_t sort=true)
Definition: AliMpArrayI.cxx:46
ClassImp(TPCGenInfo)
Definition: AliTPCCmpNG.C:254
Bool_t fSort
Option to sort the values.
Definition: AliMpArrayI.h:50
Bool_t Revert()
void SetSize(Int_t size)
Bool_t Remove(Int_t value)
Helper class for sorted integer array.
Definition: AliMpArrayI.h:21
TArrayI fValues
Array of values.
Definition: AliMpArrayI.h:52
Int_t fMaxValue
The maximum value in the array.
Definition: AliMpArrayI.h:54
Int_t GetValue(Int_t index) const
static const Int_t fgkDefaultSize
Default initial size.
Definition: AliMpArrayI.h:47
Int_t GetPosition(Int_t value) const
Definition: AliMpArrayI.cxx:81