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