numeric.hpp 4.8 KB

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