-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearch.h
62 lines (53 loc) · 1.79 KB
/
search.h
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
#ifndef SEARCH_H
#define SEARCH_H
#include <algorithm>
#include <iterator>
template <typename I, typename P>
// I is InputIterator, P is UnaryPredicate
// value type of I is the same as argument type of P
I find_if(I first, I last, P pred) {
while (first != last && !pred(*first)) ++first;
return first;
}
template <typename I, typename P>
// I is InputIterator, P is UnaryPredicate
// value type of I is the same as argument type of P
I find_if_not(I first, I last, P pred) {
while (first != last && pred(*first)) ++first;
return first;
}
template <typename I, typename N, typename P>
// I is InputIterator, N is Integral, P is UnaryPredicate
// value type of I is the same as argument type of P
std::pair<I, N> find_if_n(I first, N n, P pred) {
while (n && !pred(*first)) {--n; ++first;}
return std::make_pair(first, n);
}
template <typename I, typename N, typename P>
// I is InputIterator, N is Integral, P is UnaryPredicate
// value type of I is the same as argument type of P
std::pair<I, N> find_if_not_n(I first, N n, P pred) {
while (n && pred(*first)) {--n; ++first;}
return std::make_pair<I, N> (first, n);
}
template <typename I, typename P>
// I is InputIterator, P is UnaryPredicate
// value type of I is the same as argument type of P
inline
bool all_of(I first, I last, P pred) {
return find_if_not(first, last, pred) == last;
}
template <typename I, typename P>
// I is InputIterator, P is UnaryPredicate
// value type of I is the same as argument type of P
inline
bool none_of(I first, I last, P pred) {
return find_if(first, last, pred) == last;
}
template <typename I, typename P>
// I is InputIterator, P is UnaryPredicate
// value type of I is the same as argument type of P
bool any_of(I first, I last, P pred) {
return find_if(first, last, pred) != last;
}
#endif // SEARCH_H