| 1 | /***************************************************************************
 | 
|---|
| 2 |  * blitz/array/iter.h     Declaration of ArrayIterator<P_numtype,N_rank>
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * $Id: iter.h,v 1.1.1.1 1999-04-09 17:59:03 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.2  1998/03/14 00:04:47  tveldhui
 | 
|---|
| 27 |  * 0.2-alpha-05
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  * Revision 1.1  1997/07/16 14:51:20  tveldhui
 | 
|---|
| 30 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  */
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #ifndef BZ_ARRAYITER_H
 | 
|---|
| 35 | #define BZ_ARRAYITER_H
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #ifdef BZ_HAVE_STD
 | 
|---|
| 38 |  #include <strstream>
 | 
|---|
| 39 | #else
 | 
|---|
| 40 |  #include <strstream.h>
 | 
|---|
| 41 | #endif
 | 
|---|
| 42 | 
 | 
|---|
| 43 | BZ_NAMESPACE(blitz)
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #ifndef BZ_ARRAY_H
 | 
|---|
| 46 |  #error <blitz/array/iter.h> must be included via <blitz/array.h>
 | 
|---|
| 47 | #endif
 | 
|---|
| 48 | 
 | 
|---|
| 49 | template<class P_numtype, int N_rank>
 | 
|---|
| 50 | class ArrayIterator {
 | 
|---|
| 51 | public:
 | 
|---|
| 52 |     typedef P_numtype                T_numtype;
 | 
|---|
| 53 |     typedef Array<T_numtype, N_rank> T_array;
 | 
|---|
| 54 |     typedef ArrayIterator<P_numtype, N_rank> T_iterator;
 | 
|---|
| 55 |     typedef const T_array& T_ctorArg1;
 | 
|---|
| 56 |     typedef int            T_ctorArg2;    // dummy
 | 
|---|
| 57 | 
 | 
|---|
| 58 |     enum { numArrayOperands = 1, numIndexPlaceholders = 0,
 | 
|---|
| 59 |         rank = N_rank };
 | 
|---|
| 60 | 
 | 
|---|
| 61 |     // NB: this ctor does NOT preserve stack and stride
 | 
|---|
| 62 |     // parameters.  This is for speed purposes.
 | 
|---|
| 63 |     ArrayIterator(const ArrayIterator<P_numtype, N_rank>& x)
 | 
|---|
| 64 |         : array_(x.array_), data_(x.data_)
 | 
|---|
| 65 |     { }
 | 
|---|
| 66 | 
 | 
|---|
| 67 |     void operator=(const ArrayIterator<P_numtype, N_rank>& x)
 | 
|---|
| 68 |     {
 | 
|---|
| 69 |         array_ = x.array_;
 | 
|---|
| 70 |         data_ = x.data_;
 | 
|---|
| 71 |         stack_ = x.stack_;
 | 
|---|
| 72 |         stride_ = x.stride_;
 | 
|---|
| 73 |     }
 | 
|---|
| 74 | 
 | 
|---|
| 75 |     ArrayIterator(const T_array& array)
 | 
|---|
| 76 |         : array_(array)
 | 
|---|
| 77 |     {
 | 
|---|
| 78 |         data_   = array.data();
 | 
|---|
| 79 |     }
 | 
|---|
| 80 | 
 | 
|---|
| 81 |     ~ArrayIterator()
 | 
|---|
| 82 |     { }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | #ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE
 | 
|---|
| 85 |     T_numtype operator()(TinyVector<int, N_rank> i)
 | 
|---|
| 86 |     { return array_(i); }
 | 
|---|
| 87 | #else
 | 
|---|
| 88 |     T_numtype operator()(const TinyVector<int, N_rank>& i)
 | 
|---|
| 89 |     { return array_(i); }
 | 
|---|
| 90 | #endif
 | 
|---|
| 91 | 
 | 
|---|
| 92 |     int lbound(int rank)
 | 
|---|
| 93 |     { 
 | 
|---|
| 94 |         if (rank < N_rank)
 | 
|---|
| 95 |             return array_.lbound(rank); 
 | 
|---|
| 96 |         else
 | 
|---|
| 97 |             return INT_MIN;   // tiny(int());
 | 
|---|
| 98 |     }
 | 
|---|
| 99 | 
 | 
|---|
| 100 |     int ubound(int rank)
 | 
|---|
| 101 |     { 
 | 
|---|
| 102 |         if (rank < N_rank)
 | 
|---|
| 103 |             return array_.ubound(rank); 
 | 
|---|
| 104 |         else
 | 
|---|
| 105 |             return INT_MAX;   // huge(int());
 | 
|---|
| 106 |     }
 | 
|---|
| 107 | 
 | 
|---|
| 108 |     T_numtype operator*()
 | 
|---|
| 109 |     { return *data_; }
 | 
|---|
| 110 | 
 | 
|---|
| 111 |     T_numtype operator[](int i)
 | 
|---|
| 112 |     { return data_[i * stride_]; }
 | 
|---|
| 113 | 
 | 
|---|
| 114 |     T_numtype fastRead(int i)
 | 
|---|
| 115 |     { return data_[i]; }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |     int suggestStride(int rank) const
 | 
|---|
| 118 |     { return array_.stride(rank); }
 | 
|---|
| 119 | 
 | 
|---|
| 120 |     _bz_bool isStride(int rank, int stride) const
 | 
|---|
| 121 |     { return array_.stride(rank) == stride; }
 | 
|---|
| 122 | 
 | 
|---|
| 123 |     void push(int position)
 | 
|---|
| 124 |     {
 | 
|---|
| 125 |         stack_[position] = data_;
 | 
|---|
| 126 |     }
 | 
|---|
| 127 |   
 | 
|---|
| 128 |     void pop(int position)
 | 
|---|
| 129 |     { 
 | 
|---|
| 130 |         data_ = stack_[position];
 | 
|---|
| 131 |     }
 | 
|---|
| 132 | 
 | 
|---|
| 133 |     void advance()
 | 
|---|
| 134 |     {
 | 
|---|
| 135 |         data_ += stride_;
 | 
|---|
| 136 |     }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |     void advance(int n)
 | 
|---|
| 139 |     {
 | 
|---|
| 140 |         data_ += n * stride_;
 | 
|---|
| 141 |     }
 | 
|---|
| 142 | 
 | 
|---|
| 143 |     void loadStride(int rank)
 | 
|---|
| 144 |     {
 | 
|---|
| 145 |         stride_ = array_.stride(rank);
 | 
|---|
| 146 |     }
 | 
|---|
| 147 | 
 | 
|---|
| 148 |     const T_numtype * _bz_restrict data() const
 | 
|---|
| 149 |     { return data_; }
 | 
|---|
| 150 | 
 | 
|---|
| 151 |     int stride() const
 | 
|---|
| 152 |     { return stride_; }
 | 
|---|
| 153 | 
 | 
|---|
| 154 |     _bz_bool isUnitStride(int rank) const
 | 
|---|
| 155 |     { return array_.stride(rank) == 1; }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |     void advanceUnitStride()
 | 
|---|
| 158 |     { ++data_; }
 | 
|---|
| 159 | 
 | 
|---|
| 160 |     _bz_bool canCollapse(int outerLoopRank, int innerLoopRank) const
 | 
|---|
| 161 |     { return array_.canCollapse(outerLoopRank, innerLoopRank); }
 | 
|---|
| 162 | 
 | 
|---|
| 163 |     void prettyPrint(string& str, prettyPrintFormat& format) const
 | 
|---|
| 164 |     {
 | 
|---|
| 165 |         if (format.tersePrintingSelected())
 | 
|---|
| 166 |             str += format.nextArrayOperandSymbol();
 | 
|---|
| 167 |         else if (format.dumpArrayShapesMode())
 | 
|---|
| 168 |         {
 | 
|---|
| 169 |             ostrstream ostr;
 | 
|---|
| 170 |             ostr << array_.shape();
 | 
|---|
| 171 |             str += ostr.str();
 | 
|---|
| 172 |         }
 | 
|---|
| 173 |         else {
 | 
|---|
| 174 |             str += "Array<";
 | 
|---|
| 175 |             str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype);
 | 
|---|
| 176 |             str += ",";
 | 
|---|
| 177 | 
 | 
|---|
| 178 |             char tmpBuf[10];
 | 
|---|
| 179 |             sprintf(tmpBuf, "%d", N_rank);
 | 
|---|
| 180 | 
 | 
|---|
| 181 |             str += tmpBuf;
 | 
|---|
| 182 |             str += ">";
 | 
|---|
| 183 |         }
 | 
|---|
| 184 |     }
 | 
|---|
| 185 | 
 | 
|---|
| 186 |     template<class T_shape>
 | 
|---|
| 187 |     _bz_bool shapeCheck(const T_shape& shape)
 | 
|---|
| 188 |     { return areShapesConformable(shape, array_.length()); }
 | 
|---|
| 189 | 
 | 
|---|
| 190 |     // Experimental
 | 
|---|
| 191 |     T_numtype& operator()(int i, int j)
 | 
|---|
| 192 |     {
 | 
|---|
| 193 |         return (T_numtype&)data_[i*array_.stride(0) + j*array_.stride(1)];
 | 
|---|
| 194 |     }
 | 
|---|
| 195 | 
 | 
|---|
| 196 |     // Experimental
 | 
|---|
| 197 |     T_numtype& operator()(int i, int j, int k)
 | 
|---|
| 198 |     {
 | 
|---|
| 199 |         return (T_numtype&)data_[i*array_.stride(0) + j*array_.stride(1)
 | 
|---|
| 200 |           + k*array_.stride(2)];
 | 
|---|
| 201 |     }
 | 
|---|
| 202 | 
 | 
|---|
| 203 |     // Experimental
 | 
|---|
| 204 | 
 | 
|---|
| 205 |     void moveTo(int i, int j)
 | 
|---|
| 206 |     {
 | 
|---|
| 207 |         data_ = &const_cast<T_array&>(array_)(i,j);
 | 
|---|
| 208 |     }
 | 
|---|
| 209 | 
 | 
|---|
| 210 |     void moveTo(int i, int j, int k)
 | 
|---|
| 211 |     {
 | 
|---|
| 212 |         data_ = &const_cast<T_array&>(array_)(i,j,k);
 | 
|---|
| 213 |     }
 | 
|---|
| 214 | 
 | 
|---|
| 215 |     void moveTo(const TinyVector<int,N_rank>& i)
 | 
|---|
| 216 |     {
 | 
|---|
| 217 |         data_ = &const_cast<T_array&>(array_)(i);
 | 
|---|
| 218 |     }
 | 
|---|
| 219 | 
 | 
|---|
| 220 |     // Experimental
 | 
|---|
| 221 |     void operator=(T_numtype x)
 | 
|---|
| 222 |     {   *const_cast<T_numtype* _bz_restrict>(data_) = x; }
 | 
|---|
| 223 | 
 | 
|---|
| 224 |     // Experimental
 | 
|---|
| 225 |     template<class T_value>
 | 
|---|
| 226 |     void operator=(T_value x)
 | 
|---|
| 227 |     {   *const_cast<T_numtype* _bz_restrict>(data_) = x; }
 | 
|---|
| 228 | 
 | 
|---|
| 229 |     // Experimental
 | 
|---|
| 230 |     template<class T_value>
 | 
|---|
| 231 |     void operator+=(T_value x)
 | 
|---|
| 232 |     { *const_cast<T_numtype* _bz_restrict>(data_) += x; }
 | 
|---|
| 233 | 
 | 
|---|
| 234 |     // NEEDS_WORK: other operators
 | 
|---|
| 235 | 
 | 
|---|
| 236 |     // Experimental
 | 
|---|
| 237 |     operator T_numtype() const
 | 
|---|
| 238 |     { return *data_; }
 | 
|---|
| 239 | 
 | 
|---|
| 240 |     // Experimental
 | 
|---|
| 241 |     T_numtype shift(int offset, int dim)
 | 
|---|
| 242 |     {
 | 
|---|
| 243 |         return data_[offset*array_.stride(dim)];
 | 
|---|
| 244 |     }
 | 
|---|
| 245 | 
 | 
|---|
| 246 |     // Experimental
 | 
|---|
| 247 |     T_numtype shift(int offset1, int dim1, int offset2, int dim2)
 | 
|---|
| 248 |     {
 | 
|---|
| 249 |         return data_[offset1*array_.stride(dim1) 
 | 
|---|
| 250 |             + offset2*array_.stride(dim2)];
 | 
|---|
| 251 |     }
 | 
|---|
| 252 | 
 | 
|---|
| 253 | private:
 | 
|---|
| 254 |     const T_array&                          array_;
 | 
|---|
| 255 |     const T_numtype * _bz_restrict          data_;
 | 
|---|
| 256 |     const T_numtype * _bz_restrict          stack_[N_rank];
 | 
|---|
| 257 |     int                                     stride_;
 | 
|---|
| 258 | };
 | 
|---|
| 259 | 
 | 
|---|
| 260 | BZ_NAMESPACE_END
 | 
|---|
| 261 | 
 | 
|---|
| 262 | #endif // BZ_ARRAYITER_H
 | 
|---|