Towards icc-8.0 compatibility...
2 * lemon/radix_sort.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 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 if (first == last) return;
321 typedef typename std::iterator_traits<Iterator>::value_type Key;
322 typedef std::allocator<Key> Allocator;
325 int length = std::distance(first, last);
327 buffer = allocator.allocate(2 * length);
330 std::copy(first, last, buffer);
331 for (int i = 0; i < (int)sizeof(Value) - 1; ++i) {
333 counterIntroSort(buffer, buffer + length, buffer + length,
336 counterIntroSort(buffer + length, buffer + 2 * length, buffer,
342 signedCounterIntroSort(buffer, buffer + length, buffer + length,
343 sizeof(Value) - 1, functor);
344 std::copy(buffer + length, buffer + 2 * length, first);
346 signedCounterIntroSort(buffer + length, buffer + 2 * length, buffer,
347 sizeof(Value) - 1, functor);
348 std::copy(buffer, buffer + length, first);
351 allocator.deallocate(buffer, 2 * length);
354 allocator.deallocate(buffer, 2 * length);
357 template <typename Value, typename Iterator, typename Functor>
358 void counterUnsignedSort(Iterator first, Iterator last, Functor functor) {
359 if (first == last) return;
360 typedef typename std::iterator_traits<Iterator>::value_type Key;
361 typedef std::allocator<Key> Allocator;
364 int length = std::distance(first, last);
366 buffer = allocator.allocate(2 * length);
369 std::copy(first, last, buffer);
370 for (int i = 0; i < (int)sizeof(Value); ++i) {
372 counterIntroSort(buffer, buffer + length, buffer + length, i, functor);
374 counterIntroSort(buffer + length, buffer + 2 * length, buffer, i, functor);
379 std::copy(buffer, buffer + length, first);
381 std::copy(buffer + length, buffer + 2 * length, first);
384 allocator.deallocate(buffer, 2 * length);
387 allocator.deallocate(buffer, 2 * length);
390 namespace _radix_sort_bits {
392 template <typename Value,
393 bool sign = std::numeric_limits<Value>::is_signed >
394 struct CounterSortSelector {
395 template <typename Iterator, typename Functor>
396 static void sort(Iterator first, Iterator last, Functor functor) {
397 counterSignedSort<Value>(first, last, functor);
401 template <typename Value>
402 struct CounterSortSelector<Value, false> {
403 template <typename Iterator, typename Functor>
404 static void sort(Iterator first, Iterator last, Functor functor) {
405 counterUnsignedSort<Value>(first, last, functor);
413 /// \brief Sorts stable the stl compatible range into ascending order.
415 /// The \c counterSort sorts the stl compatible range into ascending order.
416 /// The counter sort algorithm can sort the items which mapped to an
417 /// integer by the adaptable unary function \c functor and the order
418 /// will be ascending by these mapped values. As function specialization
419 /// there is possible to use a normal function as the functor object
420 /// or if the functor is not given it will use an identity function instead.
422 /// This implemented counter sort use a radix forward sort on the bytes of
423 /// the integer. The algorithm can sort the items on a given byte.
424 /// First time it counts how many times occurs a byte value in the container.
425 /// By the occurence number it is possible to copy the container
426 /// in the right order in \c O(n) time. The algorithm sorts the container
427 /// by each bytes in forward direction which sorts the container by the
428 /// whole value. This way, let be \c c the maximal capacity of the integer
429 /// type and \c n the number of the items in
430 /// the container, the time complexity of the algorithm \c O(log(c)*n)
431 /// and the additional space complexity is \c O(n).
433 /// This sorting algorithm is stable so the order of two equal element
434 /// stay in the same order.
436 /// \param first The begin of the given range.
437 /// \param last The end of the given range.
438 /// \param functor An adaptible unary function or a normal function which
439 /// maps the items to any integer type which can be wheter signed or
441 template <typename Iterator, typename Functor>
442 void counterSort(Iterator first, Iterator last, Functor functor) {
443 using namespace _radix_sort_bits;
444 typedef typename Functor::result_type Value;
445 CounterSortSelector<Value>::sort(first, last, functor);
448 template <typename Iterator, typename Value, typename Key>
449 void counterSort(Iterator first, Iterator last, Value (*functor)(Key)) {
450 using namespace _radix_sort_bits;
451 CounterSortSelector<Value>::sort(first, last, functor);
454 template <typename Iterator, typename Value, typename Key>
455 void counterSort(Iterator first, Iterator last, Value& (*functor)(Key)) {
456 using namespace _radix_sort_bits;
457 CounterSortSelector<Value>::sort(first, last, functor);
460 template <typename Iterator, typename Value, typename Key>
461 void counterSort(Iterator first, Iterator last, Value (*functor)(Key&)) {
462 using namespace _radix_sort_bits;
463 CounterSortSelector<Value>::sort(first, last, functor);
466 template <typename Iterator, typename Value, typename Key>
467 void counterSort(Iterator first, Iterator last, Value& (*functor)(Key&)) {
468 using namespace _radix_sort_bits;
469 CounterSortSelector<Value>::sort(first, last, functor);
472 template <typename Iterator>
473 void counterSort(Iterator first, Iterator last) {
474 using namespace _radix_sort_bits;
475 typedef typename std::iterator_traits<Iterator>::value_type Value;
476 CounterSortSelector<Value>::sort(first, last, Identity<Value>());