MADNESS 0.10.1
indexit.h
Go to the documentation of this file.
1/*
2 This file is part of MADNESS.
3
4 Copyright (C) 2007,2010 Oak Ridge National Laboratory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 For more information please contact:
21
22 Robert J. Harrison
23 Oak Ridge National Laboratory
24 One Bethel Valley Road
25 P.O. Box 2008, MS-6367
26
27 email: harrisonrj@ornl.gov
28 tel: 865-241-3937
29 fax: 865-572-0680
30
31 $Id$
32*/
33#ifndef MADNESS_MRA_INDEXIT_H__INCLUDED
34#define MADNESS_MRA_INDEXIT_H__INCLUDED
35
36/// \file indexit.h
37/// \brief Provides IndexIterator
38
39#include <vector>
40#include <madness/world/print.h>
42
43namespace madness {
44
45 /// Facilitates iteration through a multidimensional index space.
46 /// Since there are multiple ways for navigating index space (column- vs.
47 /// row-major, etc.), this class should be abstract, with an abstract ++
48 /// operator. The original IndexIterator assumed the highest dimension
49 /// (NDIM-1) would be iterated quickest (this index changes each time ++
50 /// is called), however, sometimes a different order is desired.
51 ///
52 /// For legacy purposes, operator++ is thus NOT abstract, but has the
53 /// implementation of the HighDimIndexIterator defined below. Eventually,
54 /// the IndexIterator::operator++ should be deprecated, and such
55 /// instances of IndexIterator replaced with HighDimIndexIterator.
57 private:
58 /// Bury the default constructor
60
61 protected:
62 std::vector<long> n; ///< User specified upper limits for each dimension
63 std::vector<long> i; ///< Current index
65
66 public:
67 /// Iterates dimension d from 0 to limits[d]-1 inclusive
68 template<typename V>
69 IndexIterator(const V& limits) :
70 n(limits.size()), i(limits.size(), 0l), finished(false) {
71 for (unsigned int d = 0; d < n.size(); ++d)
72 n[d] = limits[d];
73 }
74
75 /// Iterates dimension d from 0 to limits[d]-1 inclusive
76 IndexIterator(int ndim, const long limits[]) :
77 n(ndim), i(ndim, 0l), finished(false) {
78 for (unsigned int d = 0; d < n.size(); ++d)
79 n[d] = limits[d];
80 }
81
82 /// Iterates all dimensions from 0 to top-1 inclusive
83 IndexIterator(int ndim, long top) :
84 n(ndim,top), i(ndim, 0l), finished(false) {
85 }
86
87 virtual ~IndexIterator() {}
88
91 for (unsigned int d = 0; d < n.size(); ++d)
92 i[d] = 0;
93 finished = false;
94 return *this;
95 }
96
97 long
98 operator[](int d) const {
100 return i[d];
101 }
102
103 const std::vector<long>&
104 operator*() const {
106 return i;
107 }
108
109 operator bool() const {
110 return !finished;
111 }
112
113 /// this function should be abstracted and deprecated
114 virtual IndexIterator&
116 for (int d = n.size() - 1; d >= 0; --d) {
117 ++(i[d]);
118 if (i[d] < n[d])
119 return *this;
120 else
121 i[d] = 0;
122 }
123 finished = true;
124 return *this;
125 }
126
127 /// this function should also be deprecated
128 static void
130 Vector<int, 4> n(3);
131 for (IndexIterator it(n); it; ++it) {
132 print(*it);
133 }
134 }
135 };
136
137 /// The inherited IndexIterator for iterating over the high dimensions
138 /// quickly and the low dimensions slowly (all elements of dimension 0
139 /// at index i will be visited before any element of index i+1 in dim. 0).
140 ///
141 /// This is equivalent to the original implementation of IndexIterator.
143 private:
144 /// Bury the default constructor
146
147 public:
148 /// Iterates dimension d from 0 to limts[d]-1 inclusive
149 template<typename V>
150 HighDimIndexIterator(const V& limits) : IndexIterator(limits) {}
151
152 /// Iterates dimension d from 0 to limts[d]-1 inclusive
153 HighDimIndexIterator(int ndim, const long limits[]) :
154 IndexIterator(ndim, limits) {}
155
156 /// Iterates all dimensions from 0 to top-1 inclusive
157 HighDimIndexIterator(int ndim, long top) : IndexIterator(ndim, top) {}
158
160
161 /// increment the highest dimension first and check for overflows
162 /// up through dimension 0
163 virtual IndexIterator&
165 for (int d = n.size() - 1; d >= 0; --d) {
166 ++(i[d]);
167 if (i[d] < n[d])
168 return *this;
169 else
170 i[d] = 0;
171 }
172 finished = true;
173 return *this;
174 }
175 };
176
177 /// The inherited IndexIterator for iterating over the low dimensions
178 /// quickly and the high dimensions slowly (all elements of dimension
179 /// ndim-1 at index i will be visited before any element of index i+1 in
180 /// dim. ndim-1).
182 private:
183 /// Bury the default constructor
185
186 public:
187 /// Iterates dimension d from 0 to limts[d]-1 inclusive
188 template<typename V>
189 LowDimIndexIterator(const V& limits) : IndexIterator(limits) {}
190
191 /// Iterates dimension d from 0 to limts[d]-1 inclusive
192 LowDimIndexIterator(int ndim, const long limits[]) :
193 IndexIterator(ndim, limits) {}
194
195 /// Iterates all dimensions from 0 to top-1 inclusive
196 LowDimIndexIterator(int ndim, long top) : IndexIterator(ndim, top) {}
197
199
200 /// increment the lowest dimension first and check for overflows
201 /// up through dimension 0
202 virtual IndexIterator&
204 int ndim = n.size();
205 for (int d = 0; d < ndim; ++d) {
206 ++(i[d]);
207 if (i[d] < n[d])
208 return *this;
209 else
210 i[d] = 0;
211 }
212 finished = true;
213 return *this;
214 }
215 };
216
217 /// The inherited IndexIterator for iterating over the dimensions in a
218 /// specified order.
219 ///
220 /// NOTE: if iterating quickly over the high dimensions and slowly over
221 /// the low dimensions (in dimensional order), use HighDimIndexIterator.
222 ///
223 /// NOTE: if iterating quickly over the low dimensions and slowly over
224 /// the high dimensions (in dimensional order), use LowDimIndexIterator.
226 private:
227 /// Bury the default constructor
229
230 protected:
231 /// the array storing the dimensional order for iteration
232 /// dim[0] is the quickest dimension over which to iterate
233 /// dim[ndim-1] is the slowest dimension
234 std::vector<int> dim;
235
236 public:
237 /// Iterates dimension d from 0 to limts[d]-1 inclusive
238 ///
239 /// order[0] is the dimension to be iterated over quickest
240 /// ...
241 /// order[d-1] is the dimension to be iterated over slowest
242 template<typename V, typename D>
243 NonstandardIndexIterator(const V& limits, const D& order) :
244 IndexIterator(limits), dim(order.size()) {
245 int i, j, ndim = order.size();
246
247 MADNESS_ASSERT(limits.size() == ndim);
248 for(i = 0; i < ndim; ++i) {
249 MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
250
251 // make sure we haven't seen this dimension before
252 for(j = 0; j < i; ++j)
253 MADNESS_ASSERT(order[i] != order[j]);
254
255 dim[i] = order[i];
256 }
257 }
258
259 /// Iterates dimension d from 0 to limts[d]-1 inclusive
260 ///
261 /// order[0] is the dimension to be iterated over quickest
262 /// ...
263 /// order[d-1] is the dimension to be iterated over slowest
264 NonstandardIndexIterator(int ndim, const long limits[],
265 const int order[]) : IndexIterator(ndim, limits) {
266 int i, j;
267
268 for(i = 0; i < ndim; ++i) {
269 MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
270
271 // make sure we haven't seen this dimension before
272 for(j = 0; j < i; ++j)
273 MADNESS_ASSERT(order[i] != order[j]);
274
275 dim[i] = order[i];
276 }
277 }
278
279 /// Iterates all dimensions from 0 to top-1 inclusive
280 ///
281 /// order[0] is the dimension to be iterated over quickest
282 /// ...
283 /// order[d-1] is the dimension to be iterated over slowest
284 template<typename D>
285 NonstandardIndexIterator(int ndim, long top, const D &order) :
286 IndexIterator(ndim, top), dim(order.size()) {
287 int i, j;
288
289 MADNESS_ASSERT(order.size() == ndim);
290 for(i = 0; i < ndim; ++i) {
291 MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
292
293 // make sure we haven't seen this dimension before
294 for(j = 0; j < i; ++j)
295 MADNESS_ASSERT(order[i] != order[j]);
296
297 dim[i] = order[i];
298 }
299 }
300
301 /// Iterates all dimensions from 0 to top-1 inclusive
302 ///
303 /// order[0] is the dimension to be iterated over quickest
304 /// ...
305 /// order[d-1] is the dimension to be iterated over slowest
306 NonstandardIndexIterator(int ndim, long top, const int order[]) :
307 IndexIterator(ndim, top), dim(ndim) {
308 int i, j;
309
310 for(i = 0; i < ndim; ++i) {
311 MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
312
313 // make sure we haven't seen this dimension before
314 for(j = 0; j < i; ++j)
315 MADNESS_ASSERT(order[i] != order[j]);
316
317 dim[i] = order[i];
318 }
319 }
320
322
323 /// increment the dimensions in the order detailed in dim
324 virtual IndexIterator&
326 int ndim = n.size();
327 for (int d = 0; d < ndim; ++d) {
328 ++(i[dim[d]]);
329 if (i[dim[d]] < n[dim[d]])
330 return *this;
331 else
332 i[dim[d]] = 0;
333 }
334 finished = true;
335 return *this;
336 }
337 };
338}
339
340#endif // MADNESS_MRA_INDEXIT_H__INCLUDED
Definition indexit.h:142
HighDimIndexIterator()
Bury the default constructor.
Definition indexit.h:145
virtual ~HighDimIndexIterator()
Definition indexit.h:159
virtual IndexIterator & operator++()
Definition indexit.h:164
HighDimIndexIterator(int ndim, const long limits[])
Iterates dimension d from 0 to limts[d]-1 inclusive.
Definition indexit.h:153
HighDimIndexIterator(int ndim, long top)
Iterates all dimensions from 0 to top-1 inclusive.
Definition indexit.h:157
HighDimIndexIterator(const V &limits)
Iterates dimension d from 0 to limts[d]-1 inclusive.
Definition indexit.h:150
Definition indexit.h:56
virtual IndexIterator & operator++()
this function should be abstracted and deprecated
Definition indexit.h:115
bool finished
Definition indexit.h:64
IndexIterator(int ndim, const long limits[])
Iterates dimension d from 0 to limits[d]-1 inclusive.
Definition indexit.h:76
static void test()
this function should also be deprecated
Definition indexit.h:129
std::vector< long > i
Current index.
Definition indexit.h:63
virtual ~IndexIterator()
Definition indexit.h:87
long operator[](int d) const
Definition indexit.h:98
IndexIterator & reset()
Definition indexit.h:90
std::vector< long > n
User specified upper limits for each dimension.
Definition indexit.h:62
IndexIterator()
Bury the default constructor.
Definition indexit.h:59
IndexIterator(const V &limits)
Iterates dimension d from 0 to limits[d]-1 inclusive.
Definition indexit.h:69
IndexIterator(int ndim, long top)
Iterates all dimensions from 0 to top-1 inclusive.
Definition indexit.h:83
const std::vector< long > & operator*() const
Definition indexit.h:104
Definition indexit.h:181
virtual IndexIterator & operator++()
Definition indexit.h:203
LowDimIndexIterator(int ndim, const long limits[])
Iterates dimension d from 0 to limts[d]-1 inclusive.
Definition indexit.h:192
virtual ~LowDimIndexIterator()
Definition indexit.h:198
LowDimIndexIterator()
Bury the default constructor.
Definition indexit.h:184
LowDimIndexIterator(const V &limits)
Iterates dimension d from 0 to limts[d]-1 inclusive.
Definition indexit.h:189
LowDimIndexIterator(int ndim, long top)
Iterates all dimensions from 0 to top-1 inclusive.
Definition indexit.h:196
Definition indexit.h:225
std::vector< int > dim
Definition indexit.h:234
NonstandardIndexIterator(const V &limits, const D &order)
Definition indexit.h:243
NonstandardIndexIterator(int ndim, long top, const D &order)
Definition indexit.h:285
virtual IndexIterator & operator++()
increment the dimensions in the order detailed in dim
Definition indexit.h:325
virtual ~NonstandardIndexIterator()
Definition indexit.h:321
NonstandardIndexIterator(int ndim, long top, const int order[])
Definition indexit.h:306
NonstandardIndexIterator(int ndim, const long limits[], const int order[])
Definition indexit.h:264
NonstandardIndexIterator()
Bury the default constructor.
Definition indexit.h:228
A simple, fixed dimension vector.
Definition vector.h:64
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:227
static const double d
Definition nonlinschro.cc:121
Defines simple templates for printing to std::cout "a la Python".
Definition test_ar.cc:204
static double V(const coordT &r)
Definition tdse.cc:288
Implement the madness:Vector class, an extension of std::array that supports some mathematical operat...