ivl 679
ivl/details/core/math/array/binary_element_functions.hpp
Go to the documentation of this file.
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00033 #ifndef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP
00034 #define IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP
00035 
00036 // Macros are be used only because this file is full of repeated definitions.
00037 // they are undefined after the definitions are made. Therefore they are
00038 // temporary and they do not affect the developpers code who include ivl.
00039 #ifdef BD
00040 #define IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BD BD
00041 #undef BD
00042 #endif
00043 #ifdef BO
00044 #define IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO BO
00045 #undef BO
00046 #endif
00047 #ifdef BO_AR
00048 #define IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_AR BO_AR
00049 #undef BO_AR
00050 #endif
00051 #ifdef BO_LAR
00052 #define IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_LAR BO_LAR
00053 #undef BO_LAR
00054 #endif
00055 
00056 namespace ivl {
00057 
00064 #define BD(rtype, name, costval) \
00065  \
00066 template<class T, class J, class K> \
00067 struct name ## _class \
00068 { \
00069         typedef types::number<costval> cost; \
00070         typedef typename types::t_if<types::t_eq<rtype, types::term>, \
00071                 J, rtype>::type default_ret_type; \
00072         typedef typename types::t_if<types::t_eq<T, types::term>, \
00073                 default_ret_type, T>::type func_ret_type; \
00074         static inline func_ret_type elem_op(const J& elem1, const K& elem2) \
00075         { \
00076                 return math::name(elem1, elem2); \
00077         } \
00078 }; \
00079 \
00080 template <class A1, class A2> \
00081 inline \
00082 typename binary_elem_func_result< \
00083         typename name ## _class< \
00084                 types::term, \
00085                 typename types::get_value<A1>::type, \
00086                 typename types::get_value<A2>::type>::default_ret_type, \
00087                 name ## _class, A1, A2>::type name ( \
00088         const A1& a1, const A2& a2) \
00089 { \
00090         return binary_elem_func_result< \
00091         typename name ## _class< \
00092                 types::term, \
00093                 typename types::get_value<A1>::type, \
00094                 typename types::get_value<A2>::type>::default_ret_type, \
00095                 name ## _class, A1, A2>::from(a1, a2); \
00096 } \
00097 
00098 #define BO(oper, name) \
00099  \
00100 template <class A1, class A2> \
00101 inline \
00102 typename binary_elem_func_result< \
00103         typename name ## _class< \
00104                 types::term, \
00105                 typename types::get_value<A1>::type, \
00106                 typename types::get_value<A2>::type>::default_ret_type, \
00107                 name ## _class, A1, A2>::type operator oper ( \
00108         const A1& a1, const A2& a2) \
00109 { \
00110         return binary_elem_func_result< \
00111                 typename name ## _class< \
00112                 types::term, \
00113                 typename types::get_value<A1>::type, \
00114                 typename types::get_value<A2>::type>::default_ret_type, \
00115                 name ## _class, A1, A2>::from(a1, a2); \
00116 } \
00117 
00118 #define BO_AR(oper, name) \
00119  \
00120 template <class T, class S, class A2> \
00121 inline \
00122 typename binary_elem_func_result< \
00123         typename name ## _class< \
00124                 types::term, \
00125                 typename types::get_value<T>::type, \
00126                 typename types::get_value<A2>::type>::default_ret_type, \
00127                 name ## _class, array_base<T, S>, A2>::type operator oper ( \
00128         const array_base<T, S>& a1, const A2& a2) \
00129 { \
00130         return binary_elem_func_result< \
00131                 typename name ## _class< \
00132                 types::term, \
00133                 typename types::get_value<T>::type, \
00134                 typename types::get_value<A2>::type>::default_ret_type, \
00135                 name ## _class, array_base<T, S>, A2>::from(a1, a2); \
00136 } \
00137 \
00138 template <class A1, class T, class S> \
00139 inline \
00140 typename binary_elem_func_result< \
00141         typename name ## _class< \
00142                 types::term, \
00143                 typename types::get_value<A1>::type, \
00144                 typename types::get_value<T>::type>::default_ret_type, \
00145                 name ## _class, A1, array_base<T, S> >::type operator oper ( \
00146         const A1& a1, const array_base<T, S, \
00147                 types::t_not<types::is_ivl_array<A1> >::value >& a2) \
00148 { \
00149         return binary_elem_func_result< \
00150                 typename name ## _class< \
00151                 types::term, \
00152                 typename types::get_value<A1>::type, \
00153                 typename types::get_value<T>::type>::default_ret_type, \
00154                 name ## _class, A1, array_base<T, S> >::from(a1, a2); \
00155 }
00156 
00157 
00158 
00159 
00160 #define BO_LAR(oper, name) \
00161  \
00162 template <class T, class S, class A2> \
00163 inline \
00164 typename binary_elem_func_result< \
00165         typename name ## _class< \
00166                 types::term, \
00167                 typename types::get_value<T>::type, \
00168                 typename types::get_value<A2>::type>::default_ret_type, \
00169                 name ## _class, array<T, S>, A2>::type operator oper ( \
00170         const array<T, S>& a1, const A2& a2) \
00171 { \
00172         return binary_elem_func_result< \
00173                 typename name ## _class< \
00174                 types::term, \
00175                 typename types::get_value<T>::type, \
00176                 typename types::get_value<A2>::type>::default_ret_type, \
00177                 name ## _class, array<T, S>, A2>::from(a1, a2); \
00178 }
00179 
00180 // definitions for elem functions:
00181 // BD(return type, math::function name)
00182 // for the return type you can use the types
00183 // J, K: first, second element type accordingly
00184 
00185 // trigonometric
00186 BD(J, atan2, 150)
00187 
00188 // functions
00189 BD(J, mod, 50);
00190 
00191 // functions that correspond to operators
00192 BD(K, rem, 50);
00193 BO_AR(%, rem);
00194 BD(J, power, 150);
00195 BO_AR(^, power);
00196 BD(J, plus, 7);
00197 //BO_AR(+, plus);
00198 BD(J, minus, 7);
00199 //BO_AR(-, minus);
00200 BD(J, times, 12);
00201 BO_AR(*, times);
00202 
00203 //BD(K, rdivide, 40);
00204 //BO_AR(/, rdivide);
00205 BD(J, rdivide, 40);
00206 BO_AR(/, rdivide);
00207 
00208 // rel operators
00209 BD(bool, lt, 7);
00210 BO_AR(<, lt);
00211 BD(bool, le, 7);
00212 BO_AR(<=, le);
00213 BD(bool, gt, 7);
00214 BO_AR(>, gt);
00215 BD(bool, ge, 7);
00216 BO_AR(>=, ge);
00217 BD(bool, eq, 7);
00218 BO_AR(==, eq);
00219 BD(bool, ne, 7);
00220 BO_AR(!=, ne);
00221 
00222 // logical operators
00223 BD(bool, logical_and, 7)
00224 BO_AR(&&, logical_and)
00225 BD(bool, logical_or, 7)
00226 BO_AR(||, logical_or)
00227 
00228 // bit operators
00229 //todo:write as methods
00230 BD(J, cbitor, 7)
00231 //BO_AR(|, cbitor)
00232 BD(J, cbitand, 7)
00233 BO_AR(&, cbitand)
00234 BD(J, bitlshift, 12)
00235 BO_LAR(<<, bitlshift)
00236 BD(J, bitrshift, 12)
00237 BO_LAR(>>, bitrshift)
00238 
00239 
00240 /*
00241 template <class T, class S, class K, class A2>
00242 inline
00243 typename binary_elem_func_result<
00244         typename plus_class<
00245                 types::term,
00246                 typename types::get_value<T>::type,
00247                 typename types::get_value<A2>::type>::default_ret_type,
00248                 plus_class, array_base<T, S, K>, A2>::type operator + (
00249         const array_base<T, S, K>& a1, const A2& a2)
00250 {
00251         return binary_elem_func_result<
00252                 typename plus_class<
00253                 types::term,
00254                 typename types::get_value<T>::type,
00255                 typename types::get_value<A2>::type>::default_ret_type,
00256                 plus_class, array_base<T, S, K>, A2>::from(a1, a2);
00257 }
00258 
00259 template <class A1, class T, class S, class K>
00260 inline
00261 typename binary_elem_func_result<
00262         typename plus_class<
00263                 types::term,
00264                 typename types::get_value<A1>::type,
00265                 typename types::get_value<T>::type>::default_ret_type,
00266                 plus_class, A1, array_base<T, S, K> >::type operator + (
00267         const A1& a1, const array_base<T, S, K,
00268                 types::t_not<types::is_ivl_array<A1> >::value >& a2)
00269 {
00270         return binary_elem_func_result<
00271                 typename plus_class<
00272                 types::term,
00273                 typename types::get_value<A1>::type,
00274                 typename types::get_value<T>::type>::default_ret_type,
00275                 plus_class, A1, array_base<T, S, K> >::from(a1, a2);
00276 }
00277 */
00278 
00279 
00280 
00281 } /* namespace ivl */
00282 
00283 #undef BD
00284 #undef BO
00285 #undef BO_AR
00286 #undef BO_LAR
00287 
00288 #ifdef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BD
00289 #define BD IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BD
00290 #undef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BD
00291 #endif
00292 #ifdef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO
00293 #define BO IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO
00294 #undef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO
00295 #endif
00296 #ifdef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_AR
00297 #define BO_AR IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_AR
00298 #undef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_AR
00299 #endif
00300 #ifdef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_LAR
00301 #define BO_LAR IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_LAR
00302 #undef IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP_SAVE_BO_LAR
00303 #endif
00304 
00305 #endif // IVL_ARRAY_DETAILS_BINARY_ELEMENT_FUNCTIONS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations