3 #ifndef ALIHLTINDEXGRID_H
4 #define ALIHLTINDEXGRID_H
5 //* This file is property of and copyright by the ALICE HLT Project *
6 //* ALICE Experiment at CERN, All rights reserved. *
7 //* See cxx source for full Copyright notice *
9 /// @file AliHLTIndexGrid.h
10 /// @author Matthias Richter
12 /// @brief Index grid for 3 dimensional coordinates
15 #include "AliHLTDataTypes.h"
20 template <typename T, typename V>
21 class AliHLTIndexGrid {
23 AliHLTIndexGrid(T maxX, T stepX,
26 int initialDataSize=-1)
39 , fDataDimension(initialDataSize)
45 if (fMaxX>0. && fMaxY>0. && fMaxZ>0 &&
46 fStepX>0. && fStepY>0. && fStepZ>0) {
47 fDimX=(int)ceil(fMaxX/fStepX);
48 fDimY=(int)ceil(fMaxY/fStepY);
49 fDimZ=(int)ceil(fMaxZ/fStepZ);
51 fCellDimension=fDimX*fDimY*fDimZ;
52 fCells=new AliHLTIndexGridCell[fCellDimension];
53 if (fDataDimension<0) fDataDimension=fgkDefaultDataSize;
54 fData=new V[fDataDimension];
59 virtual ~AliHLTIndexGrid() {
61 if (fData) delete [] fData;
62 if (fCells) delete [] fCells;
65 // for now array of spacepoint ids
68 int GetDimensionX() const {return fDimX;}
69 int GetDimensionY() const {return fDimY;}
70 int GetDimensionZ() const {return fDimZ;}
71 int GetXIndex(T x) const {
72 if (x>fMaxX) return fDimX-1;
74 return (int)(x/fStepX);
76 int GetYIndex(T y) const {
77 if (y>fMaxY) return fDimY-1;
79 return (int)(y/fStepY);
81 int GetZIndex(T z) const {
82 if (z>fMaxZ) return fDimZ-1;
84 return (int)(z/fStepZ);
86 T GetLowerBoundX(int cell) const {
87 if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
88 int index=cell/(fDimY*fDimZ);
91 T GetLowerBoundY(int cell) const {
92 if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
93 int index=cell%(fDimY*fDimZ); index/=fDimZ;
96 T GetLowerBoundZ(int cell) const {
97 if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
98 int index=cell%(fDimY*fDimZ); index%=fDimZ;
101 int GetCellIndex(T x, T y, T z) const {
102 return GetXIndex(x)*fDimY*fDimZ + (y<0?0:GetYIndex(y))*fDimZ + (z<0?0:GetZIndex(z));
104 int GetNumberOfSpacePoints(int index, int endIndex) const {
105 if (!fCells) return 0;
107 for (int cell=index; cell<endIndex && cell<fCellDimension && count<fCount; cell++) if (fCells[cell].fCount>0) count+=fCells[cell].fCount;
111 // increment counter of the cell where the spacepoint is
112 int CountSpacePoint(T x, T y, T z) {
113 // increment counter of the cell where the spacepoint is
114 int cell=GetCellIndex(x, y, z);
115 if (cell<0 || !fCells || cell>=fCellDimension) return -EFAULT;
116 if (fCells[cell].fCount<0) fCells[cell].fCount=1;
117 else fCells[cell].fCount++;
122 // add spacepoint, all spacepoints must have been counted before
123 int AddSpacePoint(ValueType t, T x, T y, T z) {
124 // add spacepoint, all spacepoints must have been counted before
125 int cell=GetCellIndex(x, y, z);
126 if (cell<0 || !fCells || cell>=fCellDimension) return -EFAULT;
127 if (fCells[cell].fFilled==fCells[cell].fCount) return -ENOSPC;
128 if (fCells[cell].fStartIndex<0 && IndexCells()<0) return -EACCES;
129 int offset=fCells[cell].fStartIndex+fCells[cell].fFilled;
131 fCells[cell].fFilled++;
136 void Clear(const char* /*option*/="") {
137 // clear internal data
138 if (fCells) memset(fCells, 0xff, fCellDimension*sizeof(AliHLTIndexGridCell));
139 if (fData) memset(fData, 0, fDataDimension*sizeof(V));
143 void Print(const char* /*option*/="") {
145 bool bPrintEmpty=false;
146 std::streamsize precision=cout.precision();
147 cout << "AliHLTIndexGrid: " << (fCells?fCellDimension:0) << " cells" << endl;
148 cout << " x: " << fDimX << " [0," << fMaxX << "]" << endl;
149 cout << " y: " << fDimY << " [0," << fMaxY << "]" << endl;
150 cout << " z: " << fDimZ << " [0," << fMaxZ << "]" << endl;
151 cout << " " << GetNumberOfSpacePoints(0, fCellDimension) << " point(s)" << endl;
153 for (int i=0; i<fCellDimension; i++) {
154 if (!bPrintEmpty && fCells[i].fCount<=0) continue;
155 cout << " " << setfill(' ') << setw(7) << setprecision(0) << i << " ("
156 << " " << setw(3) << GetLowerBoundX(i)
157 << " " << setw(3) << GetLowerBoundY(i)
158 << " " << setw(4) << GetLowerBoundZ(i)
160 cout << setw(3) << fCells[i].fCount << " entries, " << setw(3) << fCells[i].fFilled << " filled";
161 cout << " start index " << setw(5) << fCells[i].fStartIndex;
163 if (fCells[i].fCount>0) {
165 for (iterator id=begin(GetLowerBoundX(i), GetLowerBoundY(i), GetLowerBoundZ(i));
167 cout << " 0x" << hex << setw(8) << setfill('0') << id.Data();
173 cout << setprecision(precision);
181 iterator(const ValueType* pData)
183 iterator(const iterator& i)
185 iterator& operator=(const iterator& i)
186 { fData=i.fData; return *this;}
187 ~iterator() {fData=NULL;}
189 bool operator==(const iterator& i) const {return (fData!=NULL) && (fData==i.fData);}
190 bool operator!=(const iterator& i) const {return (fData!=NULL) && (fData!=i.fData);}
192 iterator& operator++() {fData++; return *this;}
193 iterator& operator--() {fData--; return *this;}
195 iterator operator++(int) {iterator i(*this); fData++; return i;}
196 iterator operator--(int) {iterator i(*this); fData--; return i;}
198 iterator& operator+=(int step) {fData+=step; return *this;}
200 const ValueType& Data() const {return *fData;}
204 const ValueType* fData; //! data
207 // prepare iterator and end marker
208 iterator& begin(T x=(T)-1, T y=(T)-1, T z=(T)-1) {
209 fIterator.~iterator();
210 fIteratorEnd.~iterator();
216 new (&fIterator) iterator(fData);
217 fIteratorEnd=fIterator;
218 fIteratorEnd+=fCount;
223 // only search for the start index if specific x selected
224 int cell=GetCellIndex(x, y, z);
225 if (cell<0 || !fCells || cell>=fCellDimension) return fIterator;
226 // get the index of the cell
227 startIndex=fCells[cell].fStartIndex;
228 if (startIndex<0 || !fData || startIndex>=fDataDimension) return fIterator;
230 // get the range end position
232 if (x<0) endCell=fCellDimension;
233 else if (y<0) endCell=GetCellIndex(x+fStepX, -1., -1.); // all entries for fixed x
234 else if (z<0) endCell=GetCellIndex(x, y+fStepY, -1.); // all entries for fixed x and y
236 // cell index returned is never outside the array
237 // so this is a special case where we get to the bounds of the array
238 endCell=fCellDimension;
241 new (&fIterator) iterator(fData+startIndex);
242 fIteratorEnd=fIterator;
243 fIteratorEnd+=GetNumberOfSpacePoints(cell, endCell);
247 // get loop end marker
252 struct AliHLTIndexGridCell {
260 // standard constructor prohibited
262 // copy constructor prohibited
263 AliHLTIndexGrid(const AliHLTIndexGrid&);
264 // assignment operator prohibited
265 AliHLTIndexGrid& operator=(const AliHLTIndexGrid&);
268 // set the start index for data of every cell based on the counts
269 if (!fCells || fCellDimension<=0) return -ENOBUFS;
272 for (; cell<fCellDimension; cell++) {
273 if (fCells[cell].fCount<0) continue;
274 fCells[cell].fStartIndex=offset;
275 offset+=fCells[cell].fCount;
276 fCells[cell].fFilled=0;
279 if (offset>fDataDimension) {
280 // grow the data array
281 auto_ptr<V> newArray(new V[offset]);
282 if (newArray.get()) {
283 memcpy(newArray.get(), fData, fDataDimension);
284 memset(newArray.get()+fDataDimension, 0, (offset-fDataDimension)*sizeof(V));
286 fData=newArray.release();
287 fDataDimension=offset;
289 for (cell=0; cell<fCellDimension; cell++) {
290 fCells[cell].fStartIndex=-1;
309 AliHLTIndexGridCell* fCells; //! cell array
310 int fCellDimension; //! size of cell array
311 ValueType* fData; //! spacepoint data
312 int fDataDimension; //! size of spacepoint data
315 iterator fIterator; //! iterator
316 iterator fIteratorEnd; //! end marker iterator
318 static const int fgkDefaultDataSize=10000; //! the default data size
320 ClassDef(AliHLTIndexGrid, 0)