[2b666a]: blitz / array / iter.h  Maximize  Restore  History

Download this file

318 lines (259 with data), 9.1 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
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
// -*- C++ -*-
/***************************************************************************
* blitz/array/iter.h Basic iterator for arrays.
*
* Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Suggestions: blitz-dev@oonumerics.org
* Bugs: blitz-bugs@oonumerics.org
*
* For more information, please see the Blitz++ Home Page:
* http://oonumerics.org/blitz/
*
****************************************************************************/
#ifndef BZ_ARRAY_H
#error <blitz/array/iter.h> must be included via <blitz/array.h>
#endif
#ifndef BZ_ARRAY_ITER_H
#define BZ_ARRAY_ITER_H
#ifdef BZ_HAVE_STL
#include <iterator>
#endif
#if defined(BZ_DEBUG)
#define CheckIteratorValidity(X,Y) \
BZPRECHECK(data_!=0, X " invalid iterator (empty array)"); \
BZPRECHECK((data_>=beg_+Y && data_<=end_+Y), ((data_<beg_+Y) ? \
X " invalid iterator (before beginning of array)" : \
X " invalid iterator (past end of array)"));
#else
#define CheckIteratorValidity(X,Y)
#endif
BZ_NAMESPACE(blitz)
template<typename T, int N>
class ConstArrayIterator {
private:
// Initialization common to begin,end constructors.
//
void Init(const Array<T,N>& array) {
// Making internal copies of these avoids keeping
// a pointer to the array and doing indirection.
lbound_ = array.lbound();
order_ = array.ordering();
ubound_(0) = array.ubound(0)+1;
dataincr_(order_(0)) = array.stride(order_(0));
for (int i=1,r,s=order_(0);i<N;s=r,++i) {
r = order_(i);
ubound_(i) = array.ubound(i)+1;
dataincr_(r) = array.stride(r)-array.extent(s)*array.stride(s);
}
#if defined(BZ_DEBUG)
beg_ = array.data();
end_ = end_value(array)-1;
if (beg_>end_)
std::swap(beg_,end_);
#endif
}
public:
ConstArrayIterator() : data_(0) { }
ConstArrayIterator(const Array<T,N>& array) :
data_(const_cast<T*>(array.data())) {
Init(array);
pos_ = lbound_;
}
ConstArrayIterator(const Array<T,N>& array, const int) :
data_(end_value(array)) {
Init(array);
pos_ = array.ubound();
++pos_(order_(0));
}
bool operator==(const ConstArrayIterator<T,N>& x) const
{ return data_ == x.data_; }
bool operator!=(const ConstArrayIterator<T,N>& x) const
{ return data_ != x.data_; }
const T& operator*() const {
CheckIteratorValidity("Attempted to dereference",0);
return *data_;
}
const T* restrict operator->() const {
CheckIteratorValidity("Attempted to dereference",0);
return data_;
}
ConstArrayIterator<T,N>& operator++();
ConstArrayIterator<T,N>& operator--();
ConstArrayIterator<T,N> operator++(int) {
ConstArrayIterator<T,N> tmp = *this;
++(*this);
return tmp;
}
ConstArrayIterator<T,N> operator--(int) {
ConstArrayIterator<T,N> tmp = *this;
--(*this);
return tmp;
}
// get the current position of the Array iterator in index space
const TinyVector<int,N>& position() const {
CheckIteratorValidity("Array<T,N>::iterator::position() called on",0);
return pos_;
}
private:
TinyVector<int,N> dataincr_, lbound_, ubound_, order_;
static T* end_value(const Array<T,N>& array) {
T* endval = const_cast<T*>(array.data()) +
array.stride(array.ordering(0));
for (int i=0;i<N;++i)
endval += array.stride(i)*(array.extent(i)-1);
return endval;
}
protected:
TinyVector<int,N> pos_;
T * restrict data_;
#if defined(BZ_DEBUG)
const T* restrict beg_;
const T* restrict end_;
#endif
};
template<typename T, int N>
class ArrayIterator : public ConstArrayIterator<T,N> {
private:
typedef ConstArrayIterator<T,N> T_base;
using T_base::data_;
#if defined(BZ_DEBUG)
using T_base::beg_;
using T_base::end_;
#endif
public:
ArrayIterator() { }
ArrayIterator(Array<T,N>& x) : T_base(x) { }
ArrayIterator(const Array<T,N>& array, const int): T_base(array,0) { }
T& operator*() const {
CheckIteratorValidity("Attempted to dereference",0);
return *data_;
}
T* restrict operator->() const {
CheckIteratorValidity("Attempted to dereference",0);
return data_;
}
ArrayIterator<T,N>& operator++() {
T_base::operator++();
return *this;
}
ArrayIterator<T,N> operator++(int) {
ArrayIterator<T,N> tmp = *this;
++(*this);
return tmp;
}
ArrayIterator<T,N>& operator--() {
T_base::operator--();
return *this;
}
ArrayIterator<T,N> operator--(int) {
ArrayIterator<T,N> tmp = *this;
--(*this);
return tmp;
}
};
template<typename T, int N>
ConstArrayIterator<T,N>& ConstArrayIterator<T,N>::operator++() {
CheckIteratorValidity("Attempted to increment",0);
// The first loop iteration is peeled as it increases performance.
// The same improvement can be obtained by telling the compiler that
// the test is likely to be true, but this has too many portability issues
// for now.
// With a compiler peeling loops correctly (or with an effective BZ_LIKELY
// macro, this could be simply written as:
//
// for (int i=0;i<N;++i) {
// const int r = order_(i);
// data_ += dataincr_[r];
// if (BZ_LIKELY(++pos_(r)!=ubound_(r)))
// return *this;
// pos_(r) = lbound_(r);
// }
const int r0 = order_(0);
data_ += dataincr_[r0];
if (BZ_LIKELY(++pos_(r0)!=ubound_(r0)))
return *this;
pos_(r0) = lbound_(r0);
for (int i=1;i<N;++i) {
const int r = order_(i);
data_ += dataincr_[r];
if (BZ_LIKELY(++pos_(r)!=ubound_(r)))
return *this;
pos_(r) = lbound_(r);
}
// At this place the value of data_ should match that of the end iterator.
// Do the proper correction to achieve that.
for (int i=1;i<N;++i) {
const int r = order_(i);
data_ -= dataincr_[r];
pos_(r) = ubound_(r)-1;
}
pos_(r0) = ubound_(r0);
return *this;
}
template<typename T, int N>
ConstArrayIterator<T,N>& ConstArrayIterator<T,N>::operator--() {
CheckIteratorValidity("Attempted to decrement",1);
// The first loop iteration is peeled as it increases performance.
// The same improvement can be obtained by telling the compiler that
// the test is likely to be true, but this has too many portability issues
// for now.
// With a compiler peeling loops correctly (or with an effective BZ_LIKELY
// macro, this could be simply written as:
//
// for (int i=0;i<N;++i) {
// const int r = order_(i);
// data_ -= dataincr_[r];
// if (BZ_LIKELY(pos_(r)--!=lbound_(r)))
// return *this;
// pos_(r) = ubound_(r)-1;
// }
const int r0 = order_(0);
data_ -= dataincr_[r0];
if (BZ_LIKELY(pos_(r0)--!=lbound_(r0)))
return *this;
pos_(r0) = ubound_(r0)-1;
for (int i=1;i<N;++i) {
const int r = order_(i);
data_ -= dataincr_[r];
if (BZ_LIKELY(pos_(r)--!=lbound_(r)))
return *this;
pos_(r) = ubound_(r)-1;
}
// At this place the value of data_ should match that of the end iterator.
// No correction is needed for operator--
return *this;
}
BZ_NAMESPACE_END
#ifdef BZ_HAVE_STL
// support for std::iterator_traits
BZ_NAMESPACE(std)
template <typename T, int N>
struct iterator_traits< BZ_BLITZ_SCOPE(ConstArrayIterator)<T,N> > {
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef blitz::diffType difference_type;
typedef const T* pointer;
typedef const T& reference;
};
template <typename T, int N>
struct iterator_traits< BZ_BLITZ_SCOPE(ArrayIterator)<T,N> > {
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef blitz::diffType difference_type;
typedef T* pointer;
typedef T& reference;
};
BZ_NAMESPACE_END
#endif // BZ_HAVE_STL
#endif // BZ_ARRAY_ITER_H

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks