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

612 lines
16 KiB
C++
Raw Normal View History

#pragma once
2021-03-31 12:18:16 +02:00
#include <utility>
2021-11-11 20:12:50 +01:00
#include "Array.h"
2022-11-29 16:34:42 +01:00
#include "Pair.h"
2023-07-13 21:01:20 +02:00
#include "Serialization/Serialization.h"
namespace Seele
{
2021-11-01 20:25:16 +01:00
template <typename K,
typename V,
typename Compare = std::less<K>,
typename Allocator = std::pmr::polymorphic_allocator<Pair<const K,V>>>
2020-04-12 15:47:19 +02:00
struct Map
{
private:
2021-11-01 20:25:16 +01:00
struct Node
{
2023-11-16 23:56:15 +01:00
Node* leftChild;
Node* rightChild;
2021-11-01 20:25:16 +01:00
Pair<K, V> pair;
Node()
2023-11-16 23:56:15 +01:00
: leftChild(nullptr)
, rightChild(nullptr)
2021-11-04 23:47:51 +01:00
, pair()
2021-11-01 20:25:16 +01:00
{
}
Node(const Node& other) = default;
Node(Node&& other) = default;
Node& operator=(const Node& other) = default;
Node& operator=(Node&& other) = default;
Node(K key)
2023-11-16 23:56:15 +01:00
: leftChild(nullptr)
, rightChild(nullptr)
2021-11-01 20:25:16 +01:00
, pair(std::move(key))
{
}
~Node()
{
}
};
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
2020-04-12 15:47:19 +02:00
public:
2021-11-01 20:25:16 +01:00
template<typename PairType>
class IteratorBase
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = PairType;
using difference_type = std::ptrdiff_t;
using reference = PairType&;
using pointer = PairType*;
2023-11-16 23:56:15 +01:00
constexpr IteratorBase(Node* x = nullptr, Array<Node*> &&beginIt = Array<Node*>())
: node(x), traversal(std::move(beginIt))
2021-11-01 20:25:16 +01:00
{
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase(const IteratorBase &i)
: node(i.node), traversal(i.traversal)
2021-11-01 20:25:16 +01:00
{
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase(IteratorBase&& i)
: node(std::move(i.node)), traversal(std::move(i.traversal))
2021-11-01 20:25:16 +01:00
{
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase& operator=(const IteratorBase& other)
2021-11-01 20:25:16 +01:00
{
if(this != &other)
{
2021-11-08 15:39:46 +01:00
node = other.node;
2021-11-01 20:25:16 +01:00
traversal = other.traversal;
}
return *this;
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase& operator=(IteratorBase&& other)
2021-11-01 20:25:16 +01:00
{
if(this != &other)
{
node = std::move(other.node);
traversal = std::move(other.traversal);
}
return *this;
}
2023-11-16 23:56:15 +01:00
constexpr reference operator*() const
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
return node->pair;
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
constexpr pointer operator->() const
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
return &(node->pair);
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
constexpr bool operator!=(const IteratorBase &other)
2021-11-01 20:25:16 +01:00
{
return node != other.node;
}
2023-11-16 23:56:15 +01:00
constexpr bool operator==(const IteratorBase &other)
2021-11-01 20:25:16 +01:00
{
return node == other.node;
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase &operator++()
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
node = node->rightChild;
while (node != nullptr
&& node->leftChild != nullptr)
2021-11-01 20:25:16 +01:00
{
traversal.add(node);
2023-11-16 23:56:15 +01:00
node = node->leftChild;
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
if (node == nullptr
2021-11-08 15:39:46 +01:00
&& traversal.size() > 0)
2021-11-01 20:25:16 +01:00
{
node = traversal.back();
traversal.pop();
}
return *this;
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase &operator--()
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
node = node->leftChild;
while (node != nullptr
&& node->rightChild != nullptr)
2021-11-01 20:25:16 +01:00
{
traversal.add(node);
2023-11-16 23:56:15 +01:00
node = node->rightchild;
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
if (node == nullptr
2021-11-08 15:39:46 +01:00
&& traversal.size() > 0)
2021-11-01 20:25:16 +01:00
{
node = traversal.back();
traversal.pop();
}
return *this;
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase operator--(int)
2021-11-01 20:25:16 +01:00
{
IteratorBase tmp(*this);
--*this;
return tmp;
}
2023-11-16 23:56:15 +01:00
constexpr IteratorBase operator++(int)
2021-11-01 20:25:16 +01:00
{
IteratorBase tmp(*this);
++*this;
return tmp;
}
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
private:
2023-11-16 23:56:15 +01:00
Node* node;
Array<Node*> traversal;
2021-11-01 20:25:16 +01:00
};
using Iterator = IteratorBase<Pair<K,V>>;
using ConstIterator = IteratorBase<const Pair<K,V>>;
using key_type = K;
using mapped_type = V;
using value_type = Pair<K,V>;
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>;
2022-04-15 11:19:30 +02:00
constexpr Map() noexcept
2023-11-16 23:56:15 +01:00
: root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
2021-11-05 00:01:12 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(0)
, comp(Compare())
{
}
2022-04-15 11:19:30 +02:00
constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
2023-11-16 23:56:15 +01:00
: alloc(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
2021-11-05 00:01:12 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(0)
, comp(comp)
{
}
2023-10-29 09:20:23 +01:00
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare()))
2023-11-16 23:56:15 +01:00
: alloc(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
2021-11-05 00:01:12 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(0)
, comp(Compare())
{
}
2022-04-15 11:19:30 +02:00
constexpr Map(const Map& other)
2023-11-16 23:56:15 +01:00
: alloc(other.alloc)
2021-11-08 15:39:46 +01:00
, root(other.root)
2023-11-06 14:47:21 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(other._size)
, comp(other.comp)
{
}
2022-04-15 11:19:30 +02:00
constexpr Map(Map&& other) noexcept
2023-11-16 23:56:15 +01:00
: alloc(std::move(other.alloc))
2021-11-08 15:39:46 +01:00
, root(std::move(other.root))
2023-11-06 14:47:21 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(std::move(other._size))
, comp(std::move(other.comp))
{
}
2022-04-15 11:19:30 +02:00
constexpr ~Map() noexcept
2021-11-01 20:25:16 +01:00
{
2023-11-30 14:11:04 +01:00
clear();
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr Map& operator=(const Map& other)
2021-11-01 20:25:16 +01:00
{
if(this != &other)
{
2023-11-16 23:56:15 +01:00
alloc = other.alloc;
2021-11-08 15:39:46 +01:00
root = other.root;
2021-11-01 20:25:16 +01:00
_size = other._size;
comp = other.comp;
2021-12-02 13:00:03 +01:00
markIteratorsDirty();
2021-11-01 20:25:16 +01:00
}
return *this;
}
2022-04-15 11:19:30 +02:00
constexpr Map& operator=(Map&& other)
2021-11-01 20:25:16 +01:00
{
if(this != &other)
{
2023-11-16 23:56:15 +01:00
alloc = std::move(other.alloc);
2021-11-08 15:39:46 +01:00
root = std::move(other.root);
2021-11-01 20:25:16 +01:00
_size = std::move(other._size);
comp = std::move(other.comp);
2021-12-02 13:00:03 +01:00
markIteratorsDirty();
2021-11-01 20:25:16 +01:00
}
return *this;
}
2022-04-15 11:19:30 +02:00
constexpr mapped_type& operator[](const key_type& key)
2021-11-01 20:25:16 +01:00
{
root = splay(root, key);
2023-11-16 23:56:15 +01:00
if (root == nullptr || !equal(root->pair.key, key))
2021-11-01 20:25:16 +01:00
{
root = insert(root, key);
_size++;
}
2021-12-02 13:00:03 +01:00
markIteratorsDirty();
2023-11-16 23:56:15 +01:00
return root->pair.value;
2021-11-01 20:25:16 +01:00
}
2023-11-05 10:36:01 +01:00
constexpr const mapped_type& operator[](const key_type& key) const
{
2023-11-16 23:56:15 +01:00
Node* it = root;
while (!equal(it->pair.key, key))
2023-11-05 10:36:01 +01:00
{
2023-11-16 23:56:15 +01:00
if (comp(key, it->pair.key))
2023-11-05 10:36:01 +01:00
{
2023-11-16 23:56:15 +01:00
it = it->leftChild;
2023-11-05 10:36:01 +01:00
}
else
{
2023-11-16 23:56:15 +01:00
it = it->rightChild;
2023-11-05 10:36:01 +01:00
}
}
2023-11-16 23:56:15 +01:00
return it->pair.value;
2023-11-05 10:36:01 +01:00
}
2023-02-13 14:56:13 +01:00
constexpr mapped_type& at(const key_type& key)
{
root = splay(root, key);
2023-11-16 23:56:15 +01:00
if (root == nullptr || !equal(root->pair.key, key))
2023-02-13 14:56:13 +01:00
{
throw std::logic_error("Key not found");
}
markIteratorsDirty();
2023-11-16 23:56:15 +01:00
return root->pair.value;
2023-02-13 14:56:13 +01:00
}
constexpr mapped_type& at(key_type&& key)
{
root = splay(root, std::move(key));
2023-11-16 23:56:15 +01:00
if (root == nullptr || !equal(root->pair.key, key))
2023-02-13 14:56:13 +01:00
{
throw std::logic_error("Key not found");
}
markIteratorsDirty();
2023-11-16 23:56:15 +01:00
return root->pair.value;
2023-02-13 14:56:13 +01:00
}
2023-02-24 22:09:07 +01:00
constexpr const mapped_type& at(const key_type& key) const
{
2023-11-16 23:56:15 +01:00
Node* it = root;
while (!equal(it->pair.key, key))
2023-02-24 22:09:07 +01:00
{
2023-11-16 23:56:15 +01:00
if (comp(key, it->pair.key))
2023-02-24 22:09:07 +01:00
{
2023-11-16 23:56:15 +01:00
it = it->leftChild;
}
else
{
it = it->rightChild;
2023-02-24 22:09:07 +01:00
}
}
2023-11-16 23:56:15 +01:00
return it->pair.value;
2023-02-24 22:09:07 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr iterator find(const key_type& key)
2021-11-01 20:25:16 +01:00
{
root = splay(root, key);
2021-11-04 23:47:51 +01:00
refreshIterators();
2023-11-16 23:56:15 +01:00
if (root == nullptr || !equal(root->pair.key, key))
2021-11-01 20:25:16 +01:00
{
return endIt;
}
2023-11-16 23:56:15 +01:00
return iterator(root);
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr iterator find(key_type&& key)
2021-11-01 20:25:16 +01:00
{
root = splay(root, std::move(key));
2021-11-04 23:47:51 +01:00
refreshIterators();
2023-11-16 23:56:15 +01:00
if (root == nullptr || !equal(root->pair.key, key))
2021-11-01 20:25:16 +01:00
{
return endIt;
}
2023-11-16 23:56:15 +01:00
return iterator(root);
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr iterator erase(const key_type& key)
2021-11-01 20:25:16 +01:00
{
root = remove(root, key);
2021-11-04 23:47:51 +01:00
refreshIterators();
2023-11-16 23:56:15 +01:00
assert(root != nullptr || _size == 0);
return iterator(root);
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr iterator erase(key_type&& key)
2021-11-01 20:25:16 +01:00
{
root = remove(root, std::move(key));
2021-11-04 23:47:51 +01:00
refreshIterators();
2023-11-16 23:56:15 +01:00
return iterator(root);
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr void clear()
2021-11-01 20:25:16 +01:00
{
2023-11-30 14:11:04 +01:00
while(_size > 0)
2023-11-16 23:56:15 +01:00
{
root = remove(root, root->pair.key);
}
root = nullptr;
2021-12-02 13:00:03 +01:00
markIteratorsDirty();
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr bool exists(const key_type& key)
2021-11-01 20:25:16 +01:00
{
2022-04-15 11:19:30 +02:00
return find(key) != endIt;
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr bool exists(key_type&& key)
{
return find(std::move(key)) != endIt;
}
2023-02-13 14:56:13 +01:00
constexpr bool contains(const key_type& key)
{
return exists(key);
}
constexpr bool contains(key_type&& key)
{
return exists(key);
}
2022-04-15 11:19:30 +02:00
constexpr iterator begin()
2021-11-01 20:25:16 +01:00
{
if(iteratorsDirty)
{
refreshIterators();
}
return beginIt;
}
2022-04-15 11:19:30 +02:00
constexpr iterator end()
2021-11-01 20:25:16 +01:00
{
if(iteratorsDirty)
{
refreshIterators();
}
return endIt;
}
2022-04-15 11:19:30 +02:00
constexpr iterator begin() const
2021-11-01 20:25:16 +01:00
{
if(iteratorsDirty)
{
return calcBeginIterator();
}
return beginIt;
}
2022-04-15 11:19:30 +02:00
constexpr iterator end() const
2021-11-01 20:25:16 +01:00
{
if(iteratorsDirty)
{
return calcEndIterator();
}
return endIt;
}
2022-04-15 11:19:30 +02:00
constexpr bool empty() const
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
return _size == 0;
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr size_type size() const
2021-11-01 20:25:16 +01:00
{
return _size;
}
2023-07-13 21:01:20 +02:00
void save(ArchiveBuffer& buffer) const
{
buffer.writeBytes(&_size, sizeof(uint64));
for(const auto& [k, v] : *this)
{
Serialization::save(buffer, k);
Serialization::save(buffer, v);
}
}
void load(ArchiveBuffer& buffer)
{
uint64 len = 0;
buffer.readBytes(&len, sizeof(uint64));
for(uint64 i = 0; i < len; ++i)
{
2023-11-05 10:36:01 +01:00
K k = K();
V v = V();
2023-07-13 21:01:20 +02:00
Serialization::load(buffer, k);
Serialization::load(buffer, v);
2023-11-05 10:36:01 +01:00
this->operator[](std::move(k)) = std::move(v);
2023-07-13 21:01:20 +02:00
}
}
2020-04-12 15:47:19 +02:00
private:
2021-11-09 00:11:02 +01:00
void verifyTree()
{
2023-11-16 23:56:15 +01:00
size_t numElems = 0;
2023-11-30 14:11:04 +01:00
for (const auto& it : *this)
2021-11-09 00:11:02 +01:00
{
2023-11-16 23:56:15 +01:00
numElems++;
2021-11-09 00:11:02 +01:00
}
2023-11-16 23:56:15 +01:00
assert(numElems == _size);
2021-11-04 23:47:51 +01:00
}
2021-12-02 13:00:03 +01:00
void markIteratorsDirty()
2021-11-01 20:25:16 +01:00
{
iteratorsDirty = true;
}
void refreshIterators()
{
beginIt = calcBeginIterator();
endIt = calcEndIterator();
iteratorsDirty = false;
}
2023-11-06 14:47:21 +01:00
constexpr Iterator calcBeginIterator() const
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
Node* begin = root;
Array<Node*> beginTraversal;
while (begin != nullptr)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
beginTraversal.add(begin);
begin = begin->leftChild;
2021-11-01 20:25:16 +01:00
}
2021-12-02 13:00:03 +01:00
if(!beginTraversal.empty())
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
begin = beginTraversal.back();
2021-11-01 20:25:16 +01:00
beginTraversal.pop();
}
2023-11-16 23:56:15 +01:00
return Iterator(begin, std::move(beginTraversal));
2021-11-01 20:25:16 +01:00
}
2023-11-06 14:47:21 +01:00
constexpr Iterator calcEndIterator() const
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
Node* endIndex = root;
Array<Node*> endTraversal;
while (endIndex != nullptr)
2021-11-01 20:25:16 +01:00
{
2021-12-02 13:00:03 +01:00
endTraversal.add(endIndex);
2023-11-16 23:56:15 +01:00
endIndex = endIndex->rightChild;
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
return Iterator(endIndex, std::move(endTraversal));
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
NodeAlloc alloc;
Node* root;
2021-11-01 20:25:16 +01:00
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
2021-11-05 00:01:12 +01:00
size_type _size;
2021-11-01 20:25:16 +01:00
Compare comp;
2023-11-16 23:56:15 +01:00
Node* rotateLeft(Node* x)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
Node* y = x->rightChild;
2021-11-08 15:39:46 +01:00
x->rightChild = y->leftChild;
2023-11-16 23:56:15 +01:00
y->leftChild = x;
return y;
}
Node* rotateRight(Node* x)
{
Node* y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;
return y;
2021-11-01 20:25:16 +01:00
}
template<class KeyType>
2023-11-16 23:56:15 +01:00
Node* insert(Node* r, KeyType&& key)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
if (r == nullptr)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
root = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, root, std::forward<KeyType>(key));
return root;
2021-11-01 20:25:16 +01:00
}
r = splay(r, key);
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
if (equal(r->pair.key, key))
2021-11-01 20:25:16 +01:00
return r;
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
Node *newNode = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, std::forward<KeyType>(key));
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
if (comp(newNode->pair.key, r->pair.key))
2021-11-01 20:25:16 +01:00
{
newNode->rightChild = r;
2023-11-16 23:56:15 +01:00
newNode->leftChild = r->leftChild;
r->leftChild = nullptr;
2021-11-01 20:25:16 +01:00
}
else
{
newNode->leftChild = r;
2023-11-16 23:56:15 +01:00
newNode->rightChild = r->rightChild;
r->rightChild = nullptr;
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
return newNode;
2021-11-01 20:25:16 +01:00
}
template<class KeyType>
2023-11-16 23:56:15 +01:00
Node* remove(Node* r, KeyType&& key)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
Node* temp;
if (r == nullptr)
return nullptr;
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
r = splay(r, key);
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
if (!equal(r->pair.key, key))
2021-11-01 20:25:16 +01:00
return r;
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
if (r->leftChild == nullptr)
2021-11-01 20:25:16 +01:00
{
temp = r;
2023-11-16 23:56:15 +01:00
r = r->rightChild;
2021-11-01 20:25:16 +01:00
}
else
{
temp = r;
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
r = splay(r->leftChild, key);
r->rightChild = temp->rightChild;
2021-11-01 20:25:16 +01:00
}
2023-11-16 23:56:15 +01:00
std::allocator_traits<NodeAlloc>::destroy(alloc, temp);
alloc.deallocate(temp, 1);
2021-11-01 20:25:16 +01:00
_size--;
return r;
}
template<class KeyType>
2023-11-16 23:56:15 +01:00
Node* splay(Node* r, KeyType&& key)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
if (r == nullptr || equal(r->pair.key, key))
2021-11-01 20:25:16 +01:00
{
return r;
}
2023-11-16 23:56:15 +01:00
if (comp(key, r->pair.key))
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
if (r->leftChild == nullptr)
2021-11-01 20:25:16 +01:00
return r;
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
if (comp(key, r->leftChild->pair.key))
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
r->leftChild->leftChild = splay(r->leftChild->leftChild, key);
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
r = rotateRight(r);
}
2023-11-16 23:56:15 +01:00
else if (comp(r->leftChild->pair.key, key))
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
r->leftChild->rightChild = splay(r->leftChild->rightChild, key);
2020-04-12 15:47:19 +02:00
2023-11-16 23:56:15 +01:00
if (r->leftChild->rightChild != nullptr)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
r->leftChild = rotateLeft(r->leftChild);
2021-11-01 20:25:16 +01:00
}
}
2023-11-16 23:56:15 +01:00
return (r->leftChild == nullptr) ? r : rotateRight(r);
2021-11-01 20:25:16 +01:00
}
else
{
2023-11-16 23:56:15 +01:00
if (r->rightChild == nullptr)
2021-11-01 20:25:16 +01:00
return r;
2023-11-16 23:56:15 +01:00
if (comp(key, r->rightChild->pair.key))
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
r->rightChild->leftChild = splay(r->rightChild->leftChild, key);
2023-11-16 23:56:15 +01:00
if (r->rightChild->leftChild != nullptr)
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
r->rightChild = rotateRight(r->rightChild);
2021-11-01 20:25:16 +01:00
}
}
2023-11-16 23:56:15 +01:00
else if (comp(r->rightChild->pair.key, key))
2021-11-01 20:25:16 +01:00
{
2023-11-16 23:56:15 +01:00
r->rightChild->rightChild = splay(r->rightChild->rightChild, key);
2021-11-08 15:39:46 +01:00
2021-11-01 20:25:16 +01:00
r = rotateLeft(r);
}
2023-11-16 23:56:15 +01:00
return (r->rightChild == nullptr) ? r : rotateLeft(r);
2021-11-01 20:25:16 +01:00
}
}
2023-02-24 22:09:07 +01:00
bool equal(const key_type& a, const key_type& b) const
2023-02-13 14:56:13 +01:00
{
return !comp(a, b) && !comp(b, a);
}
2020-04-12 15:47:19 +02:00
};
2023-10-29 09:20:23 +01:00
} // namespace Seele