mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
[kutil] Remove unused kutil headers
Part two of rearranging kutil code. (See 042f061) Removing unused kutil
headers:
I can imagine that avl_tree or slab_allocated may want to be returned to
at some point, but for now they're just clutter.
This commit is contained in:
@@ -1,251 +0,0 @@
|
||||
#pragma once
|
||||
/// \file avl_tree.h
|
||||
/// Templated container class for an AVL tree
|
||||
|
||||
#include <stdint.h>
|
||||
#include "kutil/assert.h"
|
||||
#include "kutil/slab_allocated.h"
|
||||
|
||||
namespace kutil {
|
||||
|
||||
template <typename T> class avl_tree;
|
||||
|
||||
|
||||
/// A node in a `avl_tree<T>`
|
||||
template <typename T>
|
||||
class avl_node :
|
||||
public T,
|
||||
public slab_allocated<avl_node<T>>
|
||||
{
|
||||
public:
|
||||
using item_type = T;
|
||||
using node_type = avl_node<T>;
|
||||
|
||||
/// Dereference operator. Helper to cast this node to the contained type.
|
||||
/// \returns A pointer to the node, cast to T*.
|
||||
inline item_type & operator*() { return *this; }
|
||||
|
||||
/// Dereference operator. Helper to cast this node to the contained type.
|
||||
/// \returns A pointer to the node, cast to T*.
|
||||
inline const item_type & operator*() const { return *this; }
|
||||
|
||||
/// Cast operator. Helper to cast this node to the contained type.
|
||||
/// \returns A reference to the node, cast to T&.
|
||||
inline operator item_type& () { return *this; }
|
||||
|
||||
/// Cast operator. Helper to cast this node to the contained type.
|
||||
/// \returns A reference to the node, cast to const T&.
|
||||
inline operator const item_type& () { return *this; }
|
||||
|
||||
/// Helper to cast this node to the contained type.
|
||||
/// \returns A reference to the node, cast to const T&.
|
||||
inline const item_type& item() const { return *this; }
|
||||
|
||||
/// Helper to cast this node to the contained type.
|
||||
/// \returns A reference to the node, cast to T&.
|
||||
inline item_type& item() { return *this; }
|
||||
|
||||
/// Accessor for the left child.
|
||||
/// \returns A pointer to the left child, or nullptr.
|
||||
inline node_type * left() { return m_left; }
|
||||
|
||||
/// Accessor for the left child.
|
||||
/// \returns A pointer to the left child, or nullptr.
|
||||
inline const node_type * left() const { return m_left; }
|
||||
|
||||
/// Accessor for the right child.
|
||||
/// \returns A pointer to the right child, or nullptr.
|
||||
inline node_type * right() { return m_right; }
|
||||
|
||||
/// Accessor for the right child.
|
||||
/// \returns A pointer to the right child, or nullptr.
|
||||
inline const node_type * right() const { return m_right; }
|
||||
|
||||
private:
|
||||
friend class avl_tree<T>;
|
||||
|
||||
inline static int height(node_type *l) { return (l ? l->m_height : 0); }
|
||||
|
||||
// Update this node's height and return its new balance factor
|
||||
inline int update_height()
|
||||
{
|
||||
int left = height(m_left);
|
||||
int right = height(m_right);
|
||||
m_height = left > right ? left : right;
|
||||
return left - right;
|
||||
}
|
||||
|
||||
int bias(node_type *addend)
|
||||
{
|
||||
const item_type &this_item = *this;
|
||||
const item_type &that_item = *addend;
|
||||
if (that_item < this_item)
|
||||
return -1;
|
||||
else if (that_item > this_item)
|
||||
return 1;
|
||||
|
||||
kassert(false, "Equal items not allowed in AVL tree");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static node_type * rotate_right(node_type *existing)
|
||||
{
|
||||
node_type *root = existing->m_left;
|
||||
node_type *left = root->m_right;
|
||||
|
||||
root->m_right = existing;
|
||||
existing->m_left = left;
|
||||
|
||||
existing->update_height();
|
||||
root->update_height();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
static node_type * rotate_left(node_type *existing)
|
||||
{
|
||||
node_type *root = existing->m_right;
|
||||
node_type *right = root->m_left;
|
||||
|
||||
root->m_left = existing;
|
||||
existing->m_right = right;
|
||||
|
||||
existing->update_height();
|
||||
root->update_height();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
static node_type * insert(node_type *existing, node_type *addend)
|
||||
{
|
||||
if (existing == nullptr)
|
||||
return addend;
|
||||
|
||||
if (existing->compare(addend) < 0)
|
||||
existing->m_left = insert(existing->m_left, addend);
|
||||
else
|
||||
existing->m_right = insert(existing->m_right, addend);
|
||||
|
||||
int balance = existing->update_height();
|
||||
if (balance > 1) {
|
||||
// Left-heavy
|
||||
if (existing->m_left->compare(addend) < 0) {
|
||||
// Left Left
|
||||
return rotate_right(existing);
|
||||
} else {
|
||||
// Left Right
|
||||
existing->m_left = rotate_left(existing->m_left);
|
||||
return rotate_right(existing);
|
||||
}
|
||||
} else if (balance < -1) {
|
||||
// Right-heavy
|
||||
if (existing->m_right->compare(addend) > 0) {
|
||||
// Right Right
|
||||
return rotate_left(existing);
|
||||
} else {
|
||||
// Right Left
|
||||
existing->m_right = rotate_right(existing->m_right);
|
||||
return rotate_left(existing);
|
||||
}
|
||||
}
|
||||
|
||||
return existing;
|
||||
}
|
||||
|
||||
static node_type * remove(node_type *existing, node_type *subtrahend)
|
||||
{
|
||||
if (existing == nullptr)
|
||||
return existing;
|
||||
|
||||
if (existing == subtrahend) {
|
||||
if (!existing->m_left || !existing->m_right) {
|
||||
// At least one child is null
|
||||
node_type *temp = existing->m_left ?
|
||||
existing->m_left : existing->m_right;
|
||||
|
||||
if (temp == nullptr) {
|
||||
// Both were null
|
||||
temp = existing;
|
||||
existing = nullptr;
|
||||
} else {
|
||||
*existing = *temp;
|
||||
}
|
||||
|
||||
delete temp;
|
||||
} else {
|
||||
// Both children exist, find next node
|
||||
node_type *temp = existing->m_right;
|
||||
while (temp->m_left)
|
||||
temp = temp->m_left;
|
||||
|
||||
*existing = *temp;
|
||||
existing->m_right = remove(existing->m_right, temp);
|
||||
}
|
||||
} else if (existing->compare(subtrahend) < 0) {
|
||||
existing->m_left = remove(existing->m_left, subtrahend);
|
||||
} else {
|
||||
existing->m_right = remove(existing->m_right, subtrahend);
|
||||
}
|
||||
|
||||
if (!existing)
|
||||
return nullptr;
|
||||
|
||||
int balance = existing->update_height();
|
||||
if (balance > 1) {
|
||||
int left_balance = existing->m_left->update_height();
|
||||
|
||||
if (left_balance < 0)
|
||||
existing->m_left = rotate_left(existing->m_left);
|
||||
|
||||
return rotate_right(existing);
|
||||
} else if (balance < -1) {
|
||||
int right_balance = existing->m_right->update_height();
|
||||
|
||||
if (right_balance > 0)
|
||||
existing->m_right = rotate_right(existing->m_right);
|
||||
|
||||
return rotate_left(existing);
|
||||
}
|
||||
|
||||
return existing;
|
||||
}
|
||||
|
||||
int m_height;
|
||||
node_type *m_left;
|
||||
node_type *m_right;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class avl_tree
|
||||
{
|
||||
public:
|
||||
using item_type = T;
|
||||
using node_type = avl_node<T>;
|
||||
|
||||
avl_tree() = default;
|
||||
avl_tree(avl_tree &&other) :
|
||||
m_count(other.m_count), m_root(other.m_root)
|
||||
{
|
||||
other.m_root = nullptr;
|
||||
other.m_count = 0;
|
||||
}
|
||||
|
||||
inline node_type * root() { return m_root; }
|
||||
inline unsigned count() const { return m_count; }
|
||||
|
||||
inline void remove(node_type *subtrahend) {
|
||||
m_root = node_type::remove(m_root, subtrahend);
|
||||
m_count--;
|
||||
}
|
||||
|
||||
inline void insert(node_type *addend) {
|
||||
m_root = node_type::insert(m_root, addend);
|
||||
m_count++;
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned m_count {0};
|
||||
node_type *m_root {nullptr};
|
||||
};
|
||||
|
||||
} // namespace kutil
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace kutil {
|
||||
|
||||
template <typename T>
|
||||
struct coord
|
||||
{
|
||||
T x, y;
|
||||
coord() : x(T{}), y(T{}) {}
|
||||
coord(T x, T y) : x(x), y(y) {}
|
||||
T size() const { return x * y; }
|
||||
};
|
||||
|
||||
} // namespace kutil
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
/// \file guid.h
|
||||
/// Definition of the guid type and related functions
|
||||
#include <stdint.h>
|
||||
#include "kutil/misc.h"
|
||||
|
||||
namespace kutil {
|
||||
|
||||
|
||||
/// A GUID
|
||||
struct guid
|
||||
{
|
||||
uint64_t a, b;
|
||||
};
|
||||
|
||||
/// Make a GUID by writing it naturally-ordered in code:
|
||||
/// AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE becomes:
|
||||
/// make_guid(0xAAAAAAAA, 0xBBBB, 0xCCCC, 0xDDDD, 0xEEEEEEEEEEEE);
|
||||
/// \returns The guid object
|
||||
inline constexpr guid make_guid(uint32_t a, uint16_t b, uint16_t c, uint16_t d, uint64_t e)
|
||||
{
|
||||
const uint64_t h =
|
||||
static_cast<uint64_t>(c) << 48 |
|
||||
static_cast<uint64_t>(b) << 32 |
|
||||
a;
|
||||
|
||||
const uint64_t l =
|
||||
static_cast<uint64_t>(byteswap(e & 0xffffffff)) << 32 |
|
||||
(byteswap(e >> 32) & 0xffff0000) |
|
||||
((d << 8) & 0xff00) | ((d >> 8) & 0xff);
|
||||
|
||||
return {h, l};
|
||||
}
|
||||
|
||||
} // namespace kutil
|
||||
|
||||
|
||||
inline bool operator==(const kutil::guid &a, const kutil::guid &b) { return a.a == b.a && a.b == b.b; }
|
||||
@@ -1,47 +0,0 @@
|
||||
#pragma once
|
||||
/// \file slab_allocated.h
|
||||
/// A parent template class for slab-allocated objects
|
||||
|
||||
#include "kernel_memory.h"
|
||||
#include "kutil/memory.h"
|
||||
#include "kutil/vector.h"
|
||||
|
||||
namespace kutil {
|
||||
|
||||
template <typename T, unsigned N = 1>
|
||||
class slab_allocated
|
||||
{
|
||||
public:
|
||||
void * operator new(size_t size)
|
||||
{
|
||||
kassert(size == sizeof(T), "Slab allocator got wrong size allocation");
|
||||
if (s_free.count() == 0)
|
||||
allocate_chunk();
|
||||
|
||||
T *item = s_free.pop();
|
||||
kutil::memset(item, 0, sizeof(T));
|
||||
return item;
|
||||
}
|
||||
|
||||
void operator delete(void *p) { s_free.append(reinterpret_cast<T*>(p)); }
|
||||
|
||||
private:
|
||||
static void allocate_chunk()
|
||||
{
|
||||
size_t size = N * ::memory::frame_size;
|
||||
s_free.ensure_capacity(size / sizeof(T));
|
||||
|
||||
void *memory = kalloc(size);
|
||||
T *current = reinterpret_cast<T *>(memory);
|
||||
T *end = offset_pointer(current, size);
|
||||
while (current < end)
|
||||
s_free.append(current++);
|
||||
}
|
||||
|
||||
static vector<T*> s_free;
|
||||
};
|
||||
|
||||
#define DEFINE_SLAB_ALLOCATOR(type, N) \
|
||||
template<> ::kutil::vector<type*> kutil::slab_allocated<type, N>::s_free {};
|
||||
|
||||
} // namespace kutil
|
||||
@@ -1,3 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#define __weak __attribute__ ((weak))
|
||||
Reference in New Issue
Block a user