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"
22 /**********************************************************************************************/
23 /* Sparse vector class, used as row of the AliMatrixSparse class */
25 /* Author: ruben.shahoyan@cern.ch */
27 /**********************************************************************************************/
29 ClassImp(AliVectorSparse)
31 //___________________________________________________________
32 AliVectorSparse::AliVectorSparse()
33 : fNElems(0),fIndex(0),fElems(0) {}
35 //___________________________________________________________
36 AliVectorSparse::AliVectorSparse(const AliVectorSparse& src)
37 : TObject(src),fNElems(src.fNElems),fIndex(0),fElems(0)
40 fIndex = new UShort_t[fNElems];
41 fElems = new Double_t[fNElems];
42 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
43 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
46 //___________________________________________________________
47 void AliVectorSparse::Clear(Option_t*)
50 delete[] fIndex; fIndex = 0;
51 delete[] fElems; fElems = 0;
55 //___________________________________________________________
56 AliVectorSparse& AliVectorSparse::operator=(const AliVectorSparse& src)
59 if (&src==this) return *this;
61 TObject::operator=(src);
62 fNElems = src.fNElems;
63 fIndex = new UShort_t[fNElems];
64 fElems = new Double_t[fNElems];
65 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
66 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
71 //___________________________________________________________
72 Double_t AliVectorSparse::FindIndex(Int_t ind) const
74 // return an element with given index
75 //printf("V: findindex\n");
79 int mid = (first+last)>>1;
80 if (ind>fIndex[mid]) first = mid+1;
81 else if (ind<fIndex[mid]) last = mid-1;
82 else return fElems[mid];
87 //___________________________________________________________
88 void AliVectorSparse::SetToZero(Int_t ind)
90 // set element to 0 if it was already defined
94 int mid = (first+last)>>1;
95 if (ind>fIndex[mid]) first = mid+1;
96 else if (ind<fIndex[mid]) last = mid-1;
97 else {fElems[mid] = 0.; return;}
101 //___________________________________________________________
102 Double_t& AliVectorSparse::FindIndexAdd(Int_t ind)
104 // increment an element with given index
105 //printf("V: findindexAdd\n");
107 int last = fNElems-1;
108 while (first<=last) {
109 int mid = (first+last)>>1;
110 if (ind>fIndex[mid]) first = mid+1;
111 else if (ind<fIndex[mid]) last = mid-1;
112 else return fElems[mid];
114 // need to insert a new element
115 UShort_t *arrI = new UShort_t[fNElems+1];
116 memcpy(arrI,fIndex,first*sizeof(UShort_t));
118 memcpy(arrI+first+1,fIndex+first,(fNElems-first)*sizeof(UShort_t));
122 Double_t *arrE = new Double_t[fNElems+1];
123 memcpy(arrE,fElems,first*sizeof(Double_t));
125 memcpy(arrE+first+1,fElems+first,(fNElems-first)*sizeof(Double_t));
130 return fElems[first];
134 //__________________________________________________________
135 void AliVectorSparse::ReSize(Int_t sz,Bool_t copy)
138 if (sz<1) {Clear(); return;}
139 // need to insert a new element
140 UShort_t *arrI = new UShort_t[sz];
141 Double_t *arrE = new Double_t[sz];
142 memset(arrI,0,sz*sizeof(UShort_t));
143 memset(arrE,0,sz*sizeof(Double_t));
145 if (copy && fIndex) {
146 int cpsz = TMath::Min(fNElems,sz);
147 memcpy(arrI,fIndex,cpsz*sizeof(UShort_t));
148 memcpy(arrE,fElems,cpsz*sizeof(Double_t));
158 //__________________________________________________________
159 void AliVectorSparse::SortIndices(Bool_t valuesToo)
161 // sort indices in increasing order. Used to fix the row after ILUk decomposition
162 for (int i=fNElems;i--;) for (int j=i;j--;) if (fIndex[i]<fIndex[j]) { //swap
163 UShort_t tmpI = fIndex[i]; fIndex[i] = fIndex[j]; fIndex[j]=tmpI;
164 if (valuesToo) {Double_t tmpV = fElems[i];fElems[i]=fElems[j];fElems[j]=tmpV;}
168 //__________________________________________________________
169 void AliVectorSparse::Print(Option_t* opt) const
172 TString sopt = opt; sopt.ToLower();
173 int ndig = sopt.Atoi();
174 if (ndig<=1) ndig = 2;
179 for (int i=0;i<fNElems;i++) printf(sopt.Data(),fIndex[i],fElems[i]);
183 //___________________________________________________________
184 void AliVectorSparse::Add(Double_t *valc,Int_t *indc,Int_t n)
186 // add indiced array to row. Indices must be in increasing order
190 int last = fNElems-1;
193 // if the element with this index is already defined, just add the value
195 Bool_t toAdd = kTRUE;
197 while (first<=last) {
198 mid = (first+last)>>1;
199 if (indx>fIndex[mid]) first = mid+1;
200 else if (indx<fIndex[mid]) last = mid-1;
202 fElems[mid] += valc[i];
205 last = mid-1; // profit from the indices being ordered
212 if (nadd<1) return; // nothing to do anymore
214 // need to expand the row
215 UShort_t *arrI = new UShort_t[fNElems+nadd];
216 Double_t *arrE = new Double_t[fNElems+nadd];
217 // copy old elems embedding the new ones
219 for (int i=0;i<n;i++) {
220 if ( (indx=indc[i])<0) continue;
221 while (iold<fNElems && fIndex[iold]<indx) {
222 arrI[inew] = fIndex[iold];
223 arrE[inew++] = fElems[iold++];
226 arrE[inew++] = valc[i];
229 while (iold<fNElems) {
230 arrI[inew] = fIndex[iold];
231 arrE[inew++] = fElems[iold++];