-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinsort.cpp
63 lines (51 loc) · 1.47 KB
/
binsort.cpp
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
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <parallel/algorithm>
#include <vector>
int main (int argc, char** argv) {
assert(argc == 2 || argc == 3 || argc == 4);
size_t compareOffset = 0;
size_t compareBytes = 0;
size_t totalBytes = 0;
// eg. 32, compare bytes[0:32], 32 total bytes
if (argc == 2) {
assert(sscanf(argv[1], "%lu", &compareBytes) == 1);
totalBytes = compareBytes;
}
// eg. 20 40, compare bytes[0:20], 40 total bytes
if (argc == 3) {
assert(sscanf(argv[1], "%lu", &compareBytes) == 1);
assert(sscanf(argv[2], "%lu", &totalBytes) == 1);
}
// eg. 2 22 22, compare bytes[2:22], 22 total bytes
if (argc == 4) {
assert(sscanf(argv[1], "%lu", &compareOffset) == 1);
assert(sscanf(argv[2], "%lu", &compareBytes) == 1);
assert(compareBytes >= compareOffset);
compareBytes = compareBytes - compareOffset;
assert(sscanf(argv[3], "%lu", &totalBytes) == 1);
}
assert(compareBytes > 0);
assert(compareOffset < totalBytes);
std::vector<uint8_t*> vector;
while (true) {
const auto slice = new uint8_t[totalBytes];
const auto read = fread(slice, totalBytes, 1, stdin);
// EOF?
if (read == 0) break;
vector.emplace_back(slice);
}
__gnu_parallel::sort(
vector.begin(), vector.end(),
[=](const auto& a, const auto& b) {
return memcmp(a + compareOffset, b + compareOffset, compareBytes) < 0;
}
);
for (auto&& slice : vector) {
fwrite(slice, totalBytes, 1, stdout);
// delete[] slice;
}
return 0;
}