2 * lemon/radix_sort.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
30 #include <lemon/error.h>
34 namespace _radix_sort_bits {
36 template <typename Value>
38 const Value& operator()(const Value& val) {
45 template <typename Value, typename Iterator, typename Functor>
46 Iterator radixSortPartition(Iterator first, Iterator last,
47 Functor functor, Value mask) {
48 while (first != last && !(functor(*first) & mask)) {
55 while (first != last && (functor(*last) & mask)) {
61 std::iter_swap(first, last);
63 if (!(first < last)) {
67 while (!(functor(*first) & mask)) {
71 while (functor(*last) & mask) {
74 if (!(first < last)) {
77 std::iter_swap(first, last);
82 template <typename Iterator, typename Functor>
83 Iterator radixSortSignPartition(Iterator first, Iterator last,
85 while (first != last && functor(*first) < 0) {
92 while (first != last && functor(*last) >= 0) {
98 std::iter_swap(first, last);
100 if (!(first < last)) {
104 while (functor(*first) < 0) {
108 while (functor(*last) >= 0) {
111 if (!(first < last)) {
114 std::iter_swap(first, last);
119 template <typename Value, typename Iterator, typename Functor>
120 void radixIntroSort(Iterator first, Iterator last,
121 Functor functor, Value mask) {
122 while (mask != 0 && last - first > 1) {
123 Iterator cut = radixSortPartition(first, last, functor, mask);
125 radixIntroSort(first, cut, functor, mask);
130 template <typename Value, typename Iterator, typename Functor>
131 void radixSignedSort(Iterator first, Iterator last, Functor functor) {
132 Iterator cut = radixSortSignPartition(first, last, functor);
138 mask = 0; max_digit = 0;
139 for (it = first; it != cut; ++it) {
140 if ((mask | functor(*it)) != ~0) {
146 radixIntroSort(first, cut, functor, 1 << max_digit);
148 mask = ~0; max_digit = 0;
149 for (it = cut; it != last; ++it) {
150 if (mask & functor(*it)) {
155 radixIntroSort(cut, last, functor, 1 << max_digit);
158 template <typename Value, typename Iterator, typename Functor>
159 void radixUnsignedSort(Iterator first, Iterator last, Functor functor) {
165 for (it = first; it != last; ++it) {
166 if (mask & functor(*it)) {
171 radixIntroSort(first, last, functor, 1 << max_digit);
174 namespace _radix_sort_bits {
176 template <typename Value,
177 bool sign = std::numeric_limits<Value>::is_signed >
178 struct RadixSortSelector {
179 template <typename Iterator, typename Functor>
180 static void sort(Iterator first, Iterator last, Functor functor) {
181 radixSignedSort<Value>(first, last, functor);
185 template <typename Value>
186 struct RadixSortSelector<Value, false> {
187 template <typename Iterator, typename Functor>
188 static void sort(Iterator first, Iterator last, Functor functor) {
189 radixUnsignedSort<Value>(first, last, functor);
197 /// \brief Sorts the stl compatible range into ascending order.
199 /// The \c radixSort sorts the stl compatible range into ascending order.
200 /// The radix sort algorithm can sort the items which mapped to an
201 /// integer by the adaptable unary function \c functor and the order
202 /// will be ascending by these mapped values. As function specialization
203 /// there is possible to use a normal function as the functor object
204 /// or if the functor is not given it will use an identity function instead.
206 /// This implemented radix sort is a special quick sort which pivot value
207 /// is choosen to partite the items on the next bit. This way, let be
208 /// \c c the maximal capacity and \c n the number of the items in
209 /// the container, the time complexity of the algorithm \c O(log(c)*n)
210 /// and the additional space complexity is \c O(log(c)).
212 /// \param first The begin of the given range.
213 /// \param last The end of the given range.
214 /// \param functor An adaptible unary function or a normal function which
215 /// maps the items to any integer type which can be wheter signed or
217 template <typename Iterator, typename Functor>
218 void radixSort(Iterator first, Iterator last, Functor functor) {
219 using namespace _radix_sort_bits;
220 typedef typename Functor::result_type Value;
221 RadixSortSelector<Value>::sort(first, last, functor);
224 template <typename Iterator, typename Value, typename Key>
225 void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) {
226 using namespace _radix_sort_bits;
227 RadixSortSelector<Value>::sort(first, last, functor);
230 template <typename Iterator, typename Value, typename Key>
231 void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
232 using namespace _radix_sort_bits;
233 RadixSortSelector<Value>::sort(first, last, functor);
236 template <typename Iterator, typename Value, typename Key>
237 void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
238 using namespace _radix_sort_bits;
239 RadixSortSelector<Value>::sort(first, last, functor);
242 template <typename Iterator, typename Value, typename Key>
243 void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
244 using namespace _radix_sort_bits;
245 RadixSortSelector<Value>::sort(first, last, functor);
248 template <typename Iterator>
249 void radixSort(Iterator first, Iterator last) {
250 using namespace _radix_sort_bits;
251 typedef typename std::iterator_traits<Iterator>::value_type Value;
252 RadixSortSelector<Value>::sort(first, last, Identity<Value>());
255 template <typename Value>
256 unsigned char valueByte(Value value, int byte) {
257 return value >> (std::numeric_limits<unsigned char>::digits * byte);
260 template <typename Functor, typename Key>
261 void counterIntroSort(Key *first, Key *last, Key *target,
262 int byte, Functor functor) {
264 (unsigned int)std::numeric_limits<unsigned char>::max() + 1;
266 for (int i = 0; i < size; ++i) {
270 while (first != last) {
271 ++counter[valueByte(functor(*first), byte)];
275 for (int i = 0; i < size; ++i) {
281 target[counter[valueByte(functor(*it), byte)]++] = *it;
286 template <typename Functor, typename Key>
287 void signedCounterIntroSort(Key *first, Key *last, Key *target,
288 int byte, Functor functor) {
290 (unsigned int)std::numeric_limits<unsigned char>::max() + 1;
292 for (int i = 0; i < size; ++i) {
296 while (first != last) {
297 counter[valueByte(functor(*first), byte)]++;
301 for (int i = size / 2; i < size; ++i) {
306 for (int i = 0; i < size / 2; ++i) {
312 target[counter[valueByte(functor(*it), byte)]++] = *it;
318 template <typename Value, typename Iterator, typename Functor>
319 void counterSignedSort(Iterator first, Iterator last, Functor functor) {
320 typedef typename std::iterator_traits<Iterator>::value_type Key;
321 typedef std::allocator<Key> Allocator;
324 int length = std::distance(first, last);
326 buffer = allocator.allocate(2 * length);
329 std::copy(first, last, buffer);
330 for (int i = 0; i < (int)sizeof(Value) - 1; ++i) {
332 counterIntroSort(buffer, buffer + length, buffer + length,
335 counterIntroSort(buffer + length, buffer + 2 * length, buffer,
341 signedCounterIntroSort(buffer, buffer + length, buffer + length,
342 sizeof(Value) - 1, functor);
343 std::copy(buffer + length, buffer + 2 * length, first);
345 signedCounterIntroSort(buffer + length, buffer + 2 * length, buffer,
346 sizeof(Value) - 1, functor);
347 std::copy(buffer, buffer + length, first);
350 allocator.deallocate(buffer, 2 * length);
353 allocator.deallocate(buffer, 2 * length);
356 template <typename Value, typename Iterator, typename Functor>
357 void counterUnsignedSort(Iterator first, Iterator last, Functor functor) {
358 typedef typename std::iterator_traits<Iterator>::value_type Key;
359 typedef std::allocator<Key> Allocator;
362 int length = std::distance(first, last);
364 buffer = allocator.allocate(2 * length);
367 std::copy(first, last, buffer);
368 for (int i = 0; i < (int)sizeof(Value); ++i) {
370 counterIntroSort(buffer, buffer + length, buffer + length, i, functor);
372 counterIntroSort(buffer + length, buffer + 2 * length, buffer, i, functor);
377 std::copy(buffer, buffer + length, first);
379 std::copy(buffer + length, buffer + 2 * length, first);
382 allocator.deallocate(buffer, 2 * length);
385 allocator.deallocate(buffer, 2 * length);
388 namespace _radix_sort_bits {
390 template <typename Value,
391 bool sign = std::numeric_limits<Value>::is_signed >
392 struct CounterSortSelector {
393 template <typename Iterator, typename Functor>
394 static void sort(Iterator first, Iterator last, Functor functor) {
395 counterSignedSort<Value>(first, last, functor);
399 template <typename Value>
400 struct CounterSortSelector<Value, false> {
401 template <typename Iterator, typename Functor>
402 static void sort(Iterator first, Iterator last, Functor functor) {
403 counterUnsignedSort<Value>(first, last, functor);
411 /// \brief Sorts stable the stl compatible range into ascending order.
413 /// The \c counterSort sorts the stl compatible range into ascending order.
414 /// The counter sort algorithm can sort the items which mapped to an
415 /// integer by the adaptable unary function \c functor and the order
416 /// will be ascending by these mapped values. As function specialization
417 /// there is possible to use a normal function as the functor object
418 /// or if the functor is not given it will use an identity function instead.
420 /// This implemented counter sort use a radix forward sort on the bytes of
421 /// the integer. The algorithm can sort the items on a given byte.
422 /// First time it counts how many times occurs a byte value in the container.
423 /// By the occurence number it is possible to copy the container
424 /// in the right order in \c O(n) time. The algorithm sorts the container
425 /// by each bytes in forward direction which sorts the container by the
426 /// whole value. This way, let be \c c the maximal capacity of the integer
427 /// type and \c n the number of the items in
428 /// the container, the time complexity of the algorithm \c O(log(c)*n)
429 /// and the additional space complexity is \c O(n).
431 /// This sorting algorithm is stable so the order of two equal element
432 /// stay in the same order.
434 /// \param first The begin of the given range.
435 /// \param last The end of the given range.
436 /// \param functor An adaptible unary function or a normal function which
437 /// maps the items to any integer type which can be wheter signed or
439 template <typename Iterator, typename Functor>
440 void counterSort(Iterator first, Iterator last, Functor functor) {
441 using namespace _radix_sort_bits;
442 typedef typename Functor::result_type Value;
443 CounterSortSelector<Value>::sort(first, last, functor);
446 template <typename Iterator, typename Value, typename Key>
447 void counterSort(Iterator first, Iterator last, Value (*functor)(Key)) {
448 using namespace _radix_sort_bits;
449 CounterSortSelector<Value>::sort(first, last, functor);
452 template <typename Iterator, typename Value, typename Key>
453 void counterSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
454 using namespace _radix_sort_bits;
455 CounterSortSelector<Value>::sort(first, last, functor);
458 template <typename Iterator, typename Value, typename Key>
459 void counterSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
460 using namespace _radix_sort_bits;
461 CounterSortSelector<Value>::sort(first, last, functor);
464 template <typename Iterator, typename Value, typename Key>
465 void counterSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
466 using namespace _radix_sort_bits;
467 CounterSortSelector<Value>::sort(first, last, functor);
470 template <typename Iterator>
471 void counterSort(Iterator first, Iterator last) {
472 using namespace _radix_sort_bits;
473 typedef typename std::iterator_traits<Iterator>::value_type Value;
474 CounterSortSelector<Value>::sort(first, last, Identity<Value>());