Skip to content

Commit 43c01ff

Browse files
committed
Added c++11 algorithms to Boost.Algorithm
[SVN r77060]
1 parent 8bfaa6d commit 43c01ff

26 files changed

+2380
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 all_of.hpp
9+
/// \brief Test ranges to see if all elements match a value or predicate.
10+
/// \author Marshall Clow
11+
12+
#ifndef BOOST_ALGORITHM_ALL_OF_HPP
13+
#define BOOST_ALGORITHM_ALL_OF_HPP
14+
15+
#include <boost/range/begin.hpp>
16+
#include <boost/range/end.hpp>
17+
18+
namespace boost { namespace algorithm {
19+
20+
#if __cplusplus >= 201103L
21+
// Use the C++11 versions of all_of if it is available
22+
using std::all_of; // Section 25.2.1
23+
#else
24+
/// \fn all_of ( InputIterator first, InputIterator last, Predicate p )
25+
/// \return true if all elements in [first, last) satisfy the predicate 'p'
26+
/// \note returns true on an empty range
27+
///
28+
/// \param first The start of the input sequence
29+
/// \param last One past the end of the input sequence
30+
/// \param p A predicate for testing the elements of the sequence
31+
///
32+
/// \note This function is part of the C++2011 standard library.
33+
/// We will use the standard one if it is available,
34+
/// otherwise we have our own implementation.
35+
template<typename InputIterator, typename Predicate>
36+
bool all_of ( InputIterator first, InputIterator last, Predicate p )
37+
{
38+
for ( ; first != last; ++first )
39+
if ( !p(*first))
40+
return false;
41+
return true;
42+
}
43+
#endif
44+
45+
/// \fn all_of ( const Range &r, Predicate p )
46+
/// \return true if all elements in the range satisfy the predicate 'p'
47+
/// \note returns true on an empty range
48+
///
49+
/// \param r The input range
50+
/// \param p A predicate for testing the elements of the range
51+
///
52+
template<typename Range, typename Predicate>
53+
bool all_of ( const Range &r, Predicate p )
54+
{
55+
return boost::algorithm::all_of ( boost::begin (r), boost::end (r), p );
56+
}
57+
58+
/// \fn all_of_equal ( InputIterator first, InputIterator last, const T &val )
59+
/// \return true if all elements in [first, last) are equal to 'val'
60+
/// \note returns true on an empty range
61+
///
62+
/// \param first The start of the input sequence
63+
/// \param last One past the end of the input sequence
64+
/// \param val A value to compare against
65+
///
66+
template<typename InputIterator, typename T>
67+
bool all_of_equal ( InputIterator first, InputIterator last, const T &val )
68+
{
69+
for ( ; first != last; ++first )
70+
if ( val != *first )
71+
return false;
72+
return true;
73+
}
74+
75+
/// \fn all_of_equal ( const Range &r, const T &val )
76+
/// \return true if all elements in the range are equal to 'val'
77+
/// \note returns true on an empty range
78+
///
79+
/// \param r The input range
80+
/// \param val A value to compare against
81+
///
82+
template<typename Range, typename T>
83+
bool all_of_equal ( const Range &r, const T &val )
84+
{
85+
return boost::algorithm::all_of_equal ( boost::begin (r), boost::end (r), val );
86+
}
87+
88+
}} // namespace boost and algorithm
89+
90+
#endif // BOOST_ALGORITHM_ALL_OF_HPP
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
For more information, see http://www.boost.org
8+
*/
9+
10+
/// \file
11+
/// \brief Test ranges to see if any elements match a value or predicate.
12+
/// \author Marshall Clow
13+
14+
#ifndef BOOST_ALGORITHM_ANY_OF_HPP
15+
#define BOOST_ALGORITHM_ANY_OF_HPP
16+
17+
#include <boost/range/begin.hpp>
18+
#include <boost/range/end.hpp>
19+
20+
namespace boost { namespace algorithm {
21+
22+
// Use the C++11 versions of any_of if it is available
23+
#if __cplusplus >= 201103L
24+
using std::any_of; // Section 25.2.2
25+
#else
26+
/// \fn any_of ( InputIterator first, InputIterator last, Predicate p )
27+
/// \return true if any of the elements in [first, last) satisfy the predicate
28+
/// \note returns false on an empty range
29+
///
30+
/// \param first The start of the input sequence
31+
/// \param last One past the end of the input sequence
32+
/// \param p A predicate for testing the elements of the sequence
33+
///
34+
template<typename InputIterator, typename Predicate>
35+
bool any_of ( InputIterator first, InputIterator last, Predicate p )
36+
{
37+
for ( ; first != last; ++first )
38+
if ( p(*first))
39+
return true;
40+
return false;
41+
}
42+
#endif
43+
44+
/// \fn any_of ( const Range &r, Predicate p )
45+
/// \return true if any elements in the range satisfy the predicate 'p'
46+
/// \note returns false on an empty range
47+
///
48+
/// \param r The input range
49+
/// \param p A predicate for testing the elements of the range
50+
///
51+
template<typename Range, typename Predicate>
52+
bool any_of ( const Range &r, Predicate p )
53+
{
54+
return boost::algorithm::any_of (boost::begin (r), boost::end (r), p);
55+
}
56+
57+
/// \fn any_of_equal ( InputIterator first, InputIterator last, const V &val )
58+
/// \return true if any of the elements in [first, last) are equal to 'val'
59+
/// \note returns false on an empty range
60+
///
61+
/// \param first The start of the input sequence
62+
/// \param last One past the end of the input sequence
63+
/// \param val A value to compare against
64+
///
65+
template<typename InputIterator, typename V>
66+
bool any_of_equal ( InputIterator first, InputIterator last, const V &val )
67+
{
68+
for ( ; first != last; ++first )
69+
if ( val == *first )
70+
return true;
71+
return false;
72+
}
73+
74+
/// \fn any_of_equal ( const Range &r, const V &val )
75+
/// \return true if any of the elements in the range are equal to 'val'
76+
/// \note returns false on an empty range
77+
///
78+
/// \param r The input range
79+
/// \param val A value to compare against
80+
///
81+
template<typename Range, typename V>
82+
bool any_of_equal ( const Range &r, const V &val )
83+
{
84+
return boost::algorithm::any_of_equal (boost::begin (r), boost::end (r), val);
85+
}
86+
87+
}} // namespace boost and algorithm
88+
89+
#endif // BOOST_ALGORITHM_ANY_OF_HPP
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 <algorithm> // for std::copy_if, if available
16+
#include <boost/range/begin.hpp>
17+
#include <boost/range/end.hpp>
18+
19+
namespace boost { namespace algorithm {
20+
21+
#if __cplusplus >= 201103L
22+
// Use the C++11 versions of copy_if if it is available
23+
using std::copy_if; // Section 25.3.1
24+
#else
25+
/// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
26+
/// \brief Copies all the elements from the input range that satisfy the
27+
/// predicate to the output range.
28+
/// \return The updated output iterator
29+
///
30+
/// \param first The start of the input sequence
31+
/// \param last One past the end of the input sequence
32+
/// \param result An output iterator to write the results into
33+
/// \param p A predicate for testing the elements of the range
34+
/// \note This function is part of the C++2011 standard library.
35+
/// We will use the standard one if it is available,
36+
/// otherwise we have our own implementation.
37+
template<typename InputIterator, typename OutputIterator, typename Predicate>
38+
OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
39+
{
40+
for ( ; first != last; ++first )
41+
if (p(*first))
42+
*result++ = first;
43+
return result;
44+
}
45+
#endif
46+
47+
/// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
48+
/// \brief Copies all the elements from the input range that satisfy the
49+
/// predicate to the output range.
50+
/// \return The updated output iterator
51+
///
52+
/// \param r The input range
53+
/// \param result An output iterator to write the results into
54+
/// \param p A predicate for testing the elements of the range
55+
///
56+
template<typename Range, typename OutputIterator, typename Predicate>
57+
OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
58+
{
59+
return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
60+
}
61+
62+
63+
/// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
64+
/// \brief Copies all the elements at the start of the input range that
65+
/// satisfy the predicate to the output range.
66+
/// \return The updated output iterator
67+
///
68+
/// \param first The start of the input sequence
69+
/// \param last One past the end of the input sequence
70+
/// \param result An output iterator to write the results into
71+
/// \param p A predicate for testing the elements of the range
72+
///
73+
template<typename InputIterator, typename OutputIterator, typename Predicate>
74+
OutputIterator copy_while ( InputIterator first, InputIterator last,
75+
OutputIterator result, Predicate p )
76+
{
77+
for ( ; first != last && p(*first); ++first )
78+
*result++ = first;
79+
return result;
80+
}
81+
82+
/// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
83+
/// \brief Copies all the elements at the start of the input range that
84+
/// satisfy the predicate to the output range.
85+
/// \return The updated output iterator
86+
///
87+
/// \param r The input range
88+
/// \param result An output iterator to write the results into
89+
/// \param p A predicate for testing the elements of the range
90+
///
91+
template<typename Range, typename OutputIterator, typename Predicate>
92+
OutputIterator copy_while ( const Range &r, OutputIterator result, Predicate p )
93+
{
94+
return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
95+
}
96+
97+
98+
/// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
99+
/// \brief Copies all the elements at the start of the input range that do not
100+
/// satisfy the predicate to the output range.
101+
/// \return The updated output iterator
102+
///
103+
/// \param first The start of the input sequence
104+
/// \param last One past the end of the input sequence
105+
/// \param result An output iterator to write the results into
106+
/// \param p A predicate for testing the elements of the range
107+
///
108+
template<typename InputIterator, typename OutputIterator, typename Predicate>
109+
OutputIterator copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
110+
{
111+
for ( ; first != last && !p(*first); ++first )
112+
*result++ = first;
113+
return result;
114+
}
115+
116+
/// \fn copy_until ( const Range &r, OutputIterator result, Predicate p )
117+
/// \brief Copies all the elements at the start of the input range that do not
118+
/// satisfy the predicate to the output range.
119+
/// \return The updated output iterator
120+
///
121+
/// \param r The input range
122+
/// \param result An output iterator to write the results into
123+
/// \param p A predicate for testing the elements of the range
124+
///
125+
template<typename Range, typename OutputIterator, typename Predicate>
126+
OutputIterator copy_until ( const Range &r, OutputIterator result, Predicate p )
127+
{
128+
return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
129+
}
130+
131+
}} // namespace boost and algorithm
132+
133+
#endif // BOOST_ALGORITHM_COPY_IF_HPP
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright (c) Marshall Clow 2011-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_n.hpp
9+
/// \brief Copy n items from one sequence to another
10+
/// \author Marshall Clow
11+
12+
#ifndef BOOST_ALGORITHM_COPY_N_HPP
13+
#define BOOST_ALGORITHM_COPY_N_HPP
14+
15+
#include <algorithm> // for std::copy_n, if available
16+
17+
namespace boost { namespace algorithm {
18+
19+
#if __cplusplus >= 201103L
20+
// Use the C++11 versions of copy_n if it is available
21+
using std::copy_n; // Section 25.3.1
22+
#else
23+
/// \fn copy_n ( InputIterator first, Size n, OutputIterator result )
24+
/// \brief Copies exactly n (n > 0) elements from the range starting at first to
25+
/// the range starting at result.
26+
/// \return The updated output iterator
27+
///
28+
/// \param first The start of the input sequence
29+
/// \param n The number of elements to copy
30+
/// \param result An output iterator to write the results into
31+
/// \note This function is part of the C++2011 standard library.
32+
/// We will use the standard one if it is available,
33+
/// otherwise we have our own implementation.
34+
template <typename InputIterator, typename Size, typename OutputIterator>
35+
OutputIterator copy_n ( InputIterator first, Size n, OutputIterator result )
36+
{
37+
for ( ; n > 0; --n, ++first, ++result )
38+
*result = *first;
39+
return result;
40+
}
41+
#endif
42+
}} // namespace boost and algorithm
43+
44+
#endif // BOOST_ALGORITHM_COPY_IF_HPP

0 commit comments

Comments
 (0)