]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpArrayI.cxx
A helper class for ordered integer array
[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 // Class AliMpArrayI
21 // ------------------------
22 // Helper class for sorted integer array
23 // Author:Ivana Hrivnacova; IPN Orsay
24
25 #include "AliMpArrayI.h"
26 #include "AliMpIntPair.h"
27
28 #include "AliLog.h"
29
30 #include <TClass.h>
31 #include <TString.h>
32 #include <Riostream.h>
33
34 #include <stdlib.h>
35
36 /// \cond CLASSIMP
37 ClassImp(AliMpArrayI)
38 /// \endcond
39
40 const Int_t AliMpArrayI::fgkDefaultSize = 100;
41
42 //_____________________________________________________________________________
43 AliMpArrayI::AliMpArrayI() 
44   : TObject(),
45     fNofValues(0),
46     fValues(fgkDefaultSize)
47 {
48 /// Standard & default constructor
49
50 }
51
52 //_____________________________________________________________________________
53 AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/) 
54   : TObject(),
55     fNofValues(),
56     fValues()
57 {
58 /// IO constructor
59 }
60
61 //_____________________________________________________________________________
62 AliMpArrayI::~AliMpArrayI() 
63 {
64 /// Destructor 
65 }
66
67 //
68 // private methods
69 //
70
71 //_____________________________________________________________________________
72 Int_t  AliMpArrayI::GetPosition(Int_t value) const
73 {
74 /// Return the new positon where the value should be put
75
76   for ( Int_t i=0; i<fNofValues; i++ ) {
77     if ( fValues.At(i) > value ) return i;
78   }  
79
80   return fNofValues;
81 }  
82
83 //
84 // public methods
85 //
86
87 //_____________________________________________________________________________
88 Bool_t AliMpArrayI::Add(Int_t value)
89 {
90 /// Add object with its key to the map and arrays
91   
92   // Resize array if needed
93   if ( fValues.GetSize() == fNofValues ) {
94    fValues.Set(2*fValues.GetSize());
95    AliWarningStream() << "Resized array." << endl;
96   }
97   
98   // Find the position for the new value  
99   Int_t pos = GetPosition(value); 
100    
101   // Move elements 
102   for ( Int_t i=fNofValues; i>=pos; i-- )
103     fValues.AddAt(fValues.At(i), i+1);
104     
105   // Add the new value in freed space
106   fValues.AddAt(value, pos);  
107   ++fNofValues;
108   
109   return true;
110 }
111
112 //_____________________________________________________________________________
113 Bool_t AliMpArrayI::Remove(Int_t value)
114 {
115 /// Add object with its key to the map and arrays
116   
117   // Find the position for the new value  
118   Int_t pos = GetPosition(value); 
119    
120   // Return if value is not present
121   if ( pos == fNofValues ) return false;
122
123   // Move elements 
124   for ( Int_t i=pos; i<fNofValues-1; i++ )
125     fValues.AddAt(fValues.At(i+1), i);
126     
127   // Decrement number of values
128   --fNofValues;
129   
130   return true;
131 }
132
133 //_____________________________________________________________________________
134 void AliMpArrayI::SetSize(Int_t size)
135 {
136 /// Set given size to the array
137
138   fValues.Set(size);
139
140
141 //_____________________________________________________________________________
142 Int_t AliMpArrayI::GetSize() const
143 {
144 /// Return the map size
145
146   return fNofValues;
147 }
148
149 //_____________________________________________________________________________
150 Int_t AliMpArrayI::GetValue(Int_t index) const
151 {
152 /// Return the index-th value 
153
154   if ( index < 0 || index >= fNofValues ) {
155     AliErrorStream() << "Index outside limits" << endl;
156     return 0;
157   }
158   
159   return fValues.At(index);
160 }
161
162 //_____________________________________________________________________________
163 Bool_t AliMpArrayI::HasValue(Int_t value) const
164 {
165 /// Return true if contains the given value
166
167   if ( ! fNofValues ) return false;
168
169   if ( fValues.At(0) > value || fValues.At(fNofValues-1) < value ) 
170     return false;
171
172   for ( Int_t i=0; i<fNofValues; i++ )
173     if ( fValues.At(i) == value ) return true;
174     
175   return false;  
176 }
177