]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpArrayI.cxx
Replacement of AliMpIntPair object with algoritmic
[u/mrichter/AliRoot.git] / MUON / mapping / 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 /// \cond CLASSIMP
39 ClassImp(AliMpArrayI)
40 /// \endcond
41
42 const Int_t AliMpArrayI::fgkDefaultSize = 100;
43
44 //_____________________________________________________________________________
45 AliMpArrayI::AliMpArrayI(Bool_t sort) 
46   : TObject(),
47     fSort(sort),
48     fNofValues(0),
49     fValues(fgkDefaultSize),
50     fMinValue(INT_MAX),
51     fMaxValue(INT_MIN)
52 {
53 /// Standard & default constructor
54
55 }
56
57 //_____________________________________________________________________________
58 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/) 
59   : TObject(),
60     fSort(),
61     fNofValues(),
62     fValues(),
63     fMinValue(),
64     fMaxValue()
65 {
66 /// IO constructor
67 }
68
69 //_____________________________________________________________________________
70 AliMpArrayI::~AliMpArrayI() 
71 {
72 /// Destructor 
73 }
74
75 //
76 // private methods
77 //
78
79 //_____________________________________________________________________________
80 Int_t  AliMpArrayI::GetPosition(Int_t value) const
81 {
82 /// Return the new positon where the value should be put
83
84   for ( Int_t i=0; i<fNofValues; i++ ) {
85     if ( fValues.At(i) > value ) return i;
86   }  
87
88   return fNofValues;
89 }  
90
91 //
92 // public methods
93 //
94
95 //_____________________________________________________________________________
96 Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
97 {
98 /// Add object with its key to the map and arrays
99   
100   // Resize array if needed
101   if ( fValues.GetSize() == fNofValues ) 
102   {
103     fValues.Set(2*fValues.GetSize());
104     if ( warn ) 
105     {
106       AliWarningStream() << "Resized array." << endl;
107     }
108   }
109   
110   // The position for the new value  
111   Int_t pos;
112   if ( fSort ) {
113     pos = GetPosition(value);
114
115     // Move elements 
116     for ( Int_t i=fNofValues; i>=pos; i-- )
117       fValues.AddAt(fValues.At(i), i+1);
118   }  
119   else
120     pos = fNofValues;     
121      
122   // Add the new value in freed space
123   fValues.AddAt(value, pos);  
124   ++fNofValues;
125   
126   // Update linits
127   if ( value < fMinValue )  fMinValue = value;
128   if ( value > fMaxValue )  fMaxValue = value;;
129   
130   return true;
131 }
132
133 //_____________________________________________________________________________
134 Bool_t AliMpArrayI::Remove(Int_t value)
135 {
136 /// Remove value from the array
137   
138   // Find the position for the new value  
139   Int_t pos = GetPosition(value); 
140    
141   // Return if value is not present
142   if ( pos == fNofValues ) return false;
143
144   // Move elements 
145   for ( Int_t i=pos; i<fNofValues-1; i++ )
146     fValues.AddAt(fValues.At(i+1), i);
147     
148   // Decrement number of values
149   --fNofValues;
150   
151   return true;
152 }
153
154 //_____________________________________________________________________________
155 Bool_t  AliMpArrayI::Revert()
156 {
157 /// Revert the order of elements
158
159   if ( fSort ) {
160     AliErrorStream() << "Cannot revert sorted array." << endl;
161     return false;
162   }  
163
164   Int_t size = GetSize();
165   TArrayI newArray(size);
166   Int_t idx = 0 ;
167   for ( Int_t i = size-1 ; i >= 0 ; i--) {
168     Int_t value = GetValue(i);
169     newArray.AddAt(value,idx++);
170   }
171
172   for (Int_t i = 0; i < size ; i++) {
173     fValues[i]=newArray.At(i);
174   }
175   return true;
176 }  
177
178 //_____________________________________________________________________________
179 void AliMpArrayI::Reset()
180 {
181 /// Reset the array
182
183   fValues.Set(fgkDefaultSize);
184   fNofValues = 0;
185   fMinValue = INT_MAX;
186   fMaxValue = INT_MIN;
187
188
189 //_____________________________________________________________________________
190 void AliMpArrayI::SetSize(Int_t size)
191 {
192 /// Set given size to the array
193
194   fValues.Set(size);
195
196
197 //_____________________________________________________________________________
198 Int_t AliMpArrayI::GetSize() const
199 {
200 /// Return the map size
201
202   return fNofValues;
203 }
204
205 //_____________________________________________________________________________
206 Int_t AliMpArrayI::GetValue(Int_t index) const
207 {
208 /// Return the index-th value 
209
210   if ( index < 0 || index >= fNofValues ) {
211     AliErrorStream() << "Index outside limits" << endl;
212     return 0;
213   }
214   
215   return fValues.At(index);
216 }
217
218 //_____________________________________________________________________________
219 Bool_t AliMpArrayI::HasValue(Int_t value) const
220 {
221 /// Return true if contains the given value
222
223   if ( ! fNofValues ) return false;
224
225   if ( value < fMinValue || value > fMaxValue ) 
226     return false;
227
228   for ( Int_t i=0; i<fNofValues; i++ )
229     if ( fValues.At(i) == value ) return true;
230     
231   return false;  
232 }
233