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