prime/include/rstl/red_black_tree.hpp

285 lines
7.9 KiB
C++
Raw Permalink Normal View History

#ifndef _RSTL_RED_BLACK_TREE
#define _RSTL_RED_BLACK_TREE
#include "types.h"
#include "rstl/functional.hpp"
#include "rstl/iterator.hpp"
2022-10-25 12:50:23 +00:00
#include "rstl/pair.hpp"
#include "rstl/rmemory_allocator.hpp"
namespace rstl {
enum node_color {
kNC_Red,
kNC_Black,
};
2022-10-25 12:50:23 +00:00
void rbtree_rebalance(void*, void*);
2022-10-16 00:03:19 +00:00
void* rbtree_traverse_forward(const void*, void*);
2022-11-10 02:06:24 +00:00
void* rbtree_rebalance_for_erase(void* header_void, void* node_void);
2022-10-16 00:03:19 +00:00
template < typename T, typename P, int U, typename S = select1st< P >, typename Cmp = less< T >,
typename Alloc = rmemory_allocator >
class red_black_tree {
private:
struct node {
node* mLeft;
node* mRight;
2022-10-25 12:50:23 +00:00
node* mParent;
node_color mColor;
uchar mValue[sizeof(P)];
2022-10-25 15:11:05 +00:00
node(node* left, node* right, node* parent, node_color color, const P& value)
: mLeft(left), mRight(right), mParent(parent), mColor(color) {
2022-10-25 12:50:23 +00:00
construct(get_value(), value);
}
~node() { get_value()->~P(); }
2022-10-25 12:50:23 +00:00
P* get_value() { return reinterpret_cast< P* >(&mValue); }
const P* get_value() const { return reinterpret_cast< const P* >(&mValue); }
node* get_left() { return mLeft; }
void set_left(node* n) { mLeft = n; }
node* get_right() { return mRight; }
void set_right(node* n) { mRight = n; }
};
2022-10-25 15:43:42 +00:00
class header {
public:
header() : mLeftmost(nullptr), mRightmost(nullptr), mRootNode(nullptr) {}
2022-10-25 15:11:05 +00:00
void set_root(node* n) { mRootNode = n; }
void set_leftmost(node* n) { mLeftmost = n; }
void set_rightmost(node* n) { mRightmost = n; }
2022-10-25 12:50:23 +00:00
2022-10-25 15:11:05 +00:00
node* get_root() const { return mRootNode; }
node* get_leftmost() const { return mLeftmost; }
node* get_rightmost() const { return mRightmost; }
2022-10-25 15:43:42 +00:00
private:
node* mLeftmost;
node* mRightmost;
node* mRootNode;
2022-10-25 12:50:23 +00:00
};
public:
struct const_iterator {
2022-11-17 15:46:10 +00:00
typedef int difference_type;
typedef forward_iterator_tag iterator_category;
typedef P* value_type;
node* mNode;
const header* mHeader;
2022-11-04 05:25:39 +00:00
// bool x8_;
2022-11-04 05:25:39 +00:00
// TODO why is this bool here?
2022-10-25 15:11:05 +00:00
const_iterator(node* node, const header* header, bool b)
: mNode(node), mHeader(header) /*, x8_(b)*/ {}
2022-10-25 14:29:23 +00:00
const P* operator->() const { return mNode->get_value(); }
2022-11-04 05:25:39 +00:00
const P* operator*() const { return mNode->get_value(); }
bool operator==(const const_iterator& other) const {
return mNode == other.mNode && mHeader == other.mHeader;
}
bool operator!=(const const_iterator& other) const {
2022-10-16 00:03:19 +00:00
// return !(*this == other);
return mNode != other.mNode || mHeader != other.mHeader;
}
const_iterator& operator++() {
2022-10-25 12:50:23 +00:00
mNode = static_cast< node* >(rbtree_traverse_forward(static_cast< const void* >(mHeader),
static_cast< void* >(mNode)));
2022-10-16 00:03:19 +00:00
return *this;
}
const_iterator operator++(int) {
const_iterator result = *this;
2022-10-25 12:50:23 +00:00
mNode = static_cast< node* >(rbtree_traverse_forward(static_cast< const void* >(mHeader),
static_cast< void* >(mNode)));
2022-10-16 00:03:19 +00:00
return result;
}
};
2022-10-25 15:11:05 +00:00
struct iterator : public const_iterator {
iterator(node* node, const header* header, bool b) : const_iterator(node, header, b) {}
2022-11-10 02:06:24 +00:00
P* operator->() { return mNode->get_value(); }
P* operator*() { return mNode->get_value(); }
node* get_node() { return mNode; }
2022-10-25 15:11:05 +00:00
};
2022-10-25 15:43:42 +00:00
red_black_tree() : x0_(0), x1_(0), x4_count(0) {}
~red_black_tree() { clear(); }
2022-10-25 15:11:05 +00:00
iterator insert_into(node* n, const P& item);
iterator insert(const P& item) { return insert_into(x8_header.get_root(), item); }
2022-10-25 12:50:23 +00:00
2022-10-25 14:29:23 +00:00
const_iterator begin() const {
// TODO
2022-10-25 15:11:05 +00:00
return const_iterator(x8_header.get_leftmost(), &x8_header, false);
2022-10-25 14:29:23 +00:00
}
const_iterator end() const {
// TODO
2022-10-25 15:11:05 +00:00
return const_iterator(nullptr, &x8_header, false);
2022-10-25 14:29:23 +00:00
}
2022-11-17 15:46:10 +00:00
iterator begin() {
// TODO
return iterator(x8_header.get_leftmost(), &x8_header, false);
}
iterator end() {
// TODO
return iterator(nullptr, &x8_header, false);
}
2022-10-25 14:29:23 +00:00
const_iterator find(const T& key) const {
2022-10-25 15:11:05 +00:00
node* n = x8_header.get_root();
2022-10-25 14:29:23 +00:00
node* needle = nullptr;
while (n != nullptr) {
2022-10-25 15:43:42 +00:00
if (!x2_cmp(x3_selector(*n->get_value()), key)) {
2022-10-25 14:29:23 +00:00
needle = n;
n = n->get_left();
} else {
n = n->get_right();
}
}
bool noResult = false;
2022-10-25 15:43:42 +00:00
if (needle == nullptr || x2_cmp(key, x3_selector(*needle->get_value()))) {
2022-10-25 14:29:23 +00:00
noResult = true;
}
if (noResult) {
needle = nullptr;
}
2022-10-25 15:11:05 +00:00
return const_iterator(needle, &x8_header, false);
2022-10-25 14:29:23 +00:00
}
2022-11-10 02:06:24 +00:00
iterator find(const T& key) {
node* n = x8_header.get_root();
node* needle = nullptr;
while (n != nullptr) {
if (!x2_cmp(x3_selector(*n->get_value()), key)) {
needle = n;
n = n->get_left();
} else {
n = n->get_right();
}
}
bool noResult = false;
if (needle == nullptr || x2_cmp(key, x3_selector(*needle->get_value()))) {
noResult = true;
}
if (noResult) {
needle = nullptr;
}
return iterator(needle, &x8_header, false);
}
2022-11-10 02:06:24 +00:00
iterator erase(iterator it) {
node* node = it.get_node();
++it;
free_node(rebalance_for_erase(node));
x4_count--;
return it;
}
2022-10-25 15:11:05 +00:00
void clear() {
node* root = x8_header.get_root();
if (root != nullptr) {
free_node_and_sub_nodes(root);
}
x8_header.set_root(nullptr);
x8_header.set_leftmost(nullptr);
x8_header.set_rightmost(nullptr);
x4_count = 0;
}
2022-11-17 15:46:10 +00:00
int size() const { return x4_count; }
private:
2022-10-25 15:43:42 +00:00
uchar x0_;
uchar x1_;
Cmp x2_cmp;
S x3_selector;
2022-10-25 12:50:23 +00:00
int x4_count;
2022-10-25 15:11:05 +00:00
header x8_header;
node* create_node(node* left, node* right, node* parent, node_color color, const P& value) {
node* n;
2022-10-25 15:43:42 +00:00
Alloc::allocate(n, 1);
2022-10-25 15:11:05 +00:00
new (n) node(left, right, parent, color, value);
return n;
}
void free_node_and_sub_nodes(node* n) {
if (node* left = n->get_left()) {
free_node_and_sub_nodes(left);
}
if (node* right = n->get_right()) {
free_node_and_sub_nodes(right);
}
free_node(n);
}
void free_node(node* n) {
n->~node();
2022-10-25 15:43:42 +00:00
Alloc::deallocate(n);
2022-10-25 15:11:05 +00:00
}
void rebalance(node* n) { rbtree_rebalance(&x8_header, n); }
2022-11-10 02:06:24 +00:00
node* rebalance_for_erase(node* n) {
return static_cast< node* >(rbtree_rebalance_for_erase(&x8_header, n));
}
};
2022-10-25 12:50:23 +00:00
static bool kUnknownValueNewRoot = true;
static bool kUnknownValueEqualKey = false;
static bool kUnknownValueNewItem = true;
template < typename T, typename P, int U, typename S, typename Cmp, typename Alloc >
2022-10-25 15:11:05 +00:00
typename red_black_tree< T, P, U, S, Cmp, Alloc >::iterator
2022-10-25 12:50:23 +00:00
red_black_tree< T, P, U, S, Cmp, Alloc >::insert_into(node* n, const P& item) {
if (n == nullptr) {
2022-10-25 15:11:05 +00:00
x8_header.set_root(create_node(nullptr, nullptr, nullptr, kNC_Red, item));
2022-10-25 12:50:23 +00:00
x4_count += 1;
2022-10-25 15:11:05 +00:00
x8_header.set_leftmost(x8_header.get_root());
x8_header.set_rightmost(x8_header.get_root());
return iterator(x8_header.get_root(), &x8_header, kUnknownValueNewRoot);
2022-10-25 12:50:23 +00:00
} else {
node* newNode = nullptr;
while (newNode == nullptr) {
2022-10-25 15:43:42 +00:00
bool firstComp = x2_cmp(x3_selector(*n->get_value()), x3_selector(item));
if (!firstComp && !x2_cmp(x3_selector(item), x3_selector(*n->get_value()))) {
2022-10-25 15:11:05 +00:00
return iterator(n, &x8_header, kUnknownValueEqualKey);
2022-10-25 12:50:23 +00:00
}
if (firstComp) {
if (n->get_left() == nullptr) {
2022-10-25 15:11:05 +00:00
newNode = create_node(nullptr, nullptr, n, kNC_Red, item);
2022-10-25 12:50:23 +00:00
n->set_left(newNode);
2022-10-25 15:11:05 +00:00
if (n == x8_header.get_leftmost()) {
x8_header.set_leftmost(newNode);
2022-10-25 12:50:23 +00:00
}
} else {
n = n->get_left();
}
} else {
if (n->get_right() == nullptr) {
2022-10-25 15:11:05 +00:00
newNode = create_node(nullptr, nullptr, n, kNC_Black, item);
2022-10-25 12:50:23 +00:00
n->set_right(newNode);
2022-10-25 15:11:05 +00:00
if (n == x8_header.get_rightmost()) {
x8_header.set_rightmost(newNode);
2022-10-25 12:50:23 +00:00
}
} else {
n = n->get_right();
}
}
}
x4_count += 1;
2022-10-25 15:11:05 +00:00
rebalance(newNode);
return iterator(newNode, &x8_header, kUnknownValueNewItem);
2022-10-25 12:50:23 +00:00
}
}
}; // namespace rstl
#endif // _RSTL_RED_BLACK_TREE