numeric.hpp 4.92 KB
Newer Older
1 2 3 4 5 6 7 8 9
///////////////////////////////////////////////////////////////////////////////
/// \file algorithm.hpp
///   Contains range-based versions of the std algorithms
//
/////////////////////////////////////////////////////////////////////////////
// Copyright 2009 Neil Groves.
// 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)
10
//
11 12 13 14 15

// Copyright 2006 Thorsten Ottosen.
// 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)
16
//
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Copyright 2004 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)

#if defined(_MSC_VER) && _MSC_VER >= 1000
    #pragma once
#endif

#ifndef BOOST_RANGE_NUMERIC_HPP
#define BOOST_RANGE_NUMERIC_HPP

#include <boost/config.hpp>
#include <boost/assert.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/concepts.hpp>
34
#include <boost/range/distance.hpp>
35 36 37 38 39 40 41 42
#include <numeric>


namespace boost
{
    template< class SinglePassRange, class Value >
    inline Value accumulate( const SinglePassRange& rng, Value init )
    {
43
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
44 45 46 47 48 49
        return std::accumulate( boost::begin(rng), boost::end(rng), init );
    }

    template< class SinglePassRange, class Value, class BinaryOperation >
    inline Value accumulate( const SinglePassRange& rng, Value init, BinaryOperation op )
    {
50
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
51 52 53 54 55 56 57
        return std::accumulate( boost::begin(rng), boost::end(rng), init, op );
    }


    template< class SinglePassRange1, class SinglePassRange2, class Value >
    inline Value inner_product( const SinglePassRange1& rng1, const SinglePassRange2& rng2, Value init )
    {
58 59
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
60
        BOOST_ASSERT( boost::distance(rng2) >= boost::distance(rng1) );
61 62
        return std::inner_product( boost::begin(rng1), boost::end(rng1),
            boost::begin(rng2), init );
63 64 65 66
    }

    template< class SinglePassRange1,
              class SinglePassRange2,
67
              class Value,
68 69
              class BinaryOperation1, class BinaryOperation2 >
    inline Value inner_product( const SinglePassRange1& rng1, const SinglePassRange2& rng2,
70
                                Value init,
71 72
                                BinaryOperation1 op1, BinaryOperation2 op2 )
    {
73 74
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
75 76
        BOOST_ASSERT( boost::distance(rng2) >= boost::distance(rng1) );

77 78
        return std::inner_product( boost::begin(rng1), boost::end(rng1),
                                   boost::begin(rng2), init, op1, op2 );
79 80 81
    }

    template< class SinglePassRange, class OutputIterator >
82
    inline OutputIterator partial_sum ( const SinglePassRange& rng,
83 84
                                        OutputIterator result )
    {
85
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
86 87 88 89
        return std::partial_sum( boost::begin(rng), boost::end(rng), result );
    }

    template< class SinglePassRange, class OutputIterator, class BinaryOperation >
90
    inline OutputIterator partial_sum ( const SinglePassRange& rng, OutputIterator result,
91 92
                                        BinaryOperation op )
    {
93
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
94 95 96 97 98 99 100
        return std::partial_sum( boost::begin(rng), boost::end(rng), result, op );
    }

    template< class SinglePassRange, class OutputIterator >
    inline OutputIterator adjacent_difference ( const SinglePassRange& rng,
                                                OutputIterator result )
    {
101 102
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> ));
        return std::adjacent_difference( boost::begin(rng), boost::end(rng),
103 104 105 106 107 108 109 110
                                         result );
    }

    template< class SinglePassRange, class OutputIterator, class BinaryOperation >
    inline OutputIterator adjacent_difference ( const SinglePassRange& rng,
                                                OutputIterator result,
                                                BinaryOperation op )
    {
111 112
        BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> ));
        return std::adjacent_difference( boost::begin(rng), boost::end(rng),
113 114
                                         result, op );
    }
115

116 117 118
}

#endif