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