cgv
Loading...
Searching...
No Matches
bucket_sort.h
1#pragma once
2
3#include <vector>
4
5namespace cgv {
6 namespace math {
8 template <typename key_type, typename idx_type>
9 void bucket_sort(const std::vector<key_type>& keys, size_t nr_keys, std::vector<idx_type>& perm, std::vector<idx_type>* perm_in_ptr = 0)
10 {
11 // prepare links and lookup table
12 std::vector<idx_type> links(keys.size(), idx_type(-1));
13 std::vector<idx_type> lookup(nr_keys, idx_type(-1));
14 // sort indices into lookup table
15 idx_type i;
16 for (i = 0; i < keys.size(); ++i) {
17 key_type k = keys[perm_in_ptr ? perm_in_ptr->at(i) : i];
18 idx_type j = lookup[k];
19 if (j != idx_type(-1))
20 links[i] = j;
21 lookup[k] = i;
22 }
23 // construct permutation
24 perm.resize(keys.size());
25 i = idx_type(keys.size());
26 for (key_type k = key_type(nr_keys); k > 0; ) {
27 idx_type j = lookup[--k];
28 while (j != idx_type(-1)) {
29 perm[--i] = perm_in_ptr ? perm_in_ptr->at(j) : j;
30 j = links[j];
31 }
32 }
33 }
34 }
35}
the cgv namespace
Definition print.h:11