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

597 lines
16 KiB
C++
Raw Normal View History

#pragma once
2021-03-31 12:18:16 +02:00
#include <utility>
#include "Array.h"
namespace Seele
{
2020-04-12 15:47:19 +02:00
template <typename K, typename V>
struct Pair
{
public:
2021-11-01 20:25:16 +01:00
Pair()
: key(K()), value(V())
{}
Pair(const Pair& other) = default;
Pair(Pair&& other) = default;
~Pair(){}
Pair& operator=(const Pair& other) = default;
Pair& operator=(Pair&& other) = default;
template<class KeyType>
explicit Pair(KeyType&& key)
: key(std::forward<KeyType>(key)), value(V())
{}
template<class KeyType, class ValueType>
explicit Pair(KeyType&& key, ValueType&& value)
: key(std::forward<KeyType>(key)), value(std::forward<ValueType>(value))
{}
K key;
V value;
2020-04-12 15:47:19 +02:00
};
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 self;
size_t leftChild;
size_t rightChild;
2021-11-01 20:25:16 +01:00
Pair<K, V> pair;
Node()
2021-11-04 23:47:51 +01:00
: self(-1)
, leftChild(-1)
, rightChild(-1)
, 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;
2021-11-04 23:47:51 +01:00
Node(size_t self, K key)
: self(self)
, leftChild(-1)
, rightChild(-1)
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*;
2021-11-01 20:25:16 +01:00
IteratorBase(Node *x = nullptr)
: node(x)
{
}
2021-11-04 23:47:51 +01:00
IteratorBase(Node *x, Array<size_t> &&beginIt, const Array<Node, NodeAlloc>* nodeContainer)
: node(x), traversal(std::move(beginIt)), nodeContainer(nodeContainer)
2021-11-01 20:25:16 +01:00
{
}
IteratorBase(const IteratorBase &i)
: node(i.node), traversal(i.traversal)
{
}
IteratorBase(IteratorBase&& i)
: node(std::move(i.node)), traversal(std::move(i.traversal))
{
}
IteratorBase& operator=(const IteratorBase& other)
{
if(this != &other)
{
node = other.node; // No copy, since no ownership
traversal = other.traversal;
}
return *this;
}
IteratorBase& operator=(IteratorBase&& other)
{
if(this != &other)
{
node = std::move(other.node);
traversal = std::move(other.traversal);
}
return *this;
}
reference operator*() const
{
return node->pair;
}
pointer operator->() const
{
return &node->pair;
}
inline bool operator!=(const IteratorBase &other)
{
return node != other.node;
}
inline bool operator==(const IteratorBase &other)
{
return node == other.node;
}
IteratorBase &operator++()
{
2021-11-04 23:47:51 +01:00
size_t nextIndex = node->rightChild;
while (nextIndex != -1 && (*nodeContainer)[nextIndex].leftChild != -1)
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
traversal.add(nextIndex);
nextIndex = (*nodeContainer)[nextIndex].leftChild;
2021-11-01 20:25:16 +01:00
}
2021-11-04 23:47:51 +01:00
if (nextIndex == -1 && traversal.size() > 0)
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
nextIndex = traversal.back();
2021-11-01 20:25:16 +01:00
traversal.pop();
}
2021-11-04 23:47:51 +01:00
node = nextIndex != -1 ? &(*nodeContainer)[nextIndex] : nullptr;
2021-11-01 20:25:16 +01:00
return *this;
}
IteratorBase &operator--()
{
2021-11-04 23:47:51 +01:00
size_t nextIndex = node->leftChild;
while (nextIndex != -1 && (*nodeContainer)[nextIndex].rightChild != -1)
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
traversal.add(nextIndex);
nextIndex = (*nodeContainer)[nextIndex].rightchild;
2021-11-01 20:25:16 +01:00
}
2021-11-04 23:47:51 +01:00
if (nextIndex == -1 && traversal.size() > 0)
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
nextIndex = traversal.back();
2021-11-01 20:25:16 +01:00
traversal.pop();
}
2021-11-04 23:47:51 +01:00
node = nextIndex != -1 ? &(*nodeContainer)[nextIndex] : nullptr;
2021-11-01 20:25:16 +01:00
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:
Node *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>;
Map()
: root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
2021-11-02 19:42:00 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(0)
, comp(Compare())
{
}
explicit Map(const Compare& comp,
const Allocator& alloc = Allocator())
: nodeContainer(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
2021-11-02 19:42:00 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(0)
, comp(comp)
{
}
explicit Map(const Allocator& alloc)
: nodeContainer(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
2021-11-02 19:42:00 +01:00
, iteratorsDirty(true)
2021-11-01 20:25:16 +01:00
, _size(0)
, comp(Compare())
{
}
Map(const Map& other)
: nodeContainer(other.nodeContainer)
, _size(other._size)
, comp(other.comp)
{
root = &nodeContainer[nodeContainer.indexOf(other.root)];
2021-11-02 19:42:00 +01:00
markIteratorDirty();
2021-11-01 20:25:16 +01:00
}
Map(Map&& other)
: nodeContainer(other.nodeContainer)
, _size(std::move(other._size))
, comp(std::move(other.comp))
{
root = &nodeContainer[nodeContainer.indexOf(other.root)];
2021-11-02 19:42:00 +01:00
markIteratorDirty();
2021-11-01 20:25:16 +01:00
}
~Map()
{
}
Map& operator=(const Map& other)
{
if(this != &other)
{
nodeContainer = other.nodeContainer;
root = &nodeContainer[nodeContainer.indexOf(other.root)];
_size = other._size;
comp = other.comp;
2021-11-02 19:42:00 +01:00
markIteratorDirty();
2021-11-01 20:25:16 +01:00
}
return *this;
}
Map& operator=(Map&& other)
{
if(this != &other)
{
nodeContainer = std::move(other.nodeContainer);
root = &nodeContainer[nodeContainer.indexOf(other.root)];
_size = std::move(other._size);
comp = std::move(other.comp);
2021-11-02 19:42:00 +01:00
markIteratorDirty();
2021-11-01 20:25:16 +01:00
}
return *this;
}
inline mapped_type& operator[](const key_type& key)
{
root = splay(root, key);
2021-11-02 19:42:00 +01:00
markIteratorDirty();
2021-11-01 20:25:16 +01:00
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
root = insert(root, key);
_size++;
}
return root->pair.value;
}
inline mapped_type& operator[](key_type&& key)
{
root = splay(root, std::move(key));
2021-11-02 19:42:00 +01:00
markIteratorDirty();
2021-11-01 20:25:16 +01:00
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
root = insert(root, std::move(key));
_size++;
}
return root->pair.value;
}
iterator find(const key_type& key)
{
root = splay(root, key);
2021-11-04 23:47:51 +01:00
refreshIterators();
2021-11-01 20:25:16 +01:00
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
return endIt;
}
return iterator(root);
}
iterator find(key_type&& key)
{
root = splay(root, std::move(key));
2021-11-04 23:47:51 +01:00
refreshIterators();
2021-11-01 20:25:16 +01:00
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
return endIt;
}
return iterator(root);
}
iterator erase(const key_type& key)
{
root = remove(root, key);
2021-11-04 23:47:51 +01:00
refreshIterators();
2021-11-01 20:25:16 +01:00
return iterator(root);
}
iterator erase(K&& key)
{
root = remove(root, std::move(key));
2021-11-04 23:47:51 +01:00
refreshIterators();
2021-11-01 20:25:16 +01:00
return iterator(root);
}
void clear()
{
nodeContainer.clear();
root = nullptr;
_size = 0;
2021-11-02 19:42:00 +01:00
markIteratorDirty();
2021-11-01 20:25:16 +01:00
}
bool exists(key_type&& key)
{
return find(std::forward<K>(key)) != endIt;
}
iterator begin()
{
if(iteratorsDirty)
{
refreshIterators();
}
return beginIt;
}
iterator end()
{
if(iteratorsDirty)
{
refreshIterators();
}
return endIt;
}
iterator begin() const
{
if(iteratorsDirty)
{
return calcBeginIterator();
}
return beginIt;
}
iterator end() const
{
if(iteratorsDirty)
{
return calcEndIterator();
}
return endIt;
}
bool empty() const
{
return nodeContainer.empty();
}
size_type size() const
{
return _size;
}
2020-04-12 15:47:19 +02:00
private:
2021-11-04 23:47:51 +01:00
Node* leftChild(Node* node)
{
if(node->leftChild >= nodeContainer.size()) return nullptr;
return &nodeContainer[node->leftChild];
}
Node* rightChild(Node* node)
{
if(node->rightChild >= nodeContainer.size()) return nullptr;
return &nodeContainer[node->rightChild];
}
Node* leftChild(Node* node) const
{
if(node->leftChild >= nodeContainer.size()) return nullptr;
return &nodeContainer[node->leftChild];
}
Node* rightChild(Node* node) const
{
if(node->rightChild >= nodeContainer.size()) return nullptr;
return &nodeContainer[node->rightChild];
}
2021-11-01 20:25:16 +01:00
void markIteratorDirty()
{
iteratorsDirty = true;
}
void refreshIterators()
{
beginIt = calcBeginIterator();
endIt = calcEndIterator();
iteratorsDirty = false;
}
inline Iterator calcBeginIterator() const
{
if (root == nullptr)
{
return Iterator(nullptr);
}
else
{
2021-11-04 23:47:51 +01:00
size_t beginIndex = root->self;
Array<size_t> beginTraversal;
while (beginIndex < nodeContainer.size())
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
beginTraversal.add(beginIndex);
beginIndex = nodeContainer[beginIndex].leftChild;
2021-11-01 20:25:16 +01:00
}
2021-11-04 23:47:51 +01:00
Node* beginNode = &nodeContainer[beginTraversal.back()];
2021-11-01 20:25:16 +01:00
beginTraversal.pop();
2021-11-04 23:47:51 +01:00
return Iterator(beginNode, std::move(beginTraversal), &nodeContainer);
2021-11-01 20:25:16 +01:00
}
}
inline Iterator calcEndIterator() const
{
if (root == nullptr)
{
return Iterator(nullptr);
}
else
{
2021-11-04 23:47:51 +01:00
size_t endIndex = root->self;
Array<size_t> endTraversal;
while (endIndex < nodeContainer.size())
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
endTraversal.add(endIndex);
endIndex = nodeContainer[endIndex].rightChild;
2021-11-01 20:25:16 +01:00
}
2021-11-04 23:47:51 +01:00
return Iterator(nullptr, std::move(endTraversal), &nodeContainer);
2021-11-01 20:25:16 +01:00
}
}
Array<Node, NodeAlloc> nodeContainer;
Node *root;
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
uint32 _size;
Compare comp;
Node *rotateRight(Node *node)
{
2021-11-04 23:47:51 +01:00
Node *y = leftChild(node);
2021-11-01 20:25:16 +01:00
node->leftChild = y->rightChild;
2021-11-04 23:47:51 +01:00
y->rightChild = node->self;
2021-11-01 20:25:16 +01:00
return y;
}
Node *rotateLeft(Node *node)
{
2021-11-04 23:47:51 +01:00
Node *y = rightChild(node);
2021-11-01 20:25:16 +01:00
node->rightChild = y->leftChild;
2021-11-04 23:47:51 +01:00
y->leftChild = node->self;
2021-11-01 20:25:16 +01:00
return y;
}
template<class KeyType>
Node *insert(Node *r, KeyType&& key)
{
if (r == nullptr)
{
2021-11-04 23:47:51 +01:00
return &nodeContainer.emplace(nodeContainer.size(), std::forward<KeyType>(key));
2021-11-01 20:25:16 +01:00
}
r = splay(r, key);
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
if (!(comp(r->pair.key, key) || comp(key, r->pair.key)))
return r;
2020-04-12 15:47:19 +02:00
2021-11-04 23:47:51 +01:00
Node *newNode = &nodeContainer.emplace(nodeContainer.size(), std::forward<KeyType>(key));
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
if (comp(key, r->pair.key))
{
2021-11-04 23:47:51 +01:00
newNode->rightChild = r->self;
2021-11-01 20:25:16 +01:00
newNode->leftChild = r->leftChild;
2021-11-04 23:47:51 +01:00
r->leftChild = -1;
2021-11-01 20:25:16 +01:00
}
else
{
2021-11-04 23:47:51 +01:00
newNode->leftChild = r->self;
2021-11-01 20:25:16 +01:00
newNode->rightChild = r->rightChild;
2021-11-04 23:47:51 +01:00
r->rightChild = -1;
2021-11-01 20:25:16 +01:00
}
return newNode;
}
template<class KeyType>
Node *remove(Node *r, KeyType&& key)
{
Node *temp;
if (!r)
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
2021-11-01 20:25:16 +01:00
if (comp(r->pair.key, key) || comp(key, r->pair.key))
return r;
2020-04-12 15:47:19 +02:00
2021-11-04 23:47:51 +01:00
if (r->leftChild == -1)
2021-11-01 20:25:16 +01:00
{
temp = r;
2021-11-04 23:47:51 +01:00
r = rightChild(r);
2021-11-01 20:25:16 +01:00
}
else
{
temp = r;
2020-04-12 15:47:19 +02:00
2021-11-04 23:47:51 +01:00
r = splay(leftChild(r), key);
2021-11-01 20:25:16 +01:00
r->rightChild = temp->rightChild;
}
2021-11-02 19:42:00 +01:00
Node& lastNode = nodeContainer.back();
2021-11-04 23:47:51 +01:00
size_t removedIndex = temp->self;
//Arrays can only pop back, so we need to move the last element to the deleted index
if(removedIndex != lastNode.self)
2021-11-02 19:42:00 +01:00
{
2021-11-04 23:47:51 +01:00
nodeContainer[removedIndex] = std::move(lastNode);
for(auto it : nodeContainer)
2021-11-02 19:42:00 +01:00
{
2021-11-04 23:47:51 +01:00
if(it.leftChild == lastNode.self)
{
it.leftChild = removedIndex;
}
if(it.rightChild == lastNode.self)
{
it.rightChild = removedIndex;
}
2021-11-02 19:42:00 +01:00
}
2021-11-04 23:47:51 +01:00
lastNode.self = 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>
Node *splay(Node *r, KeyType&& key)
{
if (r == nullptr || !(comp(r->pair.key, key) || comp(key, r->pair.key)))
{
return r;
}
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
if (comp(key, r->pair.key))
{
2021-11-04 23:47:51 +01:00
if (r->leftChild >= nodeContainer.size())
2021-11-01 20:25:16 +01:00
return r;
2020-04-12 15:47:19 +02:00
2021-11-04 23:47:51 +01:00
if (comp(key, leftChild(r)->pair.key))
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
Node* res = splay(leftChild(leftChild(r)), key);
leftChild(r)->leftChild = res ? res->self : -1;
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
r = rotateRight(r);
}
2021-11-04 23:47:51 +01:00
else if (comp(leftChild(r)->pair.key, key))
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
Node* res = splay(rightChild(leftChild(r)), key);
leftChild(r)->rightChild = res ? res->self : -1;
2020-04-12 15:47:19 +02:00
2021-11-04 23:47:51 +01:00
if (leftChild(r)->rightChild < nodeContainer.size())
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
r->leftChild = rotateLeft(leftChild(r))->self;
2021-11-01 20:25:16 +01:00
}
}
2021-11-04 23:47:51 +01:00
return (r->leftChild >= nodeContainer.size()) ? r : rotateRight(r);
2021-11-01 20:25:16 +01:00
}
else
{
2021-11-04 23:47:51 +01:00
if (r->rightChild >= nodeContainer.size())
2021-11-01 20:25:16 +01:00
return r;
2021-11-04 23:47:51 +01:00
if (comp(key, rightChild(r)->pair.key))
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
Node* res = splay(leftChild(rightChild(r)), key);
rightChild(r)->leftChild = res ? res->self : -1;
2021-11-04 23:47:51 +01:00
if (rightChild(r)->leftChild < nodeContainer.size())
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
r->rightChild = rotateRight(rightChild(r))->self;
2021-11-01 20:25:16 +01:00
}
}
2021-11-04 23:47:51 +01:00
else if (comp(rightChild(r)->pair.key, key))
2021-11-01 20:25:16 +01:00
{
2021-11-04 23:47:51 +01:00
Node* res = splay(rightChild(rightChild(r)), key);
rightChild(r)->rightChild = res ? res->self : -1;
2021-11-01 20:25:16 +01:00
r = rotateLeft(r);
}
2021-11-04 23:47:51 +01:00
return (r->rightChild >= nodeContainer.size()) ? r : rotateLeft(r);
2021-11-01 20:25:16 +01:00
}
}
2020-04-12 15:47:19 +02:00
};
} // namespace Seele