]>
Commit | Line | Data |
---|---|---|
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, Bool_t warn) | |
95 | { | |
96 | /// Add object with its key to the map and arrays | |
97 | ||
98 | // Resize array if needed | |
99 | if ( fValues.GetSize() == fNofValues ) | |
100 | { | |
101 | fValues.Set(2*fValues.GetSize()); | |
102 | if ( warn ) | |
103 | { | |
104 | AliWarningStream() << "Resized array." << endl; | |
105 | } | |
106 | } | |
107 | ||
108 | // The position for the new value | |
109 | Int_t pos; | |
110 | if ( fSort ) { | |
111 | pos = GetPosition(value); | |
112 | ||
113 | // Move elements | |
114 | for ( Int_t i=fNofValues; i>=pos; i-- ) | |
115 | fValues.AddAt(fValues.At(i), i+1); | |
116 | } | |
117 | else | |
118 | pos = fNofValues; | |
119 | ||
120 | // Add the new value in freed space | |
121 | fValues.AddAt(value, pos); | |
122 | ++fNofValues; | |
123 | ||
124 | // Update linits | |
125 | if ( value < fLimits.GetFirst() ) fLimits.SetFirst(value); | |
126 | if ( value > fLimits.GetSecond() ) fLimits.SetSecond(value); | |
127 | ||
128 | return true; | |
129 | } | |
130 | ||
131 | //_____________________________________________________________________________ | |
132 | Bool_t AliMpArrayI::Remove(Int_t value) | |
133 | { | |
134 | /// Remove value from the array | |
135 | ||
136 | // Find the position for the new value | |
137 | Int_t pos = GetPosition(value); | |
138 | ||
139 | // Return if value is not present | |
140 | if ( pos == fNofValues ) return false; | |
141 | ||
142 | // Move elements | |
143 | for ( Int_t i=pos; i<fNofValues-1; i++ ) | |
144 | fValues.AddAt(fValues.At(i+1), i); | |
145 | ||
146 | // Decrement number of values | |
147 | --fNofValues; | |
148 | ||
149 | return true; | |
150 | } | |
151 | ||
152 | //_____________________________________________________________________________ | |
153 | void AliMpArrayI::SetSize(Int_t size) | |
154 | { | |
155 | /// Set given size to the array | |
156 | ||
157 | fValues.Set(size); | |
158 | } | |
159 | ||
160 | //_____________________________________________________________________________ | |
161 | Int_t AliMpArrayI::GetSize() const | |
162 | { | |
163 | /// Return the map size | |
164 | ||
165 | return fNofValues; | |
166 | } | |
167 | ||
168 | //_____________________________________________________________________________ | |
169 | Int_t AliMpArrayI::GetValue(Int_t index) const | |
170 | { | |
171 | /// Return the index-th value | |
172 | ||
173 | if ( index < 0 || index >= fNofValues ) { | |
174 | AliErrorStream() << "Index outside limits" << endl; | |
175 | return 0; | |
176 | } | |
177 | ||
178 | return fValues.At(index); | |
179 | } | |
180 | ||
181 | //_____________________________________________________________________________ | |
182 | Bool_t AliMpArrayI::HasValue(Int_t value) const | |
183 | { | |
184 | /// Return true if contains the given value | |
185 | ||
186 | if ( ! fNofValues ) return false; | |
187 | ||
188 | if ( value < fLimits.GetFirst() || value > fLimits.GetSecond() ) | |
189 | return false; | |
190 | ||
191 | for ( Int_t i=0; i<fNofValues; i++ ) | |
192 | if ( fValues.At(i) == value ) return true; | |
193 | ||
194 | return false; | |
195 | } | |
196 |