[658] | 1 | #ifndef BZ_ARRAY_INDIRECT_H
|
---|
| 2 | #define BZ_ARRAY_INDIRECT_H
|
---|
| 3 |
|
---|
| 4 | #include <blitz/array/asexpr.h>
|
---|
| 5 | #include <blitz/array/cartesian.h>
|
---|
| 6 |
|
---|
| 7 | BZ_NAMESPACE(blitz)
|
---|
| 8 |
|
---|
| 9 | template<class T_array, class T_index>
|
---|
| 10 | class IndirectArray {
|
---|
| 11 |
|
---|
| 12 | public:
|
---|
| 13 | IndirectArray(T_array& array, T_index& index)
|
---|
| 14 | : array_(array), index_(index)
|
---|
| 15 | { }
|
---|
| 16 |
|
---|
| 17 | template<class T_expr>
|
---|
| 18 | void operator=(T_expr expr);
|
---|
| 19 |
|
---|
| 20 | protected:
|
---|
| 21 | T_array& array_;
|
---|
| 22 | T_index& index_;
|
---|
| 23 | };
|
---|
| 24 |
|
---|
| 25 | // Forward declarations
|
---|
| 26 | template<class T_array, class T_arrayiter, class T_subdomain, class T_expr>
|
---|
| 27 | inline void applyOverSubdomain(const T_array& array, T_arrayiter& arrayIter,
|
---|
| 28 | T_subdomain subdomain, T_expr expr);
|
---|
| 29 | template<class T_array, class T_arrayiter, int N_rank, class T_expr>
|
---|
| 30 | inline void applyOverSubdomain(const T_array& array, T_arrayiter& arrayIter,
|
---|
| 31 | RectDomain<N_rank> subdomain,
|
---|
| 32 | T_expr expr);
|
---|
| 33 |
|
---|
| 34 | template<class T_array, class T_index> template<class T_rhs>
|
---|
| 35 | void IndirectArray<T_array, T_index>::operator=(T_rhs rhs)
|
---|
| 36 | {
|
---|
| 37 | typedef _bz_typename asExpr<T_rhs>::T_expr T_expr;
|
---|
| 38 | T_expr expr(rhs);
|
---|
| 39 |
|
---|
| 40 | _bz_typename T_array::T_iterator arrayIter(array_);
|
---|
| 41 |
|
---|
| 42 | _bz_typename T_index::iterator iter = index_.begin(),
|
---|
| 43 | end = index_.end();
|
---|
| 44 |
|
---|
| 45 | for (; iter != end; ++iter)
|
---|
| 46 | {
|
---|
| 47 | _bz_typename T_index::value_type subdomain = *iter;
|
---|
| 48 | applyOverSubdomain(array_, arrayIter, subdomain, expr);
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | template<class T_array, class T_arrayiter, class T_subdomain, class T_expr>
|
---|
| 53 | inline void applyOverSubdomain(const T_array& array, T_arrayiter& arrayIter,
|
---|
| 54 | T_subdomain subdomain, T_expr expr)
|
---|
| 55 | {
|
---|
| 56 | BZPRECHECK(array.isInRange(subdomain),
|
---|
| 57 | "In indirection using an STL container of TinyVector<int,"
|
---|
| 58 | << array.rank() << ">, one of the" << endl << "positions is out of"
|
---|
| 59 | " range: " << endl << subdomain << endl
|
---|
| 60 | << "Array lower bounds: " << array.lbound() << endl
|
---|
| 61 | << "Array upper bounds: " << array.ubound() << endl)
|
---|
| 62 |
|
---|
| 63 | arrayIter.moveTo(subdomain);
|
---|
| 64 | expr.moveTo(subdomain);
|
---|
| 65 |
|
---|
| 66 | *const_cast<_bz_typename T_arrayiter::T_numtype*>(arrayIter.data()) = *expr;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | // Specialization for RectDomain<N>
|
---|
| 70 | template<class T_array, class T_arrayiter, int N_rank, class T_expr>
|
---|
| 71 | inline void applyOverSubdomain(const T_array& array, T_arrayiter& arrayIter,
|
---|
| 72 | RectDomain<N_rank> subdomain,
|
---|
| 73 | T_expr expr)
|
---|
| 74 | {
|
---|
| 75 | typedef _bz_typename T_array::T_numtype T_numtype;
|
---|
| 76 |
|
---|
| 77 | // Assume that the RectDomain<N_rank> is a 1-D strip.
|
---|
| 78 | // Find the dimension in which the strip is oriented. This
|
---|
| 79 | // variable is static so that we cache the value; likely to be
|
---|
| 80 | // the same for all strips within a container.
|
---|
| 81 |
|
---|
| 82 | static int stripDim = 0;
|
---|
| 83 |
|
---|
| 84 | if (subdomain.lbound(stripDim) == subdomain.ubound(stripDim))
|
---|
| 85 | {
|
---|
| 86 | // Cached value was wrong, find the correct value of stripDim
|
---|
| 87 | for (stripDim=0; stripDim < N_rank; ++stripDim)
|
---|
| 88 | if (subdomain.lbound(stripDim) != subdomain.ubound(stripDim))
|
---|
| 89 | break;
|
---|
| 90 |
|
---|
| 91 | // Handle case where the strip is just a single point
|
---|
| 92 | if (stripDim == N_rank)
|
---|
| 93 | stripDim = 0;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #ifdef BZ_DEBUG
|
---|
| 97 | // Check that this is in fact a 1D strip
|
---|
| 98 | for (int i=0; i < N_rank; ++i)
|
---|
| 99 | if ((i != stripDim) && (subdomain.lbound(i) != subdomain.ubound(i)))
|
---|
| 100 | BZPRECHECK(0, "In indirection using an STL container of RectDomain<"
|
---|
| 101 | << N_rank << ">, one of" << endl << "the RectDomain objects was not"
|
---|
| 102 | " a one-dimensional strip:" << endl << "RectDomain<" << N_rank
|
---|
| 103 | << ">::lbound() = " << subdomain.lbound() << endl
|
---|
| 104 | << "RectDomain<" << N_rank << ">::ubound() = " << subdomain.ubound())
|
---|
| 105 | #endif
|
---|
| 106 |
|
---|
| 107 | // Check that the start and end position are in range
|
---|
| 108 | BZPRECHECK(array.isInRange(subdomain.lbound()),
|
---|
| 109 | "In indirection using an STL container of RectDomain<"
|
---|
| 110 | << N_rank << ">, one of" << endl << "the RectDomain objects has a"
|
---|
| 111 | " lbound which is out of range:" << endl
|
---|
| 112 | << subdomain.lbound() << endl
|
---|
| 113 | << "Array lower bounds: " << array.lbound() << endl
|
---|
| 114 | << "Array upper bounds: " << array.ubound() << endl)
|
---|
| 115 |
|
---|
| 116 | BZPRECHECK(array.isInRange(subdomain.ubound()),
|
---|
| 117 | "In indirection using an STL container of RectDomain<"
|
---|
| 118 | << N_rank << ">, one of" << endl << "the RectDomain objects has a"
|
---|
| 119 | " ubound which is out of range:" << endl
|
---|
| 120 | << subdomain.lbound() << endl
|
---|
| 121 | << "Array lower bounds: " << array.lbound() << endl
|
---|
| 122 | << "Array upper bounds: " << array.ubound() << endl)
|
---|
| 123 |
|
---|
| 124 | // Position at the beginning of the strip
|
---|
| 125 | arrayIter.moveTo(subdomain.lbound());
|
---|
| 126 | expr.moveTo(subdomain.lbound());
|
---|
| 127 |
|
---|
| 128 | // Loop through the strip
|
---|
| 129 |
|
---|
| 130 | #ifdef BZ_USE_FAST_READ_ARRAY_EXPR
|
---|
| 131 |
|
---|
| 132 | _bz_bool useUnitStride = arrayIter.isUnitStride(stripDim)
|
---|
| 133 | && expr.isUnitStride(stripDim);
|
---|
| 134 |
|
---|
| 135 | int lbound = subdomain.lbound(stripDim);
|
---|
| 136 | int ubound = subdomain.ubound(stripDim);
|
---|
| 137 |
|
---|
| 138 | if (useUnitStride)
|
---|
| 139 | {
|
---|
| 140 | T_numtype* _bz_restrict data = const_cast<T_numtype*>(arrayIter.data());
|
---|
| 141 |
|
---|
| 142 | int length = ubound - lbound + 1;
|
---|
| 143 | for (int i=0; i < length; ++i)
|
---|
| 144 | data[i] = expr.fastRead(i);
|
---|
| 145 | }
|
---|
| 146 | else {
|
---|
| 147 | #endif
|
---|
| 148 |
|
---|
| 149 | arrayIter.loadStride(stripDim);
|
---|
| 150 | expr.loadStride(stripDim);
|
---|
| 151 |
|
---|
| 152 | for (int i=lbound; i <= ubound; ++i)
|
---|
| 153 | {
|
---|
| 154 | *const_cast<_bz_typename T_arrayiter::T_numtype*>(arrayIter.data())
|
---|
| 155 | = *expr;
|
---|
| 156 | expr.advance();
|
---|
| 157 | arrayIter.advance();
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | #ifdef BZ_USE_FAST_READ_ARRAY_EXPR
|
---|
| 161 | }
|
---|
| 162 | #endif
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | // Global functions for cartesian product of index sets
|
---|
| 166 | template<class T_container>
|
---|
| 167 | CartesianProduct<TinyVector<int,2>,T_container,2>
|
---|
| 168 | indexSet(const T_container& container0, const T_container& container1)
|
---|
| 169 | {
|
---|
| 170 | return CartesianProduct<TinyVector<int,2>,T_container,2>(
|
---|
| 171 | const_cast<T_container&>(container0),
|
---|
| 172 | const_cast<T_container&>(container1));
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | template<class T_container>
|
---|
| 176 | CartesianProduct<TinyVector<int,3>,T_container,3>
|
---|
| 177 | indexSet(const T_container& container0, const T_container& container1,
|
---|
| 178 | const T_container& container2)
|
---|
| 179 | {
|
---|
| 180 | return CartesianProduct<TinyVector<int,3>,T_container,3>(
|
---|
| 181 | const_cast<T_container&>(container0),
|
---|
| 182 | const_cast<T_container&>(container1),
|
---|
| 183 | const_cast<T_container&>(container2));
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | // Mixture of singletons and containers, e.g. A[indexSet(I,3,K)]
|
---|
| 187 |
|
---|
| 188 | // cp_findContainerType<T1,T2,T3,...,Tn>::T_container
|
---|
| 189 | // The set of parameters T1, T2, T3, ... Tn is a mixture of
|
---|
| 190 | // int and T_container. This traits class finds the container
|
---|
| 191 | // type, and sets T_container.
|
---|
| 192 | //
|
---|
| 193 | // e.g. cp_findContainerType<int,int,list<int>,int>::T_container is list<int>
|
---|
| 194 | // cp_findContainerType<int,deque<int>,deque<int>>::T_container
|
---|
| 195 | // is deque<int>
|
---|
| 196 |
|
---|
| 197 | template<class T1, class T2, class T3=int, class T4=int>
|
---|
| 198 | struct cp_findContainerType {
|
---|
| 199 | typedef T1 T_container;
|
---|
| 200 | };
|
---|
| 201 |
|
---|
| 202 | template<class T2, class T3, class T4>
|
---|
| 203 | struct cp_findContainerType<int,T2,T3,T4> {
|
---|
| 204 | typedef _bz_typename cp_findContainerType<T2,T3,T4>::T_container T_container;
|
---|
| 205 | };
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | // The cp_traits class handles promotion of singleton integers to
|
---|
| 209 | // containers. It takes two template parameters:
|
---|
| 210 | // T = argument type
|
---|
| 211 | // T2 = container type
|
---|
| 212 | // If T is an integer, then a container of type T2 is created and the
|
---|
| 213 | // integer is inserted. This container is returned.
|
---|
| 214 | // Otherwise, T is assumed to be the same type as T2, and the original
|
---|
| 215 | // container is returned.
|
---|
| 216 |
|
---|
| 217 | template<class T, class T2>
|
---|
| 218 | struct cp_traits {
|
---|
| 219 | typedef T T_container;
|
---|
| 220 |
|
---|
| 221 | static const T_container& make(const T& x)
|
---|
| 222 | { return x; }
|
---|
| 223 | };
|
---|
| 224 |
|
---|
| 225 | template<class T2>
|
---|
| 226 | struct cp_traits<int,T2> {
|
---|
| 227 | typedef T2 T_container;
|
---|
| 228 |
|
---|
| 229 | static T2 make(int x)
|
---|
| 230 | {
|
---|
| 231 | T2 singleton;
|
---|
| 232 | singleton.push_back(x);
|
---|
| 233 | return singleton;
|
---|
| 234 | }
|
---|
| 235 | };
|
---|
| 236 |
|
---|
| 237 | // These versions of indexSet() allow mixtures of integer
|
---|
| 238 | // and container arguments. At least one integer must be
|
---|
| 239 | // specified.
|
---|
| 240 |
|
---|
| 241 | template<class T1, class T2>
|
---|
| 242 | CartesianProduct<TinyVector<int,2>, _bz_typename
|
---|
| 243 | cp_findContainerType<T1,T2>::T_container,2>
|
---|
| 244 | indexSet(const T1& c1, const T2& c2)
|
---|
| 245 | {
|
---|
| 246 | typedef _bz_typename cp_findContainerType<T1,T2>::T_container
|
---|
| 247 | T_container;
|
---|
| 248 |
|
---|
| 249 | return CartesianProduct<TinyVector<int,2>, T_container, 2>(
|
---|
| 250 | cp_traits<T1,T_container>::make(c1),
|
---|
| 251 | cp_traits<T2,T_container>::make(c2));
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | template<class T1, class T2, class T3>
|
---|
| 255 | CartesianProduct<TinyVector<int,3>, _bz_typename
|
---|
| 256 | cp_findContainerType<T1,T2,T3>::T_container, 3>
|
---|
| 257 | indexSet(const T1& c1, const T2& c2, const T3& c3)
|
---|
| 258 | {
|
---|
| 259 | typedef _bz_typename cp_findContainerType<T1,T2,T3>::T_container
|
---|
| 260 | T_container;
|
---|
| 261 |
|
---|
| 262 | return CartesianProduct<TinyVector<int,3>, T_container, 3>(
|
---|
| 263 | cp_traits<T1,T_container>::make(c1),
|
---|
| 264 | cp_traits<T2,T_container>::make(c2),
|
---|
| 265 | cp_traits<T3,T_container>::make(c3));
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | BZ_NAMESPACE_END
|
---|
| 269 |
|
---|
| 270 | #endif // BZ_ARRAY_INDIRECT_H
|
---|