]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 copy_if.hpp | |
9 | /// \brief Copy a subset of a sequence to a new sequence | |
10 | /// \author Marshall Clow | |
11 | ||
12 | #ifndef BOOST_ALGORITHM_COPY_IF_HPP | |
13 | #define BOOST_ALGORITHM_COPY_IF_HPP | |
14 | ||
15 | #include <utility> // for std::pair, std::make_pair | |
16 | #include <boost/range/begin.hpp> | |
17 | #include <boost/range/end.hpp> | |
18 | ||
19 | namespace boost { namespace algorithm { | |
20 | ||
21 | /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) | |
22 | /// \brief Copies all the elements from the input range that satisfy the | |
23 | /// predicate to the output range. | |
24 | /// \return The updated output iterator | |
25 | /// | |
26 | /// \param first The start of the input sequence | |
27 | /// \param last One past the end of the input sequence | |
28 | /// \param result An output iterator to write the results into | |
29 | /// \param p A predicate for testing the elements of the range | |
30 | /// \note This function is part of the C++2011 standard library. | |
31 | template<typename InputIterator, typename OutputIterator, typename Predicate> | |
32 | OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) | |
33 | { | |
34 | for ( ; first != last; ++first ) | |
35 | if (p(*first)) | |
36 | *result++ = *first; | |
37 | return result; | |
38 | } | |
39 | ||
40 | /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p ) | |
41 | /// \brief Copies all the elements from the input range that satisfy the | |
42 | /// predicate to the output range. | |
43 | /// \return The updated output iterator | |
44 | /// | |
45 | /// \param r The input range | |
46 | /// \param result An output iterator to write the results into | |
47 | /// \param p A predicate for testing the elements of the range | |
48 | /// | |
49 | template<typename Range, typename OutputIterator, typename Predicate> | |
50 | OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p ) | |
51 | { | |
52 | return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p); | |
53 | } | |
54 | ||
55 | ||
56 | /// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) | |
57 | /// \brief Copies all the elements at the start of the input range that | |
58 | /// satisfy the predicate to the output range. | |
59 | /// \return The updated input and output iterators | |
60 | /// | |
61 | /// \param first The start of the input sequence | |
62 | /// \param last One past the end of the input sequence | |
63 | /// \param result An output iterator to write the results into | |
64 | /// \param p A predicate for testing the elements of the range | |
65 | /// | |
66 | template<typename InputIterator, typename OutputIterator, typename Predicate> | |
67 | std::pair<InputIterator, OutputIterator> | |
68 | copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) | |
69 | { | |
70 | for ( ; first != last && p(*first); ++first ) | |
71 | *result++ = *first; | |
72 | return std::make_pair(first, result); | |
73 | } | |
74 | ||
75 | /// \fn copy_while ( const Range &r, OutputIterator result, Predicate p ) | |
76 | /// \brief Copies all the elements at the start of the input range that | |
77 | /// satisfy the predicate to the output range. | |
78 | /// \return The updated input and output iterators | |
79 | /// | |
80 | /// \param r The input range | |
81 | /// \param result An output iterator to write the results into | |
82 | /// \param p A predicate for testing the elements of the range | |
83 | /// | |
84 | template<typename Range, typename OutputIterator, typename Predicate> | |
85 | std::pair<typename boost::range_iterator<const Range>::type, OutputIterator> | |
86 | copy_while ( const Range &r, OutputIterator result, Predicate p ) | |
87 | { | |
88 | return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p); | |
89 | } | |
90 | ||
91 | ||
92 | /// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) | |
93 | /// \brief Copies all the elements at the start of the input range that do not | |
94 | /// satisfy the predicate to the output range. | |
95 | /// \return The updated output iterator | |
96 | /// | |
97 | /// \param first The start of the input sequence | |
98 | /// \param last One past the end of the input sequence | |
99 | /// \param result An output iterator to write the results into | |
100 | /// \param p A predicate for testing the elements of the range | |
101 | /// | |
102 | template<typename InputIterator, typename OutputIterator, typename Predicate> | |
103 | std::pair<InputIterator, OutputIterator> | |
104 | copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) | |
105 | { | |
106 | for ( ; first != last && !p(*first); ++first ) | |
107 | *result++ = *first; | |
108 | return std::make_pair(first, result); | |
109 | } | |
110 | ||
111 | /// \fn copy_until ( const Range &r, OutputIterator result, Predicate p ) | |
112 | /// \brief Copies all the elements at the start of the input range that do not | |
113 | /// satisfy the predicate to the output range. | |
114 | /// \return The updated output iterator | |
115 | /// | |
116 | /// \param r The input range | |
117 | /// \param result An output iterator to write the results into | |
118 | /// \param p A predicate for testing the elements of the range | |
119 | /// | |
120 | template<typename Range, typename OutputIterator, typename Predicate> | |
121 | std::pair<typename boost::range_iterator<const Range>::type, OutputIterator> | |
122 | copy_until ( const Range &r, OutputIterator result, Predicate p ) | |
123 | { | |
124 | return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p); | |
125 | } | |
126 | ||
127 | }} // namespace boost and algorithm | |
128 | ||
129 | #endif // BOOST_ALGORITHM_COPY_IF_HPP |