/////////////////////////////////////////////////////////////////////////////// /// \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 BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005 #define BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT # include #endif #ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT # include #endif #ifdef BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT # include #endif /// INTERNAL ONLY /// #define BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED #ifdef 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 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(Name, Op) \ namespace functional \ { \ template \ struct result_of_ ## Name \ { \ BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \ nested \ , Op boost::numeric::functional::detail::lvalue_of() \ ) \ typedef typename nested::type type; \ }; \ template \ struct Name ## _base \ : std::unary_function< \ typename remove_const::type \ , typename result_of_ ## Name::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 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(Name, Op, RetType) \ namespace functional \ { \ template \ struct result_of_ ## Name \ { \ RetType(Left, Op, Right) \ }; \ template \ struct Name ## _base \ : std::binary_function< \ typename remove_const::type \ , typename remove_const::type \ , typename result_of_ ## Name::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; \ } \ /**/ /// INTERNAL ONLY /// #define BOOST_NUMERIC_FUNCTIONAL_DEDUCED(Left, Op, Right) \ 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 BOOST_NUMERIC_FUNCTIONAL_LEFT(Left, Op, Right) \ typedef Left &type; \ /**/ BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus, +, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus, -, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies, *, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides, /, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus, %, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater, >, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater_equal, >=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less, <, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less_equal, <=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(equal_to, ==, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(not_equal_to, !=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(assign, =, BOOST_NUMERIC_FUNCTIONAL_LEFT) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus_assign, +=, BOOST_NUMERIC_FUNCTIONAL_LEFT) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus_assign, -=, BOOST_NUMERIC_FUNCTIONAL_LEFT) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies_assign, *=, BOOST_NUMERIC_FUNCTIONAL_LEFT) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides_assign, /=, BOOST_NUMERIC_FUNCTIONAL_LEFT) BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus_assign, %=, BOOST_NUMERIC_FUNCTIONAL_LEFT) BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_plus, +) BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_minus, -) BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(complement, ~) BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(logical_not, !) #undef BOOST_NUMERIC_FUNCTIONAL_LEFT #undef BOOST_NUMERIC_FUNCTIONAL_DEDUCED #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP namespace functional { template struct min_assign_base : std::binary_function { void operator ()(Left &left, Right &right) const { if(numeric::less(right, left)) { left = right; } } }; template struct max_assign_base : std::binary_function { void operator ()(Left &left, Right &right) const { if(numeric::greater(right, left)) { left = right; } } }; template struct average_base : functional::divides {}; // partial specialization that promotes the arguments to double for // integral division. template struct average_base >::type> : functional::divides {}; template struct promote_base : std::unary_function { To operator ()(From &from) const { return from; } }; template struct promote_base : std::unary_function { ToFrom &operator ()(ToFrom &tofrom) { return tofrom; } }; template struct as_min_base : std::unary_function::type> { BOOST_STATIC_ASSERT(std::numeric_limits::type>::is_specialized); typename remove_const::type operator ()(Arg &) const { return (std::numeric_limits::type>::min)(); } }; template struct as_min_base >::type> : std::unary_function::type> { BOOST_STATIC_ASSERT(std::numeric_limits::type>::is_specialized); typename remove_const::type operator ()(Arg &) const { return -(std::numeric_limits::type>::max)(); } }; template struct as_max_base : std::unary_function::type> { BOOST_STATIC_ASSERT(std::numeric_limits::type>::is_specialized); typename remove_const::type operator ()(Arg &) const { return (std::numeric_limits::type>::max)(); } }; template struct as_zero_base : std::unary_function::type> { typename remove_const::type operator ()(Arg &) const { return numeric::zero::type>::value; } }; template struct as_one_base : std::unary_function::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 average : average_base {}; 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 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::average const &average = boost::detail::pod_singleton::instance; 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; } /////////////////////////////////////////////////////////////////////////////// // 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