/
distributions.h
414 lines (362 loc) · 13.9 KB
/
distributions.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright (c) 2015-2022 Morwenn
* SPDX-License-Identifier: MIT
*/
#include <algorithm>
#include <cstddef>
#include <ctime>
#include <random>
#include <string>
#include <utility>
#include <vector>
#include <cpp-sort/detail/bitops.h>
#include <cpp-sort/detail/type_traits.h>
#include <cpp-sort/utility/as_function.h>
#include <cpp-sort/utility/functional.h>
#include <cpp-sort/utility/functional.h>
// Pseudo-random number generator, used by some distributions
thread_local std::mt19937_64 distributions_prng(std::time(nullptr));
////////////////////////////////////////////////////////////
// Distributions for benchmarks
//
// Distributions are function objects used to fill the
// collections to be sorted before benchmarking the sorting
// algorithms. These distributions allow to check how a
// sorting algorithm behaves with common patterns found in
// real-life data sets, or with patterns known to adversely
// affect some classes of sorting algorithms.
namespace dist
{
template<typename Derived>
struct base_distribution
{
template<typename OutputIterator>
using fptr_t = void(*)(OutputIterator, long long int);
template<typename OutputIterator, typename Projection>
using fptr_proj_t = void(*)(OutputIterator, long long int, Projection);
template<typename OutputIterator>
operator fptr_t<OutputIterator>() const
{
return [](OutputIterator out, long long int size)
{
return Derived{}(out, size);
};
}
template<typename OutputIterator, typename Projection>
operator fptr_proj_t<OutputIterator, Projection>() const
{
return [](OutputIterator out, long long int size, Projection projection)
{
return Derived{}(out, size, std::move(projection));
};
}
};
struct shuffled:
base_distribution<shuffled>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
std::vector<int> vec;
for (long long int i = 0 ; i < size ; ++i) {
vec.emplace_back(i);
}
std::shuffle(vec.begin(), vec.end(), distributions_prng);
auto&& proj = cppsort::utility::as_function(projection);
std::transform(vec.begin(), vec.end(), out, proj);
}
static constexpr const char* output = "shuffled.txt";
};
struct shuffled_16_values:
base_distribution<shuffled_16_values>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
std::vector<int> vec;
for (long long int i = 0 ; i < size ; ++i) {
vec.emplace_back(i % 16);
}
std::shuffle(vec.begin(), vec.end(), distributions_prng);
auto&& proj = cppsort::utility::as_function(projection);
std::transform(vec.begin(), vec.end(), out, proj);
}
static constexpr const char* output = "shuffled.txt";
};
struct all_equal:
base_distribution<all_equal>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
for (long long int i = 0 ; i < size ; ++i) {
*out++ = proj(0);
}
}
static constexpr const char* output = "all_equal.txt";
};
struct ascending:
base_distribution<ascending>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
for (long long int i = 0 ; i < size ; ++i) {
*out++ = proj(i);
}
}
static constexpr const char* output = "ascending.txt";
};
struct descending:
base_distribution<descending>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
while (size--) {
*out++ = proj(size);
}
}
static constexpr const char* output = "descending.txt";
};
struct pipe_organ:
base_distribution<pipe_organ>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
for (long long int i = 0 ; i < size / 2 ; ++i) {
*out++ = proj(i);
}
for (long long int i = size / 2 ; i < size ; ++i) {
*out++ = proj(size - i);
}
}
static constexpr const char* output = "pipe_organ.txt";
};
struct push_front:
base_distribution<push_front>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
if (size > 0) {
for (long long int i = 0 ; i < size - 1 ; ++i) {
*out++ = proj(i);
}
*out = proj(0);
}
}
static constexpr const char* output = "push_front.txt";
};
struct push_middle:
base_distribution<push_middle>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
if (size > 0) {
for (long long int i = 0 ; i < size ; ++i) {
if (i != size / 2) {
*out++ = proj(i);
}
}
*out = proj(size / 2);
}
}
static constexpr const char* output = "push_middle.txt";
};
struct ascending_sawtooth:
base_distribution<ascending_sawtooth>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
long long int limit = size / cppsort::detail::log2(size) + 50;
for (long long int i = 0 ; i < size ; ++i) {
*out++ = proj(i % limit);
}
}
static constexpr const char* output = "ascending_sawtooth.txt";
};
struct ascending_sawtooth_bad:
base_distribution<ascending_sawtooth_bad>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
long long int limit = size / cppsort::detail::log2(size) - 50;
for (long long int i = 0 ; i < size ; ++i) {
*out++ = proj(i % limit);
}
}
static constexpr const char* output = "ascending_sawtooth.txt";
};
struct descending_sawtooth:
base_distribution<descending_sawtooth>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
long long int limit = size / cppsort::detail::log2(size) + 50;
while (size--) {
*out++ = proj(size % limit);
}
}
static constexpr const char* output = "descending_sawtooth.txt";
};
struct descending_sawtooth_bad:
base_distribution<descending_sawtooth_bad>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
long long int limit = size / cppsort::detail::log2(size) - 50;
while (size--) {
*out++ = proj(size % limit);
}
}
static constexpr const char* output = "descending_sawtooth.txt";
};
struct alternating:
base_distribution<alternating>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
for (long long int i = 0 ; i < size ; ++i) {
*out++ = proj((i % 2) ? i : -i);
}
}
static constexpr const char* output = "alternating.txt";
};
struct reversed_alternating:
base_distribution<reversed_alternating>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
// Especially interesting for a special case of melsort
auto&& proj = cppsort::utility::as_function(projection);
for (long long int i = size ; i > 0 ; --i) {
*out++ = proj((i % 2) ? i : -i);
}
}
static constexpr const char* output = "reversed_alternating.txt";
};
struct descending_plateau:
base_distribution<descending_plateau>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
long long int i = size;
while (i > 2 * size / 3) {
*out++ = proj(i);
--i;
}
while (i > size / 3) {
*out++ = proj(size / 2);
--i;
}
while (i > 0) {
*out++ = proj(i);
--i;
}
}
static constexpr const char* output = "descending_plateau.txt";
};
struct inversions:
base_distribution<inversions>
{
// Percentage of chances that an "out-of-place" value
// is produced for each position, the goal is to test
// Inv-adaptive algorithms
double factor;
constexpr explicit inversions(double factor) noexcept:
factor(factor)
{}
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
auto&& proj = cppsort::utility::as_function(projection);
// Generate a percentage of error
std::uniform_real_distribution<double> percent_dis(0.0, 1.0);
// Generate a random value
std::uniform_int_distribution<long long int> value_dis(0, size - 1);
for (long long int i = 0 ; i < size ; ++i) {
if (percent_dis(distributions_prng) < factor) {
*out++ = value_dis(distributions_prng);
} else {
*out++ = i;
}
}
}
static constexpr const char* output = "inversions.txt";
};
struct vergesort_killer:
base_distribution<vergesort_killer>
{
template<typename OutputIterator, typename Projection=cppsort::utility::identity>
auto operator()(OutputIterator out, long long int size, Projection projection={}) const
-> void
{
// WARNING: not for small collections, mostly because I'm lazy...
const auto size_run = size / cppsort::detail::log2(size);
auto desc = descending{};
auto killer = pipe_organ{};
auto size_output_left = size;
while (true) {
killer(out, size_run - 50, projection);
size_output_left -= size_run - 50;
if (size_output_left < size_run + 50) break;
desc(out, size_run + 50, projection);
size_output_left -= size_run + 50;
if (size_output_left < size_run - 50) break;
};
// Just in case
if (size_output_left) {
shuffled{}(out, size_output_left, projection);
}
}
static constexpr const char* output = "vergesort_killer.txt";
};
struct as_long_string:
cppsort::utility::projection_base
{
auto operator()(long long int value) const
-> std::string
{
auto str = std::to_string(value);
return std::string(50 - str.size(), '0') + std::move(str);
}
};
}