aboutsummaryrefslogtreecommitdiff
path: root/libk/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'libk/stdlib')
-rw-r--r--libk/stdlib/console.cc4
-rw-r--r--libk/stdlib/memory.cc42
-rw-r--r--libk/stdlib/quicksort.h31
-rw-r--r--libk/stdlib/virtual.cc6
4 files changed, 28 insertions, 55 deletions
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 <stdlib.h>
-
-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<uint8_t*>(dstptr);
- const auto* src = static_cast<const uint8_t*>(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<uint8_t*>(bufptr);
- for (size_t i = 0; i < n; ++i) buf[i] = static_cast<uint8_t>(value);
- return bufptr;
-}
-
-extern "C" void* memmove(void* dstptr, const void* srcptr, const size_t n) {
- auto* dst = static_cast<uint8_t*>(dstptr);
- const auto* src = static_cast<const uint8_t*>(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<const uint8_t*>(aptr);
- const auto* b = static_cast<const uint8_t*>(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 <types.h>
+enum struct SortOrder { Ascending, Descending };
+
template <typename T>
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 <typename T>
+template <typename T, auto order = SortOrder::Ascending>
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 <typename T>
+template <typename T, auto order = SortOrder::Ascending>
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<T, order>(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<T, order>(a, begin, limit.left);
+ qsort<T, order>(a, limit.right, end);
} else {
- qsort(a, limit.right, end);
- qsort(a, begin, limit.left);
+ qsort<T, order>(a, limit.right, end);
+ qsort<T, order>(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 <stdlib.h>
+
+extern "C" void __cxa_pure_virtual() { // NOLINT
+ printk("__cxa_pure_virtual called, aborting...\n");
+ abort();
+}