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),
50 fLimits(INT_MIN,INT_MAX)
52 /// Standard & default constructor
56 //_____________________________________________________________________________
57 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/)
67 //_____________________________________________________________________________
68 AliMpArrayI::~AliMpArrayI()
77 //_____________________________________________________________________________
78 Int_t AliMpArrayI::GetPosition(Int_t value) const
80 /// Return the new positon where the value should be put
82 for ( Int_t i=0; i<fNofValues; i++ ) {
83 if ( fValues.At(i) > value ) return i;
93 //_____________________________________________________________________________
94 Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
96 /// Add object with its key to the map and arrays
98 // Resize array if needed
99 if ( fValues.GetSize() == fNofValues )
101 fValues.Set(2*fValues.GetSize());
104 AliWarningStream() << "Resized array." << endl;
108 // The position for the new value
111 pos = GetPosition(value);
114 for ( Int_t i=fNofValues; i>=pos; i-- )
115 fValues.AddAt(fValues.At(i), i+1);
120 // Add the new value in freed space
121 fValues.AddAt(value, pos);
125 if ( value < fLimits.GetFirst() ) fLimits.SetFirst(value);
126 if ( value > fLimits.GetSecond() ) fLimits.SetSecond(value);
131 //_____________________________________________________________________________
132 Bool_t AliMpArrayI::Remove(Int_t value)
134 /// Remove value from the array
136 // Find the position for the new value
137 Int_t pos = GetPosition(value);
139 // Return if value is not present
140 if ( pos == fNofValues ) return false;
143 for ( Int_t i=pos; i<fNofValues-1; i++ )
144 fValues.AddAt(fValues.At(i+1), i);
146 // Decrement number of values
152 //_____________________________________________________________________________
153 void AliMpArrayI::SetSize(Int_t size)
155 /// Set given size to the array
160 //_____________________________________________________________________________
161 Int_t AliMpArrayI::GetSize() const
163 /// Return the map size
168 //_____________________________________________________________________________
169 Int_t AliMpArrayI::GetValue(Int_t index) const
171 /// Return the index-th value
173 if ( index < 0 || index >= fNofValues ) {
174 AliErrorStream() << "Index outside limits" << endl;
178 return fValues.At(index);
181 //_____________________________________________________________________________
182 Bool_t AliMpArrayI::HasValue(Int_t value) const
184 /// Return true if contains the given value
186 if ( ! fNofValues ) return false;
188 if ( value < fLimits.GetFirst() || value > fLimits.GetSecond() )
191 for ( Int_t i=0; i<fNofValues; i++ )
192 if ( fValues.At(i) == value ) return true;