85dc1f4c9f44779fb16cbd9d990f4257462b7798
[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     fLimits(INT_MIN,INT_MAX)
51 {
52 /// Standard & default constructor
53
54 }
55
56 //_____________________________________________________________________________
57 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/) 
58   : TObject(),
59     fSort(),
60     fNofValues(),
61     fValues(),
62     fLimits()
63 {
64 /// IO constructor
65 }
66
67 //_____________________________________________________________________________
68 AliMpArrayI::~AliMpArrayI() 
69 {
70 /// Destructor 
71 }
72
73 //
74 // private methods
75 //
76
77 //_____________________________________________________________________________
78 Int_t  AliMpArrayI::GetPosition(Int_t value) const
79 {
80 /// Return the new positon where the value should be put
81
82   for ( Int_t i=0; i<fNofValues; i++ ) {
83     if ( fValues.At(i) > value ) return i;
84   }  
85
86   return fNofValues;
87 }  
88
89 //
90 // public methods
91 //
92
93 //_____________________________________________________________________________
94 Bool_t AliMpArrayI::Add(Int_t value)
95 {
96 /// Add object with its key to the map and arrays
97   
98   // Resize array if needed
99   if ( fValues.GetSize() == fNofValues ) {
100    fValues.Set(2*fValues.GetSize());
101    AliWarningStream() << "Resized array." << endl;
102   }
103   
104   
105   // The position for the new value  
106   Int_t pos;
107   if ( fSort ) {
108     pos = GetPosition(value);
109
110     // Move elements 
111     for ( Int_t i=fNofValues; i>=pos; i-- )
112       fValues.AddAt(fValues.At(i), i+1);
113   }  
114   else
115     pos = fNofValues;     
116      
117   // Add the new value in freed space
118   fValues.AddAt(value, pos);  
119   ++fNofValues;
120   
121   // Update linits
122   if ( value < fLimits.GetFirst() )  fLimits.SetFirst(value);
123   if ( value > fLimits.GetSecond() ) fLimits.SetSecond(value);
124   
125   return true;
126 }
127
128 //_____________________________________________________________________________
129 Bool_t AliMpArrayI::Remove(Int_t value)
130 {
131 /// Remove value from the array
132   
133   // Find the position for the new value  
134   Int_t pos = GetPosition(value); 
135    
136   // Return if value is not present
137   if ( pos == fNofValues ) return false;
138
139   // Move elements 
140   for ( Int_t i=pos; i<fNofValues-1; i++ )
141     fValues.AddAt(fValues.At(i+1), i);
142     
143   // Decrement number of values
144   --fNofValues;
145   
146   return true;
147 }
148
149 //_____________________________________________________________________________
150 void AliMpArrayI::SetSize(Int_t size)
151 {
152 /// Set given size to the array
153
154   fValues.Set(size);
155
156
157 //_____________________________________________________________________________
158 Int_t AliMpArrayI::GetSize() const
159 {
160 /// Return the map size
161
162   return fNofValues;
163 }
164
165 //_____________________________________________________________________________
166 Int_t AliMpArrayI::GetValue(Int_t index) const
167 {
168 /// Return the index-th value 
169
170   if ( index < 0 || index >= fNofValues ) {
171     AliErrorStream() << "Index outside limits" << endl;
172     return 0;
173   }
174   
175   return fValues.At(index);
176 }
177
178 //_____________________________________________________________________________
179 Bool_t AliMpArrayI::HasValue(Int_t value) const
180 {
181 /// Return true if contains the given value
182
183   if ( ! fNofValues ) return false;
184
185   if ( value < fLimits.GetFirst() || value > fLimits.GetSecond() ) 
186     return false;
187
188   for ( Int_t i=0; i<fNofValues; i++ )
189     if ( fValues.At(i) == value ) return true;
190     
191   return false;  
192 }
193