From 2b927b9729506d071b29b7e03efa772adec251c5 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 8 Dec 2023 18:58:47 +0200 Subject: [PATCH] BAN: Restructure sort functions and namespaces --- BAN/include/BAN/Sort.h | 38 ++++++++++++++++----------------- userspace/aoc2023/day7/main.cpp | 2 +- userspace/test-sort/main.cpp | 12 +++++------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/BAN/include/BAN/Sort.h b/BAN/include/BAN/Sort.h index 8835cdf283..e9a91e2eba 100644 --- a/BAN/include/BAN/Sort.h +++ b/BAN/include/BAN/Sort.h @@ -4,11 +4,11 @@ #include #include -namespace BAN +namespace BAN::sort { template> - void sort_exchange(It begin, It end, Comp comp = {}) + void exchange_sort(It begin, It end, Comp comp = {}) { for (It lhs = begin; lhs != end; ++lhs) for (It rhs = next(lhs, 1); rhs != end; ++rhs) @@ -16,7 +16,7 @@ namespace BAN swap(*lhs, *rhs); } - namespace detail::sort + namespace detail { template @@ -42,17 +42,17 @@ namespace BAN } template> - void sort_quick(It begin, It end, Comp comp = {}) + void quick_sort(It begin, It end, Comp comp = {}) { if (begin == end || next(begin, 1) == end) return; - It mid = detail::sort::partition(begin, end, comp); - sort_quick(begin, mid, comp); - sort_quick(++mid, end, comp); + It mid = detail::partition(begin, end, comp); + quick_sort(begin, mid, comp); + quick_sort(++mid, end, comp); } template> - void sort_insertion(It begin, It end, Comp comp = {}) + void insertion_sort(It begin, It end, Comp comp = {}) { if (begin == end || next(begin, 1) == end) return; @@ -67,7 +67,7 @@ namespace BAN } template> - void sort_heap(It begin, It end, Comp comp = {}) + void heap_sort(It begin, It end, Comp comp = {}) { if (begin == end || next(begin, 1) == end) return; @@ -101,34 +101,34 @@ namespace BAN } } - namespace detail::sort + namespace detail { template - void intro_impl(It begin, It end, size_t max_depth, Comp comp) + void intro_sort_impl(It begin, It end, size_t max_depth, Comp comp) { if (distance(begin, end) < 16) - return sort_insertion(begin, end, comp); + return insertion_sort(begin, end, comp); if (max_depth == 0) - return sort_heap(begin, end, comp); - It mid = detail::sort::partition(begin, end, comp); - intro_impl(begin, mid, max_depth - 1, comp); - intro_impl(++mid, end, max_depth - 1, comp); + return heap_sort(begin, end, comp); + It mid = detail::partition(begin, end, comp); + intro_sort_impl(begin, mid, max_depth - 1, comp); + intro_sort_impl(++mid, end, max_depth - 1, comp); } } template> - void sort_intro(It begin, It end, Comp comp = {}) + void intro_sort(It begin, It end, Comp comp = {}) { size_t max_depth = Math::ilog2(distance(begin, end)); - detail::sort::intro_impl(begin, end, max_depth, comp); + detail::intro_sort_impl(begin, end, max_depth, comp); } template> void sort(It begin, It end, Comp comp = {}) { - return sort_intro(begin, end, comp); + return sort::intro_sort(begin, end, comp); } } diff --git a/userspace/aoc2023/day7/main.cpp b/userspace/aoc2023/day7/main.cpp index cf3c3dc205..b1dbdbb247 100644 --- a/userspace/aoc2023/day7/main.cpp +++ b/userspace/aoc2023/day7/main.cpp @@ -116,7 +116,7 @@ i64 puzzle(FILE* fp, bool joker) )); } - BAN::sort(hands.begin(), hands.end(), + BAN::sort::sort(hands.begin(), hands.end(), [joker] (const Hand& lhs, const Hand& rhs) { return hand_score(lhs, joker) < hand_score(rhs, joker); } diff --git a/userspace/test-sort/main.cpp b/userspace/test-sort/main.cpp index 28d55faf18..9b0b4910ad 100644 --- a/userspace/test-sort/main.cpp +++ b/userspace/test-sort/main.cpp @@ -31,11 +31,11 @@ bool is_sorted(BAN::Vector& vec) int main() { srand(time(0)); - TEST("exchange sort", BAN::sort_exchange, 100); - TEST("exchange sort", BAN::sort_exchange, 1000); - TEST("exchange sort", BAN::sort_exchange, 10000); + TEST("exchange sort", BAN::sort::exchange_sort, 100); + TEST("exchange sort", BAN::sort::exchange_sort, 1000); + TEST("exchange sort", BAN::sort::exchange_sort, 10000); - TEST("quick sort", BAN::sort_quick, 100); - TEST("quick sort", BAN::sort_quick, 1000); - TEST("quick sort", BAN::sort_quick, 10000); + TEST("quick sort", BAN::sort::quick_sort, 100); + TEST("quick sort", BAN::sort::quick_sort, 1000); + TEST("quick sort", BAN::sort::quick_sort, 10000); }