aimstil  5.0.5
basicFunctors.h
Go to the documentation of this file.
1 #ifndef TIL_BASIC_FUNCTORS_H
2 #define TIL_BASIC_FUNCTORS_H
3 
4 // includes from STL
5 #include <cassert>
6 #include <limits>
7 
8 namespace til {
9 namespace functor {
10 
14 
15 template < typename T1, typename T2, typename T3 >
16 inline
17 typename boost::enable_if_c<
18  std::numeric_limits<T1>::is_specialized &&
19  std::numeric_limits<T2>::is_specialized
20 >::type
21 add(T1 x, T2 y, T3& z)
22 {
23  z = x + y;
24 }
25 
26 template < typename T1, typename T2, typename T3 >
27 inline
28 typename boost::enable_if_c<
29  std::numeric_limits<T1>::is_specialized &&
30  std::numeric_limits<T2>::is_specialized
31 >::type
32 sub(T1 x, T2 y, T3& z)
33 {
34  z = x - y;
35 }
36 
37 template < typename T1, typename T2, typename T3 >
38 inline
39 typename boost::enable_if_c<
40  std::numeric_limits<T1>::is_specialized &&
41  std::numeric_limits<T2>::is_specialized
42 >::type
43 mul(T1 x, T2 y, T3& z)
44 {
45  z = x * y;
46 }
47 
48 template < typename T1, typename T2, typename T3 >
49 inline
50 typename boost::enable_if_c<
51  std::numeric_limits<T1>::is_specialized &&
52  std::numeric_limits<T2>::is_specialized
53 >::type
54 div(T1 x, T2 y, T3& z)
55 {
56  assert(y != 0);
57  z = x / y;
58 }
59 /*
60 
61  template < typename T1 = None, typename T2 = None, typename T3 = typename combine<T1, T2>::type >
62  struct Add
63  {
64  private: // classes
65  struct FlagNone {};
66  struct FlagOther {};
67 
68  template < typename T > struct Flag { typedef FlagOther type; };
69  template <> struct Flag<None> { typedef FlagNone type; };
70 
71  public: // operators
72 
73  template < typename X1, typename X2 >
74  void operator() (X1 &x, const X2 & y)
75  {
76  dispatch(x, y, typename Flag<T1>::type(), typename Flag<T2>::type());
77  }
78 
79  private: // functions
80  template < typename X1, typename X2 >
81  void dispatch(X1 x, X2 y, FlagNone, FlagNone)
82  {
83  x += y;
84  }
85 
86  void dispatch(T1 x1, T2 x2, FlagOther, FlagOther)
87  {
88  x += y;
89  }
90 
91  //t T1 T2 add(T1 T2)
92 
93  template <typename _T1, typename T2>
94  inline void operator()(T1 &x, const T2 &y) { x += y; }
95 
96  template <typename T1, typename T2, typename T3>
97  inline void operator()(const T1 &x, const T2 &y, T3 & z) { add(x,y,z); }
98  };
99 */
100 
101  /*
102  struct Add
103  {
104  template <typename T1, typename T2>
105  inline void operator()(T1 &x, const T2 &y) { x += y; }
106 
107  template <typename T1, typename T2, typename T3>
108  inline void operator()(const T1 &x, const T2 &y, T3 & z) { add(x,y,z); }
109  };
110 
111  struct Sub
112  {
113  template <typename T1, typename T2>
114  inline void operator()(T1 &x, const T2 &y) { x -= y; }
115 
116  template <typename T1, typename T2, typename T3>
117  inline void operator()(const T1 &x, const T2 &y, T3 & z) { sub(x,y,z); }
118  };
119 
120  struct Div
121  {
122  template < typename T1, typename T2 >
123  inline void operator()(T1 & x, const T2 & y) { x /= y); }
124 
125  template < typename T1, typename T2, typename T3>
126  inline void operator()(const T1 &x, const T2 &y, T3 & z) { div(x,y,z); }
127  };
128 
129  // Actually it might be a better idea to use *=/ * operators in there rather
130  // than mul. Because these functors should be used at the numeric point,
131  // meaning these operators should be defined on these types.
132  // No. the problem stems from z = x*y which is a bad idea (constructor and copy
133  // called). So mul has to be called. And thus, for consistency, also for the
134  // *= operation.
135  // TODO: It just might be that we never need a binary form. Since ternary forms
136  // are inlined, it may see that two of the elements are the same and thus
137  // simplify the whole thing.
138  struct Mul
139  {
140  template < typename T1, typename T2 >
141  inline void operator()(T1 & x, const T2 & y) { x *= y; }
142 
143  template < typename T1, typename T2, typename T3 >
144  inline void operator()(const T1 & x, const T2 & y, T3 & z) { mul(x,y,z); }
145  };
146 
147  / *
148  template < typename T1 = void, typename T2 = T1 >
149  struct Sqrt
150  {
151 
152  //TODO: use a numeric_cast
153  template < typename LT1, typename LT2 >
154  inline void operator()(const LT1 & x, LT2 & y) { y = static_cast<LT2>(std::sqrt<double>(x)); }
155 
156  inline void operator()(const T1 & x, T2 & y) { y = static_cast<T2>(std::sqrt<double>(x)); }
157  };
158  */
159 
160 } // namespace functor
161 } // namespace til
162 
163 #endif
164 
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type sub(T1 x, T2 y, T3 &z)
Definition: basicFunctors.h:32
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type add(T1 x, T2 y, T3 &z)
The following functions are necessary for the coming functors.
Definition: basicFunctors.h:21
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type div(T1 x, T2 y, T3 &z)
Definition: basicFunctors.h:54
boost::enable_if_c< std::numeric_limits< T1 >::is_specialized &&std::numeric_limits< T2 >::is_specialized >::type mul(T1 x, T2 y, T3 &z)
Definition: basicFunctors.h:43