From c2f48fb5df0981df1df23de2b277274f9fe75080 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Mon, 8 Jan 2024 22:28:41 -0500 Subject: first commit --- include/mercury/kernel/utility.hpp | 124 +++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 include/mercury/kernel/utility.hpp (limited to 'include/mercury/kernel/utility.hpp') diff --git a/include/mercury/kernel/utility.hpp b/include/mercury/kernel/utility.hpp new file mode 100644 index 0000000..dbf0cf9 --- /dev/null +++ b/include/mercury/kernel/utility.hpp @@ -0,0 +1,124 @@ +#ifndef MERCURY_KERNEL_UTILITY_HPP +#define MERCURY_KERNEL_UTILITY_HPP + +#include +#include + +namespace mercury::kernel::utility { + + template + static inline t min(t a, t b) { + return a < b ? a : b; + } + + //includes start_i, does not include end_i + void mark_bitmap_region_zero(uint64_t *bitmap, uint64_t start_i, uint64_t end_i); + //includes start_i, does not include end_i + void mark_bitmap_region_one(uint64_t *bitmap, uint64_t start_i, uint64_t end_i); + + struct uuid { + uint8_t bytes[16]; + }; + + template + struct string_tree { + + std::optional value_here = {}; + string_tree *parent = 0; + string_tree *subtrees[256] = {}; + + //if there is a node whose key has a prefix in common with str, then this + //returns the deepest such node, and sets leftover_out to point to the + //character of str after the common prefix with that returned node. if + //there is no such node, this returns the current node and sets + //leftover_out to a null pointer. + string_tree &max_common_prefix( + const char *str, size_t str_len, const char *&leftover_out) { + + string_tree *last_with_value = 0; + const char *leftover_at_last_with_value = 0; + string_tree *on = this; + size_t len_so_far = 0; + + while (true) { + + if (on->value_here) { + last_with_value = on; + leftover_at_last_with_value = str + len_so_far; + } + + if (len_so_far == str_len) + break; + + on = on->subtrees[(uint8_t)str[len_so_far]]; + if (!on) + break; + + ++len_so_far; + + } + + if (last_with_value) { + leftover_out = leftover_at_last_with_value; + return *last_with_value; + } + + leftover_out = 0; + return *this; + + } + + bool try_insert(const char *str, size_t str_len, value_t value) { + + string_tree *on = this; + for (size_t i = 0; i < str_len; ++i) { + string_tree *&subtree = on->subtrees[(uint8_t)str[i]]; + if (!subtree) { + subtree = new string_tree(); + subtree->parent = on; + } + on = subtree; + } + + if (on->value_here) + return false; + on->value_here = value; + return true; + + } + + }; + + //if c appears in str, this returns the index of the first time it appears. + //otherwise, this returns len. + static inline size_t find(const char *str, size_t len, char c) { + for (size_t i = 0; i < len; ++i) + if (str[i] == c) + return i; + return len; + } + + template + struct flist { + + struct node { + value_t value; + node *next; + }; + + node *first; + + flist() : first(0) {} + + void insert(value_t value) { + first = new node { + .value = value, + .next = first + }; + } + + }; + +} + +#endif -- cgit v1.2.3