]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliVectorSparse.cxx
fix warning found by Federico...
[u/mrichter/AliRoot.git] / STEER / AliVectorSparse.cxx
CommitLineData
de34b538 1#include "AliVectorSparse.h"
2
3/**********************************************************************************************/
4/* Sparse vector class, used as row of the AliMatrixSparse class */
5/* */
6/* Author: ruben.shahoyan@cern.ch */
7/* */
8/**********************************************************************************************/
9
10ClassImp(AliVectorSparse)
11
12//___________________________________________________________
13AliVectorSparse::AliVectorSparse()
14 : fNElems(0),fIndex(0),fElems(0) {}
15
16//___________________________________________________________
17AliVectorSparse::AliVectorSparse(const AliVectorSparse& src)
18 : TObject(src),fNElems(src.fNElems),fIndex(0),fElems(0)
19{
20 fIndex = new UShort_t[fNElems];
21 fElems = new Double_t[fNElems];
22 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
23 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
24}
25
26//___________________________________________________________
27void AliVectorSparse::Clear(Option_t*)
28{
29 delete[] fIndex; fIndex = 0;
30 delete[] fElems; fElems = 0;
31 fNElems = 0;
32}
33
34//___________________________________________________________
35AliVectorSparse& AliVectorSparse::operator=(const AliVectorSparse& src)
36{
37 if (&src==this) return *this;
38 Clear();
39 TObject::operator=(src);
40 fNElems = src.fNElems;
41 fIndex = new UShort_t[fNElems];
42 fElems = new Double_t[fNElems];
43 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
44 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
45 //
46 return *this;
47}
48
49//___________________________________________________________
50Double_t AliVectorSparse::FindIndex(Int_t ind) const
51{
52 // return an element with given index
53 //printf("V: findindex\n");
54 int first = 0;
55 int last = fNElems-1;
56 while (first<=last) {
57 int mid = (first+last)>>1;
58 if (ind>fIndex[mid]) first = mid+1;
59 else if (ind<fIndex[mid]) last = mid-1;
60 else return fElems[mid];
61 }
62 return 0.0;
63}
64
65//___________________________________________________________
66void AliVectorSparse::SetToZero(Int_t ind)
67{
68 // set element to 0 if it was already defined
69 int first = 0;
70 int last = fNElems-1;
71 while (first<=last) {
72 int mid = (first+last)>>1;
73 if (ind>fIndex[mid]) first = mid+1;
74 else if (ind<fIndex[mid]) last = mid-1;
75 else {fElems[mid] = 0.; return;}
76 }
77}
78
79//___________________________________________________________
80Double_t& AliVectorSparse::FindIndexAdd(Int_t ind)
81{
82 // increment an element with given index
83 //printf("V: findindexAdd\n");
84 int first = 0;
85 int last = fNElems-1;
86 while (first<=last) {
87 int mid = (first+last)>>1;
88 if (ind>fIndex[mid]) first = mid+1;
89 else if (ind<fIndex[mid]) last = mid-1;
90 else return fElems[mid];
91 }
92 // need to insert a new element
93 UShort_t *arrI = new UShort_t[fNElems+1];
94 memcpy(arrI,fIndex,first*sizeof(UShort_t));
95 arrI[first] = ind;
96 memcpy(arrI+first+1,fIndex+first,(fNElems-first)*sizeof(UShort_t));
97 delete[] fIndex;
98 fIndex = arrI;
99 //
100 Double_t *arrE = new Double_t[fNElems+1];
101 memcpy(arrE,fElems,first*sizeof(Double_t));
102 arrE[first] = 0;
103 memcpy(arrE+first+1,fElems+first,(fNElems-first)*sizeof(Double_t));
104 delete[] fElems;
105 fElems = arrE;
106 //
107 fNElems++;
108 return fElems[first];
109 //
110}
111
112//__________________________________________________________
113void AliVectorSparse::ReSize(Int_t sz,Bool_t copy)
114{
115 if (sz<1) {Clear(); return;}
116 // need to insert a new element
117 UShort_t *arrI = new UShort_t[sz];
118 Double_t *arrE = new Double_t[sz];
119 memset(arrI,0,sz*sizeof(UShort_t));
120 memset(arrE,0,sz*sizeof(Double_t));
121 //
122 if (copy && fIndex) {
123 int cpsz = TMath::Min(fNElems,sz);
124 memcpy(arrI,fIndex,cpsz*sizeof(UShort_t));
125 memcpy(arrE,fElems,cpsz*sizeof(Double_t));
126 }
127 delete[] fIndex;
128 delete[] fElems;
129 fIndex = arrI;
130 fElems = arrE;
131 fNElems = sz;
132 //
133}
134
135//__________________________________________________________
136void AliVectorSparse::SortIndices(Bool_t valuesToo)
137{
138 // sort indices in increasing order. Used to fix the row after ILUk decomposition
139 for (int i=fNElems;i--;) for (int j=i;j--;) if (fIndex[i]<fIndex[j]) { //swap
140 UShort_t tmpI = fIndex[i]; fIndex[i] = fIndex[j]; fIndex[j]=tmpI;
141 if (valuesToo) {Double_t tmpV = fElems[i];fElems[i]=fElems[j];fElems[j]=tmpV;}
142 }
143}
144
145//__________________________________________________________
146void AliVectorSparse::Print(Option_t* ) const
147{
148 printf("|");
149 for (int i=0;i<fNElems;i++) printf("%2d:%+.2e|",fIndex[i],fElems[i]);
150 printf("|\n");
151}
152
153//___________________________________________________________
154void AliVectorSparse::Add(Double_t *valc,Int_t *indc,Int_t n)
155{
156 // add indiced array to row. Indices must be in increasing order
157 int indx;
158 int nadd = 0;
159 //
160 int last = fNElems-1;
161 int mid = 0;
162 for (int i=n;i--;) {
163 // if the element with this index is already defined, just add the value
164 int first = 0;
165 Bool_t toAdd = kTRUE;
166 indx = indc[i];
167 while (first<=last) {
168 mid = (first+last)>>1;
169 if (indx>fIndex[mid]) first = mid+1;
170 else if (indx<fIndex[mid]) last = mid-1;
171 else {
172 fElems[mid] += valc[i];
173 indc[i] = -1;
174 toAdd = kFALSE;
175 last = mid-1; // profit from the indices being ordered
176 break;
177 }
178 }
179 if (toAdd) nadd++;
180 }
181 //
182 if (nadd<1) return; // nothing to do anymore
183 //
184 // need to expand the row
185 UShort_t *arrI = new UShort_t[fNElems+nadd];
186 Double_t *arrE = new Double_t[fNElems+nadd];
187 // copy old elems embedding the new ones
188 int inew=0,iold=0;
189 for (int i=0;i<n;i++) {
190 if ( (indx=indc[i])<0) continue;
191 while (iold<fNElems && fIndex[iold]<indx) {
192 arrI[inew] = fIndex[iold];
193 arrE[inew++] = fElems[iold++];
194 }
195 arrI[inew] = indx;
196 arrE[inew++] = valc[i];
197 }
198 // copy the rest
199 while (iold<fNElems) {
200 arrI[inew] = fIndex[iold];
201 arrE[inew++] = fElems[iold++];
202 }
203 //
204 delete[] fIndex;
205 delete[] fElems;
206 fIndex = arrI;
207 fElems = arrE;
208 //
209 fNElems += nadd;
210 //
211}
212