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