1#ifndef ROUTING_KIT_SORT_H
2#define ROUTING_KIT_SORT_H
55template<
class T,
class C>
60 [&](
unsigned l,
unsigned r){
61 return is_less(v[l], v[r]);
67template<
class T,
class C>
70 std::stable_sort(r.begin(), r.end(), is_less);
74template<
class T,
class C>
76 std::stable_sort(v.begin(), v.end(), is_less);
80template<
class T,
class C>
85 [&](
unsigned l,
unsigned r){
86 return is_less(v[l], v[r]);
92template<
class T,
class C>
95 std::sort(r.begin(), r.end(), is_less);
99template<
class T,
class C>
101 std::sort(v.begin(), v.end(), is_less);
106template<
class T,
class C>
108 for(
unsigned i=1; i<v.size(); ++i)
109 if(is_less(v[i], v[i-1]))
115template<
class T,
class C>
120template<
class T,
class C>
130 template<
class T,
class K>
131 std::vector<unsigned>
compute_key_pos(
const std::vector<T>&v,
unsigned key_count,
const K&get_key){
132 std::vector<unsigned>key_pos(key_count, 0);
133 for(
unsigned i=0; i<v.size(); ++i){
134 unsigned k = get_key(v[i]);
135 assert(k <= key_count &&
"key is too large");
139 for(
unsigned i=0; i<key_count; ++i){
140 unsigned tmp = sum + key_pos[i];
163 return l_key < r_key;
189 template<
bool is_stable,
class T,
class K>
191 std::vector<unsigned>p;
196 for(
unsigned i=0; i<v.size(); ++i){
197 unsigned k = get_key(v[i]);
198 assert(k <= key_count &&
"key is too large");
212template<
class T,
class K>
214 return detail::compute_maybe_stable_sort_permutation_using_key<false>(v, key_count, get_key);
217template<
class T,
class K>
219 return detail::compute_maybe_stable_sort_permutation_using_key<true>(v, key_count, get_key);
223 template<
bool is_stable,
class T,
class K>
225 std::vector<unsigned>p;
229 for(
unsigned i=0; i<v.size(); ++i){
230 unsigned k = get_key(v[i]);
231 assert(k <= key_count &&
"key is too large");
235 }
else if(is_stable){
244template<
class T,
class K>
246 return detail::compute_inverse_maybe_stable_sort_permutation_using_key<false>(v, key_count, get_key);
249template<
class T,
class K>
251 return detail::compute_inverse_maybe_stable_sort_permutation_using_key<true>(v, key_count, get_key);
255 template<
bool is_stable,
class T,
class K>
263 for(
unsigned i=0; i<v.size(); ++i){
264 unsigned k = get_key(v[i]);
265 assert(k <= key_count &&
"key is too large");
266 r[key_pos[k]] = v[i];
269 }
else if(is_stable) {
277 template<
bool is_stable,
class T,
class K>
284 for(
unsigned i=0; i<v.size(); ++i){
285 unsigned k = get_key(v[i]);
286 assert(k <= key_count &&
"key is too large");
287 r[key_pos[k]] = std::move(v[i]);
290 }
else if(is_stable) {
299template<
class T,
class K>
300std::vector<T>
sort_using_key(
const std::vector<T>&v,
unsigned key_count,
const K&get_key){
301 return detail::maybe_stable_sort_using_key<false>(v, key_count, get_key);
304template<
class T,
class K>
306 return detail::maybe_stable_sort_using_key<false>(std::move(v), key_count, get_key);
309template<
class T,
class K>
311 return detail::maybe_stable_sort_using_key<true>(v, key_count, get_key);
314template<
class T,
class K>
316 return detail::maybe_stable_sort_using_key<true>(std::move(v), key_count, get_key);
320template<
class T,
class K>
322 for(
unsigned i=1; i<v.size(); ++i)
323 if(get_key(v[i]) < get_key(v[i-1]))
std::vector< unsigned > compute_maybe_stable_sort_permutation_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
CompareByKey< K > make_compare_by_key(unsigned n, const K &k)
std::vector< T > maybe_stable_sort_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
std::vector< unsigned > compute_key_pos(const std::vector< T > &v, unsigned key_count, const K &get_key)
const unsigned bucket_sort_min_key_to_element_ratio
std::vector< unsigned > compute_inverse_maybe_stable_sort_permutation_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
std::vector< unsigned > compute_stable_sort_permutation_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
std::vector< unsigned > compute_inverse_sort_permutation_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
bool is_sorted_using_less(const std::vector< T > &v)
std::vector< T > sort_using_comparator(const std::vector< T > &v, const C &is_less)
std::vector< T > stable_sort_using_comparator(const std::vector< T > &v, const C &is_less)
std::vector< unsigned > compute_sort_permutation_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
bool is_sorted_using_comparator(const std::vector< T > &v, const C &is_less)
std::vector< T > stable_sort_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
std::vector< T > sort_using_less(const std::vector< T > &v)
std::vector< T > stable_sort_using_less(const std::vector< T > &v)
std::vector< unsigned > invert_permutation(const std::vector< unsigned > &p)
std::vector< unsigned > compute_inverse_stable_sort_permutation_using_less(const std::vector< T > &v)
bool is_sorted_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
std::vector< unsigned > compute_inverse_stable_sort_permutation_using_comparator(const std::vector< T > &v, const C &is_less)
std::vector< unsigned > compute_stable_sort_permutation_using_less(const std::vector< T > &v)
std::vector< unsigned > compute_inverse_stable_sort_permutation_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
std::vector< T > sort_using_key(const std::vector< T > &v, unsigned key_count, const K &get_key)
std::vector< unsigned > compute_inverse_sort_permutation_using_less(const std::vector< T > &v)
std::vector< unsigned > compute_sort_permutation_using_less(const std::vector< T > &v)
std::vector< unsigned > identity_permutation(unsigned n)
std::vector< unsigned > compute_stable_sort_permutation_using_comparator(const std::vector< T > &v, const C &is_less)
std::vector< unsigned > compute_sort_permutation_using_comparator(const std::vector< T > &v, const C &is_less)
std::vector< unsigned > compute_inverse_sort_permutation_using_comparator(const std::vector< T > &v, const C &is_less)
bool operator()(const T &l, const T &r) const
CompareByKey(unsigned n, const K &k)