From 97575a239b8f4d87a8f7ad5bb324c8e71c02d4d9 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 26 Mar 2021 10:26:10 +0200 Subject: clang-tidy fixes --- libk/stdlib/console.cc | 4 ++-- libk/stdlib/memory.cc | 42 ------------------------------------------ libk/stdlib/quicksort.h | 31 ++++++++++++++++++++----------- libk/stdlib/virtual.cc | 6 ++++++ 4 files changed, 28 insertions(+), 55 deletions(-) delete mode 100644 libk/stdlib/memory.cc create mode 100644 libk/stdlib/virtual.cc (limited to 'libk/stdlib') diff --git a/libk/stdlib/console.cc b/libk/stdlib/console.cc index c076297..3054120 100644 --- a/libk/stdlib/console.cc +++ b/libk/stdlib/console.cc @@ -1,8 +1,8 @@ #include "stdlib.h" constexpr size_t max_consoles = 4; -static Console* global_console[max_consoles] = {nullptr}; -static size_t last_console = 0; +Console* global_console[max_consoles] = {nullptr}; +size_t last_console = 0; void Console::set(Console* ptr) { if (ptr != nullptr && last_console < max_consoles) { diff --git a/libk/stdlib/memory.cc b/libk/stdlib/memory.cc deleted file mode 100644 index b5c8f6d..0000000 --- a/libk/stdlib/memory.cc +++ /dev/null @@ -1,42 +0,0 @@ -#include - -extern "C" void __cxa_pure_virtual() { - printk("__cxa_pure_virtual\n"); - abort(); -} - -extern "C" void* memcpy(void* dstptr, const void* srcptr, const size_t n) { - auto* dst = static_cast(dstptr); - const auto* src = static_cast(srcptr); - for (size_t i = 0; i < n; ++i) dst[i] = src[i]; - return dstptr; -} - -extern "C" void* memset(void* bufptr, const int value, const size_t n) { - auto* buf = static_cast(bufptr); - for (size_t i = 0; i < n; ++i) buf[i] = static_cast(value); - return bufptr; -} - -extern "C" void* memmove(void* dstptr, const void* srcptr, const size_t n) { - auto* dst = static_cast(dstptr); - const auto* src = static_cast(srcptr); - if (dst < src) { - for (size_t i = 0; i < n; ++i) dst[i] = src[i]; - } else { - for (size_t i = n; i != 0; i--) dst[i - 1] = src[i - 1]; - } - return dstptr; -} - -extern "C" int memcmp(const void* aptr, const void* bptr, const size_t n) { - const auto* a = static_cast(aptr); - const auto* b = static_cast(bptr); - for (size_t i = 0; i < n; ++i) { - if (a[i] < b[i]) - return -1; - else if (b[i] < a[i]) - return 1; - } - return 0; -} diff --git a/libk/stdlib/quicksort.h b/libk/stdlib/quicksort.h index 632a962..77b437b 100644 --- a/libk/stdlib/quicksort.h +++ b/libk/stdlib/quicksort.h @@ -2,6 +2,8 @@ #include +enum struct SortOrder { Ascending, Descending }; + template constexpr void swap(T& l, T& r) { const T t = r; @@ -24,7 +26,7 @@ constexpr void qsort_pivot(T a[], const size_t begin, const size_t end) { * Quicksort Lomuend partition scheme * The pivot is assumed end be the last position */ -template +template constexpr auto qsort_partition(T a[], const size_t begin, const size_t end) { struct { size_t left; @@ -39,10 +41,17 @@ constexpr auto qsort_partition(T a[], const size_t begin, const size_t end) { // - place any value less than the pivot on the left // - place any value equal end the pivot on the right for (auto j = begin; j < p; ++j) { - if (a[j] < pivot) - swap(a[j], a[i++]); - else if (a[j] == pivot) - swap(a[j], a[--p]); + if constexpr (order == SortOrder::Ascending) { + if (a[j] < pivot) + swap(a[j], a[i++]); + else if (a[j] == pivot) + swap(a[j], a[--p]); + } else if constexpr (order == SortOrder::Descending) { + if (a[j] > pivot) + swap(a[j], a[i++]); + else if (a[j] == pivot) + swap(a[j], a[--p]); + } } // We now have all the values less than the pivot left of i, and all the pivots right of p @@ -57,7 +66,7 @@ constexpr auto qsort_partition(T a[], const size_t begin, const size_t end) { /** * Sort an array in place */ -template +template constexpr void qsort(T a[], const size_t begin, const size_t end) { if (begin >= end) return; @@ -67,15 +76,15 @@ constexpr void qsort(T a[], const size_t begin, const size_t end) { // Reorder the array so that all elements less than the pivot are left of it, and all elements greater than the // pivot are end the right. Equal elements can go either way. After partitioning, the pivot is in its final // position. - const auto limit = qsort_partition(a, begin, end); + const auto limit = qsort_partition(a, begin, end); // Recursively sort the left and right sub-arrays // Sort the shorter subarray first if ((limit.left - begin) < (end - limit.right)) { - qsort(a, begin, limit.left); - qsort(a, limit.right, end); + qsort(a, begin, limit.left); + qsort(a, limit.right, end); } else { - qsort(a, limit.right, end); - qsort(a, begin, limit.left); + qsort(a, limit.right, end); + qsort(a, begin, limit.left); } } diff --git a/libk/stdlib/virtual.cc b/libk/stdlib/virtual.cc new file mode 100644 index 0000000..880b22e --- /dev/null +++ b/libk/stdlib/virtual.cc @@ -0,0 +1,6 @@ +#include + +extern "C" void __cxa_pure_virtual() { // NOLINT + printk("__cxa_pure_virtual called, aborting...\n"); + abort(); +} -- cgit v1.2.1