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