1 | /////////////////////////////////////////////////////////////////////////////// |
---|
2 | /// \file algorithm.hpp |
---|
3 | /// Contains range-based versions of the std algorithms |
---|
4 | // |
---|
5 | ///////////////////////////////////////////////////////////////////////////// |
---|
6 | // Copyright 2009 Neil Groves. |
---|
7 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
8 | // accompanying file LICENSE_1_0.txt or copy at |
---|
9 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
10 | // |
---|
11 | |
---|
12 | // Copyright 2006 Thorsten Ottosen. |
---|
13 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
14 | // accompanying file LICENSE_1_0.txt or copy at |
---|
15 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
16 | // |
---|
17 | // Copyright 2004 Eric Niebler. |
---|
18 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
19 | // accompanying file LICENSE_1_0.txt or copy at |
---|
20 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
21 | |
---|
22 | #if defined(_MSC_VER) && _MSC_VER >= 1000 |
---|
23 | #pragma once |
---|
24 | #endif |
---|
25 | |
---|
26 | #ifndef BOOST_RANGE_NUMERIC_HPP |
---|
27 | #define BOOST_RANGE_NUMERIC_HPP |
---|
28 | |
---|
29 | #include <boost/config.hpp> |
---|
30 | #include <boost/assert.hpp> |
---|
31 | #include <boost/range/begin.hpp> |
---|
32 | #include <boost/range/end.hpp> |
---|
33 | #include <boost/range/concepts.hpp> |
---|
34 | #include <boost/range/distance.hpp> |
---|
35 | #include <numeric> |
---|
36 | |
---|
37 | |
---|
38 | namespace boost |
---|
39 | { |
---|
40 | template< class SinglePassRange, class Value > |
---|
41 | inline Value accumulate( const SinglePassRange& rng, Value init ) |
---|
42 | { |
---|
43 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> )); |
---|
44 | return std::accumulate( boost::begin(rng), boost::end(rng), init ); |
---|
45 | } |
---|
46 | |
---|
47 | template< class SinglePassRange, class Value, class BinaryOperation > |
---|
48 | inline Value accumulate( const SinglePassRange& rng, Value init, BinaryOperation op ) |
---|
49 | { |
---|
50 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> )); |
---|
51 | return std::accumulate( boost::begin(rng), boost::end(rng), init, op ); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | template< class SinglePassRange1, class SinglePassRange2, class Value > |
---|
56 | inline Value inner_product( const SinglePassRange1& rng1, const SinglePassRange2& rng2, Value init ) |
---|
57 | { |
---|
58 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> )); |
---|
59 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> )); |
---|
60 | BOOST_ASSERT( boost::distance(rng2) >= boost::distance(rng1) ); |
---|
61 | return std::inner_product( boost::begin(rng1), boost::end(rng1), |
---|
62 | boost::begin(rng2), init ); |
---|
63 | } |
---|
64 | |
---|
65 | template< class SinglePassRange1, |
---|
66 | class SinglePassRange2, |
---|
67 | class Value, |
---|
68 | class BinaryOperation1, class BinaryOperation2 > |
---|
69 | inline Value inner_product( const SinglePassRange1& rng1, const SinglePassRange2& rng2, |
---|
70 | Value init, |
---|
71 | BinaryOperation1 op1, BinaryOperation2 op2 ) |
---|
72 | { |
---|
73 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> )); |
---|
74 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> )); |
---|
75 | BOOST_ASSERT( boost::distance(rng2) >= boost::distance(rng1) ); |
---|
76 | |
---|
77 | return std::inner_product( boost::begin(rng1), boost::end(rng1), |
---|
78 | boost::begin(rng2), init, op1, op2 ); |
---|
79 | } |
---|
80 | |
---|
81 | template< class SinglePassRange, class OutputIterator > |
---|
82 | inline OutputIterator partial_sum ( const SinglePassRange& rng, |
---|
83 | OutputIterator result ) |
---|
84 | { |
---|
85 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> )); |
---|
86 | return std::partial_sum( boost::begin(rng), boost::end(rng), result ); |
---|
87 | } |
---|
88 | |
---|
89 | template< class SinglePassRange, class OutputIterator, class BinaryOperation > |
---|
90 | inline OutputIterator partial_sum ( const SinglePassRange& rng, OutputIterator result, |
---|
91 | BinaryOperation op ) |
---|
92 | { |
---|
93 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> )); |
---|
94 | return std::partial_sum( boost::begin(rng), boost::end(rng), result, op ); |
---|
95 | } |
---|
96 | |
---|
97 | template< class SinglePassRange, class OutputIterator > |
---|
98 | inline OutputIterator adjacent_difference ( const SinglePassRange& rng, |
---|
99 | OutputIterator result ) |
---|
100 | { |
---|
101 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange> )); |
---|
102 | return std::adjacent_difference( boost::begin(rng), boost::end(rng), |
---|
103 | result ); |
---|
104 | } |
---|
105 | |
---|
106 | template< class SinglePassRange, class OutputIterator, class BinaryOperation > |
---|
107 | inline OutputIterator adjacent_difference ( const SinglePassRange& rng, |
---|
108 | OutputIterator result, |
---|
109 | BinaryOperation op ) |
---|
110 | { |
---|
111 | BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange> )); |
---|
112 | return std::adjacent_difference( boost::begin(rng), boost::end(rng), |
---|
113 | result, op ); |
---|
114 | } |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | #endif |
---|