# HG changeset patch
# User Balazs Dezso <deba@inf.elte.hu>
# Date 1269019427 -3600
# Node ID 4e6bee56cdbbf9698ef7af810904d76dae337b3c
# Parent 87569cb5734dee71de7e2c449c1cd6500921e2a7
Bidirectional iterator support for radixSort() (#362)
diff -r 87569cb5734d -r 4e6bee56cdbb lemon/radix_sort.h
a
|
b
|
|
34 | 34 | |
35 | 35 | namespace _radix_sort_bits { |
36 | 36 | |
| 37 | template <typename Iterator> |
| 38 | bool unitRange(Iterator first, Iterator last) { |
| 39 | ++first; |
| 40 | return first == last; |
| 41 | } |
| 42 | |
37 | 43 | template <typename Value> |
38 | 44 | struct Identity { |
39 | 45 | const Value& operator()(const Value& val) { |
… |
… |
|
60 | 66 | } |
61 | 67 | std::iter_swap(first, last); |
62 | 68 | ++first; |
63 | | if (!(first < last)) { |
64 | | return first; |
65 | | } |
66 | 69 | while (true) { |
67 | 70 | while (!(functor(*first) & mask)) { |
68 | 71 | ++first; |
… |
… |
|
71 | 74 | while (functor(*last) & mask) { |
72 | 75 | --last; |
73 | 76 | } |
74 | | if (!(first < last)) { |
| 77 | if (unitRange(last, first)) { |
75 | 78 | return first; |
76 | 79 | } |
77 | 80 | std::iter_swap(first, last); |
… |
… |
|
97 | 100 | } |
98 | 101 | std::iter_swap(first, last); |
99 | 102 | ++first; |
100 | | if (!(first < last)) { |
101 | | return first; |
102 | | } |
103 | 103 | while (true) { |
104 | 104 | while (functor(*first) < 0) { |
105 | 105 | ++first; |
… |
… |
|
108 | 108 | while (functor(*last) >= 0) { |
109 | 109 | --last; |
110 | 110 | } |
111 | | if (!(first < last)) { |
| 111 | if (unitRange(last, first)) { |
112 | 112 | return first; |
113 | 113 | } |
114 | 114 | std::iter_swap(first, last); |
… |
… |
|
119 | 119 | template <typename Value, typename Iterator, typename Functor> |
120 | 120 | void radixIntroSort(Iterator first, Iterator last, |
121 | 121 | Functor functor, Value mask) { |
122 | | while (mask != 0 && last - first > 1) { |
| 122 | while (mask != 0 && first != last && !unitRange(first, last)) { |
123 | 123 | Iterator cut = radixSortPartition(first, last, functor, mask); |
124 | 124 | mask >>= 1; |
125 | 125 | radixIntroSort(first, cut, functor, mask); |