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 $
19 // ------------------------
21 // ------------------------
22 // Helper class for sorted integer array
23 // Author:Ivana Hrivnacova; IPN Orsay
25 #include "AliMpArrayI.h"
31 #include <Riostream.h>
40 const Int_t AliMpArrayI::fgkDefaultSize = 100;
42 //_____________________________________________________________________________
43 AliMpArrayI::AliMpArrayI(Bool_t sort)
47 fValues(fgkDefaultSize),
48 fLimits(INT_MIN,INT_MAX)
50 /// Standard & default constructor
54 //_____________________________________________________________________________
55 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/)
65 //_____________________________________________________________________________
66 AliMpArrayI::~AliMpArrayI()
75 //_____________________________________________________________________________
76 Int_t AliMpArrayI::GetPosition(Int_t value) const
78 /// Return the new positon where the value should be put
80 for ( Int_t i=0; i<fNofValues; i++ ) {
81 if ( fValues.At(i) > value ) return i;
91 //_____________________________________________________________________________
92 Bool_t AliMpArrayI::Add(Int_t value)
94 /// Add object with its key to the map and arrays
96 // Resize array if needed
97 if ( fValues.GetSize() == fNofValues ) {
98 fValues.Set(2*fValues.GetSize());
99 AliWarningStream() << "Resized array." << endl;
103 // The position for the new value
106 pos = GetPosition(value);
109 for ( Int_t i=fNofValues; i>=pos; i-- )
110 fValues.AddAt(fValues.At(i), i+1);
115 // Add the new value in freed space
116 fValues.AddAt(value, pos);
120 if ( value < fLimits.GetFirst() ) fLimits.SetFirst(value);
121 if ( value > fLimits.GetSecond() ) fLimits.SetSecond(value);
126 //_____________________________________________________________________________
127 Bool_t AliMpArrayI::Remove(Int_t value)
129 /// Remove value from the array
131 // Find the position for the new value
132 Int_t pos = GetPosition(value);
134 // Return if value is not present
135 if ( pos == fNofValues ) return false;
138 for ( Int_t i=pos; i<fNofValues-1; i++ )
139 fValues.AddAt(fValues.At(i+1), i);
141 // Decrement number of values
147 //_____________________________________________________________________________
148 void AliMpArrayI::SetSize(Int_t size)
150 /// Set given size to the array
155 //_____________________________________________________________________________
156 Int_t AliMpArrayI::GetSize() const
158 /// Return the map size
163 //_____________________________________________________________________________
164 Int_t AliMpArrayI::GetValue(Int_t index) const
166 /// Return the index-th value
168 if ( index < 0 || index >= fNofValues ) {
169 AliErrorStream() << "Index outside limits" << endl;
173 return fValues.At(index);
176 //_____________________________________________________________________________
177 Bool_t AliMpArrayI::HasValue(Int_t value) const
179 /// Return true if contains the given value
181 if ( ! fNofValues ) return false;
183 if ( value < fLimits.GetFirst() || value > fLimits.GetSecond() )
186 for ( Int_t i=0; i<fNofValues; i++ )
187 if ( fValues.At(i) == value ) return true;