Project
Loading...
Searching...
No Matches
VectorSparse.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13
14#include <TString.h>
15
17
18using namespace o2::fwdalign;
19
21
22//___________________________________________________________
24 : fNElems(0),
25 fIndex(nullptr),
26 fElems(nullptr)
27{
28}
29
30//___________________________________________________________
32 : TObject(src),
33 fNElems(src.fNElems),
34 fIndex(nullptr),
35 fElems(nullptr)
36{
37 fIndex = new UShort_t[fNElems];
38 fElems = new Double_t[fNElems];
39 memcpy(fIndex, src.fIndex, fNElems * sizeof(UShort_t));
40 memcpy(fElems, src.fElems, fNElems * sizeof(Double_t));
41}
42
43//___________________________________________________________
44void VectorSparse::Clear(Option_t*)
45{
46 delete[] fIndex;
47 fIndex = nullptr;
48 delete[] fElems;
49 fElems = nullptr;
50 fNElems = 0;
51}
52
53//___________________________________________________________
55{
56 if (&src == this) {
57 return *this;
58 }
59 Clear();
60 TObject::operator=(src);
61 fNElems = src.fNElems;
62 fIndex = new UShort_t[fNElems];
63 fElems = new Double_t[fNElems];
64 memcpy(fIndex, src.fIndex, fNElems * sizeof(UShort_t));
65 memcpy(fElems, src.fElems, fNElems * sizeof(Double_t));
66 //
67 return *this;
68}
69
70//___________________________________________________________
71Double_t VectorSparse::FindIndex(Int_t ind) const
72{
73 // printf("V: findindex\n");
74 int first = 0;
75 int last = fNElems - 1;
76 while (first <= last) {
77 int mid = (first + last) >> 1;
78 if (ind > fIndex[mid]) {
79 first = mid + 1;
80 } else {
81 if (ind < fIndex[mid]) {
82 last = mid - 1;
83 } else {
84 return fElems[mid];
85 }
86 }
87 }
88 return 0.0;
89}
90
91//___________________________________________________________
93{
94 int first = 0;
95 int last = fNElems - 1;
96 while (first <= last) {
97 int mid = (first + last) >> 1;
98 if (ind > fIndex[mid]) {
99 first = mid + 1;
100 } else {
101 if (ind < fIndex[mid]) {
102 last = mid - 1;
103 } else {
104 fElems[mid] = 0.;
105 return;
106 }
107 }
108 }
109}
110
111//___________________________________________________________
112Double_t& VectorSparse::FindIndexAdd(Int_t ind)
113{
114 // printf("V: findindexAdd\n");
115 int first = 0;
116 int last = fNElems - 1;
117 while (first <= last) {
118 int mid = (first + last) >> 1;
119 if (ind > fIndex[mid]) {
120 first = mid + 1;
121 } else {
122 if (ind < fIndex[mid]) {
123 last = mid - 1;
124 } else {
125 return fElems[mid];
126 }
127 }
128 }
129 // need to insert a new element
130 UShort_t* arrI = new UShort_t[fNElems + 1];
131 memcpy(arrI, fIndex, first * sizeof(UShort_t));
132 arrI[first] = ind;
133 memcpy(arrI + first + 1, fIndex + first, (fNElems - first) * sizeof(UShort_t));
134 delete[] fIndex;
135 fIndex = arrI;
136
137 Double_t* arrE = new Double_t[fNElems + 1];
138 memcpy(arrE, fElems, first * sizeof(Double_t));
139 arrE[first] = 0;
140 memcpy(arrE + first + 1, fElems + first, (fNElems - first) * sizeof(Double_t));
141 delete[] fElems;
142 fElems = arrE;
143
144 fNElems++;
145 return fElems[first];
146}
147
148//__________________________________________________________
149void VectorSparse::ReSize(Int_t sz, Bool_t copy)
150{
151 if (sz < 1) {
152 Clear();
153 return;
154 }
155 // need to insert a new element
156 UShort_t* arrI = new UShort_t[sz];
157 Double_t* arrE = new Double_t[sz];
158 memset(arrI, 0, sz * sizeof(UShort_t));
159 memset(arrE, 0, sz * sizeof(Double_t));
160 //
161 if (copy && fIndex) {
162 int cpsz = TMath::Min(fNElems, sz);
163 memcpy(arrI, fIndex, cpsz * sizeof(UShort_t));
164 memcpy(arrE, fElems, cpsz * sizeof(Double_t));
165 }
166 delete[] fIndex;
167 delete[] fElems;
168 fIndex = arrI;
169 fElems = arrE;
170 fNElems = sz;
171}
172
173//__________________________________________________________
174void VectorSparse::SortIndices(Bool_t valuesToo)
175{
176 for (int i = fNElems; i--;) {
177 for (int j = i; j--;) {
178 if (fIndex[i] < fIndex[j]) { // swap
179 UShort_t tmpI = fIndex[i];
180 fIndex[i] = fIndex[j];
181 fIndex[j] = tmpI;
182 if (valuesToo) {
183 Double_t tmpV = fElems[i];
184 fElems[i] = fElems[j];
185 fElems[j] = tmpV;
186 }
187 }
188 }
189 }
190}
191
192//__________________________________________________________
193void VectorSparse::Print(Option_t* opt) const
194{
195 TString sopt = opt;
196 sopt.ToLower();
197 int ndig = sopt.Atoi();
198 if (ndig <= 1) {
199 ndig = 2;
200 }
201 sopt = "%2d:%+.";
202 sopt += ndig;
203 sopt += "e |";
204 printf("|");
205 for (int i = 0; i < fNElems; i++) {
206 printf(sopt.Data(), fIndex[i], fElems[i]);
207 }
208 printf("\n");
209}
210
211//___________________________________________________________
212void VectorSparse::Add(Double_t* valc, Int_t* indc, Int_t n)
213{
214 int indx;
215 int nadd = 0;
216
217 int last = fNElems - 1;
218 int mid = 0;
219 for (int i = n; i--;) {
220 // if the element with this index is already defined, just add the value
221 int first = 0;
222 Bool_t toAdd = kTRUE;
223 indx = indc[i];
224 while (first <= last) {
225 mid = (first + last) >> 1;
226 if (indx > fIndex[mid]) {
227 first = mid + 1;
228 } else {
229 if (indx < fIndex[mid]) {
230 last = mid - 1;
231 } else {
232 fElems[mid] += valc[i];
233 indc[i] = -1;
234 toAdd = kFALSE;
235 last = mid - 1; // profit from the indices being ordered
236 break;
237 }
238 }
239 }
240 if (toAdd) {
241 nadd++;
242 }
243 }
244
245 if (nadd < 1) {
246 return; // nothing to do anymore
247 }
248 // need to expand the row
249 UShort_t* arrI = new UShort_t[fNElems + nadd];
250 Double_t* arrE = new Double_t[fNElems + nadd];
251 // copy old elems embedding the new ones
252 int inew = 0, iold = 0;
253 for (int i = 0; i < n; i++) {
254 if ((indx = indc[i]) < 0) {
255 continue;
256 }
257 while (iold < fNElems && fIndex[iold] < indx) {
258 arrI[inew] = fIndex[iold];
259 arrE[inew++] = fElems[iold++];
260 }
261 arrI[inew] = indx;
262 arrE[inew++] = valc[i];
263 }
264 // copy the rest
265 while (iold < fNElems) {
266 arrI[inew] = fIndex[iold];
267 arrE[inew++] = fElems[iold++];
268 }
269
270 delete[] fIndex;
271 delete[] fElems;
272 fIndex = arrI;
273 fElems = arrE;
274
275 fNElems += nadd;
276}
int32_t i
uint32_t j
Definition RawData.h:0
ClassImp(VectorSparse)
Sparse vector class (from AliROOT), used as row of the MatrixSparse class.
void Clear(Option_t *option="") override
clear all
void Print(Option_t *option="") const override
print itself
Double_t FindIndex(Int_t ind) const
return an element with given index
Double_t & FindIndexAdd(Int_t ind)
increment an element with given index
Int_t fNElems
Number of elements.
void ReSize(Int_t sz, Bool_t copy=kFALSE)
change the size
Double_t * fElems
pointer on elements
UShort_t * fIndex
Index of stored elems.
virtual void SetToZero(Int_t ind)
set element to 0 if it was already defined
VectorSparse & operator=(const VectorSparse &src)
assignment op-tor
void SortIndices(Bool_t valuesToo=kFALSE)
sort indices in increasing order. Used to fix the row after ILUk decomposition
void Add(Double_t *valc, Int_t *indc, Int_t n)
add indiced array to row. Indices must be in increasing order
GLdouble n
Definition glcorearb.h:1982
GLenum src
Definition glcorearb.h:1767
GLint first
Definition glcorearb.h:399