⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fnoperators.h

📁 精确的函数表达模板,里面包含了许多C和C++的函数表达模板
💻 H
📖 第 1 页 / 共 4 页
字号:

/****************************************************************************
 * Copyright (C) 2001        Piece Wise Functions
 *                           Carlo Galotto
 *                           Italy
 *                           Email: carlo.galotto@libero.it
 *                                  cgalotto@hotmail.com
 *
 *
 * This framework is free  software; you can  redistribute it and/or modify
 * it. This framework is distributed  in the  hope  that  it  will  be useful,
 * but WITHOUT ANY WARRANTY;  without even the   implied  warranty of
 * MERCHANTABILITY  or FITNESS FOR A PARTICULAR PURPOSE.
 * The proposed framework is based upon the paper "Expression Templates" by
 * Todd Veldhuizen.
 ****************************************************************************/

#ifndef fn_operators_h
#define fn_operators_h

#include <math.h>


   // resT = (arg1T + arg2T)
   template <class arg1T, class arg2T, class resT>   class Add {      public:         typedef resT result_type;
      public:         Add() { }         static inline resT apply(const arg1T& a, const arg2T& b) { return a+b; }   };   // resT = (arg1T - arg2T)   template <class arg1T, class arg2T, class resT>   class Minus {      public:         typedef resT result_type;
      public:         Minus() { }         static inline resT apply(const arg1T& a, const arg2T& b) { return a-b; }   };   // resT = -(arg1T)   template <class argT, class resT>   class Negate {      public:         typedef resT result_type;
      public:         Negate() { }         static inline resT apply(const argT& a) { return -a; }   };   // resT = (arg1T / arg2T)   template <class arg1T, class arg2T, class resT>   class Divide {      public:         typedef resT result_type;
      public:         Divide() { }         static inline resT apply(const arg1T& a, const arg2T& b) { return a/b; }   };   // resT = (arg1T * arg2T)   template <class arg1T, class arg2T, class resT>   class Multiply {      public:         typedef resT result_type;
      public:         Multiply() { }         static inline resT apply(const arg1T& a, const arg2T& b) { return a*b; }   };   // double = arg1T ^ arg2T
   template <class arg1T, class arg2T, class resT>   class Power {      public:         typedef resT result_type;
      public:         Power() { }         static inline resT apply(const arg1T& a, const arg2T& b) { return pow(a, b); }   };   // resT = arg1T < arg2T   template <class arg1T, class arg2T, class resT>   class Less {      public:         typedef resT result_type;
      public:         Less() { ; }         static inline resT apply(const arg1T& a, const arg2T& b) { return a < b; }   };
   // resT = arg1T < arg2T
   template <class arg1T, class arg2T, class resT>   class LessEq {      public:         typedef resT result_type;
      public:         LessEq() { ; }         static inline resT apply(const arg1T& a, const arg2T& b) { return a <= b; }   };
   // resT = !(arg1T)
   template <class argT, class resT>   class Not {      public:         typedef resT result_type;
      public:         Not() { }         static inline resT apply(const argT& a) { return !a; }   };   // resT = arg1T == arg2T
   template <class arg1T, class arg2T, class resT>   class Equal {      public:         typedef resT result_type;
      public:         Equal() { ; }         static inline resT apply(const arg1T& a, const arg2T& b) { return a == b; }   };

   // resT = arg1T != arg2T
   template <class arg1T, class arg2T, class resT>   class NotEqual {      public:         typedef resT result_type;
      public:         NotEqual() { ; }         static inline resT apply(const arg1T& a, const arg2T& b) { return a != b; }   };

   // bool = arg1T && arg2T
   template <class arg1T, class arg2T, class resT>   class And {      public:         typedef resT result_type;
      public:         And() { ; }         static inline resT apply(const arg1T& a, const arg2T& b) { return a && b; }   };   // bool = arg1T || arg2T   template <class arg1T, class arg2T, class resT>   class Or {      public:         typedef resT result_type;
      public:         Or() { ; }         static inline resT apply(const arg1T& a, const arg2T& b) { return a || b; }   };   /////////////////////////////////////////////////////////////////////////////

#include "FnTraits.h"#include "Expr.h"#include "BinExpr.h"#include "UnaryExpr.h"#include "LiteralExpr.h"#include "IdentityExpr.h"   /////////////////////////////////////////////////////////////////////////////   // operator+(Expr, Expr)
   template<class A, class B>   Expr<BinExpr< Expr<A>                                    // Arg1               , Expr<B>                                    // Arg2               , Add< typename Expr<A>::result_type         // Oper                    , typename Expr<B>::result_type                    , typename Promote< typename Expr<A>::result_type                                      , typename Expr<B>::result_type>::promoted_t> > >   operator+(const Expr<A>& a, const Expr<B>& b)   {      typedef typename Expr<A>::result_type arg1_t;      typedef typename Expr<B>::result_type arg2_t;      typedef typename Promote<arg1_t, arg2_t>::promoted_t res_t;      typedef typename BinExpr<Expr<A>, Expr<B>, Add<arg1_t, arg2_t, res_t> > binExprT;      return Expr<binExprT>(binExprT(a,b));   }   // operator+(double, Expr)   template<class B>   Expr<BinExpr< LiteralExpr<double>                        // Arg1               , Expr<B>                                    // Arg2               , Add< double                                // Oper                    , typename Expr<B>::result_type                    , typename Promote< double                                      , typename Expr<B>::result_type>::promoted_t> > >   operator+(const double& l, const Expr<B>& b)   {      typedef double   arg1_t;      typedef typename Expr<B>::result_type arg2_t;      typedef typename Promote<arg1_t, arg2_t>::promoted_t res_t;      typedef typename BinExpr<LiteralExpr<arg1_t>, Expr<B>, Add<arg1_t, arg2_t, res_t> >binExprT;      return Expr<binExprT>(binExprT(LiteralExpr<arg1_t>(l),b));   }   // operator+(Expr, double)   template<class A>   Expr<BinExpr< Expr<A>                                   // Arg1               , LiteralExpr<double>                       // Arg2               , Add< typename Expr<A>::result_type        // Oper                    , double                    , typename Promote< typename Expr<A>::result_type                                       , double>::promoted_t> > >   operator+(const Expr<A>& a, const double& l)   {      typedef double   arg2_t;      typedef typename Expr<A>::result_type arg1_t;      typedef typename Promote<arg1_t, arg2_t>::promoted_t res_t;      typedef typename BinExpr<Expr<A>, LiteralExpr<arg2_t>, Add<arg1_t, arg2_t, res_t> > binExprT;      return Expr<binExprT>(binExprT(a, LiteralExpr<arg2_t>(l)));   }   // operator+(int, Expr)   template<class B>   Expr<BinExpr< LiteralExpr<int>                           // Arg1               , Expr<B>                                    // Arg2               , Add< int                                   // Oper                    , typename Expr<B>::result_type                    , typename Promote< int                                      , typename Expr<B>::result_type>::promoted_t> > >   operator+(const int& l, const Expr<B>& b)   {      typedef int      arg1_t;      typedef typename Expr<B>::result_type arg2_t;      typedef typename Promote<arg1_t, arg2_t>::promoted_t res_t;      typedef typename BinExpr<LiteralExpr<arg1_t>, Expr<B>, Add<arg1_t, arg2_t, res_t> >binExprT;      return Expr<binExprT>(binExprT(LiteralExpr<arg1_t>(l),b));   }   // operator+(Expr, int)   template<class A>   Expr<BinExpr< Expr<A>                                   // Arg1               , LiteralExpr<int>                          // Arg2               , Add< typename Expr<A>::result_type        // Oper                    , int                    , typename Promote< typename Expr<A>::result_type                                      , int>::promoted_t> > >   operator+(const Expr<A>& a, const int& l)   {      typedef int      arg2_t;      typedef typename Expr<A>::result_type arg1_t;      typedef typename Promote<arg1_t, arg2_t>::promoted_t res_t;      typedef typename BinExpr<Expr<A>, LiteralExpr<arg2_t>, Add<arg1_t, arg2_t, res_t> > binExprT;      return Expr<binExprT>(binExprT(a, LiteralExpr<arg2_t>(l)));   }   /////////////////////////////////////////////////////////////////////////////   // operator-(Expr, Expr)   template<class A, class B>   Expr<BinExpr< Expr<A>                                   // Arg1               , Expr<B>                                   // Arg2               , Minus< typename Expr<A>::result_type      // Oper                         , typename Expr<B>::result_type                         , typename Promote< typename Expr<A>::result_type                                           , typename Expr<B>::result_type>::promoted_t> > >   operator-(const Expr<A>& a, const Expr<B>& b)   {      typedef typename Expr<A>::result_type arg1_t;      typedef typename Expr<B>::result_type arg2_t;      typedef typename Promote<arg1_t, arg2_t>::promoted_t res_t;      typedef typename BinExpr<Expr<A>, Expr<B>, Minus<arg1_t, arg2_t, res_t> > binExprT;      return Expr<binExprT>(binExprT(a,b));   }   // operator-(int, Expr)   template<class B>   Expr<BinExpr< LiteralExpr<int>                       // Arg1               , Expr<B>                                // Arg2               , Minus< int                             // Oper                         , typename Expr<B>::result_type                         , typename Promote< int                                           , typename Expr<B>::result_type>::promoted_t> > >   operator-(const int& l, const Expr<B>& b)   {      typedef int      arg1_t;      typedef typename Expr<B>::result_type arg2_t;      typedef typename Promote<arg1_t, arg2_t>::promoted_t res_t;      typedef typename BinExpr<LiteralExpr<arg1_t>, Expr<B>, Minus<arg1_t, arg2_t, res_t> >binExprT;      return Expr<binExprT>(binExprT(LiteralExpr<arg1_t>(l),b));   }   // operator-(Expr, int)   template<class A>   Expr<BinExpr< Expr<A>                                   // Arg1               , LiteralExpr<int>                          // Arg2

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -