Grid.h
Go to the documentation of this file.
1 #ifndef GRID_HEADER
2 #define GRID_HEADER
3 
4 #ifndef WIN32
5 #include <ext/hash_map>
6 #define stdext __gnu_cxx
7 #else
8 #include <hash_map>
9 #endif
10 
11 template< class CellT, unsigned int DimT >
13 {
14 public:
16  : m_array(NULL)
17  {}
18 
20  : m_array(array)
21  {}
22 
24  {
25  delete[] m_array;
26  }
27 
28  ArrayGridKernel < CellT, DimT - 1 > *& Data()
29  {
30  return m_array;
31  }
32 
33 protected:
34  ArrayGridKernel < CellT, DimT - 1 > *m_array;
35 };
36 
37 
38 template< class CellT >
39 class ArrayGridKernel< CellT, 0 >
40 {
41 public:
42  operator CellT& ()
43  {
44  return m_cell;
45  }
46 
47  operator const CellT& () const
48  {
49  return m_cell;
50  }
51 
52  CellT& Data()
53  {
54  return m_cell;
55  }
56 
57 protected:
58  CellT m_cell;
59 };
60 
61 
62 template< class CellT, unsigned int DimT >
64 {
65 public:
66  ArrayGridAccessor(unsigned int* extent,
68  : m_extent(extent)
69  , m_array(array)
70  {}
71 
72  ArrayGridAccessor < CellT, DimT - 1 > operator[](unsigned int i)
73  {
74  if (!m_array)
75  {
76  m_array = new ArrayGridKernel < CellT, DimT - 1 > [*m_extent];
77  }
78  return ArrayGridAccessor < CellT, DimT - 1 > (m_extent + 1,
79  m_array[i].Data());
80  }
81 
82 private:
83  unsigned int* m_extent;
84  ArrayGridKernel < CellT, DimT - 1 > *& m_array;
85 };
86 
87 
88 template< class CellT >
89 class ArrayGridAccessor< CellT, 0 >
90 {
91 public:
92  ArrayGridAccessor(unsigned int*, CellT& cell)
93  : m_cell(cell)
94  {}
95 
96  CellT& operator=(const CellT& c)
97  {
98  return m_cell = c;
99  }
100 
101  operator CellT& ()
102  {
103  return m_cell;
104  }
105 
106 private:
107  CellT& m_cell;
108 };
109 
110 template< class CellT, unsigned int DimT >
112 {
113 public:
115  : m_array(array)
116  {}
117 
118  const ConstArrayGridAccessor < CellT, DimT - 1 > operator[](
119  unsigned int i) const
120  {
121  return ConstArrayGridAccessor < CellT, DimT - 1 > (m_array[i].Data());
122  }
123 
124 private:
125  const ArrayGridKernel < CellT, DimT - 1 > *m_array;
126 };
127 
128 template< class CellT >
129 class ConstArrayGridAccessor< CellT, 0 >
130 {
131 public:
132  ConstArrayGridAccessor(unsigned int*, const CellT& cell)
133  : m_cell(cell)
134  {}
135 
136  operator const CellT& () const
137  {
138  return m_cell;
139  }
140 
141 private:
142  const CellT& m_cell;
143 };
144 
145 template< class CellT, unsigned int DimT >
147  : protected ArrayGridKernel< CellT, DimT >
148 {
149 public:
151  {
152  for (unsigned int i = 0; i < DimT; ++i)
153  {
154  m_extent[i] = 0;
155  }
156  }
157 
159  {
160  Extent(&extent[0]);
161  }
162 
163  ArrayGrid(const unsigned int* extent)
164  {
165  Extent(extent);
166  }
167 
168  void Clear()
169  {
170  delete this->m_array;
171  this->m_array = NULL;
172  }
173 
174  void Extent(const unsigned int* extent)
175  {
176  Clear();
177  for (unsigned int i = 0; i < DimT; ++i)
178  {
179  m_extent[i] = extent[i];
180  }
181  }
182 
183  ArrayGridAccessor < CellT, DimT - 1 > operator[](unsigned int i)
184  {
185  return ArrayGridAccessor< CellT, DimT >(m_extent, this->m_array)[i];
186  }
187 
188  const ConstArrayGridAccessor < CellT, DimT - 1 > operator[](
189  unsigned int i) const
190  {
192  }
193 
194 private:
195  unsigned int m_extent[DimT];
196 };
197 
198 
199 template< class CellT, unsigned int DimT >
201 {
202 public:
203  HashGridAccessor(const size_t* factors, size_t hashKey,
204  stdext::hash_map< size_t, CellT >& hash)
205  : m_factors(factors)
206  , m_hashKey(hashKey)
207  , m_hash(hash)
208  {}
209 
210  HashGridAccessor < CellT, DimT - 1 > operator[](size_t i)
211  {
212  return HashGridAccessor < CellT, DimT - 1 > (m_factors + 1,
213  m_hashKey + (*m_factors) * i, m_hash);
214  }
215 
216 private:
217  const size_t* m_factors;
218  size_t m_hashKey;
219  stdext::hash_map< size_t, CellT >& m_hash;
220 };
221 
222 template< class CellT >
223 class HashGridAccessor< CellT, 0 >
224 {
225 public:
226  HashGridAccessor(const size_t*, size_t hashKey,
227  stdext::hash_map< size_t, CellT >& hash)
228  : m_hashKey(hashKey)
229  , m_hash(hash)
230  {}
231 
232  CellT& operator=(const CellT& c)
233  {
234  return m_hash[m_hashKey] = c;
235  }
236 
237  operator CellT& ()
238  {
239  return m_hash[m_hashKey];
240  }
241 
242  CellT& operator()()
243  {
244  return m_hash[m_hashKey];
245  }
246 
247  CellT* operator->()
248  {
249  return &m_hash[m_hashKey];
250  }
251 
252 private:
253  size_t m_hashKey;
254  stdext::hash_map< size_t, CellT >& m_hash;
255 };
256 
257 template< class CellT, unsigned int DimT >
259 {
260 public:
261  ConstHashGridAccessor(const size_t* factors, size_t hashKey,
262  const stdext::hash_map< size_t, CellT >& hash)
263  : m_factors(factors)
264  , m_hashKey(hashKey)
265  , m_hash(hash)
266  {}
267 
268  ConstHashGridAccessor < CellT, DimT - 1 > operator[](size_t i)
269  {
270  return ConstHashGridAccessor < CellT, DimT - 1 > (m_factors + 1,
271  m_hashKey + (*m_factors) * i, m_hash);
272  }
273 
274 private:
275  const size_t* m_factors;
276  size_t m_hashKey;
277  const stdext::hash_map< size_t, CellT >& m_hash;
278 };
279 
280 template< class CellT >
281 class ConstHashGridAccessor< CellT, 0 >
282 {
283 public:
284  ConstHashGridAccessor(const size_t*, size_t hashKey,
285  const stdext::hash_map< size_t, CellT >& hash)
286  : m_hashKey(hashKey)
287  , m_hash(hash)
288  {}
289 
290  operator const CellT* ()
291  {
292  typename stdext::hash_map< size_t, CellT >::const_iterator i =
293  m_hash.find(m_hashKey);
294  if (i != m_hash.end())
295  {
296  return &i->second;
297  }
298  return NULL;
299  }
300 
301 private:
302  size_t m_hashKey;
303  const stdext::hash_map< size_t, CellT >& m_hash;
304 };
305 
306 template< class CellT, unsigned int DimT >
307 class HashGrid
308 {
309 public:
310  typedef typename stdext::hash_map< size_t, CellT >::iterator iterator;
311  typedef typename stdext::hash_map< size_t, CellT >::const_iterator
313 
315  {
316  for (unsigned int i = 0; i < DimT; ++i)
317  {
318  m_factors[i] = 0;
319  }
320  }
321 
322  void Clear()
323  {
324  m_hash.clear();
325  }
326 
327  template< class ExtentT >
328  void Extent(const ExtentT& extent)
329  {
330  m_factors[DimT - 1] = 1;
331  for (unsigned int i = DimT - 1; i != 0; --i)
332  {
333  m_factors[i - 1] = m_factors[i] * extent[i];
334  }
335  }
336 
337  HashGridAccessor < CellT, DimT - 1 > operator[](size_t i)
338  {
339  return HashGridAccessor< CellT, DimT >(m_factors, 0, m_hash)[i];
340  }
341 
342  ConstHashGridAccessor < CellT, DimT - 1 > operator[](size_t i) const
343  {
344  return ConstHashGridAccessor< CellT, DimT >(m_factors, 0, m_hash)[i];
345  }
346 
347  template< class IndexT >
348  CellT& operator[](const IndexT* index)
349  {
350  return m_hash[HashKey(index)];
351  }
352 
353  CellT& at(size_t hashKey)
354  {
355  return m_hash[hashKey];
356  }
357 
358  const CellT& at(size_t hashKey) const
359  {
360  return m_hash[hashKey];
361  }
362 
363  template< class IndexT >
364  CellT* find(const IndexT& index)
365  {
366  iterator i = m_hash.find(HashKey(index));
367  if (i != m_hash.end())
368  {
369  return &i->second;
370  }
371  return NULL;
372  }
373 
374  template< class IndexT >
375  const CellT* find(const IndexT& index) const
376  {
377  const_iterator i = m_hash.find(HashKey(index));
378  if (i != m_hash.end())
379  {
380  return &i->second;
381  }
382  return NULL;
383  }
384 
386  {
387  return m_hash.begin();
388  }
390  {
391  return m_hash.end();
392  }
394  {
395  return m_hash.begin();
396  }
398  {
399  return m_hash.end();
400  }
401  size_t size() const
402  {
403  return m_hash.size();
404  }
405 
406 private:
407  template< class IndexT >
408  size_t HashKey(const IndexT& index) const
409  {
410  size_t hashKey = m_factors[0] * index[0];
411  for (unsigned int i = 1; i < DimT; ++i)
412  {
413  hashKey += m_factors[i] * index[i];
414  }
415  return hashKey;
416  }
417 
418 private:
419  size_t m_factors[DimT];
420  stdext::hash_map< size_t, CellT > m_hash;
421 };
422 
423 #endif
ArrayGridAccessor
Definition: Grid.h:63
ArrayGridAccessor< CellT, 0 >::operator=
CellT & operator=(const CellT &c)
Definition: Grid.h:96
ArrayGrid::operator[]
ArrayGridAccessor< CellT, DimT - 1 > operator[](unsigned int i)
Definition: Grid.h:183
HashGrid::at
CellT & at(size_t hashKey)
Definition: Grid.h:353
HashGrid::operator[]
ConstHashGridAccessor< CellT, DimT - 1 > operator[](size_t i) const
Definition: Grid.h:342
index
uint8_t index
Definition: EtherCATFrame.h:59
ArrayGridKernel::ArrayGridKernel
ArrayGridKernel(ArrayGridKernel< CellT, DimT - 1 > *array)
Definition: Grid.h:19
ConstHashGridAccessor::operator[]
ConstHashGridAccessor< CellT, DimT - 1 > operator[](size_t i)
Definition: Grid.h:268
ConstArrayGridAccessor::operator[]
const ConstArrayGridAccessor< CellT, DimT - 1 > operator[](unsigned int i) const
Definition: Grid.h:118
HashGrid::iterator
stdext::hash_map< size_t, CellT >::iterator iterator
Definition: Grid.h:310
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
HashGrid::size
size_t size() const
Definition: Grid.h:401
HashGridAccessor::HashGridAccessor
HashGridAccessor(const size_t *factors, size_t hashKey, stdext::hash_map< size_t, CellT > &hash)
Definition: Grid.h:203
HashGrid
Definition: Grid.h:307
ArrayGridKernel::m_array
ArrayGridKernel< CellT, DimT - 1 > * m_array
Definition: Grid.h:34
HashGrid::begin
const_iterator begin() const
Definition: Grid.h:393
HashGrid::begin
iterator begin()
Definition: Grid.h:385
MiscLib::Vector
Definition: Vector.h:19
HashGrid::const_iterator
stdext::hash_map< size_t, CellT >::const_iterator const_iterator
Definition: Grid.h:312
HashGrid::operator[]
HashGridAccessor< CellT, DimT - 1 > operator[](size_t i)
Definition: Grid.h:337
HashGrid::find
const CellT * find(const IndexT &index) const
Definition: Grid.h:375
ArrayGrid::operator[]
const ConstArrayGridAccessor< CellT, DimT - 1 > operator[](unsigned int i) const
Definition: Grid.h:188
ArrayGridAccessor::ArrayGridAccessor
ArrayGridAccessor(unsigned int *extent, ArrayGridKernel< CellT, DimT - 1 > *&array)
Definition: Grid.h:66
HashGridAccessor< CellT, 0 >::operator->
CellT * operator->()
Definition: Grid.h:247
HashGrid::operator[]
CellT & operator[](const IndexT *index)
Definition: Grid.h:348
HashGrid::HashGrid
HashGrid()
Definition: Grid.h:314
HashGridAccessor< CellT, 0 >::operator()
CellT & operator()()
Definition: Grid.h:242
ConstArrayGridAccessor::ConstArrayGridAccessor
ConstArrayGridAccessor(const ArrayGridKernel< CellT, DimT - 1 > *array)
Definition: Grid.h:114
ArrayGridKernel::~ArrayGridKernel
~ArrayGridKernel()
Definition: Grid.h:23
ConstHashGridAccessor< CellT, 0 >::ConstHashGridAccessor
ConstHashGridAccessor(const size_t *, size_t hashKey, const stdext::hash_map< size_t, CellT > &hash)
Definition: Grid.h:284
HashGrid::end
iterator end()
Definition: Grid.h:389
ArrayGrid::ArrayGrid
ArrayGrid()
Definition: Grid.h:150
HashGrid::end
const_iterator end() const
Definition: Grid.h:397
ArrayGrid::ArrayGrid
ArrayGrid(const unsigned int *extent)
Definition: Grid.h:163
ArrayGrid::Clear
void Clear()
Definition: Grid.h:168
HashGridAccessor< CellT, 0 >::operator=
CellT & operator=(const CellT &c)
Definition: Grid.h:232
ArrayGridKernel
Definition: Grid.h:12
HashGrid::Clear
void Clear()
Definition: Grid.h:322
HashGridAccessor< CellT, 0 >::HashGridAccessor
HashGridAccessor(const size_t *, size_t hashKey, stdext::hash_map< size_t, CellT > &hash)
Definition: Grid.h:226
ArrayGridAccessor::operator[]
ArrayGridAccessor< CellT, DimT - 1 > operator[](unsigned int i)
Definition: Grid.h:72
ArrayGrid::ArrayGrid
ArrayGrid(const MiscLib::Vector< unsigned int > &extent)
Definition: Grid.h:158
ArrayGridKernel< CellT, 0 >::m_cell
CellT m_cell
Definition: Grid.h:58
HashGrid::at
const CellT & at(size_t hashKey) const
Definition: Grid.h:358
HashGridAccessor
Definition: Grid.h:200
ConstHashGridAccessor::ConstHashGridAccessor
ConstHashGridAccessor(const size_t *factors, size_t hashKey, const stdext::hash_map< size_t, CellT > &hash)
Definition: Grid.h:261
ArrayGrid::Extent
void Extent(const unsigned int *extent)
Definition: Grid.h:174
ConstArrayGridAccessor
Definition: Grid.h:111
ArrayGridKernel::ArrayGridKernel
ArrayGridKernel()
Definition: Grid.h:15
ConstHashGridAccessor
Definition: Grid.h:258
ArrayGrid
Definition: Grid.h:146
ArrayGridKernel< CellT, 0 >::Data
CellT & Data()
Definition: Grid.h:52
ArrayGridAccessor< CellT, 0 >::ArrayGridAccessor
ArrayGridAccessor(unsigned int *, CellT &cell)
Definition: Grid.h:92
ConstArrayGridAccessor< CellT, 0 >::ConstArrayGridAccessor
ConstArrayGridAccessor(unsigned int *, const CellT &cell)
Definition: Grid.h:132
HashGridAccessor::operator[]
HashGridAccessor< CellT, DimT - 1 > operator[](size_t i)
Definition: Grid.h:210
ArrayGridKernel::Data
ArrayGridKernel< CellT, DimT - 1 > *& Data()
Definition: Grid.h:28
HashGrid::find
CellT * find(const IndexT &index)
Definition: Grid.h:364
HashGrid::Extent
void Extent(const ExtentT &extent)
Definition: Grid.h:328