From 1ea7fca02b99437b14d01d76597f3fc730f8282e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 25 Apr 2009 17:04:32 +0000 Subject: 2009-04-26 Tatsuhiro Tsujikawa Rewritten expr * src/array_fun.h * test/array_funTest.cc --- src/array_fun.h | 117 +++++++++++++++++++------------------------------------- 1 file changed, 40 insertions(+), 77 deletions(-) (limited to 'src/array_fun.h') diff --git a/src/array_fun.h b/src/array_fun.h index 479e24b0..a8990b9d 100644 --- a/src/array_fun.h +++ b/src/array_fun.h @@ -118,114 +118,77 @@ public: namespace expr { -template -struct Expr { - Expr(const T& expOp):_expOp(expOp) {} +template +struct BinExpr { + BinExpr(const L& l, const R& r):_l(l), _r(r) {} + + typedef typename OpTag::returnType returnType; - typename T::returnType operator[](size_t index) const + returnType operator[](size_t index) const { - return _expOp(index); + return OpTag::apply(_l[index], _r[index]); } - const T& _expOp; + const L& _l; + const R& _r; }; -template -struct And -{ - typedef T returnType; - static inline T apply(T lhs, T rhs) { return lhs&rhs; } +template +struct UnExpr { + UnExpr(const A& a):_a(a) {} + + typedef typename OpTag::returnType returnType; + + returnType operator[](size_t index) const + { + return OpTag::apply(_a[index]); + } + + const A& _a; }; template -struct Noop +struct And { typedef T returnType; - static inline T apply(T arg) { return arg; } + static inline returnType apply(T lhs, T rhs) { return lhs&rhs; } }; template struct Negate { typedef T returnType; - static inline T apply(T arg) { return ~arg; } -}; - -template -struct ExpBinOp -{ - typedef typename BinOp::returnType returnType; - - ExpBinOp(const T1& lhs, const T2& rhs):_lhs(lhs), _rhs(rhs) {} - - returnType operator()(size_t index) const - { - return BinOp::apply(_lhs[index], _rhs[index]); - } - - const T1& _lhs; - const T2& _rhs; + static inline returnType apply(T a) { return ~a; } }; -template -struct ExpUnOp +template +struct Array { - typedef typename UnOp::returnType returnType; + typedef T returnType; - ExpUnOp(const T& arg):_arg(arg) {} + Array(const T* t):_t(t) {} - returnType operator()(size_t index) const - { - return UnOp::apply(_arg[index]); - } + const T* _t; - const T& _arg; + returnType operator[](size_t index) const { return _t[index]; } }; -// Partial specialization for pointers -template -struct ExpUnOp -{ - typedef typename UnOp::returnType returnType; - - ExpUnOp(const T* arg):_arg(arg) {} - - returnType operator()(size_t index) const - { - return UnOp::apply(_arg[index]); - } - - const T* _arg; -}; - -template -Expr > > arrayRef(T (&t)[N]) -{ - typedef ExpUnOp > ExpUnOpT; - return Expr(ExpUnOpT(t)); -} - template -Expr > > array(T* a) -{ - typedef ExpUnOp > ExpUnOpT; - return Expr(ExpUnOpT(a)); -} +Array +array(const T* t) { return Array(t); } -template -Expr, Negate > > -operator~(const Expr& arg) +template +BinExpr, R> +operator&(const L& l, const R& r) { - typedef ExpUnOp, Negate > ExpUnOpT; - return Expr(ExpUnOpT(arg)); + return BinExpr, R>(l, r); } -template -Expr, Expr, And > > -operator&(const Expr& lhs, const Expr& rhs) +template +UnExpr, A> +operator~(const A& a) { - typedef ExpBinOp, Expr, And > ExpBinOpT; - return Expr(ExpBinOpT(lhs, rhs)); + return UnExpr, A>(a); } } // namespace expr -- cgit