Chris@101: // Copyright 2009-2014 Neil Groves. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // Copyright 2006 Thorsten Ottosen. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // Copyright 2004 Eric Niebler. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@101: // Chris@101: // Contains range-based versions of the numeric std algorithms Chris@101: // Chris@101: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_RANGE_NUMERIC_HPP Chris@16: #define BOOST_RANGE_NUMERIC_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost Chris@16: { Chris@101: template Chris@101: inline Value accumulate(const SinglePassRange& rng, Value init) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return std::accumulate(boost::begin(rng), boost::end(rng), init); Chris@16: } Chris@16: Chris@101: template Chris@101: inline Value accumulate(const SinglePassRange& rng, Value init, Chris@101: BinaryOperation op) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept )); Chris@101: Chris@101: return std::accumulate(boost::begin(rng), boost::end(rng), init, op); Chris@16: } Chris@16: Chris@101: namespace range_detail Chris@101: { Chris@101: template Chris@101: inline bool inner_product_precondition( Chris@101: const SinglePassRange1&, Chris@101: const SinglePassRange2&, Chris@101: std::input_iterator_tag, Chris@101: std::input_iterator_tag) Chris@101: { Chris@101: return true; Chris@101: } Chris@16: Chris@101: template Chris@101: inline bool inner_product_precondition( Chris@101: const SinglePassRange1& rng1, Chris@101: const SinglePassRange2& rng2, Chris@101: std::forward_iterator_tag, Chris@101: std::forward_iterator_tag) Chris@101: { Chris@101: return boost::size(rng2) >= boost::size(rng1); Chris@101: } Chris@101: Chris@101: } // namespace range_detail Chris@101: Chris@101: template< Chris@101: class SinglePassRange1, Chris@101: class SinglePassRange2, Chris@101: class Value Chris@101: > Chris@101: inline Value inner_product( Chris@101: const SinglePassRange1& rng1, Chris@101: const SinglePassRange2& rng2, Chris@101: Value init) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: BOOST_ASSERT( Chris@101: range_detail::inner_product_precondition( Chris@101: rng1, rng2, Chris@101: typename range_category::type(), Chris@101: typename range_category::type())); Chris@101: Chris@101: return std::inner_product( Chris@101: boost::begin(rng1), boost::end(rng1), Chris@101: boost::begin(rng2), init); Chris@16: } Chris@16: Chris@101: template< Chris@101: class SinglePassRange1, Chris@101: class SinglePassRange2, Chris@101: class Value, Chris@101: class BinaryOperation1, Chris@101: class BinaryOperation2 Chris@101: > Chris@101: inline Value inner_product( Chris@101: const SinglePassRange1& rng1, Chris@101: const SinglePassRange2& rng2, Chris@101: Value init, Chris@101: BinaryOperation1 op1, Chris@101: BinaryOperation2 op2) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@16: Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: BOOST_ASSERT( Chris@101: range_detail::inner_product_precondition( Chris@101: rng1, rng2, Chris@101: typename range_category::type(), Chris@101: typename range_category::type())); Chris@101: Chris@101: return std::inner_product( Chris@101: boost::begin(rng1), boost::end(rng1), Chris@101: boost::begin(rng2), init, op1, op2); Chris@16: } Chris@16: Chris@101: template Chris@101: inline OutputIterator partial_sum(const SinglePassRange& rng, Chris@101: OutputIterator result) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return std::partial_sum(boost::begin(rng), boost::end(rng), result); Chris@16: } Chris@16: Chris@101: template Chris@101: inline OutputIterator partial_sum( Chris@101: const SinglePassRange& rng, Chris@101: OutputIterator result, Chris@101: BinaryOperation op) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return std::partial_sum(boost::begin(rng), boost::end(rng), result, op); Chris@16: } Chris@16: Chris@101: template Chris@101: inline OutputIterator adjacent_difference( Chris@101: const SinglePassRange& rng, Chris@101: OutputIterator result) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return std::adjacent_difference(boost::begin(rng), boost::end(rng), Chris@101: result); Chris@16: } Chris@16: Chris@101: template Chris@101: inline OutputIterator adjacent_difference( Chris@101: const SinglePassRange& rng, Chris@101: OutputIterator result, Chris@101: BinaryOperation op) Chris@16: { Chris@101: BOOST_RANGE_CONCEPT_ASSERT(( Chris@101: SinglePassRangeConcept)); Chris@101: Chris@101: return std::adjacent_difference(boost::begin(rng), boost::end(rng), Chris@101: result, op); Chris@16: } Chris@16: Chris@101: } // namespace boost Chris@16: Chris@16: #endif