1 /**************************************************************************
2 * Copyright(c) 2007-2009, 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 #include "AliRefArray.h"
21 //____________________________________________________________________
22 AliRefArray::AliRefArray() : fNElems(0),fRefSize(0),fElems(0),fRefInd(0),fRefBuff(0)
24 // default constructor
27 //____________________________________________________________________
28 AliRefArray::AliRefArray(UInt_t nelem,UInt_t depth) :
29 TObject(),fNElems(nelem),fRefSize(depth),fElems(0),fRefInd(0),fRefBuff(0)
33 // create array with nelem initial referres
34 if (fNElems<1) fNElems = 1;
35 fElems = new Int_t[fNElems];
37 fRefInd = new UInt_t[fRefSize];
38 fRefBuff = new UInt_t[fRefSize];
44 //____________________________________________________________________
45 AliRefArray::AliRefArray(const AliRefArray& src) :
48 fRefSize(src.fRefSize),
49 fElems(new Int_t[fNElems]),
50 fRefInd(new UInt_t[fRefSize]),
51 fRefBuff(new UInt_t[fRefSize])
56 memcpy(fElems,src.fElems,fNElems*sizeof(Int_t));
57 memcpy(fRefInd,src.fRefInd,fRefSize*sizeof(UInt_t));
58 memcpy(fRefBuff,src.fRefBuff,fRefSize*sizeof(UInt_t));
61 //____________________________________________________________________
62 AliRefArray& AliRefArray::operator=(const AliRefArray& src)
64 // create a copy with useful info (skip unused slots)
66 TObject::operator=(src);
67 fNElems = src.fNElems;
69 if (fElems) delete[] fElems;
70 if (fRefInd) delete[] fRefInd;
71 if (fRefBuff) delete[] fRefBuff;
76 fRefSize = src.fRefInd[0];
77 fRefInd = new UInt_t[fRefSize];
78 fRefBuff = new UInt_t[fRefSize];
79 memcpy(fRefInd, src.fRefInd, fRefSize*sizeof(UInt_t));
80 memcpy(fRefBuff,src.fRefBuff,fRefSize*sizeof(UInt_t));
83 fElems = new Int_t[fNElems];
84 memcpy(fElems,src.fElems,fNElems*sizeof(Int_t));
91 //____________________________________________________________________
92 AliRefArray::~AliRefArray()
100 //____________________________________________________________________
101 void AliRefArray::Expand(UInt_t size)
105 if (size>0) {printf("The size can be only increased\n");return;}
106 else size = (fNElems<<2) + 1;
108 else if (size==fNElems) return;
109 Int_t *tmpArr = new Int_t[size];
110 memcpy(tmpArr,fElems,fNElems*sizeof(Int_t));
111 memset(tmpArr+fNElems,0,(size-fNElems)*sizeof(UInt_t));
117 //____________________________________________________________________
118 void AliRefArray::Reset()
121 if (fNElems) memset(fElems,0,fNElems*sizeof(Int_t));
123 memset(fRefInd,0,fRefSize*sizeof(UInt_t));
124 memset(fRefBuff,0,fRefSize*sizeof(UInt_t));
129 //____________________________________________________________________
130 void AliRefArray::ExpandReferences(Int_t addSize)
133 if (addSize<3) addSize = 3;
134 UInt_t oldSize = fRefSize;
136 UInt_t* buff = new UInt_t[fRefSize];
137 UInt_t* ind = new UInt_t[fRefSize];
138 if (fRefBuff) memcpy(buff, fRefBuff, oldSize*sizeof(UInt_t)); // copy current content
139 if (fRefInd) memcpy(ind, fRefInd, oldSize*sizeof(UInt_t));
140 memset(buff+oldSize,0,addSize*sizeof(UInt_t));
141 memset(ind +oldSize,0,addSize*sizeof(UInt_t));
142 delete[] fRefBuff; fRefBuff = buff;
143 delete[] fRefInd; fRefInd = ind;
144 if (!oldSize) fRefInd[0] = 1;
147 //____________________________________________________________________
148 void AliRefArray::Print(Option_t*) const
151 for (UInt_t i=0;i<fNElems;i++) {
152 printf("Entry%4d: ",i);
154 if (!(ref=fElems[i])) {printf("None\n"); continue;}
155 if (fElems[i]<0) {printf("%d\n",-(1+ref)); continue;}
156 do { printf("%d ",fRefBuff[ref]-1); } while((ref=fRefInd[ref])); printf("\n");
160 //____________________________________________________________________
161 void AliRefArray::AddReferences(UInt_t from, UInt_t *refs, UInt_t nref)
163 // add nodes to the references of "from"
164 if (nref==1) {AddReference(from, refs[0]); return;}
167 if (from>=fNElems) Expand(from+1);
168 UInt_t chk = nref + (fElems[from]<0); // if <0, need to transfer to indices the only existing reference
169 if (!fRefInd) ExpandReferences(chk+1);
170 else if ( fRefInd[0]+chk >= fRefSize ) ExpandReferences(chk);
171 UInt_t &freeSlot = fRefInd[0];
172 // if there is already single ref, transfer it to indices
173 Int_t ref = fElems[from];
174 if (ref<0) { fRefInd[freeSlot]=0; fRefBuff[freeSlot] = -ref; ref = fElems[from] = freeSlot++; }
176 while(fRefInd[ref]) ref=fRefInd[ref]; // find last index of last entry for cluster from
177 if (fElems[from]) fRefInd[ref] = freeSlot; // not a first entry, register it in the indices
178 else fElems[from] = freeSlot; // first entry, register it in the refs
179 for (UInt_t ir=0;ir<nref;ir++) {
180 if (!ir && !fElems[from]) fElems[from] = freeSlot;
181 else ref = fRefInd[ref] = freeSlot;
182 fRefBuff[freeSlot++] = refs[ir]+1;