iota.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <boost/config.hpp>
  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. template <typename ForwardIterator, typename T>
  23. BOOST_CXX14_CONSTEXPR void iota ( ForwardIterator first, ForwardIterator last, T value )
  24. {
  25. for ( ; first != last; ++first, ++value )
  26. *first = value;
  27. }
  28. /// \fn iota ( Range &r, T value )
  29. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
  30. ///
  31. /// \param r The input range
  32. /// \param value The initial value of the sequence to be generated
  33. ///
  34. template <typename Range, typename T>
  35. BOOST_CXX14_CONSTEXPR void iota ( Range &r, T value )
  36. {
  37. boost::algorithm::iota (boost::begin(r), boost::end(r), value);
  38. }
  39. /// \fn iota_n ( OutputIterator out, T value, std::size_t n )
  40. /// \brief Generates an increasing sequence of values, and stores them in the input Range.
  41. ///
  42. /// \param out An output iterator to write the results into
  43. /// \param value The initial value of the sequence to be generated
  44. /// \param n The number of items to write
  45. ///
  46. template <typename OutputIterator, typename T>
  47. BOOST_CXX14_CONSTEXPR OutputIterator iota_n ( OutputIterator out, T value, std::size_t n )
  48. {
  49. for ( ; n > 0; --n, ++value )
  50. *out++ = value;
  51. return out;
  52. }
  53. }}
  54. #endif // BOOST_ALGORITHM_IOTA_HPP