0
2
0
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2009 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef RADIX_SORT_H |
20 | 20 |
#define RADIX_SORT_H |
21 | 21 |
|
22 | 22 |
/// \ingroup auxalg |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Radix sort |
25 | 25 |
/// |
26 | 26 |
/// Linear time sorting algorithms |
27 | 27 |
|
28 | 28 |
#include <vector> |
29 | 29 |
#include <limits> |
30 | 30 |
#include <iterator> |
31 | 31 |
#include <algorithm> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
namespace _radix_sort_bits { |
36 | 36 |
|
37 | 37 |
template <typename Value> |
38 | 38 |
struct Identity { |
39 | 39 |
const Value& operator()(const Value& val) { |
40 | 40 |
return val; |
41 | 41 |
} |
42 | 42 |
}; |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
template <typename Value, typename Iterator, typename Functor> |
46 | 46 |
Iterator radixSortPartition(Iterator first, Iterator last, |
47 | 47 |
Functor functor, Value mask) { |
48 | 48 |
while (first != last && !(functor(*first) & mask)) { |
49 | 49 |
++first; |
50 | 50 |
} |
51 | 51 |
if (first == last) { |
52 | 52 |
return first; |
53 | 53 |
} |
54 | 54 |
--last; |
55 | 55 |
while (first != last && (functor(*last) & mask)) { |
56 | 56 |
--last; |
57 | 57 |
} |
58 | 58 |
if (first == last) { |
59 | 59 |
return first; |
60 | 60 |
} |
61 | 61 |
std::iter_swap(first, last); |
62 | 62 |
++first; |
63 | 63 |
if (!(first < last)) { |
64 | 64 |
return first; |
65 | 65 |
} |
66 | 66 |
while (true) { |
67 | 67 |
while (!(functor(*first) & mask)) { |
68 | 68 |
++first; |
69 | 69 |
} |
70 | 70 |
--last; |
71 | 71 |
while (functor(*last) & mask) { |
72 | 72 |
--last; |
73 | 73 |
} |
74 | 74 |
if (!(first < last)) { |
75 | 75 |
return first; |
76 | 76 |
} |
77 | 77 |
std::iter_swap(first, last); |
78 | 78 |
++first; |
79 | 79 |
} |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
template <typename Iterator, typename Functor> |
83 | 83 |
Iterator radixSortSignPartition(Iterator first, Iterator last, |
84 | 84 |
Functor functor) { |
85 | 85 |
while (first != last && functor(*first) < 0) { |
86 | 86 |
++first; |
87 | 87 |
} |
88 | 88 |
if (first == last) { |
89 | 89 |
return first; |
90 | 90 |
} |
91 | 91 |
--last; |
92 | 92 |
while (first != last && functor(*last) >= 0) { |
93 | 93 |
--last; |
94 | 94 |
} |
95 | 95 |
if (first == last) { |
96 | 96 |
return first; |
97 | 97 |
} |
98 | 98 |
std::iter_swap(first, last); |
99 | 99 |
++first; |
100 | 100 |
if (!(first < last)) { |
101 | 101 |
return first; |
102 | 102 |
} |
103 | 103 |
while (true) { |
104 | 104 |
while (functor(*first) < 0) { |
105 | 105 |
++first; |
106 | 106 |
} |
107 | 107 |
--last; |
108 | 108 |
while (functor(*last) >= 0) { |
109 | 109 |
--last; |
110 | 110 |
} |
111 | 111 |
if (!(first < last)) { |
112 | 112 |
return first; |
113 | 113 |
} |
114 | 114 |
std::iter_swap(first, last); |
115 | 115 |
++first; |
116 | 116 |
} |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
template <typename Value, typename Iterator, typename Functor> |
120 | 120 |
void radixIntroSort(Iterator first, Iterator last, |
121 | 121 |
Functor functor, Value mask) { |
122 | 122 |
while (mask != 0 && last - first > 1) { |
123 | 123 |
Iterator cut = radixSortPartition(first, last, functor, mask); |
124 | 124 |
mask >>= 1; |
125 | 125 |
radixIntroSort(first, cut, functor, mask); |
126 | 126 |
first = cut; |
127 | 127 |
} |
128 | 128 |
} |
129 | 129 |
|
130 | 130 |
template <typename Value, typename Iterator, typename Functor> |
131 | 131 |
void radixSignedSort(Iterator first, Iterator last, Functor functor) { |
132 | 132 |
|
133 | 133 |
Iterator cut = radixSortSignPartition(first, last, functor); |
134 | 134 |
|
135 | 135 |
Value mask; |
136 | 136 |
int max_digit; |
137 | 137 |
Iterator it; |
138 | 138 |
|
139 | 139 |
mask = ~0; max_digit = 0; |
140 | 140 |
for (it = first; it != cut; ++it) { |
141 | 141 |
while ((mask & functor(*it)) != mask) { |
142 | 142 |
++max_digit; |
143 | 143 |
mask <<= 1; |
144 | 144 |
} |
145 | 145 |
} |
146 | 146 |
radixIntroSort(first, cut, functor, 1 << max_digit); |
147 | 147 |
|
148 | 148 |
mask = 0; max_digit = 0; |
149 | 149 |
for (it = cut; it != last; ++it) { |
150 | 150 |
while ((mask | functor(*it)) != mask) { |
151 | 151 |
++max_digit; |
152 | 152 |
mask <<= 1; mask |= 1; |
153 | 153 |
} |
154 | 154 |
} |
155 | 155 |
radixIntroSort(cut, last, functor, 1 << max_digit); |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
template <typename Value, typename Iterator, typename Functor> |
159 | 159 |
void radixUnsignedSort(Iterator first, Iterator last, Functor functor) { |
160 | 160 |
|
161 | 161 |
Value mask = 0; |
162 | 162 |
int max_digit = 0; |
163 | 163 |
|
164 | 164 |
Iterator it; |
165 | 165 |
for (it = first; it != last; ++it) { |
166 | 166 |
while ((mask | functor(*it)) != mask) { |
167 | 167 |
++max_digit; |
168 | 168 |
mask <<= 1; mask |= 1; |
169 | 169 |
} |
170 | 170 |
} |
171 | 171 |
radixIntroSort(first, last, functor, 1 << max_digit); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
|
175 | 175 |
template <typename Value, |
176 | 176 |
bool sign = std::numeric_limits<Value>::is_signed > |
177 | 177 |
struct RadixSortSelector { |
178 | 178 |
template <typename Iterator, typename Functor> |
179 | 179 |
static void sort(Iterator first, Iterator last, Functor functor) { |
180 | 180 |
radixSignedSort<Value>(first, last, functor); |
181 | 181 |
} |
182 | 182 |
}; |
183 | 183 |
|
184 | 184 |
template <typename Value> |
185 | 185 |
struct RadixSortSelector<Value, false> { |
186 | 186 |
template <typename Iterator, typename Functor> |
187 | 187 |
static void sort(Iterator first, Iterator last, Functor functor) { |
188 | 188 |
radixUnsignedSort<Value>(first, last, functor); |
189 | 189 |
} |
190 | 190 |
}; |
191 | 191 |
|
192 | 192 |
} |
193 | 193 |
|
194 | 194 |
/// \ingroup auxalg |
195 | 195 |
/// |
196 | 196 |
/// \brief Sorts the STL compatible range into ascending order. |
197 | 197 |
/// |
198 | 198 |
/// The \c radixSort sorts an STL compatible range into ascending |
199 | 199 |
/// order. The radix sort algorithm can sort items which are mapped |
200 | 200 |
/// to integers with an adaptable unary function \c functor and the |
201 | 201 |
/// order will be ascending according to these mapped values. |
202 | 202 |
/// |
203 | 203 |
/// It is also possible to use a normal function instead |
204 | 204 |
/// of the functor object. If the functor is not given it will use |
205 | 205 |
/// the identity function instead. |
206 | 206 |
/// |
207 | 207 |
/// This is a special quick sort algorithm where the pivot |
208 | 208 |
/// values to split the items are choosen to be \f$ 2^k \f$ for each \c k. |
209 | 209 |
/// Therefore, the time complexity of the |
210 | 210 |
/// algorithm is \f$ O(\log(c)n) \f$ and it uses \f$ O(\log(c)) \f$, |
211 | 211 |
/// additional space, where \c c is the maximal value and \c n is the |
212 | 212 |
/// number of the items in the container. |
213 | 213 |
/// |
214 | 214 |
/// \param first The begin of the given range. |
215 | 215 |
/// \param last The end of the given range. |
216 | 216 |
/// \param functor An adaptible unary function or a normal function |
217 | 217 |
/// which maps the items to any integer type which can be either |
218 | 218 |
/// signed or unsigned. |
219 | 219 |
/// |
220 | 220 |
/// \sa stableRadixSort() |
221 | 221 |
template <typename Iterator, typename Functor> |
222 | 222 |
void radixSort(Iterator first, Iterator last, Functor functor) { |
223 | 223 |
using namespace _radix_sort_bits; |
224 | 224 |
typedef typename Functor::result_type Value; |
225 | 225 |
RadixSortSelector<Value>::sort(first, last, functor); |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
template <typename Iterator, typename Value, typename Key> |
229 | 229 |
void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) { |
230 | 230 |
using namespace _radix_sort_bits; |
231 | 231 |
RadixSortSelector<Value>::sort(first, last, functor); |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
template <typename Iterator, typename Value, typename Key> |
235 | 235 |
void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) { |
236 | 236 |
using namespace _radix_sort_bits; |
237 | 237 |
RadixSortSelector<Value>::sort(first, last, functor); |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
template <typename Iterator, typename Value, typename Key> |
241 | 241 |
void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) { |
242 | 242 |
using namespace _radix_sort_bits; |
243 | 243 |
RadixSortSelector<Value>::sort(first, last, functor); |
244 | 244 |
} |
245 | 245 |
|
246 | 246 |
template <typename Iterator, typename Value, typename Key> |
247 | 247 |
void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) { |
248 | 248 |
using namespace _radix_sort_bits; |
249 | 249 |
RadixSortSelector<Value>::sort(first, last, functor); |
250 | 250 |
} |
251 | 251 |
|
252 | 252 |
template <typename Iterator> |
253 | 253 |
void radixSort(Iterator first, Iterator last) { |
254 | 254 |
using namespace _radix_sort_bits; |
255 | 255 |
typedef typename std::iterator_traits<Iterator>::value_type Value; |
256 | 256 |
RadixSortSelector<Value>::sort(first, last, Identity<Value>()); |
257 | 257 |
} |
258 | 258 |
|
259 | 259 |
namespace _radix_sort_bits { |
260 | 260 |
|
261 | 261 |
template <typename Value> |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2009 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/time_measure.h> |
20 | 20 |
#include <lemon/smart_graph.h> |
21 | 21 |
#include <lemon/maps.h> |
22 | 22 |
#include <lemon/radix_sort.h> |
23 | 23 |
#include <lemon/math.h> |
24 | 24 |
|
25 | 25 |
#include "test_tools.h" |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <algorithm> |
29 | 29 |
|
30 | 30 |
using namespace lemon; |
31 | 31 |
|
32 | 32 |
static const int n = 10000; |
33 | 33 |
|
34 | 34 |
struct Negate { |
35 | 35 |
typedef int argument_type; |
36 | 36 |
typedef int result_type; |
37 | 37 |
int operator()(int a) { return - a; } |
38 | 38 |
}; |
39 | 39 |
|
40 | 40 |
int negate(int a) { return - a; } |
41 | 41 |
|
42 | 42 |
|
43 | 43 |
void generateIntSequence(int n, std::vector<int>& data) { |
44 | 44 |
int prime = 9973; |
45 | 45 |
int root = 136, value = 1; |
46 | 46 |
for (int i = 0; i < n; ++i) { |
47 | 47 |
data.push_back(value - prime / 2); |
48 | 48 |
value = (value * root) % prime; |
49 | 49 |
} |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
void generateCharSequence(int n, std::vector<unsigned char>& data) { |
53 | 53 |
int prime = 251; |
54 | 54 |
int root = 3, value = root; |
55 | 55 |
for (int i = 0; i < n; ++i) { |
56 | 56 |
data.push_back(static_cast<unsigned char>(value)); |
57 | 57 |
value = (value * root) % prime; |
58 | 58 |
} |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
void checkRadixSort() { |
62 | 62 |
{ |
63 | 63 |
std::vector<int> data1; |
64 | 64 |
generateIntSequence(n, data1); |
65 | 65 |
|
66 | 66 |
std::vector<int> data2(data1); |
67 | 67 |
std::sort(data1.begin(), data1.end()); |
68 | 68 |
|
69 | 69 |
radixSort(data2.begin(), data2.end()); |
70 | 70 |
for (int i = 0; i < n; ++i) { |
71 | 71 |
check(data1[i] == data2[i], "Test failed"); |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
radixSort(data2.begin(), data2.end(), Negate()); |
75 | 75 |
for (int i = 0; i < n; ++i) { |
76 | 76 |
check(data1[i] == data2[n - 1 - i], "Test failed"); |
77 | 77 |
} |
78 | 78 |
|
79 | 79 |
radixSort(data2.begin(), data2.end(), negate); |
80 | 80 |
for (int i = 0; i < n; ++i) { |
81 | 81 |
check(data1[i] == data2[n - 1 - i], "Test failed"); |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
{ |
87 | 87 |
std::vector<unsigned char> data1(n); |
88 | 88 |
generateCharSequence(n, data1); |
89 | 89 |
|
90 | 90 |
std::vector<unsigned char> data2(data1); |
91 | 91 |
std::sort(data1.begin(), data1.end()); |
92 | 92 |
|
93 | 93 |
radixSort(data2.begin(), data2.end()); |
94 | 94 |
for (int i = 0; i < n; ++i) { |
95 | 95 |
check(data1[i] == data2[i], "Test failed"); |
96 | 96 |
} |
97 | 97 |
|
98 | 98 |
} |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
|
102 | 102 |
void checkStableRadixSort() { |
103 | 103 |
{ |
104 | 104 |
std::vector<int> data1; |
105 | 105 |
generateIntSequence(n, data1); |
106 | 106 |
|
107 | 107 |
std::vector<int> data2(data1); |
108 | 108 |
std::sort(data1.begin(), data1.end()); |
109 | 109 |
|
110 | 110 |
stableRadixSort(data2.begin(), data2.end()); |
111 | 111 |
for (int i = 0; i < n; ++i) { |
112 | 112 |
check(data1[i] == data2[i], "Test failed"); |
113 | 113 |
} |
114 | 114 |
|
115 | 115 |
stableRadixSort(data2.begin(), data2.end(), Negate()); |
116 | 116 |
for (int i = 0; i < n; ++i) { |
117 | 117 |
check(data1[i] == data2[n - 1 - i], "Test failed"); |
118 | 118 |
} |
119 | 119 |
|
120 | 120 |
stableRadixSort(data2.begin(), data2.end(), negate); |
121 | 121 |
for (int i = 0; i < n; ++i) { |
122 | 122 |
check(data1[i] == data2[n - 1 - i], "Test failed"); |
123 | 123 |
} |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
{ |
127 | 127 |
std::vector<unsigned char> data1(n); |
128 | 128 |
generateCharSequence(n, data1); |
129 | 129 |
|
130 | 130 |
std::vector<unsigned char> data2(data1); |
131 | 131 |
std::sort(data1.begin(), data1.end()); |
132 | 132 |
|
133 | 133 |
radixSort(data2.begin(), data2.end()); |
134 | 134 |
for (int i = 0; i < n; ++i) { |
135 | 135 |
check(data1[i] == data2[i], "Test failed"); |
136 | 136 |
} |
137 | 137 |
|
138 | 138 |
} |
139 | 139 |
} |
140 | 140 |
|
141 | 141 |
int main() { |
142 | 142 |
|
143 | 143 |
checkRadixSort(); |
144 | 144 |
checkStableRadixSort(); |
145 | 145 |
|
146 | 146 |
return 0; |
147 | 147 |
} |
0 comments (0 inline)