1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file functional.hpp
4 // Copyright 2005 Eric Niebler. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005
9 #define BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005
13 #include <boost/static_assert.hpp>
14 #include <boost/mpl/if.hpp>
15 #include <boost/mpl/and.hpp>
16 #include <boost/type_traits/remove_const.hpp>
17 #include <boost/type_traits/add_reference.hpp>
18 #include <boost/type_traits/is_empty.hpp>
19 #include <boost/type_traits/is_integral.hpp>
20 #include <boost/type_traits/is_floating_point.hpp>
21 #include <boost/utility/enable_if.hpp>
22 #include <boost/typeof/typeof.hpp>
23 #include <boost/accumulators/accumulators_fwd.hpp>
24 #include <boost/accumulators/numeric/functional_fwd.hpp>
25 #include <boost/accumulators/numeric/detail/function1.hpp>
26 #include <boost/accumulators/numeric/detail/function2.hpp>
27 #include <boost/accumulators/numeric/detail/pod_singleton.hpp>
29 #ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
30 # include <boost/accumulators/numeric/functional/vector.hpp>
33 #ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT
34 # include <boost/accumulators/numeric/functional/valarray.hpp>
37 #ifdef BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT
38 # include <boost/accumulators/numeric/functional/complex.hpp>
43 #define BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
45 #ifdef BOOST_NUMERIC_FUNCTIONAL_DOXYGEN_INVOKED
46 // Hack to make Doxygen show the inheritance relationships
53 template<class Arg, class Ret> struct unary_function {};
56 template<class Left, class Right, class Ret> struct binary_function {};
60 namespace boost { namespace numeric
66 template<typename A0, typename A1>
68 : mpl::and_<is_integral<A0>, is_integral<A1> >
71 template<typename Left, typename Right>
84 // TODO: handle complex weight, valarray, MTL vectors
88 #define BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(Name, Op) \
89 namespace functional \
91 template<typename Arg> \
92 struct result_of_ ## Name \
94 BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
96 , Op boost::numeric::functional::detail::lvalue_of<Arg>() \
98 typedef typename nested::type type; \
100 template<typename Arg, typename EnableIf> \
101 struct Name ## _base \
103 typedef typename remove_const<Arg>::type argument_type; \
104 typedef typename result_of_ ## Name<Arg>::type result_type; \
105 typename result_of_ ## Name<Arg>::type operator ()(Arg &arg) const \
110 template<typename Arg, typename ArgTag> \
112 : Name ## _base<Arg, void> \
118 : boost::detail::function1<functional::Name<_, functional::tag<_> > > \
123 op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
129 #define BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(Name, Op, RetType) \
130 namespace functional \
132 template<typename Left, typename Right, typename EnableIf> \
133 struct result_of_ ## Name \
135 RetType(Left, Op, Right) \
137 template<typename Left, typename Right, typename EnableIf> \
138 struct Name ## _base \
140 typedef typename remove_const<Left>::type first_argument_type; \
141 typedef typename remove_const<Right>::type second_argument_type; \
142 typedef typename result_of_ ## Name<Left, Right>::type result_type; \
143 typename result_of_ ## Name<Left, Right>::type \
144 operator ()(Left &left, Right &right) const \
146 return left Op right; \
149 template<typename Left, typename Right, typename LeftTag, typename RightTag> \
151 : Name ## _base<Left, Right, void> \
157 : boost::detail::function2< \
158 functional::Name<_1, _2, functional::tag<_1>, functional::tag<_2> > \
164 op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
166 BOOST_ACCUMULATORS_IGNORE_GLOBAL(Name) \
171 #define BOOST_NUMERIC_FUNCTIONAL_DEDUCED(Left, Op, Right) \
172 BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
174 , boost::numeric::functional::detail::lvalue_of<Left>() Op \
175 boost::numeric::functional::detail::lvalue_of<Right>() \
177 typedef typename nested::type type; \
182 #define BOOST_NUMERIC_FUNCTIONAL_LEFT(Left, Op, Right) \
183 typedef Left &type; \
186 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus, +, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
187 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus, -, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
188 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies, *, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
189 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides, /, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
190 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus, %, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
191 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater, >, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
192 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater_equal, >=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
193 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less, <, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
194 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less_equal, <=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
195 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(equal_to, ==, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
196 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(not_equal_to, !=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
198 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(assign, =, BOOST_NUMERIC_FUNCTIONAL_LEFT)
199 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus_assign, +=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
200 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus_assign, -=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
201 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies_assign, *=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
202 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides_assign, /=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
203 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus_assign, %=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
205 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_plus, +)
206 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_minus, -)
207 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(complement, ~)
208 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(logical_not, !)
210 #undef BOOST_NUMERIC_FUNCTIONAL_LEFT
211 #undef BOOST_NUMERIC_FUNCTIONAL_DEDUCED
212 #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP
213 #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP
217 template<typename Left, typename Right, typename EnableIf>
218 struct min_assign_base
220 typedef Left first_argument_type;
221 typedef Right second_argument_type;
222 typedef void result_type;
224 void operator ()(Left &left, Right &right) const
226 if(numeric::less(right, left))
233 template<typename Left, typename Right, typename EnableIf>
234 struct max_assign_base
236 typedef Left first_argument_type;
237 typedef Right second_argument_type;
238 typedef void result_type;
240 void operator ()(Left &left, Right &right) const
242 if(numeric::greater(right, left))
249 template<typename Left, typename Right, typename EnableIf>
251 : functional::divides<Left, Right>
254 // partial specialization that promotes the arguments to double for
255 // integral division.
256 template<typename Left, typename Right>
257 struct fdiv_base<Left, Right, typename enable_if<are_integral<Left, Right> >::type>
258 : functional::divides<double const, double const>
261 template<typename To, typename From, typename EnableIf>
264 typedef From argument_type;
265 typedef To result_type;
267 To operator ()(From &from) const
273 template<typename ToFrom>
274 struct promote_base<ToFrom, ToFrom, void>
276 typedef ToFrom argument_type;
277 typedef ToFrom result_type;
279 ToFrom &operator ()(ToFrom &tofrom)
285 template<typename Arg, typename EnableIf>
288 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
290 typedef Arg argument_type;
291 typedef typename remove_const<Arg>::type result_type;
293 typename remove_const<Arg>::type operator ()(Arg &) const
295 return (std::numeric_limits<typename remove_const<Arg>::type>::min)();
299 template<typename Arg>
300 struct as_min_base<Arg, typename enable_if<is_floating_point<Arg> >::type>
302 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
304 typedef Arg argument_type;
305 typedef typename remove_const<Arg>::type result_type;
307 typename remove_const<Arg>::type operator ()(Arg &) const
309 return -(std::numeric_limits<typename remove_const<Arg>::type>::max)();
313 template<typename Arg, typename EnableIf>
316 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
318 typedef Arg argument_type;
319 typedef typename remove_const<Arg>::type result_type;
321 typename remove_const<Arg>::type operator ()(Arg &) const
323 return (std::numeric_limits<typename remove_const<Arg>::type>::max)();
327 template<typename Arg, typename EnableIf>
330 typedef Arg argument_type;
331 typedef typename remove_const<Arg>::type result_type;
333 typename remove_const<Arg>::type operator ()(Arg &) const
335 return numeric::zero<typename remove_const<Arg>::type>::value;
339 template<typename Arg, typename EnableIf>
342 typedef Arg argument_type;
343 typedef typename remove_const<Arg>::type result_type;
345 typename remove_const<Arg>::type operator ()(Arg &) const
347 return numeric::one<typename remove_const<Arg>::type>::value;
351 template<typename To, typename From, typename ToTag, typename FromTag>
353 : promote_base<To, From, void>
356 template<typename Left, typename Right, typename LeftTag, typename RightTag>
358 : min_assign_base<Left, Right, void>
361 template<typename Left, typename Right, typename LeftTag, typename RightTag>
363 : max_assign_base<Left, Right, void>
366 template<typename Left, typename Right, typename LeftTag, typename RightTag>
368 : fdiv_base<Left, Right, void>
372 /// For back-compat only. Use fdiv.
373 template<typename Left, typename Right, typename LeftTag, typename RightTag>
375 : fdiv<Left, Right, LeftTag, RightTag>
378 template<typename Arg, typename Tag>
380 : as_min_base<Arg, void>
383 template<typename Arg, typename Tag>
385 : as_max_base<Arg, void>
388 template<typename Arg, typename Tag>
390 : as_zero_base<Arg, void>
393 template<typename Arg, typename Tag>
395 : as_one_base<Arg, void>
401 template<typename To>
403 : boost::detail::function1<functional::promote<To, _, typename functional::tag<To>::type, functional::tag<_> > >
407 : boost::detail::function2<functional::min_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
411 : boost::detail::function2<functional::max_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
415 : boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
420 : boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
424 : boost::detail::function1<functional::as_min<_, functional::tag<_> > >
428 : boost::detail::function1<functional::as_max<_, functional::tag<_> > >
432 : boost::detail::function1<functional::as_zero<_, functional::tag<_> > >
436 : boost::detail::function1<functional::as_one<_, functional::tag<_> > >
442 op::min_assign const &min_assign = boost::detail::pod_singleton<op::min_assign>::instance;
443 op::max_assign const &max_assign = boost::detail::pod_singleton<op::max_assign>::instance;
444 op::fdiv const &fdiv = boost::detail::pod_singleton<op::fdiv>::instance;
445 op::fdiv const &average = boost::detail::pod_singleton<op::fdiv>::instance; ///< INTERNAL ONLY
446 op::as_min const &as_min = boost::detail::pod_singleton<op::as_min>::instance;
447 op::as_max const &as_max = boost::detail::pod_singleton<op::as_max>::instance;
448 op::as_zero const &as_zero = boost::detail::pod_singleton<op::as_zero>::instance;
449 op::as_one const &as_one = boost::detail::pod_singleton<op::as_one>::instance;
451 BOOST_ACCUMULATORS_IGNORE_GLOBAL(min_assign)
452 BOOST_ACCUMULATORS_IGNORE_GLOBAL(max_assign)
453 BOOST_ACCUMULATORS_IGNORE_GLOBAL(fdiv)
454 BOOST_ACCUMULATORS_IGNORE_GLOBAL(average)
455 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_min)
456 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_max)
457 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_zero)
458 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_one)
461 ///////////////////////////////////////////////////////////////////////////////
463 template<typename To, typename From>
464 typename lazy_disable_if<is_const<From>, mpl::if_<is_same<To, From>, To &, To> >::type
467 return functional::promote<To, From>()(from);
470 template<typename To, typename From>
471 typename mpl::if_<is_same<To const, From const>, To const &, To const>::type
472 promote(From const &from)
474 return functional::promote<To const, From const>()(from);
480 typedef default_ type;
481 typedef T value_type;
482 static T const value;
484 operator T const & () const
486 return default_::value;
491 T const default_<T>::value = T();
497 typedef T value_type;
498 static T const value;
500 operator T const & () const
507 T const one<T>::value = T(1);
513 typedef T value_type;
514 static T const value;
516 operator T const & () const
523 T const zero<T>::value = T();
526 struct one_or_default
527 : mpl::if_<is_empty<T>, default_<T>, one<T> >::type
531 struct zero_or_default
532 : mpl::if_<is_empty<T>, default_<T>, zero<T> >::type
535 }} // namespace boost::numeric