| 1 | /*************************************************************************** | 
|---|
| 2 | * blitz/vecexpr.h      Vector<P_numtype> expression templates | 
|---|
| 3 | * | 
|---|
| 4 | * $Id: vecexpr.h,v 1.1.1.1 1999-11-26 16:37:05 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:00  ansari | 
|---|
| 27 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99 | 
|---|
| 28 | * | 
|---|
| 29 | * Revision 1.6  1998/03/14 00:04:47  tveldhui | 
|---|
| 30 | * 0.2-alpha-05 | 
|---|
| 31 | * | 
|---|
| 32 | * Revision 1.5  1997/07/16 14:51:20  tveldhui | 
|---|
| 33 | * Update: Alpha release 0.2 (Arrays) | 
|---|
| 34 | * | 
|---|
| 35 | * Revision 1.4  1997/01/24 14:42:00  tveldhui | 
|---|
| 36 | * Periodic RCS update | 
|---|
| 37 | * | 
|---|
| 38 | * Revision 1.3  1997/01/23 03:28:28  tveldhui | 
|---|
| 39 | * Periodic RCS update | 
|---|
| 40 | * | 
|---|
| 41 | * Revision 1.2  1997/01/13 22:19:58  tveldhui | 
|---|
| 42 | * Periodic RCS update | 
|---|
| 43 | * | 
|---|
| 44 | * | 
|---|
| 45 | */ | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | #ifndef BZ_VECEXPR_H | 
|---|
| 49 | #define BZ_VECEXPR_H | 
|---|
| 50 |  | 
|---|
| 51 | #ifndef BZ_VECTOR_H | 
|---|
| 52 | #include <blitz/vector.h> | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | #ifndef BZ_APPLICS_H | 
|---|
| 56 | #include <blitz/applics.h> | 
|---|
| 57 | #endif | 
|---|
| 58 |  | 
|---|
| 59 | #ifndef BZ_META_METAPROG_H | 
|---|
| 60 | #include <blitz/meta/metaprog.h> | 
|---|
| 61 | #endif | 
|---|
| 62 |  | 
|---|
| 63 | #ifndef BZ_VECEXPRWRAP_H | 
|---|
| 64 | #include <blitz/vecexprwrap.h>           // _bz_VecExpr wrapper class | 
|---|
| 65 | #endif | 
|---|
| 66 |  | 
|---|
| 67 | BZ_NAMESPACE(blitz) | 
|---|
| 68 |  | 
|---|
| 69 | template<class P_expr1, class P_expr2, class P_op> | 
|---|
| 70 | class _bz_VecExprOp { | 
|---|
| 71 |  | 
|---|
| 72 | public: | 
|---|
| 73 | typedef P_expr1 T_expr1; | 
|---|
| 74 | typedef P_expr2 T_expr2; | 
|---|
| 75 | typedef _bz_typename T_expr1::T_numtype T_numtype1; | 
|---|
| 76 | typedef _bz_typename T_expr2::T_numtype T_numtype2; | 
|---|
| 77 | typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype; | 
|---|
| 78 | typedef P_op    T_op; | 
|---|
| 79 |  | 
|---|
| 80 | #ifdef BZ_PASS_EXPR_BY_VALUE | 
|---|
| 81 | _bz_VecExprOp(T_expr1 a, T_expr2 b) | 
|---|
| 82 | : iter1_(a), iter2_(b) | 
|---|
| 83 | { } | 
|---|
| 84 | #else | 
|---|
| 85 | _bz_VecExprOp(const T_expr1& a, const T_expr2& b) | 
|---|
| 86 | : iter1_(a), iter2_(b) | 
|---|
| 87 | { } | 
|---|
| 88 | #endif | 
|---|
| 89 |  | 
|---|
| 90 | #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR | 
|---|
| 91 | _bz_VecExprOp(const _bz_VecExprOp<P_expr1, P_expr2, P_op>& x) | 
|---|
| 92 | : iter1_(x.iter1_), iter2_(x.iter2_) | 
|---|
| 93 | { } | 
|---|
| 94 | #endif | 
|---|
| 95 |  | 
|---|
| 96 | T_numtype operator[](int i) const | 
|---|
| 97 | { return T_op::apply(iter1_[i], iter2_[i]); } | 
|---|
| 98 |  | 
|---|
| 99 | T_numtype operator()(int i) const | 
|---|
| 100 | { return T_op::apply(iter1_(i), iter2_(i)); } | 
|---|
| 101 |  | 
|---|
| 102 | int length(int recommendedLength) const | 
|---|
| 103 | { | 
|---|
| 104 | BZPRECONDITION(iter2_.length(recommendedLength) == | 
|---|
| 105 | iter1_.length(recommendedLength)); | 
|---|
| 106 | return iter1_.length(recommendedLength); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | enum { | 
|---|
| 110 | _bz_staticLengthCount = | 
|---|
| 111 | BZ_ENUM_CAST(P_expr1::_bz_staticLengthCount) | 
|---|
| 112 | + BZ_ENUM_CAST(P_expr2::_bz_staticLengthCount), | 
|---|
| 113 |  | 
|---|
| 114 | _bz_dynamicLengthCount = | 
|---|
| 115 | BZ_ENUM_CAST(P_expr1::_bz_dynamicLengthCount) | 
|---|
| 116 | + BZ_ENUM_CAST(P_expr2::_bz_dynamicLengthCount), | 
|---|
| 117 |  | 
|---|
| 118 | _bz_staticLength = (BZ_ENUM_CAST(P_expr1::_bz_staticLength) > BZ_ENUM_CAST(P_expr2::_bz_staticLength)) ? BZ_ENUM_CAST(P_expr1::_bz_staticLength) : BZ_ENUM_CAST(P_expr2::_bz_staticLength) | 
|---|
| 119 |  | 
|---|
| 120 | //      _bz_meta_max<P_expr1::_bz_staticLength, P_expr2::_bz_staticLength>::max | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 | int _bz_suggestLength() const | 
|---|
| 124 | { | 
|---|
| 125 | int length1 = iter1_._bz_suggestLength(); | 
|---|
| 126 | if (length1 != 0) | 
|---|
| 127 | return length1; | 
|---|
| 128 | return iter2_._bz_suggestLength(); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | _bz_bool  _bz_hasFastAccess() const | 
|---|
| 132 | { return iter1_._bz_hasFastAccess() && iter2_._bz_hasFastAccess(); } | 
|---|
| 133 |  | 
|---|
| 134 | T_numtype _bz_fastAccess(int i) const | 
|---|
| 135 | { | 
|---|
| 136 | return T_op::apply(iter1_._bz_fastAccess(i), | 
|---|
| 137 | iter2_._bz_fastAccess(i)); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | private: | 
|---|
| 141 | _bz_VecExprOp(); | 
|---|
| 142 |  | 
|---|
| 143 | T_expr1 iter1_; | 
|---|
| 144 | T_expr2 iter2_; | 
|---|
| 145 | }; | 
|---|
| 146 |  | 
|---|
| 147 | template<class P_expr, class P_unaryOp> | 
|---|
| 148 | class _bz_VecExprUnaryOp { | 
|---|
| 149 |  | 
|---|
| 150 | public: | 
|---|
| 151 | typedef P_expr T_expr; | 
|---|
| 152 | typedef P_unaryOp T_unaryOp; | 
|---|
| 153 | typedef _bz_typename T_unaryOp::T_numtype T_numtype; | 
|---|
| 154 |  | 
|---|
| 155 | #ifdef BZ_PASS_EXPR_BY_VALUE | 
|---|
| 156 | _bz_VecExprUnaryOp(T_expr iter) | 
|---|
| 157 | : iter_(iter) | 
|---|
| 158 | { } | 
|---|
| 159 | #else | 
|---|
| 160 | _bz_VecExprUnaryOp(const T_expr& iter) | 
|---|
| 161 | : iter_(iter) | 
|---|
| 162 | { } | 
|---|
| 163 | #endif | 
|---|
| 164 |  | 
|---|
| 165 | #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR | 
|---|
| 166 | _bz_VecExprUnaryOp(const _bz_VecExprUnaryOp<P_expr, P_unaryOp>& x) | 
|---|
| 167 | : iter_(x.iter_) | 
|---|
| 168 | { } | 
|---|
| 169 | #endif | 
|---|
| 170 |  | 
|---|
| 171 | T_numtype operator[](int i) const | 
|---|
| 172 | { return T_unaryOp::apply(iter_[i]); } | 
|---|
| 173 |  | 
|---|
| 174 | T_numtype operator()(int i) const | 
|---|
| 175 | { return T_unaryOp::apply(iter_(i)); } | 
|---|
| 176 |  | 
|---|
| 177 | int length(int recommendedLength) const | 
|---|
| 178 | { return iter_.length(recommendedLength); } | 
|---|
| 179 |  | 
|---|
| 180 | enum { _bz_staticLengthCount = BZ_ENUM_CAST(P_expr::_bz_staticLengthCount), | 
|---|
| 181 | _bz_dynamicLengthCount =BZ_ENUM_CAST(P_expr::_bz_dynamicLengthCount), | 
|---|
| 182 | _bz_staticLength = BZ_ENUM_CAST(P_expr::_bz_staticLength) }; | 
|---|
| 183 |  | 
|---|
| 184 | int _bz_suggestLength() const | 
|---|
| 185 | { return iter_._bz_suggestLength(); } | 
|---|
| 186 |  | 
|---|
| 187 | _bz_bool _bz_hasFastAccess() const | 
|---|
| 188 | { return iter_._bz_hasFastAccess(); } | 
|---|
| 189 |  | 
|---|
| 190 | T_numtype _bz_fastAccess(int i) const | 
|---|
| 191 | { return T_unaryOp::apply(iter_._bz_fastAccess(i)); } | 
|---|
| 192 |  | 
|---|
| 193 | private: | 
|---|
| 194 | _bz_VecExprUnaryOp() { } | 
|---|
| 195 |  | 
|---|
| 196 | T_expr iter_; | 
|---|
| 197 | }; | 
|---|
| 198 |  | 
|---|
| 199 | template<class P_numtype> | 
|---|
| 200 | class _bz_VecExprConstant { | 
|---|
| 201 | public: | 
|---|
| 202 | typedef P_numtype T_numtype; | 
|---|
| 203 |  | 
|---|
| 204 | _bz_VecExprConstant(P_numtype value) | 
|---|
| 205 | : value_(BZ_NO_PROPAGATE(value)) | 
|---|
| 206 | { | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR | 
|---|
| 210 | _bz_VecExprConstant(const _bz_VecExprConstant<P_numtype>& x) | 
|---|
| 211 | : value_(x.value_) | 
|---|
| 212 | { } | 
|---|
| 213 | #endif | 
|---|
| 214 |  | 
|---|
| 215 | T_numtype operator[](int) const | 
|---|
| 216 | { return value_; } | 
|---|
| 217 |  | 
|---|
| 218 | T_numtype operator()(int) const | 
|---|
| 219 | { return value_; } | 
|---|
| 220 |  | 
|---|
| 221 | int length(int recommendedLength) const | 
|---|
| 222 | { return recommendedLength; } | 
|---|
| 223 |  | 
|---|
| 224 | enum { _bz_staticLengthCount = 0, | 
|---|
| 225 | _bz_dynamicLengthCount = 0, | 
|---|
| 226 | _bz_staticLength = 0 | 
|---|
| 227 | }; | 
|---|
| 228 |  | 
|---|
| 229 | int _bz_suggestLength() const | 
|---|
| 230 | { return 0; } | 
|---|
| 231 |  | 
|---|
| 232 | _bz_bool _bz_hasFastAccess() const | 
|---|
| 233 | { return 1; } | 
|---|
| 234 |  | 
|---|
| 235 | T_numtype _bz_fastAccess(int) const | 
|---|
| 236 | { return value_; } | 
|---|
| 237 |  | 
|---|
| 238 | private: | 
|---|
| 239 |  | 
|---|
| 240 | _bz_VecExprConstant() { } | 
|---|
| 241 |  | 
|---|
| 242 | T_numtype value_; | 
|---|
| 243 | }; | 
|---|
| 244 |  | 
|---|
| 245 | // Some miscellaneous operators that don't seem to belong anywhere else. | 
|---|
| 246 |  | 
|---|
| 247 | template<class P_expr> | 
|---|
| 248 | inline | 
|---|
| 249 | _bz_VecExpr<_bz_VecExprUnaryOp<_bz_VecExpr<P_expr>, | 
|---|
| 250 | _bz_negate<_bz_typename P_expr::T_numtype> > > | 
|---|
| 251 | operator-(_bz_VecExpr<P_expr> a) | 
|---|
| 252 | { | 
|---|
| 253 | typedef _bz_VecExprUnaryOp<_bz_VecExpr<P_expr>, | 
|---|
| 254 | _bz_negate<_bz_typename P_expr::T_numtype> > T_expr; | 
|---|
| 255 | return _bz_VecExpr<T_expr>(T_expr(a)); | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | template<class P_numtype> | 
|---|
| 259 | inline | 
|---|
| 260 | _bz_VecExpr<_bz_VecExprUnaryOp<VectorIterConst<P_numtype>, | 
|---|
| 261 | _bz_negate<P_numtype> > > | 
|---|
| 262 | operator-(const Vector<P_numtype>& a) | 
|---|
| 263 | { | 
|---|
| 264 | typedef _bz_VecExprUnaryOp<VectorIterConst<P_numtype>, | 
|---|
| 265 | _bz_negate<P_numtype> > T_expr; | 
|---|
| 266 |  | 
|---|
| 267 | return _bz_VecExpr<T_expr>(T_expr(a.begin())); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | inline | 
|---|
| 271 | _bz_VecExpr<_bz_VecExprUnaryOp<Range, _bz_negate<Range::T_numtype> > > | 
|---|
| 272 | operator-(Range r) | 
|---|
| 273 | { | 
|---|
| 274 | typedef _bz_VecExprUnaryOp<Range, _bz_negate<Range::T_numtype> > T_expr; | 
|---|
| 275 |  | 
|---|
| 276 | return _bz_VecExpr<T_expr>(T_expr(r)); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 |  | 
|---|
| 280 | // NEEDS_WORK: implement operator- for Range, VectorPick, TinyVector | 
|---|
| 281 |  | 
|---|
| 282 | BZ_NAMESPACE_END | 
|---|
| 283 |  | 
|---|
| 284 | #ifndef BZ_TINYVEC_H | 
|---|
| 285 | #include <blitz/tinyvec.h> | 
|---|
| 286 | #endif | 
|---|
| 287 |  | 
|---|
| 288 | #include <blitz/vecbops.cc>       // Operators with two operands | 
|---|
| 289 | #include <blitz/vecuops.cc>       // Functions with one argument | 
|---|
| 290 | #include <blitz/vecbfn.cc>        // Functions with two arguments | 
|---|
| 291 |  | 
|---|
| 292 | #endif // BZ_VECEXPR_H | 
|---|