6 #ifndef TURI_FAST_TOP_K_H_ 7 #define TURI_FAST_TOP_K_H_ 15 template <
typename T,
typename LessThan>
16 GL_HOT_NOINLINE_FLATTEN
17 void __run_top_k_small_k(std::vector<T>& v, LessThan less_than,
size_t k) {
19 std::sort(v.begin(), v.begin() + k, less_than);
21 for(
size_t i = k; i < v.size(); ++i) {
22 if(less_than(v[0], v[i])) {
26 std::swap(v[0], v[i]);
32 for(
size_t j = 1; j < k; ++j) {
33 if(!less_than(v[j-1], v[j])) {
34 std::swap(v[j], v[j-1]);
47 va.assign(v.begin(), v.begin() + k);
49 auto gt_sorter = [&](
const T& t1,
const T& t2) {
50 return less_than(t2, t1);
53 std::nth_element(v.begin(), v.begin() + k, v.end(), gt_sorter);
55 std::sort(v.begin(), v.begin() + k, gt_sorter);
56 for(
size_t j = 0; j < k; ++j) {
58 ASSERT_TRUE(!less_than(v[j], va[k - 1 - j]) && !less_than(va[k - 1 - j], v[j]));
61 for(
size_t i = k; i < v.size(); ++i) {
62 for(
size_t j = 0; j < k; ++j) {
68 for(
size_t i = 0; i < k; ++i) {
72 std::reverse(v.begin(), v.begin() + k);
75 DASSERT_TRUE(
bool(std::is_sorted(v.begin(), v.begin() + k, gt_sorter)));
86 template <
typename T,
typename LessThan>
88 std::vector<T>& v,
size_t top_k, LessThan less_than) {
90 auto gt_sorter = [&](
const T& t1,
const T& t2) {
91 return less_than(t2, t1);
94 if(v.size() <= top_k) {
100 __run_top_k_small_k(v, less_than, top_k);
104 std::nth_element(v.begin(), v.begin() + top_k, v.end(), gt_sorter);
106 std::sort(v.begin(), v.end(), gt_sorter);
116 template <
typename T>
118 std::vector<T>& v,
size_t top_k) {
void extract_and_sort_top_k(std::vector< T > &v, size_t top_k, LessThan less_than)
std::shared_ptr< sframe > sort(std::shared_ptr< planner_node > sframe_planner_node, const std::vector< std::string > column_names, const std::vector< size_t > &sort_column_indices, const std::vector< bool > &sort_orders)
#define ASSERT_TRUE(cond)
#define DASSERT_TRUE(cond)