iota.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright (c) Marshall Clow 2008-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file iota.hpp
  7. /// \brief Generate an increasing series
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_IOTA_HPP
  10. #define BOOST_ALGORITHM_IOTA_HPP
  11. #include <numeric>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. namespace boost { namespace algorithm {
  15. #if __cplusplus >= 201103L
  16. // Use the C++11 versions of iota if it is available
  17. using std::iota; // Section 26.7.6
  18. #else
  19. /// \fn iota ( ForwardIterator first, ForwardIterator last, T value )
  20. /// \brief Generates an increasing sequence of values, and stores them in [first, last)
  21. ///
  22. /// \param first The start of the input sequence
  23. /// \param last One past the end of the input sequence
  24. /// \param value The initial value of the sequence to be generated
  25. /// \note This function is part of the C++2011 standard library.
  26. /// We will use the standard one if it is available,
  27. /// otherwise we have our own implementation.
  28. template <typename ForwardIterator, typename T>
  29. void iota ( ForwardIterator first, ForwardIterator last, T value )
  30. {
  31. for ( ; first != last; ++first, ++value )
  32. *first = value;
  33. }
  34. #endif
  35. /// \fn iota ( Range &r, T value )
  36. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
  37. ///
  38. /// \param r The input range
  39. /// \param value The initial value of the sequence to be generated
  40. ///
  41. template <typename Range, typename T>
  42. void iota ( Range &r, T value )
  43. {
  44. boost::algorithm::iota (boost::begin(r), boost::end(r), value);
  45. }
  46. /// \fn iota_n ( OutputIterator out, T value, std::size_t n )
  47. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
  48. ///
  49. /// \param out An output iterator to write the results into
  50. /// \param value The initial value of the sequence to be generated
  51. /// \param n The number of items to write
  52. ///
  53. template <typename OutputIterator, typename T>
  54. OutputIterator iota_n ( OutputIterator out, T value, std::size_t n )
  55. {
  56. while ( n-- > 0 )
  57. *out++ = value++;
  58. return out;
  59. }
  60. }}
  61. #endif // BOOST_ALGORITHM_IOTA_HPP