| 1 | /*************************************************************************** | 
|---|
| 2 | * blitz/array/iter.h     Declaration of ArrayIterator<P_numtype,N_rank> | 
|---|
| 3 | * | 
|---|
| 4 | * $Id: iter.h,v 1.1.1.1 1999-11-26 16:37:07 ansari Exp $ | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca> | 
|---|
| 7 | * | 
|---|
| 8 | * This program is free software; you can redistribute it and/or | 
|---|
| 9 | * modify it under the terms of the GNU General Public License | 
|---|
| 10 | * as published by the Free Software Foundation; either version 2 | 
|---|
| 11 | * of the License, or (at your option) any later version. | 
|---|
| 12 | * | 
|---|
| 13 | * This program is distributed in the hope that it will be useful, | 
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | * GNU General Public License for more details. | 
|---|
| 17 | * | 
|---|
| 18 | * Suggestions:          blitz-suggest@cybervision.com | 
|---|
| 19 | * Bugs:                 blitz-bugs@cybervision.com | 
|---|
| 20 | * | 
|---|
| 21 | * For more information, please see the Blitz++ Home Page: | 
|---|
| 22 | *    http://seurat.uwaterloo.ca/blitz/ | 
|---|
| 23 | * | 
|---|
| 24 | *************************************************************************** | 
|---|
| 25 | * $Log: not supported by cvs2svn $ | 
|---|
| 26 | * Revision 1.1.1.1  1999/04/09  17:59:03  ansari | 
|---|
| 27 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99 | 
|---|
| 28 | * | 
|---|
| 29 | * Revision 1.2  1998/03/14 00:04:47  tveldhui | 
|---|
| 30 | * 0.2-alpha-05 | 
|---|
| 31 | * | 
|---|
| 32 | * Revision 1.1  1997/07/16 14:51:20  tveldhui | 
|---|
| 33 | * Update: Alpha release 0.2 (Arrays) | 
|---|
| 34 | * | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #ifndef BZ_ARRAYITER_H | 
|---|
| 38 | #define BZ_ARRAYITER_H | 
|---|
| 39 |  | 
|---|
| 40 | #ifdef BZ_HAVE_STD | 
|---|
| 41 | #include <strstream> | 
|---|
| 42 | #else | 
|---|
| 43 | #include <strstream.h> | 
|---|
| 44 | #endif | 
|---|
| 45 |  | 
|---|
| 46 | BZ_NAMESPACE(blitz) | 
|---|
| 47 |  | 
|---|
| 48 | #ifndef BZ_ARRAY_H | 
|---|
| 49 | #error <blitz/array/iter.h> must be included via <blitz/array.h> | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | template<class P_numtype, int N_rank> | 
|---|
| 53 | class ArrayIterator { | 
|---|
| 54 | public: | 
|---|
| 55 | typedef P_numtype                T_numtype; | 
|---|
| 56 | typedef Array<T_numtype, N_rank> T_array; | 
|---|
| 57 | typedef ArrayIterator<P_numtype, N_rank> T_iterator; | 
|---|
| 58 | typedef const T_array& T_ctorArg1; | 
|---|
| 59 | typedef int            T_ctorArg2;    // dummy | 
|---|
| 60 |  | 
|---|
| 61 | enum { numArrayOperands = 1, numIndexPlaceholders = 0, | 
|---|
| 62 | rank = N_rank }; | 
|---|
| 63 |  | 
|---|
| 64 | // NB: this ctor does NOT preserve stack and stride | 
|---|
| 65 | // parameters.  This is for speed purposes. | 
|---|
| 66 | ArrayIterator(const ArrayIterator<P_numtype, N_rank>& x) | 
|---|
| 67 | : array_(x.array_), data_(x.data_) | 
|---|
| 68 | { } | 
|---|
| 69 |  | 
|---|
| 70 | void operator=(const ArrayIterator<P_numtype, N_rank>& x) | 
|---|
| 71 | { | 
|---|
| 72 | array_ = x.array_; | 
|---|
| 73 | data_ = x.data_; | 
|---|
| 74 | stack_ = x.stack_; | 
|---|
| 75 | stride_ = x.stride_; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | ArrayIterator(const T_array& array) | 
|---|
| 79 | : array_(array) | 
|---|
| 80 | { | 
|---|
| 81 | data_   = array.data(); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | ~ArrayIterator() | 
|---|
| 85 | { } | 
|---|
| 86 |  | 
|---|
| 87 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE | 
|---|
| 88 | T_numtype operator()(TinyVector<int, N_rank> i) | 
|---|
| 89 | { return array_(i); } | 
|---|
| 90 | #else | 
|---|
| 91 | T_numtype operator()(const TinyVector<int, N_rank>& i) | 
|---|
| 92 | { return array_(i); } | 
|---|
| 93 | #endif | 
|---|
| 94 |  | 
|---|
| 95 | int lbound(int rank) | 
|---|
| 96 | { | 
|---|
| 97 | if (rank < N_rank) | 
|---|
| 98 | return array_.lbound(rank); | 
|---|
| 99 | else | 
|---|
| 100 | return INT_MIN;   // tiny(int()); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | int ubound(int rank) | 
|---|
| 104 | { | 
|---|
| 105 | if (rank < N_rank) | 
|---|
| 106 | return array_.ubound(rank); | 
|---|
| 107 | else | 
|---|
| 108 | return INT_MAX;   // huge(int()); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | T_numtype operator*() | 
|---|
| 112 | { return *data_; } | 
|---|
| 113 |  | 
|---|
| 114 | T_numtype operator[](int i) | 
|---|
| 115 | { return data_[i * stride_]; } | 
|---|
| 116 |  | 
|---|
| 117 | T_numtype fastRead(int i) | 
|---|
| 118 | { return data_[i]; } | 
|---|
| 119 |  | 
|---|
| 120 | int suggestStride(int rank) const | 
|---|
| 121 | { return array_.stride(rank); } | 
|---|
| 122 |  | 
|---|
| 123 | _bz_bool isStride(int rank, int stride) const | 
|---|
| 124 | { return array_.stride(rank) == stride; } | 
|---|
| 125 |  | 
|---|
| 126 | void push(int position) | 
|---|
| 127 | { | 
|---|
| 128 | stack_[position] = data_; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | void pop(int position) | 
|---|
| 132 | { | 
|---|
| 133 | data_ = stack_[position]; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | void advance() | 
|---|
| 137 | { | 
|---|
| 138 | data_ += stride_; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void advance(int n) | 
|---|
| 142 | { | 
|---|
| 143 | data_ += n * stride_; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void loadStride(int rank) | 
|---|
| 147 | { | 
|---|
| 148 | stride_ = array_.stride(rank); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | const T_numtype * _bz_restrict data() const | 
|---|
| 152 | { return data_; } | 
|---|
| 153 |  | 
|---|
| 154 | int stride() const | 
|---|
| 155 | { return stride_; } | 
|---|
| 156 |  | 
|---|
| 157 | _bz_bool isUnitStride(int rank) const | 
|---|
| 158 | { return array_.stride(rank) == 1; } | 
|---|
| 159 |  | 
|---|
| 160 | void advanceUnitStride() | 
|---|
| 161 | { ++data_; } | 
|---|
| 162 |  | 
|---|
| 163 | _bz_bool canCollapse(int outerLoopRank, int innerLoopRank) const | 
|---|
| 164 | { return array_.canCollapse(outerLoopRank, innerLoopRank); } | 
|---|
| 165 |  | 
|---|
| 166 | void prettyPrint(string& str, prettyPrintFormat& format) const | 
|---|
| 167 | { | 
|---|
| 168 | if (format.tersePrintingSelected()) | 
|---|
| 169 | str += format.nextArrayOperandSymbol(); | 
|---|
| 170 | else if (format.dumpArrayShapesMode()) | 
|---|
| 171 | { | 
|---|
| 172 | ostrstream ostr; | 
|---|
| 173 | ostr << array_.shape(); | 
|---|
| 174 | str += ostr.str(); | 
|---|
| 175 | } | 
|---|
| 176 | else { | 
|---|
| 177 | str += "Array<"; | 
|---|
| 178 | str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype); | 
|---|
| 179 | str += ","; | 
|---|
| 180 |  | 
|---|
| 181 | char tmpBuf[10]; | 
|---|
| 182 | sprintf(tmpBuf, "%d", N_rank); | 
|---|
| 183 |  | 
|---|
| 184 | str += tmpBuf; | 
|---|
| 185 | str += ">"; | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | template<class T_shape> | 
|---|
| 190 | _bz_bool shapeCheck(const T_shape& shape) | 
|---|
| 191 | { return areShapesConformable(shape, array_.length()); } | 
|---|
| 192 |  | 
|---|
| 193 | // Experimental | 
|---|
| 194 | T_numtype& operator()(int i, int j) | 
|---|
| 195 | { | 
|---|
| 196 | return (T_numtype&)data_[i*array_.stride(0) + j*array_.stride(1)]; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | // Experimental | 
|---|
| 200 | T_numtype& operator()(int i, int j, int k) | 
|---|
| 201 | { | 
|---|
| 202 | return (T_numtype&)data_[i*array_.stride(0) + j*array_.stride(1) | 
|---|
| 203 | + k*array_.stride(2)]; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | // Experimental | 
|---|
| 207 |  | 
|---|
| 208 | void moveTo(int i, int j) | 
|---|
| 209 | { | 
|---|
| 210 | data_ = &const_cast<T_array&>(array_)(i,j); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | void moveTo(int i, int j, int k) | 
|---|
| 214 | { | 
|---|
| 215 | data_ = &const_cast<T_array&>(array_)(i,j,k); | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | void moveTo(const TinyVector<int,N_rank>& i) | 
|---|
| 219 | { | 
|---|
| 220 | data_ = &const_cast<T_array&>(array_)(i); | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | // Experimental | 
|---|
| 224 | void operator=(T_numtype x) | 
|---|
| 225 | {   *const_cast<T_numtype* _bz_restrict>(data_) = x; } | 
|---|
| 226 |  | 
|---|
| 227 | // Experimental | 
|---|
| 228 | template<class T_value> | 
|---|
| 229 | void operator=(T_value x) | 
|---|
| 230 | {   *const_cast<T_numtype* _bz_restrict>(data_) = x; } | 
|---|
| 231 |  | 
|---|
| 232 | // Experimental | 
|---|
| 233 | template<class T_value> | 
|---|
| 234 | void operator+=(T_value x) | 
|---|
| 235 | { *const_cast<T_numtype* _bz_restrict>(data_) += x; } | 
|---|
| 236 |  | 
|---|
| 237 | // NEEDS_WORK: other operators | 
|---|
| 238 |  | 
|---|
| 239 | // Experimental | 
|---|
| 240 | operator T_numtype() const | 
|---|
| 241 | { return *data_; } | 
|---|
| 242 |  | 
|---|
| 243 | // Experimental | 
|---|
| 244 | T_numtype shift(int offset, int dim) | 
|---|
| 245 | { | 
|---|
| 246 | return data_[offset*array_.stride(dim)]; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | // Experimental | 
|---|
| 250 | T_numtype shift(int offset1, int dim1, int offset2, int dim2) | 
|---|
| 251 | { | 
|---|
| 252 | return data_[offset1*array_.stride(dim1) | 
|---|
| 253 | + offset2*array_.stride(dim2)]; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | private: | 
|---|
| 257 | const T_array&                          array_; | 
|---|
| 258 | const T_numtype * _bz_restrict          data_; | 
|---|
| 259 | const T_numtype * _bz_restrict          stack_[N_rank]; | 
|---|
| 260 | int                                     stride_; | 
|---|
| 261 | }; | 
|---|
| 262 |  | 
|---|
| 263 | BZ_NAMESPACE_END | 
|---|
| 264 |  | 
|---|
| 265 | #endif // BZ_ARRAYITER_H | 
|---|