iota.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /// \fn iota ( ForwardIterator first, ForwardIterator last, T value )
  16. /// \brief Generates an increasing sequence of values, and stores them in [first, last)
  17. ///
  18. /// \param first The start of the input sequence
  19. /// \param last One past the end of the input sequence
  20. /// \param value The initial value of the sequence to be generated
  21. /// \note This function is part of the C++2011 standard library.
  22. /// We will use the standard one if it is available,
  23. /// otherwise we have our own implementation.
  24. template <typename ForwardIterator, typename T>
  25. void iota ( ForwardIterator first, ForwardIterator last, T value )
  26. {
  27. for ( ; first != last; ++first, ++value )
  28. *first = value;
  29. }
  30. /// \fn iota ( Range &r, T value )
  31. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
  32. ///
  33. /// \param r The input range
  34. /// \param value The initial value of the sequence to be generated
  35. ///
  36. template <typename Range, typename T>
  37. void iota ( Range &r, T value )
  38. {
  39. boost::algorithm::iota (boost::begin(r), boost::end(r), value);
  40. }
  41. /// \fn iota_n ( OutputIterator out, T value, std::size_t n )
  42. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
  43. ///
  44. /// \param out An output iterator to write the results into
  45. /// \param value The initial value of the sequence to be generated
  46. /// \param n The number of items to write
  47. ///
  48. template <typename OutputIterator, typename T>
  49. OutputIterator iota_n ( OutputIterator out, T value, std::size_t n )
  50. {
  51. for ( ; n > 0; --n, ++value )
  52. *out++ = value;
  53. return out;
  54. }
  55. }}
  56. #endif // BOOST_ALGORITHM_IOTA_HPP