2023-12-22 19:46:07 +01:00
|
|
|
#pragma once
|
|
|
|
|
#include "Pair.h"
|
2025-02-14 00:39:53 +01:00
|
|
|
#include "Array.h"
|
2024-06-09 12:20:04 +02:00
|
|
|
#include <memory_resource>
|
2023-12-22 19:46:07 +01:00
|
|
|
|
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 {
|
|
|
|
|
Node(Node* left, Node* right, const NodeData& nodeData) : leftChild(left), rightChild(right), data(nodeData) {}
|
|
|
|
|
Node(Node* left, Node* right, NodeData&& nodeData) : leftChild(left), rightChild(right), data(std::move(nodeData)) {}
|
2023-12-22 19:46:07 +01:00
|
|
|
Node* leftChild;
|
|
|
|
|
Node* rightChild;
|
|
|
|
|
NodeData data;
|
|
|
|
|
};
|
|
|
|
|
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
|
2024-06-09 12:20:04 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
template <typename IterType> class IteratorBase {
|
|
|
|
|
public:
|
2023-12-22 19:46:07 +01:00
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
|
using value_type = IterType;
|
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
|
using reference = IterType&;
|
|
|
|
|
using pointer = IterType*;
|
|
|
|
|
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr IteratorBase(Node* x = nullptr, Array<Node*>&& beginIt = Array<Node*>()) : node(x), traversal(std::move(beginIt)) {}
|
|
|
|
|
constexpr IteratorBase(const IteratorBase& i) : node(i.node), traversal(i.traversal) {}
|
|
|
|
|
constexpr IteratorBase(IteratorBase&& i) noexcept : node(std::move(i.node)), traversal(std::move(i.traversal)) {}
|
|
|
|
|
constexpr IteratorBase& operator=(const IteratorBase& other) {
|
|
|
|
|
if (this != &other) {
|
2023-12-22 19:46:07 +01:00
|
|
|
node = other.node;
|
|
|
|
|
traversal = other.traversal;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr IteratorBase& operator=(IteratorBase&& other) noexcept {
|
|
|
|
|
if (this != &other) {
|
2023-12-22 19:46:07 +01:00
|
|
|
node = std::move(other.node);
|
|
|
|
|
traversal = std::move(other.traversal);
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr reference operator*() const { return node->data; }
|
|
|
|
|
constexpr pointer operator->() const { return &(node->data); }
|
|
|
|
|
constexpr bool operator!=(const IteratorBase& other) { return node != other.node; }
|
|
|
|
|
constexpr bool operator==(const IteratorBase& other) { return node == other.node; }
|
|
|
|
|
constexpr std::strong_ordering operator<=>(const IteratorBase& other) { return node <=> other.node; }
|
|
|
|
|
constexpr IteratorBase& operator++() {
|
2023-12-22 19:46:07 +01:00
|
|
|
node = node->rightChild;
|
2024-06-09 12:20:04 +02:00
|
|
|
while (node != nullptr && node->leftChild != nullptr) {
|
2023-12-22 19:46:07 +01:00
|
|
|
traversal.add(node);
|
|
|
|
|
node = node->leftChild;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
if (node == nullptr && traversal.size() > 0) {
|
2023-12-22 19:46:07 +01:00
|
|
|
node = traversal.back();
|
|
|
|
|
traversal.pop();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr IteratorBase& operator--() {
|
2023-12-22 19:46:07 +01:00
|
|
|
node = node->leftChild;
|
2024-06-09 12:20:04 +02:00
|
|
|
while (node != nullptr && node->rightChild != nullptr) {
|
2023-12-22 19:46:07 +01:00
|
|
|
traversal.add(node);
|
|
|
|
|
node = node->rightchild;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
if (node == nullptr && traversal.size() > 0) {
|
2023-12-22 19:46:07 +01:00
|
|
|
node = traversal.back();
|
|
|
|
|
traversal.pop();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr IteratorBase operator--(int) {
|
2023-12-22 19:46:07 +01:00
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
--*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr IteratorBase operator++(int) {
|
2023-12-22 19:46:07 +01:00
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
++*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
Node* getNode() { return node; }
|
|
|
|
|
|
|
|
|
|
private:
|
2023-12-22 19:46:07 +01:00
|
|
|
Node* node;
|
|
|
|
|
Array<Node*> traversal;
|
|
|
|
|
};
|
|
|
|
|
using Iterator = IteratorBase<NodeData>;
|
|
|
|
|
using ConstIterator = IteratorBase<const NodeData>;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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
|
2024-06-09 12:20:04 +02: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()))
|
2024-06-09 12:20:04 +02: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()))
|
2024-06-09 12:20:04 +02: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)
|
|
|
|
|
: alloc(NodeAlloc()), root(nullptr), beginIt(nullptr), endIt(nullptr), iteratorsDirty(true), _size(0), comp(Compare()) {
|
|
|
|
|
for (const auto& it : init) {
|
|
|
|
|
insert(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr Tree(const Tree& other) : alloc(other.alloc), root(nullptr), iteratorsDirty(true), _size(), comp(other.comp) {
|
|
|
|
|
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
|
2024-06-09 12:20:04 +02:00
|
|
|
: alloc(std::move(other.alloc)), root(std::move(other.root)), 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();
|
|
|
|
|
}
|
|
|
|
|
return iterator(root);
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
2023-12-22 19:46:07 +01:00
|
|
|
return iterator(it);
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr Pair<iterator, bool> insert(const NodeData& data) {
|
2023-12-22 19:46:07 +01:00
|
|
|
auto [r, inserted] = _insert(root, data);
|
|
|
|
|
root = r;
|
|
|
|
|
return Pair<iterator, bool>(iterator(root), inserted);
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr Pair<iterator, bool> insert(NodeData&& data) {
|
2023-12-22 19:46:07 +01:00
|
|
|
auto [r, inserted] = _insert(root, std::move(data));
|
|
|
|
|
root = r;
|
|
|
|
|
return Pair<iterator, bool>(iterator(root), inserted);
|
|
|
|
|
}
|
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);
|
|
|
|
|
return iterator(root);
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
|
|
|
|
|
private:
|
2025-02-14 00:39:53 +01:00
|
|
|
/*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);
|
2025-02-14 00:39:53 +01:00
|
|
|
}*/
|
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 {
|
2023-12-22 19:46:07 +01:00
|
|
|
Node* begin = root;
|
|
|
|
|
Array<Node*> beginTraversal;
|
2024-06-09 12:20:04 +02:00
|
|
|
while (begin != nullptr) {
|
2023-12-22 19:46:07 +01:00
|
|
|
beginTraversal.add(begin);
|
|
|
|
|
begin = begin->leftChild;
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
if (!beginTraversal.empty()) {
|
2023-12-22 19:46:07 +01:00
|
|
|
begin = beginTraversal.back();
|
|
|
|
|
beginTraversal.pop();
|
|
|
|
|
}
|
|
|
|
|
return Iterator(begin, std::move(beginTraversal));
|
|
|
|
|
}
|
2024-06-09 12:20:04 +02:00
|
|
|
constexpr Iterator calcEndIterator() const {
|
2023-12-22 19:46:07 +01:00
|
|
|
Node* endIndex = root;
|
|
|
|
|
Array<Node*> endTraversal;
|
2024-06-09 12:20:04 +02:00
|
|
|
while (endIndex != nullptr) {
|
2023-12-22 19:46:07 +01:00
|
|
|
endTraversal.add(endIndex);
|
|
|
|
|
endIndex = endIndex->rightChild;
|
|
|
|
|
}
|
|
|
|
|
return Iterator(endIndex, std::move(endTraversal));
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
std::allocator_traits<NodeAlloc>::construct(alloc, root, nullptr, nullptr, std::forward<NodeDataType>(data));
|
|
|
|
|
_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);
|
|
|
|
|
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, nullptr, nullptr, std::forward<NodeDataType>(data));
|
|
|
|
|
|
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
|