Repository navigation
Expand file tree
/
Copy pathiterator3.h
More file actions
265 lines (237 loc) · 9.82 KB
/
Copy pathiterator3.h
File metadata and controls
265 lines (237 loc) · 9.82 KB
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
#ifndef ZFP_ITERATOR3_H
#define ZFP_ITERATOR3_H
namespace zfp {
namespace internal {
namespace dim3 {
// random access const iterator that visits 3D array or view block by block
template <class Container>
class const_iterator : public const_handle<Container> {
public:
// typedefs for STL compatibility
typedef Container container_type;
typedef typename container_type::value_type value_type;
typedef ptrdiff_t difference_type;
typedef zfp::internal::dim3::reference<Container> reference;
typedef zfp::internal::dim3::pointer<Container> pointer;
typedef std::random_access_iterator_tag iterator_category;
typedef zfp::internal::dim3::const_reference<Container> const_reference;
typedef zfp::internal::dim3::const_pointer<Container> const_pointer;
// default constructor
const_iterator() : const_handle<Container>(0, 0, 0, 0) {}
// constructor
explicit const_iterator(const container_type* container, size_t x, size_t y, size_t z) : const_handle<Container>(container, x, y, z) {}
// dereference iterator
const_reference operator*() const { return const_reference(container, x, y, z); }
const_reference operator[](difference_type d) const { return *operator+(d); }
// iterator arithmetic
const_iterator operator+(difference_type d) const { const_iterator it = *this; it.advance(d); return it; }
const_iterator operator-(difference_type d) const { return operator+(-d); }
difference_type operator-(const const_iterator& it) const { return offset() - it.offset(); }
// equality operators
bool operator==(const const_iterator& it) const { return container == it.container && x == it.x && y == it.y && z == it.z; }
bool operator!=(const const_iterator& it) const { return !operator==(it); }
// relational operators
bool operator<=(const const_iterator& it) const { return container == it.container && offset() <= it.offset(); }
bool operator>=(const const_iterator& it) const { return container == it.container && offset() >= it.offset(); }
bool operator<(const const_iterator& it) const { return container == it.container && offset() < it.offset(); }
bool operator>(const const_iterator& it) const { return container == it.container && offset() > it.offset(); }
// increment and decrement
const_iterator& operator++() { increment(); return *this; }
const_iterator& operator--() { decrement(); return *this; }
const_iterator operator++(int) { const_iterator it = *this; increment(); return it; }
const_iterator operator--(int) { const_iterator it = *this; decrement(); return it; }
const_iterator operator+=(difference_type d) { advance(+d); return *this; }
const_iterator operator-=(difference_type d) { advance(-d); return *this; }
// local container index of value referenced by iterator
size_t i() const { return x - container->min_x(); }
size_t j() const { return y - container->min_y(); }
size_t k() const { return z - container->min_z(); }
protected:
// sequential offset associated with index (x, y, z) plus delta d
difference_type offset(difference_type d = 0) const
{
difference_type p = d;
size_t xmin = container->min_x();
size_t xmax = container->max_x();
size_t ymin = container->min_y();
size_t ymax = container->max_y();
size_t zmin = container->min_z();
size_t zmax = container->max_z();
size_t nx = xmax - xmin;
size_t ny = ymax - ymin;
size_t nz = zmax - zmin;
if (z == zmax)
p += nx * ny * nz;
else {
size_t m = ~size_t(3);
size_t bz = std::max(z & m, zmin); size_t sz = std::min((bz + 4) & m, zmax) - bz; p += (bz - zmin) * nx * ny;
size_t by = std::max(y & m, ymin); size_t sy = std::min((by + 4) & m, ymax) - by; p += (by - ymin) * nx * sz;
size_t bx = std::max(x & m, xmin); size_t sx = std::min((bx + 4) & m, xmax) - bx; p += (bx - xmin) * sy * sz;
p += (z - bz) * sx * sy;
p += (y - by) * sx;
p += (x - bx);
}
return p;
}
// index (x, y, z) associated with sequential offset p
void index(size_t& x, size_t& y, size_t& z, difference_type p) const
{
size_t xmin = container->min_x();
size_t xmax = container->max_x();
size_t ymin = container->min_y();
size_t ymax = container->max_y();
size_t zmin = container->min_z();
size_t zmax = container->max_z();
size_t nx = xmax - xmin;
size_t ny = ymax - ymin;
size_t nz = zmax - zmin;
if (size_t(p) == nx * ny * nz) {
x = xmin;
y = ymin;
z = zmax;
}
else {
size_t m = ~size_t(3);
size_t bz = std::max((zmin + p / (nx * ny)) & m, zmin); size_t sz = std::min((bz + 4) & m, zmax) - bz; p -= (bz - zmin) * nx * ny;
size_t by = std::max((ymin + p / (nx * sz)) & m, ymin); size_t sy = std::min((by + 4) & m, ymax) - by; p -= (by - ymin) * nx * sz;
size_t bx = std::max((xmin + p / (sy * sz)) & m, xmin); size_t sx = std::min((bx + 4) & m, xmax) - bx; p -= (bx - xmin) * sy * sz;
z = bz + p / (sx * sy); p -= (z - bz) * sx * sy;
y = by + p / sx; p -= (y - by) * sx;
x = bx + p; p -= (x - bx);
}
}
// advance iterator by d
void advance(difference_type d) { index(x, y, z, offset(d)); }
// increment iterator to next element
void increment()
{
size_t xmin = container->min_x();
size_t xmax = container->max_x();
size_t ymin = container->min_y();
size_t ymax = container->max_y();
size_t zmin = container->min_z();
size_t zmax = container->max_z();
size_t m = ~size_t(3);
++x;
if (!(x & 3u) || x == xmax) {
x = std::max((x - 1) & m, xmin);
++y;
if (!(y & 3u) || y == ymax) {
y = std::max((y - 1) & m, ymin);
++z;
if (!(z & 3u) || z == zmax) {
z = std::max((z - 1) & m, zmin);
// done with block; advance to next
x = (x + 4) & m;
if (x >= xmax) {
x = xmin;
y = (y + 4) & m;
if (y >= ymax) {
y = ymin;
z = (z + 4) & m;
if (z >= zmax)
z = zmax;
}
}
}
}
}
}
// decrement iterator to previous element
void decrement()
{
size_t xmin = container->min_x();
size_t xmax = container->max_x();
size_t ymin = container->min_y();
size_t ymax = container->max_y();
size_t zmin = container->min_z();
size_t zmax = container->max_z();
size_t m = ~size_t(3);
if (z == zmax) {
x = xmax - 1;
y = ymax - 1;
z = zmax - 1;
}
else {
if (!(x & 3u) || x == xmin) {
x = std::min((x + 4) & m, xmax);
if (!(y & 3u) || y == ymin) {
y = std::min((y + 4) & m, ymax);
if (!(z & 3u) || z == zmin) {
z = std::min((z + 4) & m, zmax);
// done with block; advance to next
x = (x - 1) & m;
if (x <= xmin) {
x = xmax;
y = (y - 1) & m;
if (y <= ymin) {
y = ymax;
z = (z - 1) & m;
if (z <= zmin)
z = zmin;
}
}
}
--z;
}
--y;
}
--x;
}
}
using const_handle<Container>::container;
using const_handle<Container>::x;
using const_handle<Container>::y;
using const_handle<Container>::z;
};
// random access iterator that visits 3D array or view block by block
template <class Container>
class iterator : public const_iterator<Container> {
public:
// typedefs for STL compatibility
typedef Container container_type;
typedef typename container_type::value_type value_type;
typedef ptrdiff_t difference_type;
typedef zfp::internal::dim3::reference<Container> reference;
typedef zfp::internal::dim3::pointer<Container> pointer;
typedef std::random_access_iterator_tag iterator_category;
// default constructor
iterator() : const_iterator<Container>(0, 0, 0, 0) {}
// constructor
explicit iterator(container_type* container, size_t x, size_t y, size_t z) : const_iterator<Container>(container, x, y, z) {}
// dereference iterator
reference operator*() const { return reference(container, x, y, z); }
reference operator[](difference_type d) const { return *operator+(d); }
// iterator arithmetic
iterator operator+(difference_type d) const { iterator it = *this; it.advance(d); return it; }
iterator operator-(difference_type d) const { return operator+(-d); }
difference_type operator-(const iterator& it) const { return offset() - it.offset(); }
// equality operators
bool operator==(const iterator& it) const { return container == it.container && x == it.x && y == it.y && z == it.z; }
bool operator!=(const iterator& it) const { return !operator==(it); }
// relational operators
bool operator<=(const iterator& it) const { return container == it.container && offset() <= it.offset(); }
bool operator>=(const iterator& it) const { return container == it.container && offset() >= it.offset(); }
bool operator<(const iterator& it) const { return container == it.container && offset() < it.offset(); }
bool operator>(const iterator& it) const { return container == it.container && offset() > it.offset(); }
// increment and decrement
iterator& operator++() { increment(); return *this; }
iterator& operator--() { decrement(); return *this; }
iterator operator++(int) { iterator it = *this; increment(); return it; }
iterator operator--(int) { iterator it = *this; decrement(); return it; }
iterator operator+=(difference_type d) { advance(+d); return *this; }
iterator operator-=(difference_type d) { advance(-d); return *this; }
protected:
using const_iterator<Container>::offset;
using const_iterator<Container>::advance;
using const_iterator<Container>::increment;
using const_iterator<Container>::decrement;
using const_iterator<Container>::container;
using const_iterator<Container>::x;
using const_iterator<Container>::y;
using const_iterator<Container>::z;
};
} // dim3
} // internal
} // zfp
#endif