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>
42 const Int_t AliMpArrayI::fgkDefaultSize = 100;
44 //_____________________________________________________________________________
45 AliMpArrayI::AliMpArrayI(Bool_t sort)
49 fValues(fgkDefaultSize),
53 /// Standard & default constructor
57 //_____________________________________________________________________________
58 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/)
69 //_____________________________________________________________________________
70 AliMpArrayI::~AliMpArrayI()
79 //_____________________________________________________________________________
80 Int_t AliMpArrayI::GetPosition(Int_t value) const
82 /// Return the new positon where the value should be put
84 for ( Int_t i=0; i<fNofValues; i++ ) {
85 if ( fValues.At(i) > value ) return i;
95 //_____________________________________________________________________________
96 Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
98 /// Add object with its key to the map and arrays
100 // Resize array if needed
101 if ( fValues.GetSize() == fNofValues )
103 fValues.Set(2*fValues.GetSize());
106 AliWarningStream() << "Resized array." << endl;
110 // The position for the new value
113 pos = GetPosition(value);
116 for ( Int_t i=fNofValues; i>=pos; i-- )
117 fValues.AddAt(fValues.At(i), i+1);
122 // Add the new value in freed space
123 fValues.AddAt(value, pos);
127 if ( value < fMinValue ) fMinValue = value;
128 if ( value > fMaxValue ) fMaxValue = value;;
133 //_____________________________________________________________________________
134 Bool_t AliMpArrayI::Remove(Int_t value)
136 /// Remove value from the array
138 // Find the position for the new value
139 Int_t pos = GetPosition(value);
141 // Return if value is not present
142 if ( pos == fNofValues ) return false;
145 for ( Int_t i=pos; i<fNofValues-1; i++ )
146 fValues.AddAt(fValues.At(i+1), i);
148 // Decrement number of values
154 //_____________________________________________________________________________
155 Bool_t AliMpArrayI::Revert()
157 /// Revert the order of elements
160 AliErrorStream() << "Cannot revert sorted array." << endl;
164 Int_t size = GetSize();
165 TArrayI newArray(size);
167 for ( Int_t i = size-1 ; i >= 0 ; i--) {
168 Int_t value = GetValue(i);
169 newArray.AddAt(value,idx++);
172 for (Int_t i = 0; i < size ; i++) {
173 fValues[i]=newArray.At(i);
178 //_____________________________________________________________________________
179 void AliMpArrayI::Reset()
183 fValues.Set(fgkDefaultSize);
189 //_____________________________________________________________________________
190 void AliMpArrayI::SetSize(Int_t size)
192 /// Set given size to the array
197 //_____________________________________________________________________________
198 Int_t AliMpArrayI::GetSize() const
200 /// Return the map size
205 //_____________________________________________________________________________
206 Int_t AliMpArrayI::GetValue(Int_t index) const
208 /// Return the index-th value
210 if ( index < 0 || index >= fNofValues ) {
211 AliErrorStream() << "Index outside limits" << endl;
215 return fValues.At(index);
218 //_____________________________________________________________________________
219 Bool_t AliMpArrayI::HasValue(Int_t value) const
221 /// Return true if contains the given value
223 if ( ! fNofValues ) return false;
225 if ( value < fMinValue || value > fMaxValue )
228 for ( Int_t i=0; i<fNofValues; i++ )
229 if ( fValues.At(i) == value ) return true;