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)
96 /// Add object with its key to the map and arrays
98 // Resize array if needed
99 if ( fValues.GetSize() == fNofValues ) {
100 fValues.Set(2*fValues.GetSize());
101 AliWarningStream() << "Resized array." << endl;
105 // The position for the new value
108 pos = GetPosition(value);
111 for ( Int_t i=fNofValues; i>=pos; i-- )
112 fValues.AddAt(fValues.At(i), i+1);
117 // Add the new value in freed space
118 fValues.AddAt(value, pos);
122 if ( value < fLimits.GetFirst() ) fLimits.SetFirst(value);
123 if ( value > fLimits.GetSecond() ) fLimits.SetSecond(value);
128 //_____________________________________________________________________________
129 Bool_t AliMpArrayI::Remove(Int_t value)
131 /// Remove value from the array
133 // Find the position for the new value
134 Int_t pos = GetPosition(value);
136 // Return if value is not present
137 if ( pos == fNofValues ) return false;
140 for ( Int_t i=pos; i<fNofValues-1; i++ )
141 fValues.AddAt(fValues.At(i+1), i);
143 // Decrement number of values
149 //_____________________________________________________________________________
150 void AliMpArrayI::SetSize(Int_t size)
152 /// Set given size to the array
157 //_____________________________________________________________________________
158 Int_t AliMpArrayI::GetSize() const
160 /// Return the map size
165 //_____________________________________________________________________________
166 Int_t AliMpArrayI::GetValue(Int_t index) const
168 /// Return the index-th value
170 if ( index < 0 || index >= fNofValues ) {
171 AliErrorStream() << "Index outside limits" << endl;
175 return fValues.At(index);
178 //_____________________________________________________________________________
179 Bool_t AliMpArrayI::HasValue(Int_t value) const
181 /// Return true if contains the given value
183 if ( ! fNofValues ) return false;
185 if ( value < fLimits.GetFirst() || value > fLimits.GetSecond() )
188 for ( Int_t i=0; i<fNofValues; i++ )
189 if ( fValues.At(i) == value ) return true;