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