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)
39 fIndex = new UShort_t[fNElems];
40 fElems = new Double_t[fNElems];
41 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
42 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
45 //___________________________________________________________
46 void AliVectorSparse::Clear(Option_t*)
48 delete[] fIndex; fIndex = 0;
49 delete[] fElems; fElems = 0;
53 //___________________________________________________________
54 AliVectorSparse& AliVectorSparse::operator=(const AliVectorSparse& src)
56 if (&src==this) return *this;
58 TObject::operator=(src);
59 fNElems = src.fNElems;
60 fIndex = new UShort_t[fNElems];
61 fElems = new Double_t[fNElems];
62 memcpy(fIndex,src.fIndex,fNElems*sizeof(UShort_t));
63 memcpy(fElems,src.fElems,fNElems*sizeof(Double_t));
68 //___________________________________________________________
69 Double_t AliVectorSparse::FindIndex(Int_t ind) const
71 // return an element with given index
72 //printf("V: findindex\n");
76 int mid = (first+last)>>1;
77 if (ind>fIndex[mid]) first = mid+1;
78 else if (ind<fIndex[mid]) last = mid-1;
79 else return fElems[mid];
84 //___________________________________________________________
85 void AliVectorSparse::SetToZero(Int_t ind)
87 // set element to 0 if it was already defined
91 int mid = (first+last)>>1;
92 if (ind>fIndex[mid]) first = mid+1;
93 else if (ind<fIndex[mid]) last = mid-1;
94 else {fElems[mid] = 0.; return;}
98 //___________________________________________________________
99 Double_t& AliVectorSparse::FindIndexAdd(Int_t ind)
101 // increment an element with given index
102 //printf("V: findindexAdd\n");
104 int last = fNElems-1;
105 while (first<=last) {
106 int mid = (first+last)>>1;
107 if (ind>fIndex[mid]) first = mid+1;
108 else if (ind<fIndex[mid]) last = mid-1;
109 else return fElems[mid];
111 // need to insert a new element
112 UShort_t *arrI = new UShort_t[fNElems+1];
113 memcpy(arrI,fIndex,first*sizeof(UShort_t));
115 memcpy(arrI+first+1,fIndex+first,(fNElems-first)*sizeof(UShort_t));
119 Double_t *arrE = new Double_t[fNElems+1];
120 memcpy(arrE,fElems,first*sizeof(Double_t));
122 memcpy(arrE+first+1,fElems+first,(fNElems-first)*sizeof(Double_t));
127 return fElems[first];
131 //__________________________________________________________
132 void AliVectorSparse::ReSize(Int_t sz,Bool_t copy)
134 if (sz<1) {Clear(); return;}
135 // need to insert a new element
136 UShort_t *arrI = new UShort_t[sz];
137 Double_t *arrE = new Double_t[sz];
138 memset(arrI,0,sz*sizeof(UShort_t));
139 memset(arrE,0,sz*sizeof(Double_t));
141 if (copy && fIndex) {
142 int cpsz = TMath::Min(fNElems,sz);
143 memcpy(arrI,fIndex,cpsz*sizeof(UShort_t));
144 memcpy(arrE,fElems,cpsz*sizeof(Double_t));
154 //__________________________________________________________
155 void AliVectorSparse::SortIndices(Bool_t valuesToo)
157 // sort indices in increasing order. Used to fix the row after ILUk decomposition
158 for (int i=fNElems;i--;) for (int j=i;j--;) if (fIndex[i]<fIndex[j]) { //swap
159 UShort_t tmpI = fIndex[i]; fIndex[i] = fIndex[j]; fIndex[j]=tmpI;
160 if (valuesToo) {Double_t tmpV = fElems[i];fElems[i]=fElems[j];fElems[j]=tmpV;}
164 //__________________________________________________________
165 void AliVectorSparse::Print(Option_t* opt) const
167 TString sopt = opt; sopt.ToLower();
168 int ndig = sopt.Atoi();
169 if (ndig<=1) ndig = 2;
174 for (int i=0;i<fNElems;i++) printf(sopt.Data(),fIndex[i],fElems[i]);
178 //___________________________________________________________
179 void AliVectorSparse::Add(Double_t *valc,Int_t *indc,Int_t n)
181 // add indiced array to row. Indices must be in increasing order
185 int last = fNElems-1;
188 // if the element with this index is already defined, just add the value
190 Bool_t toAdd = kTRUE;
192 while (first<=last) {
193 mid = (first+last)>>1;
194 if (indx>fIndex[mid]) first = mid+1;
195 else if (indx<fIndex[mid]) last = mid-1;
197 fElems[mid] += valc[i];
200 last = mid-1; // profit from the indices being ordered
207 if (nadd<1) return; // nothing to do anymore
209 // need to expand the row
210 UShort_t *arrI = new UShort_t[fNElems+nadd];
211 Double_t *arrE = new Double_t[fNElems+nadd];
212 // copy old elems embedding the new ones
214 for (int i=0;i<n;i++) {
215 if ( (indx=indc[i])<0) continue;
216 while (iold<fNElems && fIndex[iold]<indx) {
217 arrI[inew] = fIndex[iold];
218 arrE[inew++] = fElems[iold++];
221 arrE[inew++] = valc[i];
224 while (iold<fNElems) {
225 arrI[inew] = fIndex[iold];
226 arrE[inew++] = fElems[iold++];