Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:18

0001 // Copyright 2009-2014 Neil Groves.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 //
0006 // Copyright 2006 Thorsten Ottosen.
0007 // Distributed under the Boost Software License, Version 1.0. (See
0008 // accompanying file LICENSE_1_0.txt or copy at
0009 // http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Copyright 2004 Eric Niebler.
0012 // Distributed under the Boost Software License, Version 1.0. (See
0013 // accompanying file LICENSE_1_0.txt or copy at
0014 // http://www.boost.org/LICENSE_1_0.txt)
0015 //
0016 //   Contains range-based versions of the numeric std algorithms
0017 //
0018 #if defined(_MSC_VER)
0019     #pragma once
0020 #endif
0021 
0022 #ifndef BOOST_RANGE_NUMERIC_HPP
0023 #define BOOST_RANGE_NUMERIC_HPP
0024 
0025 #include <boost/config.hpp>
0026 #include <boost/assert.hpp>
0027 #include <boost/range/begin.hpp>
0028 #include <boost/range/end.hpp>
0029 #include <boost/range/category.hpp>
0030 #include <boost/range/concepts.hpp>
0031 #include <boost/range/distance.hpp>
0032 #include <boost/range/size.hpp>
0033 #include <numeric>
0034 
0035 
0036 namespace boost
0037 {
0038     template<class SinglePassRange, class Value>
0039     inline Value accumulate(const SinglePassRange& rng, Value init)
0040     {
0041         BOOST_RANGE_CONCEPT_ASSERT((
0042             SinglePassRangeConcept<const SinglePassRange>));
0043 
0044         return std::accumulate(boost::begin(rng), boost::end(rng), init);
0045     }
0046 
0047     template<class SinglePassRange, class Value, class BinaryOperation>
0048     inline Value accumulate(const SinglePassRange& rng, Value init,
0049                             BinaryOperation op)
0050     {
0051         BOOST_RANGE_CONCEPT_ASSERT((
0052                 SinglePassRangeConcept<const SinglePassRange> ));
0053 
0054         return std::accumulate(boost::begin(rng), boost::end(rng), init, op);
0055     }
0056 
0057     namespace range_detail
0058     {
0059         template<class SinglePassRange1, class SinglePassRange2>
0060         inline bool inner_product_precondition(
0061             const SinglePassRange1&,
0062             const SinglePassRange2&,
0063             std::input_iterator_tag,
0064             std::input_iterator_tag)
0065         {
0066             return true;
0067         }
0068 
0069         template<class SinglePassRange1, class SinglePassRange2>
0070         inline bool inner_product_precondition(
0071             const SinglePassRange1& rng1,
0072             const SinglePassRange2& rng2,
0073             std::forward_iterator_tag,
0074             std::forward_iterator_tag)
0075         {
0076             return boost::size(rng2) >= boost::size(rng1);
0077         }
0078 
0079     } // namespace range_detail
0080 
0081     template<
0082         class SinglePassRange1,
0083         class SinglePassRange2,
0084         class Value
0085     >
0086     inline Value inner_product(
0087         const SinglePassRange1& rng1,
0088         const SinglePassRange2& rng2,
0089         Value                   init)
0090     {
0091         BOOST_RANGE_CONCEPT_ASSERT((
0092             SinglePassRangeConcept<const SinglePassRange1>));
0093 
0094         BOOST_RANGE_CONCEPT_ASSERT((
0095             SinglePassRangeConcept<const SinglePassRange2>));
0096 
0097         BOOST_ASSERT(
0098             range_detail::inner_product_precondition(
0099                     rng1, rng2,
0100                     typename range_category<const SinglePassRange1>::type(),
0101                     typename range_category<const SinglePassRange2>::type()));
0102 
0103         return std::inner_product(
0104             boost::begin(rng1), boost::end(rng1),
0105             boost::begin(rng2), init);
0106     }
0107 
0108     template<
0109         class SinglePassRange1,
0110         class SinglePassRange2,
0111         class Value,
0112         class BinaryOperation1,
0113         class BinaryOperation2
0114     >
0115     inline Value inner_product(
0116         const SinglePassRange1& rng1,
0117         const SinglePassRange2& rng2,
0118         Value                   init,
0119         BinaryOperation1        op1,
0120         BinaryOperation2        op2)
0121     {
0122         BOOST_RANGE_CONCEPT_ASSERT((
0123                 SinglePassRangeConcept<const SinglePassRange1>));
0124 
0125         BOOST_RANGE_CONCEPT_ASSERT((
0126                 SinglePassRangeConcept<const SinglePassRange2>));
0127 
0128         BOOST_ASSERT(
0129             range_detail::inner_product_precondition(
0130                 rng1, rng2,
0131                 typename range_category<const SinglePassRange1>::type(),
0132                 typename range_category<const SinglePassRange2>::type()));
0133 
0134         return std::inner_product(
0135             boost::begin(rng1), boost::end(rng1),
0136             boost::begin(rng2), init, op1, op2);
0137     }
0138 
0139     template<class SinglePassRange, class OutputIterator>
0140     inline OutputIterator partial_sum(const SinglePassRange& rng,
0141                                       OutputIterator result)
0142     {
0143         BOOST_RANGE_CONCEPT_ASSERT((
0144                 SinglePassRangeConcept<const SinglePassRange>));
0145 
0146         return std::partial_sum(boost::begin(rng), boost::end(rng), result);
0147     }
0148 
0149     template<class SinglePassRange, class OutputIterator, class BinaryOperation>
0150     inline OutputIterator partial_sum(
0151         const SinglePassRange&  rng,
0152         OutputIterator          result,
0153         BinaryOperation         op)
0154     {
0155         BOOST_RANGE_CONCEPT_ASSERT((
0156                 SinglePassRangeConcept<const SinglePassRange>));
0157 
0158         return std::partial_sum(boost::begin(rng), boost::end(rng), result, op);
0159     }
0160 
0161     template<class SinglePassRange, class OutputIterator>
0162     inline OutputIterator adjacent_difference(
0163         const SinglePassRange&  rng,
0164         OutputIterator          result)
0165     {
0166         BOOST_RANGE_CONCEPT_ASSERT((
0167                 SinglePassRangeConcept<const SinglePassRange>));
0168 
0169         return std::adjacent_difference(boost::begin(rng), boost::end(rng),
0170                                         result);
0171     }
0172 
0173     template<class SinglePassRange, class OutputIterator, class BinaryOperation>
0174     inline OutputIterator adjacent_difference(
0175         const SinglePassRange&  rng,
0176         OutputIterator          result,
0177         BinaryOperation         op)
0178     {
0179         BOOST_RANGE_CONCEPT_ASSERT((
0180                 SinglePassRangeConcept<const SinglePassRange>));
0181 
0182         return std::adjacent_difference(boost::begin(rng), boost::end(rng),
0183                                         result, op);
0184     }
0185 
0186 } // namespace boost
0187 
0188 #endif