Files
Seele/src/Engine/Containers/Tree.h
T

401 lines
14 KiB
C++
Raw Normal View History

2023-12-22 19:46:07 +01:00
#pragma once
#include "Array.h"
2025-03-25 09:17:08 +01:00
#include "List.h"
2025-03-20 20:15:38 +01:00
#include "Pair.h"
2024-06-09 12:20:04 +02:00
namespace Seele {
template <typename KeyType, typename NodeData, typename KeyFun, typename Compare, typename Allocator> struct Tree {
protected:
struct Node {
2025-03-25 09:17:08 +01:00
Node(const NodeData& data) : data(data) {}
Node(NodeData&& data) : data(std::move(data)) {}
2025-03-20 20:15:38 +01:00
2025-03-25 09:17:08 +01:00
Node* leftChild = nullptr;
Node* rightChild = nullptr;
2023-12-22 19:46:07 +01:00
NodeData data;
};
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
2024-06-09 12:20:04 +02:00
public:
2023-12-22 19:46:07 +01:00
using key_type = KeyType;
using value_type = NodeData;
using allocator_type = Allocator;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = std::allocator_traits<Allocator>::pointer;
using const_pointer = std::allocator_traits<Allocator>::const_pointer;
2025-03-20 20:15:38 +01:00
class ConstIterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = value_type;
using difference_type = difference_type;
using pointer = const_pointer;
using reference = const value_type&;
2025-03-25 09:17:08 +01:00
constexpr ConstIterator(Node* x, List<Node*>&& traversal) : node(x), traversal(std::move(traversal)) {}
2025-03-20 20:15:38 +01:00
constexpr ConstIterator(const ConstIterator& i) = default;
constexpr ConstIterator(ConstIterator&& i) noexcept = default;
constexpr ConstIterator& operator=(const ConstIterator& other) = default;
constexpr ConstIterator& operator=(ConstIterator&& other) noexcept = default;
constexpr reference operator*() const noexcept { return node->data; }
constexpr pointer operator->() const noexcept { return std::pointer_traits<pointer>::pointer_to(**this); }
constexpr bool operator==(const ConstIterator& other) { return node == other.node; }
constexpr bool operator!=(const ConstIterator& other) { return node != other.node; }
constexpr std::strong_ordering operator<=>(const ConstIterator& other) { return node <=> other.node; }
constexpr ConstIterator& operator++() noexcept {
2025-03-25 09:17:08 +01:00
// walk up tree until we can go a single step to the right
while (traversal.size() > 0 && node->rightChild == nullptr) {
node = traversal.back();
traversal.popBack();
2025-03-20 20:15:38 +01:00
}
2025-03-25 09:17:08 +01:00
// if there is a right subtree we havent visited yet
if (node->rightChild != nullptr) {
// go that single step
node = node->rightChild;
// then to the leftmost node of that right subtree
while (node->leftChild != nullptr) {
node = node->leftChild;
}
2025-03-20 20:15:38 +01:00
}
return *this;
}
constexpr ConstIterator& operator--() {
2025-03-25 09:17:08 +01:00
while (node->leftChild == nullptr) {
node = traversal.back();
traversal.popBack();
2025-03-20 20:15:38 +01:00
}
2025-03-25 09:17:08 +01:00
node = node->leftChild;
while (node->rightChild != nullptr) {
node = node->rightChild;
2025-03-20 20:15:38 +01:00
}
return *this;
}
constexpr ConstIterator operator--(int) {
ConstIterator tmp(*this);
--*this;
return tmp;
}
constexpr ConstIterator operator++(int) {
ConstIterator tmp(*this);
++*this;
return tmp;
}
Node* getNode() { return node; }
protected:
Node* node;
2025-03-25 09:17:08 +01:00
List<Node*> traversal;
2025-03-20 20:15:38 +01:00
};
class Iterator : public ConstIterator {
public:
using value_type = value_type;
using difference_type = difference_type;
using pointer = pointer;
using reference = value_type&;
2025-03-25 09:17:08 +01:00
constexpr Iterator(Node* x, List<Node*>&& traversal) : ConstIterator(x, std::move(traversal)) {}
2025-03-20 20:15:38 +01:00
constexpr Iterator(const Iterator& i) = default;
constexpr Iterator(Iterator&& i) noexcept = default;
constexpr Iterator& operator=(const Iterator& other) = default;
constexpr Iterator& operator=(Iterator&& other) noexcept = default;
constexpr reference operator*() const noexcept { return const_cast<reference>(ConstIterator::operator*()); }
constexpr pointer operator->() const noexcept { return std::pointer_traits<pointer>::pointer_to(**this); }
constexpr Iterator& operator++() noexcept {
ConstIterator::operator++();
return *this;
}
constexpr Iterator& operator--() noexcept {
ConstIterator::operator--();
return *this;
}
constexpr Iterator& operator++(int) noexcept {
Iterator tmp = *this;
ConstIterator::operator++();
return tmp;
}
constexpr Iterator& operator--(int) noexcept {
Iterator tmp = *this;
ConstIterator::operator--();
return tmp;
}
};
2023-12-22 19:46:07 +01:00
using iterator = Iterator;
using const_iterator = ConstIterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr Tree() noexcept
2025-03-25 09:17:08 +01:00
: alloc(NodeAlloc()), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(0), comp(Compare()) {}
2023-12-22 19:46:07 +01:00
constexpr explicit Tree(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
2025-03-25 09:17:08 +01:00
: alloc(alloc), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(0), comp(comp) {}
2023-12-22 19:46:07 +01:00
constexpr explicit Tree(const Allocator& alloc) noexcept(noexcept(Compare()))
2025-03-25 09:17:08 +01:00
: alloc(alloc), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(0), comp(Compare()) {}
2024-07-12 13:33:52 +02:00
constexpr Tree(std::initializer_list<NodeData> init)
2025-03-25 09:17:08 +01:00
: alloc(NodeAlloc()), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(0), comp(Compare()) {
2024-07-12 13:33:52 +02:00
for (const auto& it : init) {
insert(it);
}
}
2025-03-25 09:17:08 +01:00
constexpr Tree(const Tree& other)
: alloc(other.alloc), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(), comp(other.comp) {
2024-06-09 12:20:04 +02:00
for (const auto& elem : other) {
2023-12-23 18:26:54 +01:00
insert(elem);
}
2023-12-22 19:46:07 +01:00
}
constexpr Tree(Tree&& other) noexcept
2025-03-25 09:17:08 +01:00
: alloc(std::move(other.alloc)), root(std::move(other.root)), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true),
_size(std::move(other._size)), comp(std::move(other.comp)) {
2023-12-24 21:49:49 +01:00
other._size = 0;
2023-12-22 19:46:07 +01:00
}
2024-06-09 12:20:04 +02:00
constexpr ~Tree() noexcept { clear(); }
constexpr Tree& operator=(const Tree& other) {
if (this != &other) {
2023-12-22 19:46:07 +01:00
clear();
2024-06-09 12:20:04 +02:00
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value) {
2023-12-23 18:26:54 +01:00
alloc = other.alloc;
}
2024-06-09 12:20:04 +02:00
for (const auto& elem : other) {
2023-12-23 18:26:54 +01:00
insert(elem);
}
2023-12-22 19:46:07 +01:00
markIteratorsDirty();
}
return *this;
}
2024-06-09 12:20:04 +02:00
constexpr Tree& operator=(Tree&& other) noexcept {
if (this != &other) {
2023-12-22 19:46:07 +01:00
clear();
2024-06-09 12:20:04 +02:00
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value) {
2023-12-24 21:49:49 +01:00
alloc = std::move(other.alloc);
}
2023-12-22 19:46:07 +01:00
root = std::move(other.root);
_size = std::move(other._size);
comp = std::move(other.comp);
2023-12-24 21:49:49 +01:00
other._size = 0;
2023-12-22 19:46:07 +01:00
markIteratorsDirty();
}
return *this;
}
2024-06-09 12:20:04 +02:00
constexpr void clear() {
while (_size > 0) {
2023-12-22 19:46:07 +01:00
root = _remove(root, keyFun(root->data));
}
root = nullptr;
markIteratorsDirty();
}
2024-06-09 12:20:04 +02:00
constexpr iterator begin() {
if (iteratorsDirty) {
2023-12-22 19:46:07 +01:00
refreshIterators();
}
return beginIt;
}
2024-06-09 12:20:04 +02:00
constexpr iterator end() {
if (iteratorsDirty) {
2023-12-22 19:46:07 +01:00
refreshIterators();
}
return endIt;
}
2024-06-09 12:20:04 +02:00
constexpr iterator begin() const {
if (iteratorsDirty) {
2023-12-22 19:46:07 +01:00
return calcBeginIterator();
}
return beginIt;
}
2024-06-09 12:20:04 +02:00
constexpr iterator end() const {
if (iteratorsDirty) {
2023-12-22 19:46:07 +01:00
return calcEndIterator();
}
return endIt;
}
2024-06-09 12:20:04 +02:00
constexpr bool empty() const { return _size == 0; }
constexpr size_type size() const { return _size; }
protected:
constexpr iterator find(const key_type& key) {
2023-12-22 19:46:07 +01:00
root = _splay(root, key);
2024-06-09 12:20:04 +02:00
if (root == nullptr || !equal(root->data, key)) {
2023-12-22 19:46:07 +01:00
return end();
}
2025-03-25 09:17:08 +01:00
return iterator(root, {});
2023-12-22 19:46:07 +01:00
}
2024-06-09 12:20:04 +02:00
constexpr iterator find(const key_type& key) const {
2023-12-22 19:46:07 +01:00
Node* it = root;
2024-06-09 12:20:04 +02:00
while (it != nullptr && !equal(it->data, key)) {
if (comp(key, keyFun(it->data))) {
2023-12-22 19:46:07 +01:00
it = it->leftChild;
2024-06-09 12:20:04 +02:00
} else {
2023-12-22 19:46:07 +01:00
it = it->rightChild;
}
}
2024-06-09 12:20:04 +02:00
if (it == nullptr) {
2024-04-26 11:42:28 +02:00
return end();
}
2025-03-25 09:17:08 +01:00
return iterator(it, {});
2023-12-22 19:46:07 +01:00
}
2024-06-09 12:20:04 +02:00
constexpr Pair<iterator, bool> insert(const NodeData& data) {
2025-03-20 20:15:38 +01:00
const auto& [r, inserted] = _insert(root, data);
2023-12-22 19:46:07 +01:00
root = r;
2025-03-25 09:17:08 +01:00
return Pair<iterator, bool>(iterator(root, {}), inserted);
2023-12-22 19:46:07 +01:00
}
2024-06-09 12:20:04 +02:00
constexpr Pair<iterator, bool> insert(NodeData&& data) {
2025-03-20 20:15:38 +01:00
const auto& [r, inserted] = _insert(root, std::move(data));
2023-12-22 19:46:07 +01:00
root = r;
2025-03-25 09:17:08 +01:00
return Pair<iterator, bool>(iterator(root, {}), inserted);
2023-12-22 19:46:07 +01:00
}
2024-06-09 12:20:04 +02:00
constexpr iterator remove(const key_type& key) {
2023-12-22 19:46:07 +01:00
root = _remove(root, key);
2025-03-25 09:17:08 +01:00
return iterator(root, {});
2023-12-22 19:46:07 +01:00
}
2024-06-09 12:20:04 +02:00
private:
/*void verifyTree() {
2023-12-22 19:46:07 +01:00
size_t numElems = 0;
2024-06-09 12:20:04 +02:00
for (const auto& it : *this) {
2023-12-22 19:46:07 +01:00
numElems++;
}
assert(numElems == _size);
}*/
2024-06-09 12:20:04 +02:00
void markIteratorsDirty() { iteratorsDirty = true; }
void refreshIterators() {
2023-12-22 19:46:07 +01:00
beginIt = calcBeginIterator();
endIt = calcEndIterator();
iteratorsDirty = false;
}
2024-06-09 12:20:04 +02:00
constexpr Iterator calcBeginIterator() const {
2025-03-25 09:17:08 +01:00
Node* beginNode = root;
List<Node*> traversal;
while (beginNode->leftChild != nullptr) {
traversal.add(beginNode);
beginNode = beginNode->leftChild;
2023-12-22 19:46:07 +01:00
}
2025-03-25 09:17:08 +01:00
return Iterator(beginNode, std::move(traversal));
2023-12-22 19:46:07 +01:00
}
2024-06-09 12:20:04 +02:00
constexpr Iterator calcEndIterator() const {
2025-03-20 20:15:38 +01:00
Node* endNode = root;
2025-03-25 09:17:08 +01:00
List<Node*> traversal;
2025-03-20 20:15:38 +01:00
while (endNode != nullptr) {
2025-03-25 09:17:08 +01:00
traversal.add(endNode);
2025-03-20 20:15:38 +01:00
endNode = endNode->rightChild;
2023-12-22 19:46:07 +01:00
}
2025-03-25 09:17:08 +01:00
return Iterator(endNode, std::move(traversal));
2023-12-22 19:46:07 +01:00
}
NodeAlloc alloc;
Node* root;
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
size_type _size;
Compare comp;
KeyFun keyFun = KeyFun();
2024-06-09 12:20:04 +02:00
Node* rotateLeft(Node* x) {
2023-12-22 19:46:07 +01:00
Node* y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;
return y;
}
2024-06-09 12:20:04 +02:00
Node* rotateRight(Node* x) {
2023-12-22 19:46:07 +01:00
Node* y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;
return y;
}
2024-06-09 12:20:04 +02:00
template <class NodeDataType> Pair<Node*, bool> _insert(Node* r, NodeDataType&& data) {
2023-12-24 21:49:49 +01:00
markIteratorsDirty();
2024-06-09 12:20:04 +02:00
if (r == nullptr) {
2023-12-22 19:46:07 +01:00
root = alloc.allocate(1);
2025-03-20 20:15:38 +01:00
std::allocator_traits<NodeAlloc>::construct(alloc, root, std::forward<NodeDataType>(data));
2023-12-22 19:46:07 +01:00
_size++;
return Pair<Node*, bool>(root, true);
}
r = _splay(r, keyFun(data));
if (equal(r->data, keyFun(data)))
return Pair<Node*, bool>(r, false);
Node* newNode = alloc.allocate(1);
2025-03-20 20:15:38 +01:00
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, std::forward<NodeDataType>(data));
2023-12-22 19:46:07 +01:00
2024-06-09 12:20:04 +02:00
if (comp(keyFun(newNode->data), keyFun(r->data))) {
2023-12-22 19:46:07 +01:00
newNode->rightChild = r;
newNode->leftChild = r->leftChild;
r->leftChild = nullptr;
2024-06-09 12:20:04 +02:00
} else {
2023-12-22 19:46:07 +01:00
newNode->leftChild = r;
newNode->rightChild = r->rightChild;
r->rightChild = nullptr;
}
_size++;
return Pair<Node*, bool>(newNode, true);
}
2024-06-09 12:20:04 +02:00
template <class K> Node* _remove(Node* r, K&& key) {
2023-12-24 21:49:49 +01:00
markIteratorsDirty();
2023-12-22 19:46:07 +01:00
Node* temp;
if (r == nullptr)
return nullptr;
r = _splay(r, key);
if (!equal(r->data, key))
return r;
2024-06-09 12:20:04 +02:00
if (r->leftChild == nullptr) {
2023-12-22 19:46:07 +01:00
temp = r;
r = r->rightChild;
2024-06-09 12:20:04 +02:00
} else {
2023-12-22 19:46:07 +01:00
temp = r;
r = _splay(r->leftChild, key);
r->rightChild = temp->rightChild;
}
std::allocator_traits<NodeAlloc>::destroy(alloc, temp);
alloc.deallocate(temp, 1);
_size--;
return r;
}
2024-06-09 12:20:04 +02:00
template <class K> Node* _splay(Node* r, K&& key) {
2023-12-24 21:49:49 +01:00
markIteratorsDirty();
2024-06-09 12:20:04 +02:00
if (r == nullptr || equal(r->data, key)) {
2023-12-22 19:46:07 +01:00
return r;
}
2024-06-09 12:20:04 +02:00
if (comp(key, keyFun(r->data))) {
2023-12-22 19:46:07 +01:00
if (r->leftChild == nullptr)
return r;
2024-06-09 12:20:04 +02:00
if (comp(key, keyFun(r->leftChild->data))) {
2023-12-22 19:46:07 +01:00
r->leftChild->leftChild = _splay(r->leftChild->leftChild, key);
r = rotateRight(r);
2024-06-09 12:20:04 +02:00
} else if (comp(keyFun(r->leftChild->data), key)) {
2023-12-22 19:46:07 +01:00
r->leftChild->rightChild = _splay(r->leftChild->rightChild, key);
2024-06-09 12:20:04 +02:00
if (r->leftChild->rightChild != nullptr) {
2023-12-22 19:46:07 +01:00
r->leftChild = rotateLeft(r->leftChild);
}
}
return (r->leftChild == nullptr) ? r : rotateRight(r);
2024-06-09 12:20:04 +02:00
} else {
2023-12-22 19:46:07 +01:00
if (r->rightChild == nullptr)
return r;
2024-06-09 12:20:04 +02:00
if (comp(key, keyFun(r->rightChild->data))) {
2023-12-22 19:46:07 +01:00
r->rightChild->leftChild = _splay(r->rightChild->leftChild, key);
2024-06-09 12:20:04 +02:00
if (r->rightChild->leftChild != nullptr) {
2023-12-22 19:46:07 +01:00
r->rightChild = rotateRight(r->rightChild);
}
2024-06-09 12:20:04 +02:00
} else if (comp(keyFun(r->rightChild->data), key)) {
2023-12-22 19:46:07 +01:00
r->rightChild->rightChild = _splay(r->rightChild->rightChild, key);
r = rotateLeft(r);
}
return (r->rightChild == nullptr) ? r : rotateLeft(r);
}
}
2024-06-09 12:20:04 +02:00
template <typename K> bool equal(const NodeData& a, K&& b) const { return !comp(keyFun(a), b) && !comp(b, keyFun(a)); }
2023-12-22 19:46:07 +01:00
};
} // namespace Seele