SourceForge Logo Tiny Vector Matrix library using Expression Templates Sourceforge Project Page

include/tvmet/BinaryFunctionals.h

Go to the documentation of this file.
00001 /*
00002  * Tiny Vector Matrix Library
00003  * Dense Vector Matrix Libary of Tiny size using Expression Templates
00004  *
00005  * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * $Id: BinaryFunctionals.h,v 1.24 2007-06-23 15:58:58 opetzold Exp $
00022  */
00023 
00024 #ifndef TVMET_BINARY_FUNCTIONAL_H
00025 #define TVMET_BINARY_FUNCTIONAL_H
00026 
00027 namespace tvmet {
00028 
00029 
00037 template <class T1, class T2>
00038 struct Fcnl_assign : public BinaryFunctional {
00039   static inline
00040   void apply_on(T1& _tvmet_restrict lhs, T2 rhs) {
00041     lhs = static_cast<T1>(rhs);
00042   }
00043 
00044   static
00045   void print_xpr(std::ostream& os, std::size_t l=0) {
00046     os << IndentLevel(l) << "fcnl_assign<T1="
00047        << typeid(T1).name() << ", T2=" << typeid(T2).name() << ">,"
00048        << std::endl;
00049   }
00050 };
00051 
00052 
00063 #define TVMET_IMPLEMENT_MACRO(NAME, OP)         \
00064 template <class T1, class T2>           \
00065 struct Fcnl_##NAME : public BinaryFunctional {        \
00066   typedef void            value_type; \
00067                   \
00068   static inline               \
00069   void apply_on(T1& _tvmet_restrict lhs, T2 rhs) {      \
00070     lhs OP rhs;               \
00071   }                 \
00072                   \
00073   static                \
00074   void print_xpr(std::ostream& os, std::size_t l=0) {     \
00075     os << IndentLevel(l)            \
00076        << "Fcnl_" << #NAME << "<T1="          \
00077        << typeid(T1).name() << ", T2=" << typeid(T2).name() << ">," \
00078        << std::endl;              \
00079   }                 \
00080 };
00081 
00082 TVMET_IMPLEMENT_MACRO(add_eq, +=)
00083 TVMET_IMPLEMENT_MACRO(sub_eq, -=)
00084 TVMET_IMPLEMENT_MACRO(mul_eq, *=)
00085 TVMET_IMPLEMENT_MACRO(div_eq, /=)
00086 TVMET_IMPLEMENT_MACRO(mod_eq, %=)
00087 TVMET_IMPLEMENT_MACRO(xor_eq, ^=)
00088 TVMET_IMPLEMENT_MACRO(and_eq, &=)
00089 TVMET_IMPLEMENT_MACRO(or_eq, |=)
00090 TVMET_IMPLEMENT_MACRO(shl_eq, <<=)
00091 TVMET_IMPLEMENT_MACRO(shr_eq, >>=)
00092 
00093 #undef TVMET_IMPLEMENT_MACRO
00094 
00095 
00106 #define TVMET_IMPLEMENT_MACRO(NAME, OP)         \
00107 template <class T1, class T2>           \
00108 struct Fcnl_##NAME : public BinaryFunctional {        \
00109   typedef typename  PromoteTraits<T1, T2>::value_type value_type; \
00110                     \
00111   static inline               \
00112   value_type apply_on(T1 lhs, T2 rhs) {         \
00113     return lhs OP rhs;              \
00114   }                 \
00115                     \
00116   static                \
00117   void print_xpr(std::ostream& os, std::size_t l=0) {     \
00118     os << IndentLevel(l)            \
00119        << "Fcnl_" << #NAME << "<T1="          \
00120        << typeid(T1).name() << ", T2=" << typeid(T2).name() << ">," \
00121        << std::endl;              \
00122   }                 \
00123 };
00124 
00125 TVMET_IMPLEMENT_MACRO(add, +)
00126 TVMET_IMPLEMENT_MACRO(sub, -)
00127 TVMET_IMPLEMENT_MACRO(mul, *)
00128 TVMET_IMPLEMENT_MACRO(div, /)
00129 TVMET_IMPLEMENT_MACRO(mod, %)
00130 TVMET_IMPLEMENT_MACRO(bitxor, ^)
00131 TVMET_IMPLEMENT_MACRO(bitand, &)
00132 TVMET_IMPLEMENT_MACRO(bitor, |)
00133 TVMET_IMPLEMENT_MACRO(shl, <<)
00134 TVMET_IMPLEMENT_MACRO(shr, >>)
00135 
00136 #undef TVMET_IMPLEMENT_MACRO
00137 
00138 
00147 #define TVMET_IMPLEMENT_MACRO(NAME, OP)         \
00148 template <class T1, class T2>           \
00149 struct Fcnl_##NAME : public BinaryFunctional {        \
00150   typedef bool            value_type; \
00151                     \
00152   static inline               \
00153   bool apply_on(T1 lhs, T2 rhs) {         \
00154     return lhs OP rhs;              \
00155   }                 \
00156                     \
00157   static                \
00158   void print_xpr(std::ostream& os, std::size_t l=0) {     \
00159     os << IndentLevel(l)            \
00160        << "Fcnl_" << #NAME << "<T1="          \
00161        << typeid(T1).name() << ", T2=" << typeid(T2).name() << ">," \
00162        << std::endl;              \
00163   }                 \
00164 };
00165 
00166 TVMET_IMPLEMENT_MACRO(greater, >)
00167 TVMET_IMPLEMENT_MACRO(less, <)
00168 TVMET_IMPLEMENT_MACRO(greater_eq, >=)
00169 TVMET_IMPLEMENT_MACRO(less_eq, <=)
00170 TVMET_IMPLEMENT_MACRO(eq, ==)
00171 TVMET_IMPLEMENT_MACRO(not_eq, !=)
00172 TVMET_IMPLEMENT_MACRO(and, &&)
00173 TVMET_IMPLEMENT_MACRO(or, ||)
00174 
00175 #undef TVMET_IMPLEMENT_MACRO
00176 
00177 
00181 #define TVMET_IMPLEMENT_MACRO(NAME)         \
00182 template <class T1, class T2>           \
00183 struct Fcnl_##NAME : public BinaryFunctional {        \
00184   typedef typename PromoteTraits<T1, T2>::value_type  value_type; \
00185                   \
00186   static inline               \
00187   value_type apply_on(T1 lhs, T2 rhs) {         \
00188     return TVMET_STD_SCOPE(NAME)(lhs, rhs);       \
00189   }                 \
00190                     \
00191   static                \
00192   void print_xpr(std::ostream& os, std::size_t l=0) {     \
00193     os << IndentLevel(l)            \
00194        << "Fcnl_" << #NAME << "<T1="          \
00195        << typeid(T1).name() << ", T2=" << typeid(T2).name() << ">," \
00196        << std::endl;              \
00197   }                 \
00198 };
00199 
00200 TVMET_IMPLEMENT_MACRO(atan2)
00201 TVMET_IMPLEMENT_MACRO(fmod)
00202 TVMET_IMPLEMENT_MACRO(pow)
00203 
00204 #undef TVMET_IMPLEMENT_MACRO
00205 
00206 
00211 #define TVMET_IMPLEMENT_MACRO(NAME)         \
00212 template <class T1, class T2>           \
00213 struct Fcnl_##NAME : public BinaryFunctional {        \
00214   typedef typename PromoteTraits<T1, T2>::value_type  value_type; \
00215                   \
00216   static inline               \
00217   value_type apply_on(T1 lhs, T2 rhs) {         \
00218     return TVMET_GLOBAL_SCOPE(NAME)(lhs, rhs);        \
00219   }                 \
00220                     \
00221   static                \
00222   void print_xpr(std::ostream& os, std::size_t l=0) {     \
00223     os << IndentLevel(l)            \
00224        << "Fcnl_" << #NAME << "<T1="          \
00225        << typeid(T1).name() << ", T2=" << typeid(T2).name() << ">," \
00226        << std::endl;              \
00227   }                 \
00228 };
00229 
00230 TVMET_IMPLEMENT_MACRO(drem)
00231 TVMET_IMPLEMENT_MACRO(hypot)
00232 TVMET_IMPLEMENT_MACRO(jn)
00233 TVMET_IMPLEMENT_MACRO(yn)
00234 
00235 #undef TVMET_IMPLEMENT_MACRO
00236 
00237 
00238 #if defined(TVMET_HAVE_COMPLEX)
00239 
00243 template <class T1, class T2> struct Fcnl_polar : public BinaryFunctional { };
00244 
00245 
00253 template <class T>
00254 struct Fcnl_polar<T,T> : public BinaryFunctional {
00255   typedef std::complex<T>                               value_type;
00256 
00257   static inline
00258   value_type apply_on(T lhs, T rhs) {
00259     return std::polar(lhs, rhs);
00260   }
00261 
00262   static
00263   void print_xpr(std::ostream& os, std::size_t l=0) {
00264     os << IndentLevel(l) << "Fcnl_polar<T1="
00265        << typeid(T).name() << ", T2=" << typeid(T).name() << ">,"
00266        << std::endl;
00267   }
00268 };
00269 #endif // defined(TVMET_HAVE_COMPLEX)
00270 
00271 
00276 template <class T1, class T2>
00277 struct Fcnl_swap : public BinaryFunctional {
00278   static inline
00279   void apply_on(T1& _tvmet_restrict lhs, T2& _tvmet_restrict rhs) {
00280     typedef typename  PromoteTraits<T1, T2>::value_type temp_type;
00281 
00282     temp_type             temp(lhs);
00283     lhs = static_cast<T1>(rhs);
00284     rhs = static_cast<T2>(temp);
00285   }
00286 
00287   static
00288   void print_xpr(std::ostream& os, std::size_t l=0) {
00289     os << IndentLevel(l) << "Fcnl_swap<T1="
00290        << typeid(T1).name() << ", T2" << typeid(T2).name() << ">,"
00291        << std::endl;
00292   }
00293 };
00294 
00295 
00296 } // namespace tvmet
00297 
00298 #endif // TVMET_BINARY_FUNCTIONAL_H
00299 
00300 // Local Variables:
00301 // mode:C++
00302 // tab-width:8
00303 // End:

Author: