-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpartition.h
218 lines (195 loc) · 6.33 KB
/
partition.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
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
#ifndef PARTITION_H
#define PARTITION_H
#include "search.h"
// the semantics of the standard:
// elements satisfying the predicate are before elements that do not
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 is_partitioned(I first, I last, P pred) {
I first_false = find_if_not(first, last, pred);
I next_true = find_if(first_false, last, pred);
return next_true == last;
}
// the semantics of the standard:
// elements satisfying the predicate are before elements that do not
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
inline
bool is_partitioned_n(I first, N n, P pred) {
std::pair<I, N> first_false = find_if_not_n(first, n, pred);
std::pair<I, N> next_true = find_if_n(first_false.first, first_false.second, pred);
return !next_true.second;
}
// the semantics of the standard:
// elements satisfying the predicate are before elements that do not
template <typename I, typename N, typename P>
// I is ForwardIterator, N is Integral, P is UnaryPredicate
// value type of I is the same as argument type of P
I partition_point_n(I first, N n, P pred) {
// precondition: is_partitioned_n(first, n, pred)
while (n) {
N half = n >> 1;
I middle = first;
std::advance(middle, half);
// [first, n) == [first, half) U [middle, n - half)
if (pred(*middle)) {
n -= (half + 1);
first = ++middle;
}
else {
n = half;
}
}
return first;
}
// the semantics of the standard:
// elements satisfying the predicate are before elements that do not
template <typename I, typename P>
// I is ForwardIterator, P is UnaryPredicate
// value type of I is the same as argument type of P
inline
I partition_point(I first, I last, P pred) {
// precondition: is_partitioned(first, last, pred)
return partition_point_n(first, std::distance(first, last), pred);
}
template <typename I, typename R>
// I is ForwardIterator
// R is WeakStrictOrdering on the value type of I
bool is_sorted(I first, I last, R r) {
if (first == last) return true;
I previous = first;
while (++first != last && !r(*first, *previous)) previous = first;
return first == last;
}
template <typename I>
// I is ForwardIterator with a totally ordered value type
inline
bool is_sorted(I first, I last) {
typedef typename std::iterator_traits<I>::value_type T;
return is_sorted(first, last, std::less<T>());
}
template <typename I, typename N, typename R>
// I is ForwardIterator
// N is Integral
// R is WeakStrictOrdering on the value type of I
bool is_sorted_n(I first, N n, R r) {
if (!n) return true;
I previous = first;
while (n && !r(*++first, *previous)) {
previous = first;
--n;
}
return !n;
}
template <typename I, typename N>
// I is ForwardIterator with a totally ordered value type
// N is Integral
inline
bool is_sorted_n(I first, N n) {
typedef typename std::iterator_traits<I>::value_type T;
return is_sorted_n(first, n, std::less<T>());
}
template <typename R, typename T>
// R is StrictWeakOrdering
// T is an argument type of R
class lower_bound_predicate
{
private:
R r;
const T* a;
public:
lower_bound_predicate(const R& r, const T& a) : r(r), a(&a) {}
bool operator()(const T& x) { return r(x, *a); }
};
template <typename I, typename N, typename R>
// I is ForwardIterator
// N is Integral
// R is WeakStrictOrdering on the value type of I
inline
I lower_bound_n(I first, N n,
const typename std::iterator_traits<I>::value_type& a, R r) {
// precondition: is_sorted_n(first, n, r)
typedef typename std::iterator_traits<I>::value_type T;
return partition_point_n(first, n, lower_bound_predicate<R, T>(r, a));
}
template <typename I, typename R>
// I is ForwardIterator
// R is WeakStrictOrdering on the value type of I
inline
I lower_bound(I first, I last, R r,
const typename std::iterator_traits<I>::value_type& a) {
// precondition: is_sorted(first, last, r)
return lower_bound_n(first, std::distance(first, last), r);
}
template <typename I, typename N>
// I is ForwardIterator with a totally ordered value type
// N is Integral
inline
I lower_bound_n(I first, N n,
const typename std::iterator_traits<I>::value_type& a) {
// precondition: is_sorted_n(first, n)
typedef typename std::iterator_traits<I>::value_type T;
return lower_bound_n(first, n, std::less<T>());
}
template <typename I>
// I is ForwardIterator with a totally ordered value type
inline
I lower_bound(I first, I last,
const typename std::iterator_traits<I>::value_type& a) {
// precondition: is_sorted(first, last)
return lower_bound_n(first, std::distance(first, last));
}
template <typename R, typename T>
// R is StrictWeakOrdering
// T is an argument type of R
class upper_bound_predicate
{
private:
R r;
const T* a;
public:
upper_bound_predicate(const R& r, const T& a) : r(r), a(&a) {}
bool operator()(const T& x) { return !r(*a, x); }
};
template <typename I, typename N, typename R>
// I is ForwardIterator
// N is Integral
// R is WeakStrictOrdering on the value type of I
inline
I upper_bound_n(I first, N n,
const typename std::iterator_traits<I>::value_type& a, R r) {
// precondition: is_sorted_n(first, n, r)
typedef typename std::iterator_traits<I>::value_type T;
return partition_point_n(first, n, upper_bound_predicate<R, T>(r, a));
}
template <typename I, typename R>
// I is ForwardIterator
// R is WeakStrictOrdering on the value type of I
inline
I upper_bound(I first, I last, R r,
const typename std::iterator_traits<I>::value_type& a) {
// precondition: is_sorted(first, last, r)
return upper_bound_n(first, std::distance(first, last), r);
}
template <typename I, typename N>
// I is ForwardIterator with a totally ordered value type
// N is Integral
inline
I upper_bound_n(I first, N n,
const typename std::iterator_traits<I>::value_type& a) {
// precondition: is_sorted_n(first, n)
typedef typename std::iterator_traits<I>::value_type T;
return upper_bound_n(first, n, std::less<T>());
}
template <typename I>
// I is ForwardIterator with a totally ordered value type
inline
I upper_bound(I first, I last,
const typename std::iterator_traits<I>::value_type& a) {
// precondition: is_sorted(first, last)
return upper_bound_n(first, std::distance(first, last));
}
#endif