/usr/include/madness/mra/indexit.h is in libmadness-dev 0.10.1~gite4aa500e-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
This file is part of MADNESS.
Copyright (C) 2007,2010 Oak Ridge National Laboratory
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more information please contact:
Robert J. Harrison
Oak Ridge National Laboratory
One Bethel Valley Road
P.O. Box 2008, MS-6367
email: harrisonrj@ornl.gov
tel: 865-241-3937
fax: 865-572-0680
$Id$
*/
#ifndef MADNESS_MRA_INDEXIT_H__INCLUDED
#define MADNESS_MRA_INDEXIT_H__INCLUDED
/// \file indexit.h
/// \brief Provides IndexIterator
#include <vector>
#include <madness/world/print.h>
namespace madness {
/// Facilitates iteration through a multidimension index space.
/// Since there are multiple ways for navigating index space (column- vs.
/// row-major, etc.), this class should be abstract, with an abstract ++
/// operator. The original IndexIterator assumed the highest dimension
/// (NDIM-1) would be iterated quickest (this index changes each time ++
/// is called), however, sometimes a different order is desired.
///
/// For legacy purposes, operator++ is thus NOT abstract, but has the
/// implementation of the HighDimIndexIterator defined below. Eventually,
/// the IndexIterator::operator++ should be deprecated, and such
/// instances of IndexIterator replaced with HighDimIndexIterator.
class IndexIterator {
private:
/// Bury the default constructor
IndexIterator() {}
protected:
std::vector<long> n; ///< User specified upper limits for each dimension
std::vector<long> i; ///< Current index
bool finished;
public:
/// Iterates dimension d from 0 to limts[d]-1 inclusive
template<typename V>
IndexIterator(const V& limits) :
n(limits.size()), i(limits.size(), 0l), finished(false) {
for (unsigned int d = 0; d < n.size(); ++d)
n[d] = limits[d];
}
/// Iterates dimension d from 0 to limts[d]-1 inclusive
IndexIterator(int ndim, const long limits[]) :
n(ndim), i(ndim, 0l), finished(false) {
for (unsigned int d = 0; d < n.size(); ++d)
n[d] = limits[d];
}
/// Iterates all dimensions from 0 to top-1 inclusive
IndexIterator(int ndim, long top) :
n(ndim,top), i(ndim, 0l), finished(false) {
}
virtual ~IndexIterator() {}
IndexIterator&
reset() {
for (unsigned int d = 0; d < n.size(); ++d)
i[d] = 0;
finished = false;
return *this;
}
long
operator[](int d) const {
MADNESS_ASSERT(!finished);
return i[d];
}
const std::vector<long>&
operator*() const {
MADNESS_ASSERT(!finished);
return i;
}
operator bool() const {
return !finished;
}
/// this function should be abstracted and deprecated
virtual IndexIterator&
operator++() {
for (int d = n.size() - 1; d >= 0; --d) {
++(i[d]);
if (i[d] < n[d])
return *this;
else
i[d] = 0;
}
finished = true;
return *this;
}
/// this function should also be deprecated
static void
test() {
Vector<int, 4> n(3);
for (IndexIterator it(n); it; ++it) {
print(*it);
}
}
};
/// The inherited IndexIterator for iterating over the high dimensions
/// quickly and the low dimensions slowly (all elements of dimension 0
/// at index i will be visited before any element of index i+1 in dim. 0).
///
/// This is equivalent to the original implementation of IndexIterator.
class HighDimIndexIterator : public IndexIterator {
private:
/// Bury the default constructor
HighDimIndexIterator() : IndexIterator(0, 0l) {}
public:
/// Iterates dimension d from 0 to limts[d]-1 inclusive
template<typename V>
HighDimIndexIterator(const V& limits) : IndexIterator(limits) {}
/// Iterates dimension d from 0 to limts[d]-1 inclusive
HighDimIndexIterator(int ndim, const long limits[]) :
IndexIterator(ndim, limits) {}
/// Iterates all dimensions from 0 to top-1 inclusive
HighDimIndexIterator(int ndim, long top) : IndexIterator(ndim, top) {}
virtual ~HighDimIndexIterator() {}
/// increment the highest dimension first and check for overflows
/// up through dimension 0
virtual IndexIterator&
operator++() {
for (int d = n.size() - 1; d >= 0; --d) {
++(i[d]);
if (i[d] < n[d])
return *this;
else
i[d] = 0;
}
finished = true;
return *this;
}
};
/// The inherited IndexIterator for iterating over the low dimensions
/// quickly and the high dimensions slowly (all elements of dimension
/// ndim-1 at index i will be visited before any element of index i+1 in
/// dim. ndim-1).
class LowDimIndexIterator : public IndexIterator {
private:
/// Bury the default constructor
LowDimIndexIterator() : IndexIterator(0, 0l) {}
public:
/// Iterates dimension d from 0 to limts[d]-1 inclusive
template<typename V>
LowDimIndexIterator(const V& limits) : IndexIterator(limits) {}
/// Iterates dimension d from 0 to limts[d]-1 inclusive
LowDimIndexIterator(int ndim, const long limits[]) :
IndexIterator(ndim, limits) {}
/// Iterates all dimensions from 0 to top-1 inclusive
LowDimIndexIterator(int ndim, long top) : IndexIterator(ndim, top) {}
virtual ~LowDimIndexIterator() {}
/// increment the lowest dimension first and check for overflows
/// up through dimension 0
virtual IndexIterator&
operator++() {
int ndim = n.size();
for (int d = 0; d < ndim; ++d) {
++(i[d]);
if (i[d] < n[d])
return *this;
else
i[d] = 0;
}
finished = true;
return *this;
}
};
/// The inherited IndexIterator for iterating over the dimensions in a
/// specified order.
///
/// NOTE: if iterating quickly over the high dimensions and slowly over
/// the low dimensions (in dimensional order), use HighDimIndexIterator.
///
/// NOTE: if iterating quickly over the low dimensions and slowly over
/// the high dimensions (in dimensional order), use LowDimIndexIterator.
class NonstandardIndexIterator : public IndexIterator {
private:
/// Bury the default constructor
NonstandardIndexIterator() : IndexIterator(0, 0l) {}
protected:
/// the array storing the dimensional order for iteration
/// dim[0] is the quickest dimension over which to iterate
/// dim[ndim-1] is the slowest dimension
std::vector<int> dim;
public:
/// Iterates dimension d from 0 to limts[d]-1 inclusive
///
/// order[0] is the dimension to be iterated over quickest
/// ...
/// order[d-1] is the dimension to be iterated over slowest
template<typename V, typename D>
NonstandardIndexIterator(const V& limits, const D& order) :
IndexIterator(limits), dim(order.size()) {
int i, j, ndim = order.size();
MADNESS_ASSERT(limits.size() == ndim);
for(i = 0; i < ndim; ++i) {
MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
// make sure we haven't seen this dimension before
for(j = 0; j < i; ++j)
MADNESS_ASSERT(order[i] != order[j]);
dim[i] = order[i];
}
}
/// Iterates dimension d from 0 to limts[d]-1 inclusive
///
/// order[0] is the dimension to be iterated over quickest
/// ...
/// order[d-1] is the dimension to be iterated over slowest
NonstandardIndexIterator(int ndim, const long limits[],
const int order[]) : IndexIterator(ndim, limits) {
int i, j;
for(i = 0; i < ndim; ++i) {
MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
// make sure we haven't seen this dimension before
for(j = 0; j < i; ++j)
MADNESS_ASSERT(order[i] != order[j]);
dim[i] = order[i];
}
}
/// Iterates all dimensions from 0 to top-1 inclusive
///
/// order[0] is the dimension to be iterated over quickest
/// ...
/// order[d-1] is the dimension to be iterated over slowest
template<typename D>
NonstandardIndexIterator(int ndim, long top, const D &order) :
IndexIterator(ndim, top), dim(order.size()) {
int i, j;
MADNESS_ASSERT(order.size() == ndim);
for(i = 0; i < ndim; ++i) {
MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
// make sure we haven't seen this dimension before
for(j = 0; j < i; ++j)
MADNESS_ASSERT(order[i] != order[j]);
dim[i] = order[i];
}
}
/// Iterates all dimensions from 0 to top-1 inclusive
///
/// order[0] is the dimension to be iterated over quickest
/// ...
/// order[d-1] is the dimension to be iterated over slowest
NonstandardIndexIterator(int ndim, long top, const int order[]) :
IndexIterator(ndim, top), dim(ndim) {
int i, j;
for(i = 0; i < ndim; ++i) {
MADNESS_ASSERT(order[i] >= 0 && order[i] < ndim);
// make sure we haven't seen this dimension before
for(j = 0; j < i; ++j)
MADNESS_ASSERT(order[i] != order[j]);
dim[i] = order[i];
}
}
virtual ~NonstandardIndexIterator() {}
/// increment the dimensions in the order detailed in dim
virtual IndexIterator&
operator++() {
int ndim = n.size();
for (int d = 0; d < ndim; ++d) {
++(i[dim[d]]);
if (i[dim[d]] < n[dim[d]])
return *this;
else
i[dim[d]] = 0;
}
finished = true;
return *this;
}
};
}
#endif // MADNESS_MRA_INDEXIT_H__INCLUDED
|