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)
32 // create array with nelem initial referres
33 if (fNElems<1) fNElems = 1;
34 fElems = new Int_t[fNElems];
36 fRefInd = new UInt_t[fRefSize];
37 fRefBuff = new UInt_t[fRefSize];
43 //____________________________________________________________________
44 AliRefArray::AliRefArray(const AliRefArray& src) :
45 TObject(),fNElems(0),fRefSize(0),fElems(0),fRefInd(0),fRefBuff(0)
47 // create a copy with useful info (skip unused slots)
51 //____________________________________________________________________
52 AliRefArray& AliRefArray::operator=(const AliRefArray& src)
54 // create a copy with useful info (skip unused slots)
56 TObject::operator=(src);
57 fNElems = src.fNElems;
59 if (fElems) delete[] fElems;
60 if (fRefInd) delete[] fRefInd;
61 if (fRefBuff) delete[] fRefBuff;
66 fRefSize = src.fRefInd[0]+1;
67 fRefInd = new UInt_t[fRefSize];
68 fRefBuff = new UInt_t[fRefSize];
69 memcpy(fRefInd, src.fRefInd, fRefSize*sizeof(UInt_t));
70 memcpy(fRefBuff,src.fRefBuff,fRefSize*sizeof(UInt_t));
73 fElems = new Int_t[fNElems];
74 memcpy(fElems,src.fElems,fNElems*sizeof(Int_t));
81 //____________________________________________________________________
82 AliRefArray::~AliRefArray()
85 delete[] fElems; fElems = 0;
86 delete[] fRefBuff; fRefBuff = 0;
87 delete[] fRefInd; fRefInd = 0;
90 //____________________________________________________________________
91 void AliRefArray::Expand(UInt_t size)
95 if (size>0) {printf("The size can be only increased\n");return;}
96 else size = (fNElems<<2) + 1;
98 else if (size==fNElems) return;
99 Int_t *tmpArr = new Int_t[size];
100 memcpy(tmpArr,fElems,fNElems*sizeof(Int_t));
101 memset(tmpArr+fNElems,0,(size-fNElems)*sizeof(UInt_t));
107 //____________________________________________________________________
108 void AliRefArray::Reset()
111 if (fNElems) memset(fElems,0,fNElems*sizeof(Int_t));
113 memset(fRefInd,0,fRefSize*sizeof(UInt_t));
114 memset(fRefBuff,0,fRefSize*sizeof(UInt_t));
119 //____________________________________________________________________
120 void AliRefArray::ExpandReferences(Int_t addSize)
123 if (addSize<3) addSize = 3;
124 UInt_t oldSize = fRefSize;
126 UInt_t* buff = new UInt_t[fRefSize];
127 UInt_t* ind = new UInt_t[fRefSize];
128 memcpy(buff, fRefBuff, oldSize*sizeof(UInt_t)); // copy current content
129 if (fRefInd) memcpy(ind, fRefInd, oldSize*sizeof(UInt_t));
130 memset(buff+oldSize,0,(fRefSize-oldSize)*sizeof(UInt_t));
131 memset(ind +oldSize,0,(fRefSize-oldSize)*sizeof(UInt_t));
132 delete[] fRefBuff; fRefBuff = buff;
133 delete[] fRefInd; fRefInd = ind;
134 if (!oldSize) fRefInd[0] = 1;
137 //____________________________________________________________________
138 void AliRefArray::Print(Option_t*) const
141 for (UInt_t i=0;i<fNElems;i++) {
142 printf("Entry%4d: ",i);
144 if (!(ref=fElems[i])) {printf("None\n"); continue;}
145 if (fElems[i]<0) {printf("%d\n",-(1+ref)); continue;}
146 do { printf("%d ",fRefBuff[ref]-1); } while((ref=fRefInd[ref])); printf("\n");
150 //____________________________________________________________________
151 void AliRefArray::AddReferences(UInt_t from, UInt_t *refs, UInt_t nref)
153 // add nodes to the references of "from"
154 if (nref==1) {AddReference(from, refs[0]); return;}
157 if (from>=fNElems) Expand(from+1);
158 UInt_t chk = nref + (fElems[from]<0); // if <0, need to transfer to indices the only existing reference
159 if (!fRefInd) ExpandReferences(chk+1);
160 else if ( fRefInd[0]+chk >= fRefSize ) ExpandReferences(chk);
161 UInt_t &freeSlot = fRefInd[0];
162 // if there is already single ref, transfer it to indices
163 Int_t ref = fElems[from];
164 if (ref<0) { fRefInd[freeSlot]=0; fRefBuff[freeSlot] = -ref; ref = fElems[from] = freeSlot++; }
166 while(fRefInd[ref]) ref=fRefInd[ref]; // find last index of last entry for cluster from
167 if (fElems[from]) fRefInd[ref] = freeSlot; // not a first entry, register it in the indices
168 else fElems[from] = freeSlot; // first entry, register it in the refs
169 for (UInt_t ir=0;ir<nref;ir++) {
170 if (!ir && !fElems[from]) fElems[from] = freeSlot;
171 else ref = fRefInd[ref] = freeSlot;
172 fRefBuff[freeSlot++] = refs[ir]+1;