1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /* $Id: AliRun.cxx 30859 2009-02-02 16:24:37Z fca $ */
19 #include "AliVectorSparse.h"
21 /**********************************************************************************************/
22 /* Sparse vector class, used as row of the AliMatrixSparse class */
24 /* Author: ruben.shahoyan@cern.ch */
26 /**********************************************************************************************/
28 ClassImp(AliVectorSparse)
30 //___________________________________________________________
31 AliVectorSparse::AliVectorSparse()
32 : fNElems(0),fIndex(0),fElems(0) {}
34 //___________________________________________________________
35 AliVectorSparse::AliVectorSparse(const AliVectorSparse& src)
36 : TObject(src),fNElems(src.fNElems),fIndex(0),fElems(0)
38 fIndex = new UShort_t[fNElems];
39 fElems = new Double_t[fNElems];
40 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
41 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
44 //___________________________________________________________
45 void AliVectorSparse::Clear(Option_t*)
47 delete[] fIndex; fIndex = 0;
48 delete[] fElems; fElems = 0;
52 //___________________________________________________________
53 AliVectorSparse& AliVectorSparse::operator=(const AliVectorSparse& src)
55 if (&src==this) return *this;
57 TObject::operator=(src);
58 fNElems = src.fNElems;
59 fIndex = new UShort_t[fNElems];
60 fElems = new Double_t[fNElems];
61 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
62 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
67 //___________________________________________________________
68 Double_t AliVectorSparse::FindIndex(Int_t ind) const
70 // return an element with given index
71 //printf("V: findindex\n");
75 int mid = (first+last)>>1;
76 if (ind>fIndex[mid]) first = mid+1;
77 else if (ind<fIndex[mid]) last = mid-1;
78 else return fElems[mid];
83 //___________________________________________________________
84 void AliVectorSparse::SetToZero(Int_t ind)
86 // set element to 0 if it was already defined
90 int mid = (first+last)>>1;
91 if (ind>fIndex[mid]) first = mid+1;
92 else if (ind<fIndex[mid]) last = mid-1;
93 else {fElems[mid] = 0.; return;}
97 //___________________________________________________________
98 Double_t& AliVectorSparse::FindIndexAdd(Int_t ind)
100 // increment an element with given index
101 //printf("V: findindexAdd\n");
103 int last = fNElems-1;
104 while (first<=last) {
105 int mid = (first+last)>>1;
106 if (ind>fIndex[mid]) first = mid+1;
107 else if (ind<fIndex[mid]) last = mid-1;
108 else return fElems[mid];
110 // need to insert a new element
111 UShort_t *arrI = new UShort_t[fNElems+1];
112 memcpy(arrI,fIndex,first*sizeof(UShort_t));
114 memcpy(arrI+first+1,fIndex+first,(fNElems-first)*sizeof(UShort_t));
118 Double_t *arrE = new Double_t[fNElems+1];
119 memcpy(arrE,fElems,first*sizeof(Double_t));
121 memcpy(arrE+first+1,fElems+first,(fNElems-first)*sizeof(Double_t));
126 return fElems[first];
130 //__________________________________________________________
131 void AliVectorSparse::ReSize(Int_t sz,Bool_t copy)
133 if (sz<1) {Clear(); return;}
134 // need to insert a new element
135 UShort_t *arrI = new UShort_t[sz];
136 Double_t *arrE = new Double_t[sz];
137 memset(arrI,0,sz*sizeof(UShort_t));
138 memset(arrE,0,sz*sizeof(Double_t));
140 if (copy && fIndex) {
141 int cpsz = TMath::Min(fNElems,sz);
142 memcpy(arrI,fIndex,cpsz*sizeof(UShort_t));
143 memcpy(arrE,fElems,cpsz*sizeof(Double_t));
153 //__________________________________________________________
154 void AliVectorSparse::SortIndices(Bool_t valuesToo)
156 // sort indices in increasing order. Used to fix the row after ILUk decomposition
157 for (int i=fNElems;i--;) for (int j=i;j--;) if (fIndex[i]<fIndex[j]) { //swap
158 UShort_t tmpI = fIndex[i]; fIndex[i] = fIndex[j]; fIndex[j]=tmpI;
159 if (valuesToo) {Double_t tmpV = fElems[i];fElems[i]=fElems[j];fElems[j]=tmpV;}
163 //__________________________________________________________
164 void AliVectorSparse::Print(Option_t* ) const
167 for (int i=0;i<fNElems;i++) printf("%2d:%+.2e|",fIndex[i],fElems[i]);
171 //___________________________________________________________
172 void AliVectorSparse::Add(Double_t *valc,Int_t *indc,Int_t n)
174 // add indiced array to row. Indices must be in increasing order
178 int last = fNElems-1;
181 // if the element with this index is already defined, just add the value
183 Bool_t toAdd = kTRUE;
185 while (first<=last) {
186 mid = (first+last)>>1;
187 if (indx>fIndex[mid]) first = mid+1;
188 else if (indx<fIndex[mid]) last = mid-1;
190 fElems[mid] += valc[i];
193 last = mid-1; // profit from the indices being ordered
200 if (nadd<1) return; // nothing to do anymore
202 // need to expand the row
203 UShort_t *arrI = new UShort_t[fNElems+nadd];
204 Double_t *arrE = new Double_t[fNElems+nadd];
205 // copy old elems embedding the new ones
207 for (int i=0;i<n;i++) {
208 if ( (indx=indc[i])<0) continue;
209 while (iold<fNElems && fIndex[iold]<indx) {
210 arrI[inew] = fIndex[iold];
211 arrE[inew++] = fElems[iold++];
214 arrE[inew++] = valc[i];
217 while (iold<fNElems) {
218 arrI[inew] = fIndex[iold];
219 arrE[inew++] = fElems[iold++];