-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathall.hpp
More file actions
274 lines (236 loc) · 8.82 KB
/
all.hpp
File metadata and controls
274 lines (236 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Copyright (c) Marshall Clow 2008-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Revision history:
05 May 2008 mtc First version - as part of BoostCon 2008
07 May 2009 mtc Changed names to match n2666
02 Jul 2010 mtc Changed namespace t
on a suggestion by Sean Parent.
24 May 2011 mtc Changed the names to match the ones in the C++11 standard,
and to use those when available.
*/
/// \file
/// \brief Test ranges against predicates.
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_ALL_HPP
#define BOOST_ALGORITHM_ALL_HPP
#include <algorithm> // for std::find and std::find_if
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
namespace boost { namespace algorithm {
// Use the C++11 versions of the routines if they are available
#if __cplusplus >= 201103L
using std::all_of; // Section 25.2.1
using std::any_of; // Section 25.2.2
using std::none_of; // Section 25.2.3
#else
/// \fn all_of ( InputIterator first, InputIterator last, Predicate p )
/// \return true if all elements in [first, last) satisfy the predicate 'p'
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param p A predicate for testing the elements of the sequence
///
template<typename InputIterator, typename Predicate>
bool all_of ( InputIterator first, InputIterator last, Predicate p )
{
for ( ; first != last; ++first )
if ( !p(*first))
return false;
return true;
}
/// \fn any_of ( InputIterator first, InputIterator last, Predicate p )
/// \return true if any of the elements in [first, last) satisfy the predicate
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param p A predicate for testing the elements of the sequence
///
template<typename InputIterator, typename Predicate>
bool any_of ( InputIterator first, InputIterator last, Predicate p)
{
for ( ; first != last; ++first )
if ( p(*first))
return true;
return false;
}
/// \fn none_of ( InputIterator first, InputIterator last, Predicate p )
/// \return true if none of the elements in [first, last) satisfy the predicate 'p'
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param p A predicate for testing the elements of the sequence
///
template<typename InputIterator, typename Predicate>
bool none_of ( InputIterator first, InputIterator last, Predicate p )
{
for ( ; first != last; ++first )
if ( p(*first))
return false;
return true;
}
#endif
/// \fn all_of ( const R &range, Predicate p )
/// \return true if all elements in the range satisfy the predicate 'p'
///
/// \param range The input range
/// \param p A predicate for testing the elements of the range
///
template<typename R, typename Predicate>
bool all_of ( const R &range, Predicate p )
{
return (all_of) ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn none_of ( const R &range, Predicate p )
/// \return true if none of the elements in the range satisfy the predicate 'p'
///
/// \param range The input range
/// \param p A predicate for testing the elements of the range
///
template<typename R, typename Predicate>
bool none_of ( const R &range, Predicate p )
{
return (none_of) ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn any_of ( const R &range, Predicate p )
/// \return true if any elements in the range satisfy the predicate 'p'
///
/// \param range The input range
/// \param p A predicate for testing the elements of the range
///
template<typename R, typename Predicate>
bool any_of ( const R &range, Predicate p )
{
return (any_of) ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn one_of ( InputIterator first, InputIterator last, Predicate p )
/// \return true if the predicate 'p' is true for exactly one item in [first, last).
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param p A predicate for testing the elements of the sequence
///
template<typename InputIterator, typename Predicate>
bool one_of ( InputIterator first, InputIterator last, Predicate p )
{
InputIterator i = std::find_if (first, last, p);
if (i == last) return false; // Didn't occur at all
return (none_of) (++i, last, p);
}
/// \fn one_of ( const R &range, Predicate p )
/// \return true if the predicate 'p' is true for exactly one item in the range.
///
/// \param range The input range
/// \param p A predicate for testing the elements of the range
///
template<typename R, typename Predicate>
bool one_of ( const R &range, Predicate p )
{
return (one_of) ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn all_of_equal ( InputIterator first, InputIterator last, const V &val )
/// \return true if all elements in [first, last) are equal to 'val'
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param val A value to compare against
///
template<typename InputIterator, typename V>
bool all_of_equal ( InputIterator first, InputIterator last, const V &val )
{
for ( ; first != last; ++first )
if ( val != *first )
return false;
return true;
}
/// \fn all_of_equal ( const R &range, const V &val )
/// \return true if all elements in the range are equal to 'val'
///
/// \param range The input range
/// \param val A value to compare against
///
template<typename R, typename V>
bool all_of_equal ( const R &range, const V &val )
{
return (all_of_equal) ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn any_of_equal ( InputIterator first, InputIterator last, const V &val )
/// \return true if any of the elements in [first, last) are equal to 'val'
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param val A value to compare against
///
template<typename InputIterator, typename V>
bool any_of_equal ( InputIterator first, InputIterator last, const V &val )
{
for ( ; first != last; ++first )
if ( val == *first )
return true;
return false;
}
/// \fn any_of_equal ( const R &range, const V &val )
/// \return true if any of the elements in the range are equal to 'val'
///
/// \param range The input range
/// \param val A value to compare against
///
template<typename R, typename V>
bool any_of_equal ( const R &range, const V &val )
{
return (any_of_equal) ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn none_of_equal ( InputIterator first, InputIterator last, const V &val )
/// \return true if none of the elements in [first, last) are equal to 'val'
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param val A value to compare against
///
template<typename InputIterator, typename V>
bool none_of_equal ( InputIterator first, InputIterator last, const V &val )
{
for ( ; first != last; ++first )
if ( val == *first )
return false;
return true;
}
/// \fn none_of_equal ( const R &range, const V &val )
/// \return true if none of the elements in the range are equal to 'val'
///
/// \param range The input range
/// \param val A value to compare against
///
template<typename R, typename V>
bool none_of_equal ( const R &range, const V & val )
{
return (none_of_equal) ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn one_of_equal ( InputIterator first, InputIterator last, const V &val )
/// \return true if the value 'val' exists only once in [first, last).
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param val A value to compare against
///
template<typename InputIterator, typename V>
bool one_of_equal ( InputIterator first, InputIterator last, const V &val )
{
InputIterator i = std::find (first, last, val); // find first occurrence of 'val'
if (i == last) return false; // Didn't occur at all
return (none_of_equal) (++i, last, val);
}
/// \fn one_of_equal ( const R &range, const V &val )
/// \return true if the value 'val' exists only once in the range.
///
/// \param range The input range
/// \param val A value to compare against
///
template<typename R, typename V>
bool one_of_equal ( const R &range, const V &val )
{
return (one_of_equal) ( boost::begin ( range ), boost::end ( range ), val );
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_ALL_HPP