diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-09 16:13:08 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-09 16:20:12 +0100 |
commit | 0d5e6593b2b7abea5ab2b9814e6055f8fff69fb8 (patch) | |
tree | 0f3e997f894e9deafc0c76a1bea13bf2beb12412 /LibGUI | |
parent | e14dd06b8c590fb1292b6219f8c1da249ec85f98 (diff) | |
download | serenity-0d5e6593b2b7abea5ab2b9814e6055f8fff69fb8.zip |
AK: Add a basic QuickSort template implementation.
It was depressing not being able to capture anything when passing a lambda
to qsort_r() so let's just have our own QuickSort. I was gonna have to do
this eventually anyway. :^)
Diffstat (limited to 'LibGUI')
-rw-r--r-- | LibGUI/GSortingProxyTableModel.cpp | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/LibGUI/GSortingProxyTableModel.cpp b/LibGUI/GSortingProxyTableModel.cpp index 364b5f3638..bdb7dfba82 100644 --- a/LibGUI/GSortingProxyTableModel.cpp +++ b/LibGUI/GSortingProxyTableModel.cpp @@ -1,4 +1,5 @@ #include <LibGUI/GSortingProxyTableModel.h> +#include <AK/QuickSort.h> #include <stdlib.h> #include <stdio.h> @@ -84,25 +85,14 @@ void GSortingProxyTableModel::resort() m_row_mappings[i] = i; if (m_key_column == -1) return; - struct Context { - GTableModel* target; - int key_column; - GSortOrder sort_order; - }; - Context context { m_target.ptr(), m_key_column, m_sort_order }; - qsort_r(m_row_mappings.data(), m_row_mappings.size(), sizeof(int), [] (const void* a, const void* b, void* ctx) -> int { - auto& context = *(Context*)(ctx); - GModelIndex index1 { *(const int*)(a), context.key_column }; - GModelIndex index2 { *(const int*)(b), context.key_column }; - auto data1 = context.target->data(index1, GTableModel::Role::Sort); - auto data2 = context.target->data(index2, GTableModel::Role::Sort); + quick_sort(m_row_mappings.begin(), m_row_mappings.end(), [&] (auto row1, auto row2) -> bool { + auto data1 = target().data({ row1, m_key_column }, GTableModel::Role::Sort); + auto data2 = target().data({ row2, m_key_column }, GTableModel::Role::Sort); if (data1 == data2) return 0; bool is_less_than = data1 < data2; - if (context.sort_order == GSortOrder::Ascending) - return is_less_than ? -1 : 1; - return is_less_than ? 1 : -1; - }, &context); + return m_sort_order == GSortOrder::Ascending ? is_less_than : !is_less_than; + }); did_update(); } |