1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 // $MpId: AliMpArrayI.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
20 //-----------------------------------------------------------------------------
22 // ------------------------
23 // Helper class for sorted integer array
24 // Author:Ivana Hrivnacova; IPN Orsay
25 //-----------------------------------------------------------------------------
27 #include "AliMpArrayI.h"
33 #include <Riostream.h>
43 const Int_t AliMpArrayI::fgkDefaultSize = 100;
45 //_____________________________________________________________________________
46 AliMpArrayI::AliMpArrayI(Bool_t sort)
50 fValues(fgkDefaultSize),
54 /// Standard & default constructor
58 //_____________________________________________________________________________
59 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/)
70 //_____________________________________________________________________________
71 AliMpArrayI::~AliMpArrayI()
80 //_____________________________________________________________________________
81 Int_t AliMpArrayI::GetPosition(Int_t value) const
83 /// Return the new positon where the value should be put
85 for ( Int_t i=0; i<fNofValues; i++ ) {
86 if ( fValues.At(i) > value ) return i;
96 //_____________________________________________________________________________
97 Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
99 /// Add object with its key to the map and arrays
101 // Resize array if needed
102 if ( fValues.GetSize() == fNofValues )
104 fValues.Set(2*fValues.GetSize());
107 AliWarningStream() << "Resized array." << endl;
111 // The position for the new value
114 pos = GetPosition(value);
117 for ( Int_t i=fNofValues; i>=pos; i-- )
118 fValues.AddAt(fValues.At(i), i+1);
123 // Add the new value in freed space
124 fValues.AddAt(value, pos);
128 if ( value < fMinValue ) fMinValue = value;
129 if ( value > fMaxValue ) fMaxValue = value;;
134 //_____________________________________________________________________________
135 Bool_t AliMpArrayI::Remove(Int_t value)
137 /// Remove value from the array
139 // Find the position for the new value
140 Int_t pos = GetPosition(value);
142 // Return if value is not present
143 if ( pos == fNofValues ) return false;
146 for ( Int_t i=pos; i<fNofValues-1; i++ )
147 fValues.AddAt(fValues.At(i+1), i);
149 // Decrement number of values
155 //_____________________________________________________________________________
156 Bool_t AliMpArrayI::Revert()
158 /// Revert the order of elements
161 AliErrorStream() << "Cannot revert sorted array." << endl;
165 Int_t size = GetSize();
166 TArrayI newArray(size);
168 for ( Int_t i = size-1 ; i >= 0 ; i--) {
169 Int_t value = GetValue(i);
170 newArray.AddAt(value,idx++);
173 for (Int_t i = 0; i < size ; i++) {
174 fValues[i]=newArray.At(i);
179 //_____________________________________________________________________________
180 void AliMpArrayI::Reset()
184 fValues.Set(fgkDefaultSize);
190 //_____________________________________________________________________________
191 void AliMpArrayI::SetSize(Int_t size)
193 /// Set given size to the array
198 //_____________________________________________________________________________
199 Int_t AliMpArrayI::GetSize() const
201 /// Return the map size
206 //_____________________________________________________________________________
207 Int_t AliMpArrayI::GetValue(Int_t index) const
209 /// Return the index-th value
211 if ( index < 0 || index >= fNofValues ) {
212 AliErrorStream() << "Index outside limits" << endl;
216 return fValues.At(index);
219 //_____________________________________________________________________________
220 Bool_t AliMpArrayI::HasValue(Int_t value) const
222 /// Return true if contains the given value
224 if ( ! fNofValues ) return false;
226 if ( value < fMinValue || value > fMaxValue )
229 for ( Int_t i=0; i<fNofValues; i++ )
230 if ( fValues.At(i) == value ) return true;