forked from Chia-Network/chiapos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.hpp
100 lines (86 loc) · 3.2 KB
/
quicksort.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
// Copyright 2018 Chia Network Inc
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_CPP_QUICKSORT_HPP_
#define SRC_CPP_QUICKSORT_HPP_
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include "util.hpp"
namespace QuickSort {
inline static void SortInner(
uint8_t *memory,
uint64_t memory_len,
uint32_t L,
uint32_t bits_begin,
uint64_t begin,
uint64_t end,
uint8_t *pivot_space)
{
if (end - begin <= 5) {
for (uint64_t i = begin + 1; i < end; i++) {
uint64_t j = i;
memcpy(pivot_space, memory + i * L, L);
while (j > begin &&
Util::MemCmpBits(memory + (j - 1) * L, pivot_space, L, bits_begin) > 0) {
memcpy(memory + j * L, memory + (j - 1) * L, L);
j--;
}
memcpy(memory + j * L, pivot_space, L);
}
return;
}
uint64_t lo = begin;
uint64_t hi = end - 1;
memcpy(pivot_space, memory + (hi * L), L);
bool left_side = true;
while (lo < hi) {
if (left_side) {
if (Util::MemCmpBits(memory + lo * L, pivot_space, L, bits_begin) < 0) {
++lo;
} else {
memcpy(memory + hi * L, memory + lo * L, L);
--hi;
left_side = false;
}
} else {
if (Util::MemCmpBits(memory + hi * L, pivot_space, L, bits_begin) > 0) {
--hi;
} else {
memcpy(memory + lo * L, memory + hi * L, L);
++lo;
left_side = true;
}
}
}
memcpy(memory + lo * L, pivot_space, L);
if (lo - begin <= end - lo) {
SortInner(memory, memory_len, L, bits_begin, begin, lo, pivot_space);
SortInner(memory, memory_len, L, bits_begin, lo + 1, end, pivot_space);
} else {
SortInner(memory, memory_len, L, bits_begin, lo + 1, end, pivot_space);
SortInner(memory, memory_len, L, bits_begin, begin, lo, pivot_space);
}
}
inline void Sort(
uint8_t *const memory,
uint32_t const entry_len,
uint64_t const num_entries,
uint32_t const bits_begin)
{
uint64_t const memory_len = (uint64_t)entry_len * num_entries;
auto const pivot_space = std::make_unique<uint8_t[]>(entry_len);
SortInner(memory, memory_len, entry_len, bits_begin, 0, num_entries, pivot_space.get());
}
}
#endif // SRC_CPP_QUICKSORT_HPP_