-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathindirect_sort_test.cpp
More file actions
348 lines (280 loc) · 12.8 KB
/
indirect_sort_test.cpp
File metadata and controls
348 lines (280 loc) · 12.8 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Copyright (c) Marshall Clow 2023.
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)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/indirect_sort.hpp>
#include <boost/algorithm/apply_permutation.hpp>
#include <boost/algorithm/cxx11/is_sorted.hpp>
#include <boost/algorithm/cxx11/all_of.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <list>
using boost::algorithm::Permutation;
// A permutation of size N is a sequence of values in the range [0..N)
// such that no value appears more than once in the permutation.
bool is_a_permutation(Permutation p, size_t N) {
if (p.size() != N) return false;
// Sort the permutation, and ensure that each value appears exactly once.
std::sort(p.begin(), p.end());
for (size_t i = 0; i < N; ++i)
if (p[i] != i) return false;
return true;
}
template <typename Iter,
typename Comp = typename std::less<typename std::iterator_traits<Iter>::value_type> >
struct indirect_comp {
indirect_comp (Iter it, Comp c = Comp())
: iter_(it), comp_(c) {}
bool operator ()(size_t a, size_t b) const { return comp_(iter_[a], iter_[b]);}
Iter iter_;
Comp comp_;
};
//// =======================
//// ==== indirect_sort ====
//// =======================
template <typename Iter>
void test_one_sort(Iter first, Iter last) {
Permutation perm = boost::algorithm::indirect_sort(first, last);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
BOOST_CHECK (boost::algorithm::is_sorted(perm.begin(), perm.end(), indirect_comp<Iter>(first)));
// Make a copy of the data, apply the permutation, and ensure that it is sorted.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
BOOST_CHECK (boost::algorithm::is_sorted(v.begin(), v.end()));
}
template <typename Iter, typename Comp>
void test_one_sort(Iter first, Iter last, Comp comp) {
Permutation perm = boost::algorithm::indirect_sort(first, last, comp);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
BOOST_CHECK (boost::algorithm::is_sorted(perm.begin(), perm.end(),
indirect_comp<Iter, Comp>(first, comp)));
// Make a copy of the data, apply the permutation, and ensure that it is sorted.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
BOOST_CHECK (boost::algorithm::is_sorted(v.begin(), v.end(), comp));
}
BOOST_AUTO_TEST_CASE(test_sort) {
int num[] = { 1,3,5,7,9, 2, 4, 6, 8, 10 };
const int sz = sizeof (num)/sizeof(num[0]);
int *first = &num[0];
int const *cFirst = &num[0];
// Test subsets
for (size_t i = 0; i <= sz; ++i) {
test_one_sort(first, first + i);
test_one_sort(first, first + i, std::greater<int>());
// test with constant inputs
test_one_sort(cFirst, cFirst + i);
test_one_sort(cFirst, cFirst + i, std::greater<int>());
}
// make sure we work with iterators as well as pointers
std::vector<int> v(first, first + sz);
test_one_sort(v.begin(), v.end());
test_one_sort(v.begin(), v.end(), std::greater<int>());
}
//// ==============================
//// ==== indirect_stable_sort ====
//// ==============================
template <typename T1, typename T2>
struct MyPair {
MyPair () {}
MyPair (const T1 &t1, const T2 &t2)
: first(t1), second(t2) {}
T1 first;
T2 second;
};
template <typename T1, typename T2>
bool operator < (const MyPair<T1, T2>& lhs, const MyPair<T1, T2>& rhs) {
return lhs.first < rhs.first; // compare only the first elements
}
template <typename T1, typename T2>
bool MyGreater (const MyPair<T1, T2>& lhs, const MyPair<T1, T2>& rhs) {
return lhs.first > rhs.first; // compare only the first elements
}
template <typename Iter>
void test_one_stable_sort(Iter first, Iter last) {
Permutation perm = boost::algorithm::indirect_stable_sort(first, last);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
BOOST_CHECK (boost::algorithm::is_sorted(perm.begin(), perm.end(), indirect_comp<Iter>(first)));
if (first != last) {
Iter iFirst = first;
Iter iSecond = first; ++iSecond;
while (iSecond != last) {
if (iFirst->first == iSecond->first)
BOOST_CHECK(iFirst->second < iSecond->second);
++iFirst;
++iSecond;
}
}
// Make a copy of the data, apply the permutation, and ensure that it is sorted.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
BOOST_CHECK (boost::algorithm::is_sorted(v.begin(), v.end()));
}
template <typename Iter, typename Comp>
void test_one_stable_sort(Iter first, Iter last, Comp comp) {
Permutation perm = boost::algorithm::indirect_stable_sort(first, last, comp);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
BOOST_CHECK (boost::algorithm::is_sorted(perm.begin(), perm.end(), indirect_comp<Iter, Comp>(first, comp)));
if (first != last) {
Iter iFirst = first;
Iter iSecond = first; ++iSecond;
while (iSecond != last) {
if (iFirst->first == iSecond->first)
BOOST_CHECK(iFirst->second < iSecond->second);
++iFirst;
++iSecond;
}
}
// Make a copy of the data, apply the permutation, and ensure that it is sorted.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
BOOST_CHECK (boost::algorithm::is_sorted(v.begin(), v.end(), comp));
}
BOOST_AUTO_TEST_CASE(test_stable_sort) {
typedef MyPair<int, long> Pair;
const int sz = 10;
Pair vals[sz];
for (int i = 0; i < sz; ++i) {
vals[i].first = 100 - (i >> 1);
vals[i].second = i;
}
Pair *first = &vals[0];
Pair const *cFirst = &vals[0];
// Test subsets
for (size_t i = 0; i <= sz; ++i) {
test_one_stable_sort(first, first + i);
test_one_stable_sort(first, first + i, MyGreater<int, long>);
// test with constant inputs
test_one_sort(cFirst, cFirst + i);
test_one_sort(cFirst, cFirst + i, MyGreater<int, long>);
}
}
//// ===============================
//// ==== indirect_partial_sort ====
//// ===============================
template <typename Iter>
void test_one_partial_sort(Iter first, Iter middle, Iter last) {
const size_t middleIdx = std::distance(first, middle);
Permutation perm = boost::algorithm::indirect_partial_sort(first, middle, last);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
BOOST_CHECK (boost::algorithm::is_sorted(perm.begin(), perm.begin() + middleIdx, indirect_comp<Iter>(first)));
// Make a copy of the data, apply the permutation, and ensure that it is sorted.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
BOOST_CHECK (boost::algorithm::is_sorted(v.begin(), v.begin() + middleIdx));
// Make sure that [middle, end) are all "greater" than the sorted part
if (middleIdx > 0) {
typename Vector::iterator lastSorted = v.begin() + middleIdx - 1;
for (typename Vector::iterator it = v.begin () + middleIdx; it != v.end(); ++it)
BOOST_CHECK(*lastSorted < *it);
}
}
template <typename Iter, typename Comp>
void test_one_partial_sort(Iter first, Iter middle, Iter last, Comp comp) {
const size_t middleIdx = std::distance(first, middle);
Permutation perm = boost::algorithm::indirect_partial_sort(first, middle, last, comp);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
BOOST_CHECK (boost::algorithm::is_sorted(perm.begin(), perm.begin() + middleIdx,
indirect_comp<Iter, Comp>(first, comp)));
// Make a copy of the data, apply the permutation, and ensure that it is sorted.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
BOOST_CHECK (boost::algorithm::is_sorted(v.begin(), v.begin() + middleIdx, comp));
// Make sure that [middle, end) are all "greater" than the sorted part
if (middleIdx > 0) {
typename Vector::iterator lastSorted = v.begin() + middleIdx - 1;
for (typename Vector::iterator it = v.begin () + middleIdx; it != v.end(); ++it)
BOOST_CHECK(comp(*lastSorted, *it));
}
}
BOOST_AUTO_TEST_CASE(test_partial_sort) {
int num[] = { 1,3,5,7,9, 2, 4, 6, 8, 10 };
const int sz = sizeof (num)/sizeof(num[0]);
int *first = &num[0];
int const *cFirst = &num[0];
// Test subsets
for (size_t i = 0; i <= sz; ++i) {
for (size_t j = 0; j < i; ++j) {
test_one_partial_sort(first, first + j, first + i);
test_one_partial_sort(first, first + j, first + i, std::greater<int>());
// test with constant inputs
test_one_partial_sort(cFirst, cFirst + j, cFirst + i);
test_one_partial_sort(cFirst, cFirst + j, cFirst + i, std::greater<int>());
}
}
// make sure we work with iterators as well as pointers
std::vector<int> v(first, first + sz);
test_one_partial_sort(v.begin(), v.begin() + (sz / 2), v.end());
test_one_partial_sort(v.begin(), v.begin() + (sz / 2), v.end(), std::greater<int>());
}
//// ===================================
//// ==== indirect_nth_element_sort ====
//// ===================================
template <typename Iter>
void test_one_nth_element(Iter first, Iter nth, Iter last) {
const size_t nthIdx = std::distance(first, nth);
Permutation perm = boost::algorithm::indirect_nth_element(first, nth, last);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
for (size_t i = 0; i < nthIdx; ++i)
BOOST_CHECK(!(first[perm[nthIdx]] < first[perm[i]])); // all items before the nth element are <= the nth element
for (size_t i = nthIdx; i < std::distance(first, last); ++i)
BOOST_CHECK(!(first[perm[i]] < first[perm[nthIdx]])); // all items before the nth element are >= the nth element
// Make a copy of the data, apply the permutation, and ensure that the result is correct.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
for (size_t i = 0; i < nthIdx; ++i)
BOOST_CHECK(!(v[nthIdx] < v[i])); // all items before the nth element are <= the nth element
for (size_t i = nthIdx; i < v.size(); ++i)
BOOST_CHECK(!(v[i] < v[nthIdx])); // all items before the nth element are >= the nth element
}
template <typename Iter, typename Comp>
void test_one_nth_element(Iter first, Iter nth, Iter last, Comp comp) {
const size_t nthIdx = std::distance(first, nth);
Permutation perm = boost::algorithm::indirect_nth_element(first, nth, last, comp);
BOOST_CHECK (is_a_permutation(perm, std::distance(first, last)));
for (size_t i = 0; i < nthIdx; ++i)
BOOST_CHECK(!comp(first[perm[nthIdx]], first[perm[i]])); // all items before the nth element are <= the nth element
for (size_t i = nthIdx; i < std::distance(first, last); ++i)
BOOST_CHECK(!comp(first[perm[i]], first[perm[nthIdx]])); // all items before the nth element are >= the nth element
// Make a copy of the data, apply the permutation, and ensure that the result is correct.
typedef std::vector<typename std::iterator_traits<Iter>::value_type> Vector;
Vector v(first, last);
boost::algorithm::apply_permutation(v.begin(), v.end(), perm.begin(), perm.end());
for (size_t i = 0; i < nthIdx; ++i)
BOOST_CHECK(!comp(v[nthIdx], v[i])); // all items before the nth element are <= the nth element
for (size_t i = nthIdx; i < v.size(); ++i)
BOOST_CHECK(!comp(v[i], v[nthIdx])); // all items before the nth element are >= the nth element
}
BOOST_AUTO_TEST_CASE(test_nth_element) {
int num[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 1, 2, 3, 4, 5 };
const int sz = sizeof (num)/sizeof(num[0]);
int *first = &num[0];
int const *cFirst = &num[0];
// Test subsets
for (size_t i = 0; i <= sz; ++i) {
for (size_t j = 0; j < i; ++j) {
test_one_nth_element(first, first + j, first + i);
test_one_nth_element(first, first + j, first + i, std::greater<int>());
// test with constant inputs
test_one_nth_element(cFirst, cFirst + j, cFirst + i);
test_one_nth_element(cFirst, cFirst + j, cFirst + i, std::greater<int>());
}
}
// make sure we work with iterators as well as pointers
std::vector<int> v(first, first + sz);
test_one_nth_element(v.begin(), v.begin() + (sz / 2), v.end());
test_one_nth_element(v.begin(), v.begin() + (sz / 2), v.end(), std::greater<int>());
}