123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- ///////////////////////////////////////////////////////////////////////////////
- /// \file vector.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_VECTOR_HPP_EAN_12_12_2005
- #define BOOST_NUMERIC_FUNCTIONAL_VECTOR_HPP_EAN_12_12_2005
- #ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
- # error Include this file before boost/accumulators/numeric/functional.hpp
- #endif
- #include <vector>
- #include <functional>
- #include <boost/assert.hpp>
- #include <boost/mpl/and.hpp>
- #include <boost/mpl/not.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/type_traits/is_scalar.hpp>
- #include <boost/type_traits/remove_const.hpp>
- #include <boost/typeof/std/vector.hpp>
- #include <boost/accumulators/numeric/functional_fwd.hpp>
- namespace boost { namespace numeric
- {
- namespace operators
- {
- namespace acc_detail
- {
- template<typename Fun>
- struct make_vector
- {
- typedef std::vector<typename Fun::result_type> type;
- };
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle vector<Left> / Right where Right is a scalar.
- template<typename Left, typename Right>
- typename lazy_enable_if<
- is_scalar<Right>
- , acc_detail::make_vector<functional::divides<Left, Right> >
- >::type
- operator /(std::vector<Left> const &left, Right const &right)
- {
- typedef typename functional::divides<Left, Right>::result_type value_type;
- std::vector<value_type> result(left.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::divides(left[i], right);
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle vector<Left> / vector<Right>.
- template<typename Left, typename Right>
- std::vector<typename functional::divides<Left, Right>::result_type>
- operator /(std::vector<Left> const &left, std::vector<Right> const &right)
- {
- typedef typename functional::divides<Left, Right>::result_type value_type;
- std::vector<value_type> result(left.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::divides(left[i], right[i]);
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle vector<Left> * Right where Right is a scalar.
- template<typename Left, typename Right>
- typename lazy_enable_if<
- is_scalar<Right>
- , acc_detail::make_vector<functional::multiplies<Left, Right> >
- >::type
- operator *(std::vector<Left> const &left, Right const &right)
- {
- typedef typename functional::multiplies<Left, Right>::result_type value_type;
- std::vector<value_type> result(left.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::multiplies(left[i], right);
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle Left * vector<Right> where Left is a scalar.
- template<typename Left, typename Right>
- typename lazy_enable_if<
- is_scalar<Left>
- , acc_detail::make_vector<functional::multiplies<Left, Right> >
- >::type
- operator *(Left const &left, std::vector<Right> const &right)
- {
- typedef typename functional::multiplies<Left, Right>::result_type value_type;
- std::vector<value_type> result(right.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::multiplies(left, right[i]);
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle vector<Left> * vector<Right>
- template<typename Left, typename Right>
- std::vector<typename functional::multiplies<Left, Right>::result_type>
- operator *(std::vector<Left> const &left, std::vector<Right> const &right)
- {
- typedef typename functional::multiplies<Left, Right>::result_type value_type;
- std::vector<value_type> result(left.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::multiplies(left[i], right[i]);
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle vector<Left> + vector<Right>
- template<typename Left, typename Right>
- std::vector<typename functional::plus<Left, Right>::result_type>
- operator +(std::vector<Left> const &left, std::vector<Right> const &right)
- {
- typedef typename functional::plus<Left, Right>::result_type value_type;
- std::vector<value_type> result(left.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::plus(left[i], right[i]);
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle vector<Left> - vector<Right>
- template<typename Left, typename Right>
- std::vector<typename functional::minus<Left, Right>::result_type>
- operator -(std::vector<Left> const &left, std::vector<Right> const &right)
- {
- typedef typename functional::minus<Left, Right>::result_type value_type;
- std::vector<value_type> result(left.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::minus(left[i], right[i]);
- }
- return result;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle vector<Left> += vector<Left>
- template<typename Left>
- std::vector<Left> &
- operator +=(std::vector<Left> &left, std::vector<Left> const &right)
- {
- BOOST_ASSERT(left.size() == right.size());
- for(std::size_t i = 0, size = left.size(); i != size; ++i)
- {
- numeric::plus_assign(left[i], right[i]);
- }
- return left;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Handle -vector<Arg>
- template<typename Arg>
- std::vector<typename functional::unary_minus<Arg>::result_type>
- operator -(std::vector<Arg> const &arg)
- {
- typedef typename functional::unary_minus<Arg>::result_type value_type;
- std::vector<value_type> result(arg.size());
- for(std::size_t i = 0, size = result.size(); i != size; ++i)
- {
- result[i] = numeric::unary_minus(arg[i]);
- }
- return result;
- }
- }
- namespace functional
- {
- struct std_vector_tag;
- template<typename T, typename Al>
- struct tag<std::vector<T, Al> >
- {
- typedef std_vector_tag type;
- };
- ///////////////////////////////////////////////////////////////////////////////
- // element-wise min of std::vector
- template<typename Left, typename Right>
- struct min_assign<Left, Right, std_vector_tag, std_vector_tag>
- {
- typedef Left first_argument_type;
- typedef Right second_argument_type;
- typedef void result_type;
- void operator ()(Left &left, Right &right) const
- {
- BOOST_ASSERT(left.size() == right.size());
- for(std::size_t i = 0, size = left.size(); i != size; ++i)
- {
- if(numeric::less(right[i], left[i]))
- {
- left[i] = right[i];
- }
- }
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // element-wise max of std::vector
- template<typename Left, typename Right>
- struct max_assign<Left, Right, std_vector_tag, std_vector_tag>
- {
- typedef Left first_argument_type;
- typedef Right second_argument_type;
- typedef void result_type;
- void operator ()(Left &left, Right &right) const
- {
- BOOST_ASSERT(left.size() == right.size());
- for(std::size_t i = 0, size = left.size(); i != size; ++i)
- {
- if(numeric::greater(right[i], left[i]))
- {
- left[i] = right[i];
- }
- }
- }
- };
- // partial specialization for std::vector.
- template<typename Left, typename Right>
- struct fdiv<Left, Right, std_vector_tag, void>
- : mpl::if_<
- are_integral<typename Left::value_type, Right>
- , divides<Left, double const>
- , divides<Left, Right>
- >::type
- {};
- // promote
- template<typename To, typename From>
- struct promote<To, From, std_vector_tag, std_vector_tag>
- {
- typedef From argument_type;
- typedef To result_type;
- To operator ()(From &arr) const
- {
- typename remove_const<To>::type res(arr.size());
- for(std::size_t i = 0, size = arr.size(); i != size; ++i)
- {
- res[i] = numeric::promote<typename To::value_type>(arr[i]);
- }
- return res;
- }
- };
- template<typename ToFrom>
- struct promote<ToFrom, ToFrom, std_vector_tag, std_vector_tag>
- {
- typedef ToFrom argument_type;
- typedef ToFrom result_type;
- ToFrom &operator ()(ToFrom &tofrom) const
- {
- return tofrom;
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // functional::as_min
- template<typename T>
- struct as_min<T, std_vector_tag>
- {
- typedef T argument_type;
- typedef typename remove_const<T>::type result_type;
- typename remove_const<T>::type operator ()(T &arr) const
- {
- return 0 == arr.size()
- ? T()
- : T(arr.size(), numeric::as_min(arr[0]));
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // functional::as_max
- template<typename T>
- struct as_max<T, std_vector_tag>
- {
- typedef T argument_type;
- typedef typename remove_const<T>::type result_type;
- typename remove_const<T>::type operator ()(T &arr) const
- {
- return 0 == arr.size()
- ? T()
- : T(arr.size(), numeric::as_max(arr[0]));
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // functional::as_zero
- template<typename T>
- struct as_zero<T, std_vector_tag>
- {
- typedef T argument_type;
- typedef typename remove_const<T>::type result_type;
- typename remove_const<T>::type operator ()(T &arr) const
- {
- return 0 == arr.size()
- ? T()
- : T(arr.size(), numeric::as_zero(arr[0]));
- }
- };
- ///////////////////////////////////////////////////////////////////////////////
- // functional::as_one
- template<typename T>
- struct as_one<T, std_vector_tag>
- {
- typedef T argument_type;
- typedef typename remove_const<T>::type result_type;
- typename remove_const<T>::type operator ()(T &arr) const
- {
- return 0 == arr.size()
- ? T()
- : T(arr.size(), numeric::as_one(arr[0]));
- }
- };
- } // namespace functional
- }} // namespace boost::numeric
- #endif
|