-
Notifications
You must be signed in to change notification settings - Fork 3
/
kxsort.h
157 lines (133 loc) · 5.21 KB
/
kxsort.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
/* The MIT License
Copyright (c) 2016 Dinghua Li <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef KXSORT_H__
#define KXSORT_H__
#include <iterator>
#include <algorithm>
namespace kx {
static const int kRadixBits = 8;
static const size_t kInsertSortThreshold = 64;
static const int kRadixMask = (1 << kRadixBits) - 1;
static const int kRadixBin = 1 << kRadixBits;
//================= HELPING FUNCTIONS ====================
template <class T>
struct RadixTraitsUnsigned {
static const int nBytes = sizeof(T);
int kth_byte (const T &x, int k) { return (x >> (kRadixBits * k)) & kRadixMask; }
bool compare(const T &x, const T &y) { return x < y; }
};
template<class T>
struct RadixTraitsSigned {
static const int nBytes = sizeof(T);
static const T kMSB = T(0x80) << ((sizeof(T) - 1) * 8);
int kth_byte (const T &x, int k) {
return ((x ^ kMSB) >> (kRadixBits * k)) & kRadixMask;
}
bool compare(const T &x, const T &y) { return x < y; }
};
template <class RandomIt, class ValueType, class RadixTraits>
inline void insert_sort_core_(RandomIt s, RandomIt e, RadixTraits radix_traits)
{
for (RandomIt i = s + 1; i < e; ++i) {
if (radix_traits.compare(*i, *(i - 1))) {
RandomIt j;
ValueType tmp = *i;
*i = *(i - 1);
for (j = i - 1; j > s && radix_traits.compare(tmp, *(j - 1)); --j) {
*j = *(j - 1);
}
*j = tmp;
}
}
}
template <class RandomIt, class ValueType, class RadixTraits, int kWhichByte>
inline void radix_sort_core_(RandomIt s, RandomIt e, RadixTraits radix_traits)
{
RandomIt last_[kRadixBin + 1];
RandomIt *last = last_ + 1;
size_t count[kRadixBin] = {0};
for (RandomIt i = s; i < e; ++i) {
++count[radix_traits.kth_byte(*i, kWhichByte)];
}
last_[0] = last_[1] = s;
for (int i = 1; i < kRadixBin; ++i) {
last[i] = last[i-1] + count[i-1];
}
for (int i = 0; i < kRadixBin; ++i) {
RandomIt end = last[i-1] + count[i];
if (end == e) { last[i] = e; break; }
while (last[i] != end) {
ValueType swapper = *last[i];
int tag = radix_traits.kth_byte(swapper, kWhichByte);
if (tag != i) {
do {
std::swap(swapper, *last[tag]++);
} while ((tag = radix_traits.kth_byte(swapper, kWhichByte)) != i);
*last[i] = swapper;
}
++last[i];
}
}
if (kWhichByte > 0) {
for (int i = 0; i < kRadixBin; ++i) {
if (count[i] > kInsertSortThreshold) {
radix_sort_core_<RandomIt, ValueType, RadixTraits,
(kWhichByte > 0 ? (kWhichByte - 1) : 0)>
(last[i-1], last[i], radix_traits);
} else if (count[i] > 1) {
insert_sort_core_<RandomIt, ValueType, RadixTraits>(last[i-1], last[i], radix_traits);
}
}
}
}
template <class RandomIt, class ValueType, class RadixTraits>
inline void radix_sort_entry_(RandomIt s, RandomIt e, ValueType*,
RadixTraits radix_traits)
{
if (e - s <= (int)kInsertSortThreshold)
insert_sort_core_<RandomIt, ValueType, RadixTraits>(s, e, radix_traits);
else
radix_sort_core_<RandomIt, ValueType, RadixTraits, RadixTraits::nBytes - 1>(s, e, radix_traits);
}
template <class RandomIt, class ValueType>
inline void radix_sort_entry_(RandomIt s, RandomIt e, ValueType *)
{
if (ValueType(-1) > ValueType(0)) {
radix_sort_entry_(s, e, (ValueType*)(0), RadixTraitsUnsigned<ValueType>());
} else {
radix_sort_entry_(s, e, (ValueType*)(0), RadixTraitsSigned<ValueType>());
}
}
//================= INTERFACES ====================
template <class RandomIt, class RadixTraits>
inline void radix_sort(RandomIt s, RandomIt e, RadixTraits radix_traits)
{
typename std::iterator_traits<RandomIt>::value_type *dummy(0);
radix_sort_entry_(s, e, dummy, radix_traits);
}
template <class RandomIt>
inline void radix_sort(RandomIt s, RandomIt e)
{
typename std::iterator_traits<RandomIt>::value_type *dummy(0);
radix_sort_entry_(s, e, dummy);
}
}
#endif