]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONcore/AliMpArrayI.cxx
Switching from CMAKE_SOURCE_DIR to AliRoot_SOURCE_DIR
[u/mrichter/AliRoot.git] / MUON / MUONcore / AliMpArrayI.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 // $Id$
17 // $MpId: AliMpArrayI.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
18 // Category: basic
19
20 //-----------------------------------------------------------------------------
21 // Class AliMpArrayI
22 // ------------------------
23 // Helper class for sorted integer array
24 // Author:Ivana Hrivnacova; IPN Orsay
25 //-----------------------------------------------------------------------------
26
27 #include "AliMpArrayI.h"
28
29 #include "AliLog.h"
30
31 #include <TClass.h>
32 #include <TString.h>
33 #include <Riostream.h>
34
35 #include <stdlib.h>
36 #include <limits.h>
37
38 using std::endl;
39 /// \cond CLASSIMP
40 ClassImp(AliMpArrayI)
41 /// \endcond
42
43 const Int_t AliMpArrayI::fgkDefaultSize = 100;
44
45 //_____________________________________________________________________________
46 AliMpArrayI::AliMpArrayI(Bool_t sort) 
47   : TObject(),
48     fSort(sort),
49     fNofValues(0),
50     fValues(fgkDefaultSize),
51     fMinValue(INT_MAX),
52     fMaxValue(INT_MIN)
53 {
54 /// Standard & default constructor
55
56 }
57
58 //_____________________________________________________________________________
59 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/) 
60   : TObject(),
61     fSort(),
62     fNofValues(),
63     fValues(),
64     fMinValue(),
65     fMaxValue()
66 {
67 /// IO constructor
68 }
69
70 //_____________________________________________________________________________
71 AliMpArrayI::~AliMpArrayI() 
72 {
73 /// Destructor 
74 }
75
76 //
77 // private methods
78 //
79
80 //_____________________________________________________________________________
81 Int_t  AliMpArrayI::GetPosition(Int_t value) const
82 {
83 /// Return the new positon where the value should be put
84
85   for ( Int_t i=0; i<fNofValues; i++ ) {
86     if ( fValues.At(i) > value ) return i;
87   }  
88
89   return fNofValues;
90 }  
91
92 //
93 // public methods
94 //
95
96 //_____________________________________________________________________________
97 Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
98 {
99 /// Add object with its key to the map and arrays
100   
101   // Resize array if needed
102   if ( fValues.GetSize() == fNofValues ) 
103   {
104     fValues.Set(2*fValues.GetSize());
105     if ( warn ) 
106     {
107       AliWarningStream() << "Resized array." << endl;
108     }
109   }
110   
111   // The position for the new value  
112   Int_t pos;
113   if ( fSort ) {
114     pos = GetPosition(value);
115
116     // Move elements 
117     for ( Int_t i=fNofValues; i>=pos; i-- )
118       fValues.AddAt(fValues.At(i), i+1);
119   }  
120   else
121     pos = fNofValues;     
122      
123   // Add the new value in freed space
124   fValues.AddAt(value, pos);  
125   ++fNofValues;
126   
127   // Update linits
128   if ( value < fMinValue )  fMinValue = value;
129   if ( value > fMaxValue )  fMaxValue = value;;
130   
131   return true;
132 }
133
134 //_____________________________________________________________________________
135 Bool_t AliMpArrayI::Remove(Int_t value)
136 {
137 /// Remove value from the array
138   
139   // Find the position for the new value  
140   Int_t pos = GetPosition(value); 
141    
142   // Return if value is not present
143   if ( pos == fNofValues ) return false;
144
145   // Move elements 
146   for ( Int_t i=pos; i<fNofValues-1; i++ )
147     fValues.AddAt(fValues.At(i+1), i);
148     
149   // Decrement number of values
150   --fNofValues;
151   
152   return true;
153 }
154
155 //_____________________________________________________________________________
156 Bool_t  AliMpArrayI::Revert()
157 {
158 /// Revert the order of elements
159
160   if ( fSort ) {
161     AliErrorStream() << "Cannot revert sorted array." << endl;
162     return false;
163   }  
164
165   Int_t size = GetSize();
166   TArrayI newArray(size);
167   Int_t idx = 0 ;
168   for ( Int_t i = size-1 ; i >= 0 ; i--) {
169     Int_t value = GetValue(i);
170     newArray.AddAt(value,idx++);
171   }
172
173   for (Int_t i = 0; i < size ; i++) {
174     fValues[i]=newArray.At(i);
175   }
176   return true;
177 }  
178
179 //_____________________________________________________________________________
180 void AliMpArrayI::Reset()
181 {
182 /// Reset the array
183
184   fValues.Set(fgkDefaultSize);
185   fNofValues = 0;
186   fMinValue = INT_MAX;
187   fMaxValue = INT_MIN;
188
189
190 //_____________________________________________________________________________
191 void AliMpArrayI::SetSize(Int_t size)
192 {
193 /// Set given size to the array
194
195   fValues.Set(size);
196
197
198 //_____________________________________________________________________________
199 Int_t AliMpArrayI::GetSize() const
200 {
201 /// Return the map size
202
203   return fNofValues;
204 }
205
206 //_____________________________________________________________________________
207 Int_t AliMpArrayI::GetValue(Int_t index) const
208 {
209 /// Return the index-th value 
210
211   if ( index < 0 || index >= fNofValues ) {
212     AliErrorStream() << "Index outside limits" << endl;
213     return 0;
214   }
215   
216   return fValues.At(index);
217 }
218
219 //_____________________________________________________________________________
220 Bool_t AliMpArrayI::HasValue(Int_t value) const
221 {
222 /// Return true if contains the given value
223
224   if ( ! fNofValues ) return false;
225
226   if ( value < fMinValue || value > fMaxValue ) 
227     return false;
228
229   for ( Int_t i=0; i<fNofValues; i++ )
230     if ( fValues.At(i) == value ) return true;
231     
232   return false;  
233 }
234