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

548 lines
14 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
{
Node *leftChild;
Node *rightChild;
Pair<K, V> pair;
Node()
: leftChild(nullptr), rightChild(nullptr), pair()
{
}
Node(const Node& other) = default;
Node(Node&& other) = default;
Node& operator=(const Node& other) = default;
Node& operator=(Node&& other) = default;
Node(K key)
: leftChild(nullptr)
, rightChild(nullptr)
, 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)
{
}
IteratorBase(Node *x, Array<Node *> &&beginIt)
: node(x), traversal(std::move(beginIt))
{
}
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++()
{
node = node->rightChild;
while (node != nullptr && node->leftChild != nullptr)
{
traversal.add(node);
node = node->leftChild;
}
if (node == nullptr && traversal.size() > 0)
{
node = traversal.back();
traversal.pop();
}
return *this;
}
IteratorBase &operator--()
{
node = node->leftChild;
while (node != nullptr && node->rightchild != nullptr)
{
traversal.add(node);
node = node->rightChild;
}
if (node == nullptr && traversal.size() > 0)
{
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:
Node *node;
Array<Node *> traversal;
};
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)
, iteratorsDirty(false)
, _size(0)
, comp(Compare())
{
}
explicit Map(const Compare& comp,
const Allocator& alloc = Allocator())
: nodeContainer(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(false)
, _size(0)
, comp(comp)
{
}
explicit Map(const Allocator& alloc)
: nodeContainer(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(false)
, _size(0)
, comp(Compare())
{
}
Map(const Map& other)
: nodeContainer(other.nodeContainer)
, _size(other._size)
, comp(other.comp)
{
root = &nodeContainer[nodeContainer.indexOf(other.root)];
refreshIterators();
}
Map(Map&& other)
: nodeContainer(other.nodeContainer)
, _size(std::move(other._size))
, comp(std::move(other.comp))
{
root = &nodeContainer[nodeContainer.indexOf(other.root)];
refreshIterators();
}
~Map()
{
}
Map& operator=(const Map& other)
{
if(this != &other)
{
nodeContainer = other.nodeContainer;
root = &nodeContainer[nodeContainer.indexOf(other.root)];
_size = other._size;
comp = other.comp;
refreshIterators();
}
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);
refreshIterators();
}
return *this;
}
inline mapped_type& operator[](const key_type& key)
{
root = splay(root, key);
refreshIterators();
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));
refreshIterators();
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);
refreshIterators();
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));
refreshIterators();
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);
refreshIterators();
return iterator(root);
}
iterator erase(K&& key)
{
root = remove(root, std::move(key));
refreshIterators();
return iterator(root);
}
void clear()
{
nodeContainer.clear();
root = nullptr;
_size = 0;
refreshIterators();
}
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-01 20:25:16 +01:00
void markIteratorDirty()
{
iteratorsDirty = true;
}
void refreshIterators()
{
beginIt = calcBeginIterator();
endIt = calcEndIterator();
iteratorsDirty = false;
}
inline Iterator calcBeginIterator() const
{
Node *beginNode = root;
if (root == nullptr)
{
return Iterator(nullptr);
}
else
{
Array<Node *> beginTraversal;
while (beginNode != nullptr)
{
beginTraversal.add(beginNode);
beginNode = beginNode->leftChild;
}
beginNode = beginTraversal.back();
beginTraversal.pop();
return Iterator(beginNode, std::move(beginTraversal));
}
}
inline Iterator calcEndIterator() const
{
Node *endNode = root;
if (root == nullptr)
{
return Iterator(nullptr);
}
else
{
Array<Node *> endTraversal;
while (endNode != nullptr)
{
endTraversal.add(endNode);
endNode = endNode->rightChild;
}
return Iterator(endNode, std::move(endTraversal));
}
}
Array<Node, NodeAlloc> nodeContainer;
Node *root;
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
uint32 _size;
Compare comp;
Node *rotateRight(Node *node)
{
Node *y = node->leftChild;
node->leftChild = y->rightChild;
y->rightChild = node;
return y;
}
Node *rotateLeft(Node *node)
{
Node *y = node->rightChild;
node->rightChild = y->leftChild;
y->leftChild = node;
return y;
}
template<class KeyType>
Node *insert(Node *r, KeyType&& key)
{
if (r == nullptr)
{
return &nodeContainer.emplace(std::forward<KeyType>(key));
}
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-01 20:25:16 +01:00
Node *newNode = &nodeContainer.emplace(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))
{
newNode->rightChild = r;
newNode->leftChild = r->leftChild;
r->leftChild = nullptr;
}
else
{
newNode->leftChild = r;
newNode->rightChild = r->rightChild;
r->rightChild = nullptr;
}
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-01 20:25:16 +01:00
if (!r->leftChild)
{
temp = r;
r = r->rightChild;
}
else
{
temp = r;
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
r = splay(r->leftChild, key);
r->rightChild = temp->rightChild;
}
temp->leftChild = nullptr;
temp->rightChild = nullptr;
_size--;
delete temp;
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))
{
if (r->leftChild == nullptr)
return r;
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
if (comp(key, r->leftChild->pair.key))
{
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);
}
else if (comp(r->leftChild->pair.key, key))
{
r->leftChild->rightChild = splay(r->leftChild->rightChild, key);
2020-04-12 15:47:19 +02:00
2021-11-01 20:25:16 +01:00
if (r->leftChild->rightChild != nullptr)
{
r->leftChild = rotateLeft(r->leftChild);
}
}
return (r->leftChild == nullptr) ? r : rotateRight(r);
}
else
{
if (r->rightChild == nullptr)
return r;
2021-11-01 20:25:16 +01:00
if (comp(key, r->rightChild->pair.key))
{
r->rightChild->leftChild = splay(r->rightChild->leftChild, key);
2021-11-01 20:25:16 +01:00
if (r->rightChild->leftChild != nullptr)
{
r->rightChild = rotateRight(r->rightChild);
}
}
else if (comp(r->rightChild->pair.key, key))
{
r->rightChild->rightChild = splay(r->rightChild->rightChild, key);
r = rotateLeft(r);
}
return (r->rightChild == nullptr) ? r : rotateLeft(r);
}
}
2020-04-12 15:47:19 +02:00
};
} // namespace Seele