/////////////////////////////////////////////////////////////////////////////// /// \file functional.hpp /// // Copyright 2005 Eric Niebler. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef ACTIV_BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005 #define ACTIV_BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef ACTIV_BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT # include #endif #ifdef ACTIV_BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT # include #endif #ifdef ACTIV_BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT # include #endif /// INTERNAL ONLY /// #define ACTIV_BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED #ifdef ACTIV_BOOST_NUMERIC_FUNCTIONAL_DOXYGEN_INVOKED // Hack to make Doxygen show the inheritance relationships /// INTERNAL ONLY /// namespace std { /// INTERNAL ONLY /// template struct unary_function {}; /// INTERNAL ONLY /// template struct binary_function {}; } #endif namespace Boost { namespace numeric { namespace functional { /// INTERNAL ONLY /// template struct are_integral : mpl::and_, is_integral > {}; template struct left_ref { typedef Left &type; }; namespace detail { template T &lvalue_of(); } } // TODO: handle complex weight, valarray, MTL vectors /// INTERNAL ONLY /// #define ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(Name, Op) \ namespace functional \ { \ template \ struct result_of_ ## Name \ { \ ACTIV_BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \ nested \ , Op Boost::numeric::functional::detail::lvalue_of() \ ) \ typedef typename nested::type type; \ }; \ template \ struct Name ## _base \ { \ typedef typename remove_const::type argument_type; \ typedef typename result_of_ ## Name::type result_type; \ typename result_of_ ## Name::type operator ()(Arg &arg) const \ { \ return Op arg; \ } \ }; \ template \ struct Name \ : Name ## _base \ {}; \ } \ namespace op \ { \ struct Name \ : Boost::detail::function1 > > \ {}; \ } \ namespace \ { \ op::Name const &Name = Boost::detail::pod_singleton::instance; \ } \ /**/ /// INTERNAL ONLY /// #define ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(Name, Op, RetType) \ namespace functional \ { \ template \ struct result_of_ ## Name \ { \ RetType(Left, Op, Right) \ }; \ template \ struct Name ## _base \ { \ typedef typename remove_const::type first_argument_type; \ typedef typename remove_const::type second_argument_type; \ typedef typename result_of_ ## Name::type result_type; \ typename result_of_ ## Name::type \ operator ()(Left &left, Right &right) const \ { \ return left Op right; \ } \ }; \ template \ struct Name \ : Name ## _base \ {}; \ } \ namespace op \ { \ struct Name \ : Boost::detail::function2< \ functional::Name<_1, _2, functional::tag<_1>, functional::tag<_2> > \ > \ {}; \ } \ namespace \ { \ op::Name const &Name = Boost::detail::pod_singleton::instance; \ } \ ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(Name) \ /**/ /// INTERNAL ONLY /// #define ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED(Left, Op, Right) \ ACTIV_BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \ nested \ , Boost::numeric::functional::detail::lvalue_of() Op \ Boost::numeric::functional::detail::lvalue_of() \ ) \ typedef typename nested::type type; \ /**/ /// INTERNAL ONLY /// #define ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT(Left, Op, Right) \ typedef Left &type; \ /**/ ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus, +, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus, -, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies, *, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides, /, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus, %, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater, >, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater_equal, >=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less, <, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less_equal, <=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(equal_to, ==, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(not_equal_to, !=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(assign, =, ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus_assign, +=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus_assign, -=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies_assign, *=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides_assign, /=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus_assign, %=, ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_plus, +) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_minus, -) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(complement, ~) ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(logical_not, !) #undef ACTIV_BOOST_NUMERIC_FUNCTIONAL_LEFT #undef ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEDUCED #undef ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP #undef ACTIV_BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP namespace functional { template struct min_assign_base { typedef Left first_argument_type; typedef Right second_argument_type; typedef void result_type; void operator ()(Left &left, Right &right) const { if(numeric::less(right, left)) { left = right; } } }; template struct max_assign_base { typedef Left first_argument_type; typedef Right second_argument_type; typedef void result_type; void operator ()(Left &left, Right &right) const { if(numeric::greater(right, left)) { left = right; } } }; template struct fdiv_base : functional::divides {}; // partial specialization that promotes the arguments to double for // integral division. template struct fdiv_base >::type> : functional::divides {}; template struct promote_base { typedef From argument_type; typedef To result_type; To operator ()(From &from) const { return from; } }; template struct promote_base { typedef ToFrom argument_type; typedef ToFrom result_type; ToFrom &operator ()(ToFrom &tofrom) { return tofrom; } }; template struct as_min_base { ACTIV_BOOST_STATIC_ASSERT(std::numeric_limits::type>::is_specialized); typedef Arg argument_type; typedef typename remove_const::type result_type; typename remove_const::type operator ()(Arg &) const { return (std::numeric_limits::type>::min)(); } }; template struct as_min_base >::type> { ACTIV_BOOST_STATIC_ASSERT(std::numeric_limits::type>::is_specialized); typedef Arg argument_type; typedef typename remove_const::type result_type; typename remove_const::type operator ()(Arg &) const { return -(std::numeric_limits::type>::max)(); } }; template struct as_max_base { ACTIV_BOOST_STATIC_ASSERT(std::numeric_limits::type>::is_specialized); typedef Arg argument_type; typedef typename remove_const::type result_type; typename remove_const::type operator ()(Arg &) const { return (std::numeric_limits::type>::max)(); } }; template struct as_zero_base { typedef Arg argument_type; typedef typename remove_const::type result_type; typename remove_const::type operator ()(Arg &) const { return numeric::zero::type>::value; } }; template struct as_one_base { typedef Arg argument_type; typedef typename remove_const::type result_type; typename remove_const::type operator ()(Arg &) const { return numeric::one::type>::value; } }; template struct promote : promote_base {}; template struct min_assign : min_assign_base {}; template struct max_assign : max_assign_base {}; template struct fdiv : fdiv_base {}; /// INTERNAL ONLY /// For back-compat only. Use fdiv. template struct average : fdiv {}; template struct as_min : as_min_base {}; template struct as_max : as_max_base {}; template struct as_zero : as_zero_base {}; template struct as_one : as_one_base {}; } namespace op { template struct promote : Boost::detail::function1::type, functional::tag<_> > > {}; struct min_assign : Boost::detail::function2, functional::tag<_2> > > {}; struct max_assign : Boost::detail::function2, functional::tag<_2> > > {}; struct fdiv : Boost::detail::function2, functional::tag<_2> > > {}; /// INTERNAL ONLY struct average : Boost::detail::function2, functional::tag<_2> > > {}; struct as_min : Boost::detail::function1 > > {}; struct as_max : Boost::detail::function1 > > {}; struct as_zero : Boost::detail::function1 > > {}; struct as_one : Boost::detail::function1 > > {}; } namespace { op::min_assign const &min_assign = Boost::detail::pod_singleton::instance; op::max_assign const &max_assign = Boost::detail::pod_singleton::instance; op::fdiv const &fdiv = Boost::detail::pod_singleton::instance; op::fdiv const &average = Boost::detail::pod_singleton::instance; ///< INTERNAL ONLY op::as_min const &as_min = Boost::detail::pod_singleton::instance; op::as_max const &as_max = Boost::detail::pod_singleton::instance; op::as_zero const &as_zero = Boost::detail::pod_singleton::instance; op::as_one const &as_one = Boost::detail::pod_singleton::instance; ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(min_assign) ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(max_assign) ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(fdiv) ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(average) ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_min) ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_max) ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_zero) ACTIV_BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_one) } /////////////////////////////////////////////////////////////////////////////// // promote template typename lazy_disable_if, mpl::if_, To &, To> >::type promote(From &from) { return functional::promote()(from); } template typename mpl::if_, To const &, To const>::type promote(From const &from) { return functional::promote()(from); } template struct default_ { typedef default_ type; typedef T value_type; static T const value; operator T const & () const { return default_::value; } }; template T const default_::value = T(); template struct one { typedef one type; typedef T value_type; static T const value; operator T const & () const { return one::value; } }; template T const one::value = T(1); template struct zero { typedef zero type; typedef T value_type; static T const value; operator T const & () const { return zero::value; } }; template T const zero::value = T(); template struct one_or_default : mpl::if_, default_, one >::type {}; template struct zero_or_default : mpl::if_, default_, zero >::type {}; }} // namespace Boost::numeric #endif