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"
22 template <typename T, typename V>
23 class AliHLTIndexGrid {
25 AliHLTIndexGrid(T maxX, T stepX,
28 int initialDataSize=-1)
41 , fDataDimension(initialDataSize)
47 if (fMaxX>0. && fMaxY>0. && fMaxZ>0 &&
48 fStepX>0. && fStepY>0. && fStepZ>0) {
49 fDimX=(int)ceil(fMaxX/fStepX);
50 fDimY=(int)ceil(fMaxY/fStepY);
51 fDimZ=(int)ceil(fMaxZ/fStepZ);
53 fCellDimension=fDimX*fDimY*fDimZ;
54 fCells=new AliHLTIndexGridCell[fCellDimension];
55 if (fDataDimension<0) fDataDimension=fgkDefaultDataSize;
56 fData=new V[fDataDimension];
61 virtual ~AliHLTIndexGrid() {
63 if (fData) delete [] fData;
64 if (fCells) delete [] fCells;
67 // for now array of spacepoint ids
70 int GetDimensionX() const {return fDimX;}
71 int GetDimensionY() const {return fDimY;}
72 int GetDimensionZ() const {return fDimZ;}
73 int GetXIndex(T x) const {
74 if (x>fMaxX) return fDimX-1;
76 return (int)(x/fStepX);
78 int GetYIndex(T y) const {
79 if (y>fMaxY) return fDimY-1;
81 return (int)(y/fStepY);
83 int GetZIndex(T z) const {
84 if (z>fMaxZ) return fDimZ-1;
86 return (int)(z/fStepZ);
88 int Index(int xindex, int yindex, int zindex) {
89 return xindex*fDimY*fDimZ + yindex*fDimZ + zindex;
91 T GetLowerBoundX(int cell) const {
92 if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
93 int index=cell/(fDimY*fDimZ);
96 T GetCenterX(int cell) const {
97 if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
98 return GetLowerBoundX(cell)+fStepX/2;
100 T GetLowerBoundY(int cell) const {
101 if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
102 int index=cell%(fDimY*fDimZ); index/=fDimZ;
105 T GetCenterY(int cell) const {
106 if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
107 return GetLowerBoundY(cell)+fStepY/2;
109 T GetLowerBoundZ(int cell) const {
110 if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
111 int index=cell%(fDimY*fDimZ); index%=fDimZ;
114 T GetCenterZ(int cell) const {
115 if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
116 return GetLowerBoundZ(cell)+fStepZ/2;
118 int GetCellIndex(T x, T y, T z) const {
119 return GetXIndex(x)*fDimY*fDimZ + (y<0?0:GetYIndex(y))*fDimZ + (z<0?0:GetZIndex(z));
121 int GetNumberOfSpacePoints(int index=0, int endIndex=-1) const {
122 if (!fCells) return 0;
123 if (endIndex<0) endIndex=fCellDimension;
125 for (int cell=index; cell<endIndex && cell<fCellDimension && count<fCount; cell++) if (fCells[cell].fCount>0) count+=fCells[cell].fCount;
129 // increment counter of the cell where the spacepoint is
130 int CountSpacePoint(T x, T y, T z) {
131 // increment counter of the cell where the spacepoint is
132 int cell=GetCellIndex(x, y, z);
133 if (cell<0 || !fCells || cell>=fCellDimension) return -EFAULT;
134 if (fCells[cell].fCount<0) fCells[cell].fCount=1;
135 else fCells[cell].fCount++;
140 // add spacepoint, all spacepoints must have been counted before
141 int AddSpacePoint(ValueType t, T x, T y, T z) {
142 // add spacepoint, all spacepoints must have been counted before
143 int cell=GetCellIndex(x, y, z);
144 if (cell<0 || !fCells || cell>=fCellDimension) return -EFAULT;
145 if (fCells[cell].fFilled==fCells[cell].fCount) return -ENOSPC;
146 if (fCells[cell].fStartIndex<0 && IndexCells()<0) return -EACCES;
147 int offset=fCells[cell].fStartIndex+fCells[cell].fFilled;
149 fCells[cell].fFilled++;
154 void Clear(const char* /*option*/="") {
155 // clear internal data
156 if (fCells) memset(fCells, 0xff, fCellDimension*sizeof(AliHLTIndexGridCell));
157 if (fData) memset(fData, 0, fDataDimension*sizeof(V));
161 void Print(const char* /*option*/="") {
163 bool bPrintEmpty=false;
164 ios::fmtflags coutflags=cout.flags(); // backup cout status flags
165 cout << "AliHLTIndexGrid: " << (fCells?fCellDimension:0) << " cells" << endl;
166 cout << " x: " << fDimX << " [0," << fMaxX << "]" << endl;
167 cout << " y: " << fDimY << " [0," << fMaxY << "]" << endl;
168 cout << " z: " << fDimZ << " [0," << fMaxZ << "]" << endl;
169 cout << " " << GetNumberOfSpacePoints(0, fCellDimension) << " point(s)" << endl;
171 for (int i=0; i<fCellDimension; i++) {
172 if (!bPrintEmpty && fCells[i].fCount<=0) continue;
173 cout << " " << setfill(' ') << setw(7) << fixed << setprecision(0) << i << " ("
174 << " " << setw(3) << GetLowerBoundX(i)
175 << " " << setw(3) << GetLowerBoundY(i)
176 << " " << setw(4) << GetLowerBoundZ(i)
178 cout << setw(3) << fCells[i].fCount << " entries, " << setw(3) << fCells[i].fFilled << " filled";
179 cout << " start index " << setw(5) << fCells[i].fStartIndex;
181 if (fCells[i].fCount>0) {
183 for (iterator id=begin(GetLowerBoundX(i), GetLowerBoundY(i), GetLowerBoundZ(i));
185 cout << " 0x" << hex << setw(8) << setfill('0') << id.Data();
191 cout.flags(coutflags); // restore the original flags
199 iterator(ValueType* pData)
201 iterator(const iterator& i)
203 iterator& operator=(const iterator& i)
204 { if (this!=&i) {fData=i.fData;} return *this;}
205 ~iterator() {fData=NULL;}
207 bool operator==(const iterator& i) const {return (fData!=NULL) && (fData==i.fData);}
208 bool operator!=(const iterator& i) const {return (fData!=NULL) && (fData!=i.fData);}
210 iterator& operator++() {fData++; return *this;}
211 iterator& operator--() {fData--; return *this;}
213 iterator operator++(int) {iterator i(*this); fData++; return i;}
214 iterator operator--(int) {iterator i(*this); fData--; return i;}
216 iterator& operator+=(int step) {fData+=step; return *this;}
218 const ValueType& Data() const {return *fData;}
219 ValueType& Data() {return *fData;}
221 ValueType operator*() {return *fData;}
225 ValueType* fData; //! data
228 // prepare iterator and end marker
229 iterator& begin(T x=(T)-1, T y=(T)-1, T z=(T)-1) {
230 fIterator.~iterator();
231 fIteratorEnd.~iterator();
237 new (&fIterator) iterator(fData);
238 fIteratorEnd=fIterator;
239 fIteratorEnd+=fCount;
244 // only search for the start index if specific x selected
245 int cell=GetCellIndex(x, y, z);
246 if (cell<0 || !fCells || cell>=fCellDimension) return fIterator;
247 // get the index of the cell
248 startIndex=fCells[cell].fStartIndex;
249 if (!fData || startIndex>=fDataDimension) return fIterator;
251 // get the range end position
253 if (x<0) endCell=fCellDimension;
254 else if (y<0) endCell=GetCellIndex(x+fStepX, (T)-1, (T)-1); // all entries for fixed x
255 else if (z<0) endCell=GetCellIndex(x, y+fStepY, (T)-1); // all entries for fixed x and y
257 // cell index returned is never outside the array
258 // so this is a special case where we get to the bounds of the array
259 endCell=fCellDimension;
262 // find the first cell with content in the range
263 for (; startIndex<0 && cell<endCell;) {
264 startIndex=fCells[++cell].fStartIndex;
266 if (startIndex<0) return fIterator;
268 new (&fIterator) iterator(fData+startIndex);
269 fIteratorEnd=fIterator;
270 fIteratorEnd+=GetNumberOfSpacePoints(cell, endCell);
274 // get loop end marker
279 iterator& find(ValueType v) {
280 for (iterator i=begin(); i!=end(); i++) {
289 // find cell of entry
290 int FindCell(ValueType v) const {
291 if (!fCells) return -1;
292 for (int cell=0; cell<fCellDimension; cell++)
293 for (int count=0; count<fCells[cell].fCount; count++)
294 if (fData[fCells[cell].fStartIndex+count]==v)
299 struct AliHLTIndexGridCell {
307 // standard constructor prohibited
309 // copy constructor prohibited
310 AliHLTIndexGrid(const AliHLTIndexGrid&);
311 // assignment operator prohibited
312 AliHLTIndexGrid& operator=(const AliHLTIndexGrid&);
315 // set the start index for data of every cell based on the counts
316 if (!fCells || fCellDimension<=0) return -ENOBUFS;
319 for (; cell<fCellDimension; cell++) {
320 if (fCells[cell].fCount<0) continue;
321 fCells[cell].fStartIndex=offset;
322 offset+=fCells[cell].fCount;
323 fCells[cell].fFilled=0;
326 if (offset>fDataDimension) {
327 // grow the data array
328 auto_ptr<V> newArray(new V[offset]);
329 if (newArray.get()) {
330 memcpy(newArray.get(), fData, fDataDimension);
331 memset(newArray.get()+fDataDimension, 0, (offset-fDataDimension)*sizeof(V));
333 fData=newArray.release();
334 fDataDimension=offset;
336 for (cell=0; cell<fCellDimension; cell++) {
337 fCells[cell].fStartIndex=-1;
356 AliHLTIndexGridCell* fCells; //! cell array
357 int fCellDimension; //! size of cell array
358 ValueType* fData; //! spacepoint data
359 int fDataDimension; //! size of spacepoint data
362 iterator fIterator; //! iterator
363 iterator fIteratorEnd; //! end marker iterator
365 static const int fgkDefaultDataSize=10000; //! the default data size
367 ClassDef(AliHLTIndexGrid, 0)