-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicate.hpp
105 lines (85 loc) · 2.44 KB
/
predicate.hpp
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
#ifndef CPPTOOLS_UTILITY_PREDICATE_HPP
#define CPPTOOLS_UTILITY_PREDICATE_HPP
#include <concepts>
#include <cpptools/utility/deduce_parameters.hpp>
namespace tools {
template<typename F, typename T>
concept predicate = requires(F& f, const T& v) {
{ f(v) } -> std::same_as<bool>;
};
namespace pred {
template<typename T, predicate<T> P>
decltype(auto) negation(P&& pred) {
return [pred] (const T& element) -> bool {
return !pred(element);
};
}
template<typename T, predicate<T> P1, predicate<T> P2>
decltype(auto) conjunction(P1&& first, P2&& second) {
return [first, second] (const T& element) -> bool {
return first(element) && second(element);
};
}
template<typename T, predicate<T> P1, predicate<T> P2>
decltype(auto) disjunction(P1&& first, P2&& second) {
return [first, second] (const T& element) -> bool {
return first(element) || second(element);
};
}
template<typename T>
decltype(auto) equals(const T& value) {
return [value] (const T& element) -> bool {
return element == value;
};
}
template<typename T>
decltype(auto) greater_than(const T& value) {
return [value] (const T& element) -> bool {
return element > value;
};
}
template<typename T>
decltype(auto) greater_equal(const T& value) {
return [value] (const T& element) -> bool {
return element >= value;
};
}
template<typename T>
decltype(auto) less_than(const T& value) {
return [value] (const T& element) -> bool {
return element < value;
};
}
template<typename T>
decltype(auto) less_equal(const T& value) {
return [value] (const T& element) -> bool {
return element <= value;
};
}
template<typename T>
decltype(auto) between(const T& low, const T& high) {
return conjunction<T>(greater_equal<T>(low), less_equal<T>(high));
}
template<typename T>
decltype(auto) strictly_between(const T& low, const T& high) {
return conjunction<T>(greater_than<T>(low), less_than<T>(high));
}
template<typename T>
decltype(auto) outside_of(const T& low, const T& high) {
return negation<T>(between<T>(low, high));
}
template<typename P>
decltype(auto) first_member(P&& pred) {
return [pred] (auto pair) -> bool {
return pred(pair.first);
};
}
template<typename P>
decltype(auto) second_member(P&& pred) {
return [pred] (auto pair) -> bool {
return pred(pair.second);
};
}
} // namespace pred
} // namespace tools
#endif//CPPTOOLS_UTILITY_PREDICATE_HPP