forked from boostorg/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.hpp
More file actions
274 lines (243 loc) · 7.92 KB
/
all.hpp
File metadata and controls
274 lines (243 loc) · 7.92 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.
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
*/
/// \file all.hpp
/// \brief Test ranges against predicates.
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_ALL_HPP
#define BOOST_ALGORITHM_ALL_HPP
#include <boost/range.hpp> // For boost::begin and boost::end
// I would love to use the all_of, any_of and none_of that are in the C++0x
// standard library if they are available, but I don't know how to do that.
// -- mtc 11-May-2009
// Something like:
// #ifdef <something>
// use std::all_of;
// #else
// template<typename I, typename V>
// bool all_of ( I first, I last, const V &val )
// ... and so on.
// #endif
namespace boost { namespace algorithm {
/// \fn all_of ( I first, I last, const V &val )
/// \brief Returns 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 I, typename V>
bool all_of ( I first, I last, const V &val )
{
while (first != last) {
if ( *first++ != val )
return false;
}
return true;
}
/// \fn all_of ( Range range, const V &val )
/// \brief Returns 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 Range, typename V>
bool all_of ( Range range, const V &val )
{
return all_of ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn all_of_if ( I first, I last, Pred p )
/// \brief Returns true if all 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
///
template<typename I, typename Pred>
bool all_of_if ( I first, I last, Pred p )
{
while (first != last) {
if ( !p(*first++))
return false;
}
return true;
}
/// \fn all_of_if ( Range range, Pred p )
/// \brief Returns true if all elements in the range satisfy the predicate
///
/// \param range The input range
/// \param p A predicate to test the elements
///
template<typename Range, typename Pred>
bool all_of_if ( Range range, Pred p )
{
return all_of_if ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn none_of ( I first, I last, const V &val )
/// \brief Returns 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 I, typename V>
bool none_of ( I first, I last, const V &val )
{
while (first != last) {
if ( *first++ == val )
return false;
}
return true;
}
/// \fn none_of ( Range range, const V &val )
/// \brief Returns 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 Range, typename V>
bool none_of ( Range range, const V & val )
{
return none_of ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn none_of_if ( I first, I last, Pred p )
/// \brief Returns true if none 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
///
template<typename I, typename Pred>
bool none_of_if ( I first, I last, Pred p )
{
while (first != last) {
if ( p(*first++))
return false;
}
return true;
}
/// \fn none_of_if ( Range range, Pred p )
/// \brief Returns true if none of the elements in the range satisfy the predicate
///
/// \param range The input range
/// \param p A predicate to test the elements
///
template<typename Range, typename Pred>
bool none_if ( Range range, Pred p )
{
return none_of_if ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn any_of ( I first, I last, const V &val )
/// \brief Returns 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 I, typename V>
bool any_of ( I first, I last, const V &val )
{
while (first != last) {
if ( *first++ == val )
return true;
}
return false;
}
/// \fn any_of ( Range range, const V &val )
/// \brief Returns 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 Range, typename V>
bool any_of ( Range range, const V &val )
{
return any_of ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn any_of_if ( I first, I last, Pred p )
/// \brief Returns 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
///
template<typename I, typename Pred>
bool any_of_if ( I first, I last, Pred p)
{
while (first != last) {
if ( p(*first++))
return true;
}
return false;
}
/// \fn any_of_if ( Range range, Pred p )
/// \brief Returns true if any elements in the range satisfy the predicate
///
/// \param range The input range
/// \param p A predicate to test the elements
///
template<typename Range, typename Pred>
bool any_of_if ( Range range, Pred p )
{
return any_of_if ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn exists_and_only ( I first, I last, const V &val )
/// \brief Returns 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 I, typename V>
bool exists_and_only ( I first, I last, const V &val )
{
I i = std::find (first, last, val);
if (i == last) return false;
if ( std::find (++i, last, val) != last) return false;
return true;
}
/// \fn exists_and_only ( Range range, const V &val )
/// \brief Returns 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 Range, typename V>
bool exists_and_only ( Range range, const V &val )
{
return exists_and_only ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn exists_and_only_if ( I first, I last, Pred p )
/// \brief Returns 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 to test the elements
///
template<typename I, typename Pred>
bool exists_and_only_if ( I first, I last, Pred p )
{
I i = find_if (first, last, p);
if (i == last) return false;
if (find_if(++i, last, p) != last) return false;
return true;
}
/// \fn exists_and_only_if ( Range range, Pred p )
/// \brief Returns true if the predicate 'p' is true for exactly one item in the range.
///
/// \param range The input range
/// \param p A predicate to test the elements
///
template<typename Range, typename Pred>
bool exists_and_only_if ( Range range, Pred p )
{
return exists_and_only_if ( boost::begin ( range ), boost::end ( range ), p );
}
}} // namespace boost and algorithm
#undef ANY_DOING_0X
#endif // BOOST_ALGORITHM_ALL_HPP