Rheolef  7.1
an efficient C++ finite element environment
expr_utilities.h
Go to the documentation of this file.
1 #ifndef _RHEOLEF_EXPR_UTILITIES_H
2 #define _RHEOLEF_EXPR_UTILITIES_H
3 // utilities for expressions: used by vec<T,M> and field_basic<T,M>
24 //
25 // author: Pierre.Saramito@imag.fr
26 //
27 // date: 14 september 2015
28 //
29 #include <functional>
30 
31 namespace rheolef { namespace details {
32 
33 // -------------------------------------------
34 // operators as functors
35 // -------------------------------------------
36 
37 #define _RHEOLEF_generic_unary_syntax_functor(OP,NAME) \
38 template <typename T = void> \
39 struct NAME; \
40  \
41 template <typename T> \
42 struct NAME: public std::unary_function<T, T> { \
43  T operator() (const T& x) const { return OP x; } \
44 }; \
45  \
46 template<> \
47 struct NAME<void> { \
48  template <typename T> \
49  auto operator() (T&& x) const \
50  noexcept (noexcept (OP std::forward<T>(x))) \
51  -> decltype(OP std::forward<T>(x)) \
52  { return OP std::forward<T>(x); } \
53 };
54 
55 #define _RHEOLEF_generic_binary_syntax_functor(OP,NAME) \
56 template <typename T = void> \
57 struct NAME; \
58  \
59 template<typename T> \
60 struct NAME: public std::binary_function<T, T, T> { \
61  T operator() (const T& x, const T& y) const { return x OP y; } \
62 }; \
63  \
64 template<> \
65 struct NAME<void> { \
66  template <typename T, typename U> \
67  auto operator() (T&& t, U&& y) const \
68  noexcept (noexcept (std::forward<T>(t) OP std::forward<U>(y))) \
69  -> decltype(std::forward<T>(t) OP std::forward<U>(y)) \
70  { return std::forward<T>(t) OP std::forward<U>(y); } \
71 };
72 
73 _RHEOLEF_generic_unary_syntax_functor (+,generic_unary_plus)
77 _RHEOLEF_generic_binary_syntax_functor (*,generic_multiplies)
79 #undef _RHEOLEF_generic_unary_syntax_functor
80 #undef _RHEOLEF_generic_binary_syntax_functor
81 
82 template<class BinaryFunction, class A1>
83 struct generic_binder1st {
84  generic_binder1st (const BinaryFunction& f, const A1& x1)
85  : _f(f), _x1(x1) {}
86  template<class A2>
87  auto operator() (A2&& x2) const
88  // TODO: assume that both BinaryFunction and A1 have a default constructor...
89  noexcept (noexcept (BinaryFunction() (A1(), std::forward<A2>(x2))))
90  -> decltype(BinaryFunction() (A1(), std::forward<A2>(x2)))
91  { return _f (_x1, std::forward<A2>(x2)); }
92 protected:
94  A1 _x1;
95 };
96 
97 template<class BinaryFunction, class A2>
99  generic_binder2nd (const BinaryFunction& f, const A2& x2)
100  : _f(f), _x2(x2) {}
101  template<class A1>
102  auto operator() (A1&& x1) const
103  // TODO: assume that both BinaryFunction and A2 have a default constructor...
104  noexcept (noexcept (BinaryFunction() (std::forward<A1>(x1), A2())))
105  -> decltype(BinaryFunction() (std::forward<A1>(x1), A2()))
106  { return _f (std::forward<A1>(x1),_x2); }
107 protected:
109  A2 _x2;
110 };
111 // -------------------------------------------
112 // computed assignement as functors
113 // -------------------------------------------
114 // for = += -= *= /= etc
115 struct assign_op {
116  template<class T, class U>
117  void operator() (T &t, const U &u) const { t = u; }
118 };
119 struct plus_assign {
120  template<class T, class U>
121  void operator() (T &t, const U &u) const { t += u; }
122 };
123 struct minus_assign {
124  template<class T, class U>
125  void operator() (T &t, const U &u) const { t -= u; }
126 };
128  template<class T, class U>
129  void operator() (T &t, const U &u) const { t *= u; }
130 };
132  template<class T, class U>
133  void operator() (T &t, const U &u) const { t /= u; }
134 };
135 template<class ForwardIterator, class InputIterator, class OpAssign>
136 void
137 assign_with_operator (ForwardIterator first, ForwardIterator last, InputIterator iter_rhs, OpAssign op_assign) {
138  for (; first != last; ++first, ++iter_rhs) {
139  op_assign (*first, *iter_rhs);
140  }
141 }
142 // -------------------------------------------
143 // misc
144 // -------------------------------------------
145 // for convenince: a dummy forward iterator that returns a constant
146 template <class T>
148 
149  typedef T value_type;
150  iterator_on_constant (const T& c) : _c(c) {}
152  value_type operator* () const { return _c; }
153 protected:
154  T _c;
155 };
156 
157 }} // namespace rheolef::details
158 #endif // _RHEOLEF_EXPR_UTILITIES_H
rheolef::BinaryFunction
rheolef::std BinaryFunction
rheolef::details::multiplies_assign
Definition: expr_utilities.h:127
rheolef::details::iterator_on_constant::value_type
T value_type
Definition: expr_utilities.h:149
rheolef::details::assign_with_operator
void assign_with_operator(ForwardIterator first, ForwardIterator last, InputIterator iter_rhs, OpAssign op_assign)
Definition: expr_utilities.h:137
rheolef::details::generic_binder2nd::_f
BinaryFunction _f
Definition: expr_utilities.h:108
rheolef::details::generic_binder1st
Definition: expr_utilities.h:82
rheolef::details::assign_op
Definition: expr_utilities.h:115
rheolef::details::iterator_on_constant::operator++
iterator_on_constant< T > & operator++()
Definition: expr_utilities.h:151
mkgeo_ball.c
c
Definition: mkgeo_ball.sh:153
rheolef::details::multiplies_assign::operator()
void operator()(T &t, const U &u) const
Definition: expr_utilities.h:129
rheolef::details::divides_assign
Definition: expr_utilities.h:131
rheolef::details::minus_assign::operator()
void operator()(T &t, const U &u) const
Definition: expr_utilities.h:125
rheolef::details::iterator_on_constant::iterator_on_constant
iterator_on_constant(const T &c)
Definition: expr_utilities.h:150
rheolef::details::assign_op::operator()
void operator()(T &t, const U &u) const
Definition: expr_utilities.h:117
rheolef::details::plus_assign::operator()
void operator()(T &t, const U &u) const
Definition: expr_utilities.h:121
rheolef::details::_RHEOLEF_generic_binary_syntax_functor
generic_negate _RHEOLEF_generic_binary_syntax_functor(+, generic_plus) _RHEOLEF_generic_binary_syntax_functor(-
rheolef::details::generic_binder2nd::operator()
auto operator()(A1 &&x1) const noexcept(noexcept(BinaryFunction()(std::forward< A1 >(x1), A2()))) -> decltype(BinaryFunction()(std::forward< A1 >(x1), A2()))
Definition: expr_utilities.h:102
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
u
Definition: leveque.h:25
rheolef::details::divides_assign::operator()
void operator()(T &t, const U &u) const
Definition: expr_utilities.h:133
rheolef::details::generic_binder2nd
Definition: expr_utilities.h:98
rheolef::details::generic_binder2nd::generic_binder2nd
generic_binder2nd(const BinaryFunction &f, const A2 &x2)
Definition: expr_utilities.h:99
rheolef::details::minus_assign
Definition: expr_utilities.h:123
rheolef::details::iterator_on_constant::_c
T _c
Definition: expr_utilities.h:154
u
Float u(const point &x)
Definition: transmission_error.cc:26
rheolef::details::iterator_on_constant
Definition: expr_utilities.h:147
rheolef::details::iterator_on_constant::operator*
value_type operator*() const
Definition: expr_utilities.h:152
rheolef::details::generic_binder1st::_f
BinaryFunction _f
Definition: expr_utilities.h:93
f
Definition: cavity_dg.h:29
rheolef::details::plus_assign
Definition: expr_utilities.h:119
rheolef::details::generic_binder1st::_x1
A1 _x1
Definition: expr_utilities.h:94
rheolef::details::_RHEOLEF_generic_unary_syntax_functor
_RHEOLEF_generic_unary_syntax_functor(+, generic_unary_plus) _RHEOLEF_generic_unary_syntax_functor(-
rheolef::details::generic_binder2nd::_x2
A2 _x2
Definition: expr_utilities.h:109
T
Expr1::float_type T
Definition: field_expr.h:218