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 \
102 : std::unary_function< \
103 typename remove_const<Arg>::type \
104 , typename result_of_ ## Name<Arg>::type \
107 typename result_of_ ## Name<Arg>::type operator ()(Arg &arg) const \
112 template<typename Arg, typename ArgTag> \
114 : Name ## _base<Arg, void> \
120 : boost::detail::function1<functional::Name<_, functional::tag<_> > > \
125 op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
131 #define BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(Name, Op, RetType) \
132 namespace functional \
134 template<typename Left, typename Right, typename EnableIf> \
135 struct result_of_ ## Name \
137 RetType(Left, Op, Right) \
139 template<typename Left, typename Right, typename EnableIf> \
140 struct Name ## _base \
141 : std::binary_function< \
142 typename remove_const<Left>::type \
143 , typename remove_const<Right>::type \
144 , typename result_of_ ## Name<Left, Right>::type \
147 typename result_of_ ## Name<Left, Right>::type \
148 operator ()(Left &left, Right &right) const \
150 return left Op right; \
153 template<typename Left, typename Right, typename LeftTag, typename RightTag> \
155 : Name ## _base<Left, Right, void> \
161 : boost::detail::function2< \
162 functional::Name<_1, _2, functional::tag<_1>, functional::tag<_2> > \
168 op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
170 BOOST_ACCUMULATORS_IGNORE_GLOBAL(Name) \
175 #define BOOST_NUMERIC_FUNCTIONAL_DEDUCED(Left, Op, Right) \
176 BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
178 , boost::numeric::functional::detail::lvalue_of<Left>() Op \
179 boost::numeric::functional::detail::lvalue_of<Right>() \
181 typedef typename nested::type type; \
186 #define BOOST_NUMERIC_FUNCTIONAL_LEFT(Left, Op, Right) \
187 typedef Left &type; \
190 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus, +, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
191 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus, -, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
192 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies, *, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
193 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides, /, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
194 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus, %, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
195 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater, >, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
196 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater_equal, >=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
197 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less, <, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
198 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less_equal, <=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
199 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(equal_to, ==, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
200 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(not_equal_to, !=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
202 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(assign, =, BOOST_NUMERIC_FUNCTIONAL_LEFT)
203 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus_assign, +=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
204 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus_assign, -=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
205 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies_assign, *=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
206 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides_assign, /=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
207 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus_assign, %=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
209 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_plus, +)
210 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_minus, -)
211 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(complement, ~)
212 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(logical_not, !)
214 #undef BOOST_NUMERIC_FUNCTIONAL_LEFT
215 #undef BOOST_NUMERIC_FUNCTIONAL_DEDUCED
216 #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP
217 #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP
221 template<typename Left, typename Right, typename EnableIf>
222 struct min_assign_base
223 : std::binary_function<Left, Right, void>
225 void operator ()(Left &left, Right &right) const
227 if(numeric::less(right, left))
234 template<typename Left, typename Right, typename EnableIf>
235 struct max_assign_base
236 : std::binary_function<Left, Right, void>
238 void operator ()(Left &left, Right &right) const
240 if(numeric::greater(right, left))
247 template<typename Left, typename Right, typename EnableIf>
249 : functional::divides<Left, Right>
252 // partial specialization that promotes the arguments to double for
253 // integral division.
254 template<typename Left, typename Right>
255 struct fdiv_base<Left, Right, typename enable_if<are_integral<Left, Right> >::type>
256 : functional::divides<double const, double const>
259 template<typename To, typename From, typename EnableIf>
261 : std::unary_function<From, To>
263 To operator ()(From &from) const
269 template<typename ToFrom>
270 struct promote_base<ToFrom, ToFrom, void>
271 : std::unary_function<ToFrom, ToFrom>
273 ToFrom &operator ()(ToFrom &tofrom)
279 template<typename Arg, typename EnableIf>
281 : std::unary_function<Arg, typename remove_const<Arg>::type>
283 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
285 typename remove_const<Arg>::type operator ()(Arg &) const
287 return (std::numeric_limits<typename remove_const<Arg>::type>::min)();
291 template<typename Arg>
292 struct as_min_base<Arg, typename enable_if<is_floating_point<Arg> >::type>
293 : std::unary_function<Arg, typename remove_const<Arg>::type>
295 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
297 typename remove_const<Arg>::type operator ()(Arg &) const
299 return -(std::numeric_limits<typename remove_const<Arg>::type>::max)();
303 template<typename Arg, typename EnableIf>
305 : std::unary_function<Arg, typename remove_const<Arg>::type>
307 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
309 typename remove_const<Arg>::type operator ()(Arg &) const
311 return (std::numeric_limits<typename remove_const<Arg>::type>::max)();
315 template<typename Arg, typename EnableIf>
317 : std::unary_function<Arg, typename remove_const<Arg>::type>
319 typename remove_const<Arg>::type operator ()(Arg &) const
321 return numeric::zero<typename remove_const<Arg>::type>::value;
325 template<typename Arg, typename EnableIf>
327 : std::unary_function<Arg, typename remove_const<Arg>::type>
329 typename remove_const<Arg>::type operator ()(Arg &) const
331 return numeric::one<typename remove_const<Arg>::type>::value;
335 template<typename To, typename From, typename ToTag, typename FromTag>
337 : promote_base<To, From, void>
340 template<typename Left, typename Right, typename LeftTag, typename RightTag>
342 : min_assign_base<Left, Right, void>
345 template<typename Left, typename Right, typename LeftTag, typename RightTag>
347 : max_assign_base<Left, Right, void>
350 template<typename Left, typename Right, typename LeftTag, typename RightTag>
352 : fdiv_base<Left, Right, void>
356 /// For back-compat only. Use fdiv.
357 template<typename Left, typename Right, typename LeftTag, typename RightTag>
359 : fdiv<Left, Right, LeftTag, RightTag>
362 template<typename Arg, typename Tag>
364 : as_min_base<Arg, void>
367 template<typename Arg, typename Tag>
369 : as_max_base<Arg, void>
372 template<typename Arg, typename Tag>
374 : as_zero_base<Arg, void>
377 template<typename Arg, typename Tag>
379 : as_one_base<Arg, void>
385 template<typename To>
387 : boost::detail::function1<functional::promote<To, _, typename functional::tag<To>::type, functional::tag<_> > >
391 : boost::detail::function2<functional::min_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
395 : boost::detail::function2<functional::max_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
399 : boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
404 : boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
408 : boost::detail::function1<functional::as_min<_, functional::tag<_> > >
412 : boost::detail::function1<functional::as_max<_, functional::tag<_> > >
416 : boost::detail::function1<functional::as_zero<_, functional::tag<_> > >
420 : boost::detail::function1<functional::as_one<_, functional::tag<_> > >
426 op::min_assign const &min_assign = boost::detail::pod_singleton<op::min_assign>::instance;
427 op::max_assign const &max_assign = boost::detail::pod_singleton<op::max_assign>::instance;
428 op::fdiv const &fdiv = boost::detail::pod_singleton<op::fdiv>::instance;
429 op::fdiv const &average = boost::detail::pod_singleton<op::fdiv>::instance; ///< INTERNAL ONLY
430 op::as_min const &as_min = boost::detail::pod_singleton<op::as_min>::instance;
431 op::as_max const &as_max = boost::detail::pod_singleton<op::as_max>::instance;
432 op::as_zero const &as_zero = boost::detail::pod_singleton<op::as_zero>::instance;
433 op::as_one const &as_one = boost::detail::pod_singleton<op::as_one>::instance;
435 BOOST_ACCUMULATORS_IGNORE_GLOBAL(min_assign)
436 BOOST_ACCUMULATORS_IGNORE_GLOBAL(max_assign)
437 BOOST_ACCUMULATORS_IGNORE_GLOBAL(fdiv)
438 BOOST_ACCUMULATORS_IGNORE_GLOBAL(average)
439 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_min)
440 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_max)
441 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_zero)
442 BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_one)
445 ///////////////////////////////////////////////////////////////////////////////
447 template<typename To, typename From>
448 typename lazy_disable_if<is_const<From>, mpl::if_<is_same<To, From>, To &, To> >::type
451 return functional::promote<To, From>()(from);
454 template<typename To, typename From>
455 typename mpl::if_<is_same<To const, From const>, To const &, To const>::type
456 promote(From const &from)
458 return functional::promote<To const, From const>()(from);
464 typedef default_ type;
465 typedef T value_type;
466 static T const value;
468 operator T const & () const
470 return default_::value;
475 T const default_<T>::value = T();
481 typedef T value_type;
482 static T const value;
484 operator T const & () const
491 T const one<T>::value = T(1);
497 typedef T value_type;
498 static T const value;
500 operator T const & () const
507 T const zero<T>::value = T();
510 struct one_or_default
511 : mpl::if_<is_empty<T>, default_<T>, one<T> >::type
515 struct zero_or_default
516 : mpl::if_<is_empty<T>, default_<T>, zero<T> >::type
519 }} // namespace boost::numeric