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

699 lines
19 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
{
2021-11-04 23:47:51 +01:00
size_t leftChild;
size_t rightChild;
2021-11-01 20:25:16 +01:00
Pair<K, V> pair;
Node()
: leftChild(SIZE_MAX)
, rightChild(SIZE_MAX)
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)
: leftChild(SIZE_MAX)
, rightChild(SIZE_MAX)
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*;
IteratorBase(size_t x = SIZE_MAX)
2021-11-01 20:25:16 +01:00
: node(x)
{
}
2021-11-08 15:39:46 +01:00
IteratorBase(size_t x, const Array<Node, NodeAlloc>* nodeContainer, Array<size_t> &&beginIt = Array<size_t>())
2021-11-04 23:47:51 +01:00
: node(x), traversal(std::move(beginIt)), nodeContainer(nodeContainer)
2021-11-01 20:25:16 +01:00
{
}
IteratorBase(const IteratorBase &i)
2021-11-08 15:39:46 +01:00
: node(i.node), traversal(i.traversal), nodeContainer(i.nodeContainer)
2021-11-01 20:25:16 +01:00
{
}
IteratorBase(IteratorBase&& i)
2021-11-08 15:39:46 +01:00
: node(std::move(i.node)), traversal(std::move(i.traversal)), nodeContainer(i.nodeContainer)
2021-11-01 20:25:16 +01:00
{
}
IteratorBase& operator=(const IteratorBase& other)
{
if(this != &other)
{
2021-11-08 15:39:46 +01:00
node = other.node;
nodeContainer = other.nodeContainer;
2021-11-01 20:25:16 +01:00
traversal = other.traversal;
}
return *this;
}
IteratorBase& operator=(IteratorBase&& other)
{
if(this != &other)
{
node = std::move(other.node);
2021-11-08 15:39:46 +01:00
nodeContainer = std::move(other.nodeContainer);
2021-11-01 20:25:16 +01:00
traversal = std::move(other.traversal);
}
return *this;
}
reference operator*() const
{
2021-11-08 15:39:46 +01:00
return getNode()->pair;
2021-11-01 20:25:16 +01:00
}
pointer operator->() const
{
2021-11-08 15:39:46 +01:00
return &(getNode()->pair);
2021-11-01 20:25:16 +01:00
}
inline bool operator!=(const IteratorBase &other)
{
return node != other.node;
}
inline bool operator==(const IteratorBase &other)
{
return node == other.node;
}
IteratorBase &operator++()
{
2021-11-08 15:39:46 +01:00
node = getNode()->rightChild;
while (node < nodeContainer->size()
&& getNode()->leftChild < nodeContainer->size())
2021-11-01 20:25:16 +01:00
{
traversal.add(node);
2021-11-08 15:39:46 +01:00
node = getNode()->leftChild;
2021-11-01 20:25:16 +01:00
}
2021-11-08 15:39:46 +01:00
if (node >= nodeContainer->size()
&& traversal.size() > 0)
2021-11-01 20:25:16 +01:00
{
node = traversal.back();
traversal.pop();
}
return *this;
}
IteratorBase &operator--()
{
2021-11-08 15:39:46 +01:00
node = getNode()->leftChild;
while (node < nodeContainer->size()
&& getNode()->rightChild < nodeContainer->size())
2021-11-01 20:25:16 +01:00
{
traversal.add(node);
2021-11-08 15:39:46 +01:00
node = getNode()->rightchild;
2021-11-01 20:25:16 +01:00
}
2021-11-08 15:39:46 +01:00
if (node >= nodeContainer->size()
&& traversal.size() > 0)
2021-11-01 20:25:16 +01:00
{
node = traversal.back();
traversal.pop();
}
return *this;
}
IteratorBase operator--(int)
{
IteratorBase tmp(*this);
--*this;
return tmp;
}
IteratorBase operator++(int)
{
IteratorBase tmp(*this);
++*this;
return tmp;
}
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
private:
2021-11-08 15:39:46 +01:00
Node* getNode() const
{
return &(*nodeContainer)[node];
}
size_t node;
2021-11-04 23:47:51 +01:00
Array<size_t> traversal;
const Array<Node, NodeAlloc>* nodeContainer;
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
: root(SIZE_MAX)
, beginIt(SIZE_MAX)
, endIt(SIZE_MAX)
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()))
2021-11-01 20:25:16 +01:00
: nodeContainer(alloc)
, root(SIZE_MAX)
, beginIt(SIZE_MAX)
, endIt(SIZE_MAX)
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()))
2021-11-01 20:25:16 +01:00
: nodeContainer(alloc)
, root(SIZE_MAX)
, beginIt(SIZE_MAX)
, endIt(SIZE_MAX)
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)
2021-11-01 20:25:16 +01:00
: nodeContainer(other.nodeContainer)
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
: nodeContainer(std::move(other.nodeContainer))
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
{
}
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)
{
nodeContainer = other.nodeContainer;
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)
{
nodeContainer = std::move(other.nodeContainer);
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-02-13 14:56:13 +01:00
if (!isValid(root) || !equal(getNode(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();
2021-11-08 15:39:46 +01:00
return getNode(root)->pair.value;
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr mapped_type& operator[](key_type&& key)
2021-11-01 20:25:16 +01:00
{
root = splay(root, std::move(key));
2023-02-13 14:56:13 +01:00
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
2021-11-01 20:25:16 +01:00
{
root = insert(root, std::move(key));
_size++;
}
2021-12-02 13:00:03 +01:00
markIteratorsDirty();
2021-11-08 15:39:46 +01:00
return getNode(root)->pair.value;
2021-11-01 20:25:16 +01:00
}
2023-02-13 14:56:13 +01:00
2023-11-05 10:36:01 +01:00
constexpr const mapped_type& operator[](const key_type& key) const
{
size_t it = root;
while (!equal(getNode(it)->pair.key, key))
{
if (comp(key, getNode(it)->pair.key))
{
it = getNode(it)->leftChild;
}
else
{
it = getNode(it)->rightChild;
}
}
return getNode(it)->pair.value;
}
constexpr const mapped_type& operator[](key_type&& key) const
{
size_t it = root;
while (!equal(getNode(it)->pair.key, key))
{
if (comp(key, getNode(it)->pair.key))
{
it = getNode(it)->leftChild;
}
else
{
it = getNode(it)->rightChild;
}
}
return getNode(it)->pair.value;
}
2023-02-13 14:56:13 +01:00
constexpr mapped_type& at(const key_type& key)
{
root = splay(root, key);
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
{
throw std::logic_error("Key not found");
}
markIteratorsDirty();
return getNode(root)->pair.value;
}
constexpr mapped_type& at(key_type&& key)
{
root = splay(root, std::move(key));
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
{
throw std::logic_error("Key not found");
}
markIteratorsDirty();
return getNode(root)->pair.value;
}
2023-02-24 22:09:07 +01:00
constexpr const mapped_type& at(const key_type& key) const
{
for(const auto& node : nodeContainer)
{
if(equal(node.pair.key, key))
{
return node.pair.value;
}
}
throw std::logic_error("Key not found");
}
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-02-13 14:56:13 +01:00
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
2021-11-01 20:25:16 +01:00
{
return endIt;
}
2021-11-08 15:39:46 +01:00
return iterator(root, &nodeContainer);
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-02-13 14:56:13 +01:00
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
2021-11-01 20:25:16 +01:00
{
return endIt;
}
2021-11-08 15:39:46 +01:00
return iterator(root, &nodeContainer);
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();
2021-11-08 15:39:46 +01:00
return iterator(root, &nodeContainer);
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();
2021-11-08 15:39:46 +01:00
return iterator(root, &nodeContainer);
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
{
nodeContainer.clear();
root = SIZE_MAX;
2021-11-01 20:25:16 +01:00
_size = 0;
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
{
return nodeContainer.empty();
}
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()
{
for(size_t i = 0; i < nodeContainer.size(); ++i)
{
bool found = false;
for(auto it : nodeContainer)
{
if(it.leftChild == i)
{
assert(!found);
found = true;
}
if(it.rightChild == i)
{
assert(!found);
found = true;
}
}
if(isValid(nodeContainer[i].leftChild))
{
assert(comp(nodeContainer[nodeContainer[i].leftChild].pair.key, nodeContainer[i].pair.key));
}
if(isValid(nodeContainer[i].rightChild))
{
assert(comp(nodeContainer[i].pair.key, nodeContainer[nodeContainer[i].rightChild].pair.key));
}
}
}
2021-11-08 15:39:46 +01:00
Node* getNode(size_t index) const
2021-11-04 23:47:51 +01:00
{
2021-11-08 15:39:46 +01:00
if(!isValid(index)) return nullptr;
return &nodeContainer[index];
2021-11-04 23:47:51 +01:00
}
2021-11-08 15:39:46 +01:00
inline bool isValid(size_t index) const
2021-11-04 23:47:51 +01:00
{
2021-11-08 15:39:46 +01:00
return index < nodeContainer.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
{
2021-12-02 13:00:03 +01:00
size_t beginIndex = root;
Array<size_t> beginTraversal;
while (isValid(beginIndex))
2021-11-01 20:25:16 +01:00
{
2021-12-02 13:00:03 +01:00
beginTraversal.add(beginIndex);
beginIndex = getNode(beginIndex)->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
{
2021-11-08 15:39:46 +01:00
beginIndex = beginTraversal.back();
2021-11-01 20:25:16 +01:00
beginTraversal.pop();
}
2021-12-02 13:00:03 +01:00
return Iterator(beginIndex, &nodeContainer, 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
{
2021-12-02 13:00:03 +01:00
size_t endIndex = root;
Array<size_t> endTraversal;
while (isValid(endIndex))
2021-11-01 20:25:16 +01:00
{
2021-12-02 13:00:03 +01:00
endTraversal.add(endIndex);
endIndex = getNode(endIndex)->rightChild;
2021-11-01 20:25:16 +01:00
}
2021-12-02 13:00:03 +01:00
return Iterator(endIndex, &nodeContainer, std::move(endTraversal));
2021-11-01 20:25:16 +01:00
}
2023-02-01 22:13:04 +01:00
Array<Node, NodeAlloc> nodeContainer = Array<Node, NodeAlloc>();
2021-11-08 15:39:46 +01:00
size_t 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;
2021-11-08 15:39:46 +01:00
size_t rotateRight(size_t node)
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
Node* x = getNode(node);
size_t res = x->leftChild;
Node* y = getNode(x->leftChild);
x->leftChild = y->rightChild;
2021-11-01 20:25:16 +01:00
y->rightChild = node;
2021-11-08 15:39:46 +01:00
return res;
2021-11-01 20:25:16 +01:00
}
2021-11-08 15:39:46 +01:00
size_t rotateLeft(size_t node)
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
Node* x = getNode(node);
size_t res = x->rightChild;
Node* y = getNode(x->rightChild);
x->rightChild = y->leftChild;
2021-11-01 20:25:16 +01:00
y->leftChild = node;
2021-11-08 15:39:46 +01:00
return res;
2021-11-01 20:25:16 +01:00
}
template<class KeyType>
2021-11-08 15:39:46 +01:00
size_t insert(size_t r, KeyType&& key)
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
if (!isValid(r))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
nodeContainer.emplace(std::forward<KeyType>(key));
return nodeContainer.size() - 1;
2021-11-01 20:25:16 +01:00
}
r = splay(r, key);
2021-11-09 00:11:02 +01:00
Node* node = getNode(r);
2020-04-12 15:47:19 +02:00
2023-02-13 14:56:13 +01:00
if (equal(node->pair.key, key))
2021-11-01 20:25:16 +01:00
return r;
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
Node *newNode = &nodeContainer.emplace(std::forward<KeyType>(key));
2021-11-09 16:38:26 +01:00
node = getNode(r);
2020-04-12 15:47:19 +02:00
2023-02-13 14:56:13 +01:00
if (comp(newNode->pair.key, node->pair.key))
2021-11-01 20:25:16 +01:00
{
newNode->rightChild = r;
2021-11-08 15:39:46 +01:00
newNode->leftChild = node->leftChild;
node->leftChild = SIZE_MAX;
2021-11-01 20:25:16 +01:00
}
else
{
newNode->leftChild = r;
2021-11-08 15:39:46 +01:00
newNode->rightChild = node->rightChild;
node->rightChild = SIZE_MAX;
2021-11-01 20:25:16 +01:00
}
2021-11-08 15:39:46 +01:00
return nodeContainer.size() - 1;
2021-11-01 20:25:16 +01:00
}
template<class KeyType>
2021-11-08 15:39:46 +01:00
size_t remove(size_t r, KeyType&& key)
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
size_t temp;
if (!isValid(r))
return SIZE_MAX;
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
r = splay(r, key);
2021-11-08 15:39:46 +01:00
Node* node = getNode(r);
2020-04-12 15:47:19 +02:00
2023-02-13 14:56:13 +01:00
if (!equal(node->pair.key, key))
2021-11-01 20:25:16 +01:00
return r;
2020-04-12 15:47:19 +02:00
2021-11-08 15:39:46 +01:00
if (!isValid(node->leftChild))
2021-11-01 20:25:16 +01:00
{
temp = r;
2021-11-08 15:39:46 +01:00
r = node->rightChild;
2021-11-01 20:25:16 +01:00
}
else
{
temp = r;
2020-04-12 15:47:19 +02:00
2021-11-08 15:39:46 +01:00
r = splay(node->leftChild, key);
node = getNode(r);
node->rightChild = getNode(temp)->rightChild;
2021-11-01 20:25:16 +01:00
}
2021-11-02 19:42:00 +01:00
Node& lastNode = nodeContainer.back();
2021-11-08 15:39:46 +01:00
size_t lastIndex = nodeContainer.size() - 1;
size_t removedIndex = temp;
2021-11-04 23:47:51 +01:00
//Arrays can only pop back, so we need to move the last element to the deleted index
2021-11-08 15:39:46 +01:00
if(removedIndex != lastIndex)
2021-11-02 19:42:00 +01:00
{
2021-11-04 23:47:51 +01:00
nodeContainer[removedIndex] = std::move(lastNode);
2021-11-08 15:39:46 +01:00
for(auto& it : nodeContainer)
2021-11-02 19:42:00 +01:00
{
2021-11-08 15:39:46 +01:00
if(it.leftChild == lastIndex)
2021-11-04 23:47:51 +01:00
{
it.leftChild = removedIndex;
}
2021-11-08 15:39:46 +01:00
if(it.rightChild == lastIndex)
2021-11-04 23:47:51 +01:00
{
it.rightChild = removedIndex;
}
2021-11-02 19:42:00 +01:00
}
}
nodeContainer.pop();
2021-11-01 20:25:16 +01:00
_size--;
return r;
}
template<class KeyType>
2021-11-08 15:39:46 +01:00
size_t splay(size_t r, KeyType&& key)
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
Node* node = getNode(r);
2023-02-13 14:56:13 +01:00
if (node == nullptr || equal(node->pair.key, key))
2021-11-01 20:25:16 +01:00
{
return r;
}
2021-11-08 15:39:46 +01:00
if (comp(key, node->pair.key))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
if (!isValid(node->leftChild))
2021-11-01 20:25:16 +01:00
return r;
2020-04-12 15:47:19 +02:00
2021-11-08 15:39:46 +01:00
if (comp(key, getNode(node->leftChild)->pair.key))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
getNode(node->leftChild)->leftChild = splay(getNode(node->leftChild)->leftChild, key);
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
r = rotateRight(r);
2021-11-08 15:39:46 +01:00
node = getNode(r);
2021-11-01 20:25:16 +01:00
}
2021-11-08 15:39:46 +01:00
else if (comp(getNode(node->leftChild)->pair.key, key))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
getNode(node->leftChild)->rightChild = splay(getNode(node->leftChild)->rightChild, key);
2020-04-12 15:47:19 +02:00
2021-11-08 15:39:46 +01:00
if (isValid(getNode(node->leftChild)->rightChild))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
node->leftChild = rotateLeft(node->leftChild);
2021-11-01 20:25:16 +01:00
}
}
2021-11-08 15:39:46 +01:00
return (!isValid(node->leftChild)) ? r : rotateRight(r);
2021-11-01 20:25:16 +01:00
}
else
{
2021-11-08 15:39:46 +01:00
if (!isValid(node->rightChild))
2021-11-01 20:25:16 +01:00
return r;
2021-11-08 15:39:46 +01:00
if (comp(key, getNode(node->rightChild)->pair.key))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
getNode(node->rightChild)->leftChild = splay(getNode(node->rightChild)->leftChild, key);
2021-11-08 15:39:46 +01:00
if (isValid(getNode(node->rightChild)->leftChild))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
node->rightChild = rotateRight(node->rightChild);
2021-11-01 20:25:16 +01:00
}
}
2021-11-08 15:39:46 +01:00
else if (comp(getNode(node->rightChild)->pair.key, key))
2021-11-01 20:25:16 +01:00
{
2021-11-08 15:39:46 +01:00
getNode(node->rightChild)->rightChild = splay(getNode(node->rightChild)->rightChild, key);
2021-11-01 20:25:16 +01:00
r = rotateLeft(r);
2021-11-08 15:39:46 +01:00
node = getNode(r);
2021-11-01 20:25:16 +01:00
}
2021-11-08 15:39:46 +01:00
return (!isValid(node->rightChild)) ? 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