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

509 lines
9.2 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:
Pair()
: key(K()), value(V())
2021-03-31 12:18:16 +02:00
{}
template<class KeyType>
explicit Pair(KeyType&& key)
: key(key), value(V())
{}
template<class KeyType, class ValueType>
explicit Pair(KeyType&& key, ValueType&& value)
: key(std::move(key)), value(std::move(value))
{}
2020-04-12 15:47:19 +02:00
K key;
V value;
};
template <typename K, typename V>
struct Map
{
private:
struct Node
{
Node *leftChild;
Node *rightChild;
2021-04-01 16:40:14 +02:00
Pair<K, V> pair;
2020-10-14 01:49:43 +02:00
Node()
2021-03-31 12:18:16 +02:00
: leftChild(nullptr), rightChild(nullptr), pair()
2020-10-14 01:49:43 +02:00
{
}
2021-03-31 12:18:16 +02:00
template<class KeyType, class ValueType>
explicit Node(KeyType&& key, ValueType&& value)
: leftChild(nullptr), rightChild(nullptr), pair(key, value)
2020-04-12 15:47:19 +02:00
{
}
2020-10-14 01:49:43 +02:00
Node(const Node& other)
: pair(other.pair.key, other.pair.value)
{
if(other.leftChild != nullptr)
{
leftChild = new Node(*other.leftChild);
}
if(other.rightChild != nullptr)
{
rightChild = new Node(*other.rightChild);
}
}
Node(Node&& other)
: leftChild(std::move(other.leftChild)), rightChild(std::move(other.rightChild))
2020-04-12 15:47:19 +02:00
{
}
~Node()
{
if (leftChild != nullptr)
{
delete leftChild;
}
if (rightChild != nullptr)
{
delete rightChild;
}
}
2020-10-14 01:49:43 +02:00
Node& operator=(const Node& other)
{
if(this != &other)
{
if(leftChild != nullptr)
{
delete leftChild;
}
if(rightChild != nullptr)
{
delete rightChild;
}
if(other.leftChild != nullptr)
{
leftChild = new Node(*other.leftChild);
}
if(other.rightChild != nullptr)
{
rightChild = new Node(*other.rightChild);
}
}
}
Node& operator=(Node&& other)
{
if(this != &other)
{
if(leftChild != nullptr)
{
delete leftChild;
}
if(rightChild != nullptr)
{
delete rightChild;
}
leftChild = std::move(other.leftChild);
rightChild = std::move(other.rightChild);
}
return *this;
}
};
2020-04-12 15:47:19 +02:00
public:
Map()
: root(nullptr), _size(0)
{
}
2020-10-14 01:49:43 +02:00
Map(const Map& other)
: root(new Node(*other.root)), _size(other._size)
{
refreshIterators();
}
Map(Map&& other)
: root(std::move(other.root)), _size(other._size)
{
refreshIterators();
}
2020-04-12 15:47:19 +02:00
~Map()
{
delete root;
}
2020-10-14 01:49:43 +02:00
Map& operator=(const Map& other)
{
if(this != &other)
{
if(root != nullptr)
{
delete root;
}
root = new Node(*other.root);
_size = other.size;
refreshIterators();
}
return *this;
}
Map& operator=(Map&& other)
{
if(this != &other)
{
if(root != nullptr)
{
delete root;
}
root = new Node(std::move(*other.root));
_size = std::move(other._size);
refreshIterators();
}
return *this;
}
2020-04-12 15:47:19 +02:00
class Iterator
{
public:
2020-04-12 15:47:19 +02:00
typedef std::bidirectional_iterator_tag iterator_category;
typedef Pair<K, V> value_type;
typedef std::ptrdiff_t difference_type;
typedef Pair<K, V> &reference;
typedef Pair<K, V> *pointer;
2020-04-12 15:47:19 +02:00
Iterator(Node *x = nullptr)
: node(x)
{
}
Iterator(Node *x, Array<Node *> &&beginIt)
: node(x), traversal(std::move(beginIt))
{
}
Iterator(const Iterator &i)
: node(i.node), traversal(i.traversal)
{
}
2020-10-14 01:49:43 +02:00
Iterator(Iterator&& i)
: node(std::move(i.node)), traversal(std::move(i.traversal))
{
}
Iterator& operator=(const Iterator& other)
{
if(this != &other)
{
node = other.node; // No copy, since no ownership
traversal = other.traversal;
}
return *this;
}
Iterator& operator=(Iterator&& other)
{
if(this != &other)
{
node = std::move(other.node);
traversal = std::move(other.traversal);
}
return *this;
}
2020-04-12 15:47:19 +02:00
reference operator*() const
{
return node->pair;
}
pointer operator->() const
{
return &node->pair;
}
inline bool operator!=(const Iterator &other)
{
return node != other.node;
}
inline bool operator==(const Iterator &other)
{
return node == other.node;
}
Iterator &operator++()
{
node = node->rightChild;
while (node != nullptr && node->leftChild != nullptr)
{
2020-04-12 15:47:19 +02:00
traversal.add(node);
2020-04-01 02:17:49 +02:00
node = node->leftChild;
}
2020-04-12 15:47:19 +02:00
if (node == nullptr && traversal.size() > 0)
2020-04-01 02:17:49 +02:00
{
2020-04-12 15:47:19 +02:00
node = traversal.back();
traversal.pop();
2020-04-01 02:17:49 +02:00
}
2020-04-12 15:47:19 +02:00
return *this;
2020-04-01 02:17:49 +02:00
}
2020-04-12 15:47:19 +02:00
Iterator &operator--()
{
2020-04-12 15:47:19 +02:00
node = node->leftChild;
while (node != nullptr && node->rightchild != nullptr)
{
2020-04-12 15:47:19 +02:00
traversal.add(node);
node = node->rightChild;
}
2020-04-12 15:47:19 +02:00
if (node == nullptr && traversal.size() > 0)
{
node = traversal.back();
traversal.pop();
}
return *this;
}
2020-04-12 15:47:19 +02:00
Iterator operator--(int)
2020-04-01 02:17:49 +02:00
{
2020-04-12 15:47:19 +02:00
Iterator tmp(*this);
2021-03-31 12:18:16 +02:00
--*this;
2020-04-12 15:47:19 +02:00
return tmp;
2020-04-01 02:17:49 +02:00
}
2020-04-12 15:47:19 +02:00
Iterator operator++(int)
{
2020-04-12 15:47:19 +02:00
Iterator tmp(*this);
++*this;
return tmp;
}
2020-04-12 15:47:19 +02:00
private:
Node *node;
Array<Node *> traversal;
};
2021-03-31 12:18:16 +02:00
V &operator[](const K& key)
2020-04-12 15:47:19 +02:00
{
root = splay(root, key);
if (root == nullptr || root->pair.key < key || key < root->pair.key)
{
2020-04-12 15:47:19 +02:00
root = insert(root, key);
_size++;
refreshIterators();
}
2020-04-12 15:47:19 +02:00
return root->pair.value;
}
2021-03-31 12:18:16 +02:00
V &operator[](K&& key)
{
root = splay(root, std::forward<K>(key));
if (root == nullptr || root->pair.key < key || key < root->pair.key)
{
root = insert(root, std::forward<K>(key));
_size++;
refreshIterators();
}
return root->pair.value;
}
Iterator find(const K& key)
2020-04-12 15:47:19 +02:00
{
root = splay(root, key);
if (root == nullptr || root->pair.key != key)
{
return endIt;
}
2020-04-12 15:47:19 +02:00
return Iterator(root);
}
2021-03-31 12:18:16 +02:00
Iterator find(K&& key)
{
root = splay(root, std::move(key));
if (root == nullptr || root->pair.key != key)
{
return endIt;
}
return Iterator(root);
}
Iterator erase(const K& key)
2020-04-12 15:47:19 +02:00
{
root = remove(root, key);
refreshIterators();
2021-03-31 12:18:16 +02:00
return Iterator(root);
}
Iterator erase(K&& key)
{
root = remove(root, std::move(key));
refreshIterators();
2020-04-12 15:47:19 +02:00
return Iterator(root);
}
void clear()
{
delete root;
root = nullptr;
_size = 0;
refreshIterators();
}
2021-03-31 12:18:16 +02:00
bool exists(K&& key)
2020-04-12 15:47:19 +02:00
{
2021-03-31 12:18:16 +02:00
return find(std::forward<K>(key)) != endIt;
2020-04-12 15:47:19 +02:00
}
Iterator begin() const
{
return beginIt;
}
Iterator end() const
{
return endIt;
}
bool empty() const
{
return root == nullptr;
}
uint32 size() const
{
return _size;
}
private:
void refreshIterators()
{
Node *beginNode = root;
if (root == nullptr)
{
2020-04-12 15:47:19 +02:00
beginIt = Iterator(nullptr);
}
else
{
Array<Node *> beginTraversal;
while (beginNode != nullptr)
2020-04-01 02:17:49 +02:00
{
2020-04-12 15:47:19 +02:00
beginTraversal.add(beginNode);
beginNode = beginNode->leftChild;
2020-04-01 02:17:49 +02:00
}
2020-04-12 15:47:19 +02:00
beginNode = beginTraversal.back();
beginTraversal.pop();
beginIt = Iterator(beginNode, std::move(beginTraversal));
}
Node *endNode = root;
if (root == nullptr)
{
endIt = Iterator(nullptr);
}
else
{
Array<Node *> endTraversal;
while (endNode != nullptr)
2020-04-01 02:17:49 +02:00
{
2020-04-12 15:47:19 +02:00
endTraversal.add(endNode);
endNode = endNode->rightChild;
2020-04-01 02:17:49 +02:00
}
2020-04-12 15:47:19 +02:00
endIt = Iterator(endNode, std::move(endTraversal));
}
}
Node *root;
Iterator beginIt;
Iterator endIt;
uint32 _size;
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;
}
2021-03-31 12:18:16 +02:00
template<class KeyType>
Node *insert(Node *r, KeyType&& key)
2020-04-12 15:47:19 +02:00
{
if (r == nullptr)
2021-03-31 12:18:16 +02:00
{
return new Node(std::forward<KeyType>(key), V());
}
2020-04-12 15:47:19 +02:00
r = splay(r, key);
if (!(r->pair.key < key || key < r->pair.key))
return r;
2021-03-31 12:18:16 +02:00
Node *newNode = new Node(std::forward<KeyType>(key), V());
2020-04-12 15:47:19 +02:00
if (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;
}
2021-03-31 12:18:16 +02:00
template<class KeyType>
Node *remove(Node *r, KeyType&& key)
2020-04-12 15:47:19 +02:00
{
Node *temp;
if (!r)
return nullptr;
r = splay(r, key);
if (r->pair.key < key || key < r->pair.key)
return r;
if (!r->leftChild)
{
temp = r;
r = r->rightChild;
}
else
{
temp = r;
r = splay(r->leftChild, key);
r->rightChild = temp->rightChild;
}
temp->leftChild = nullptr;
temp->rightChild = nullptr;
_size--;
delete temp;
return r;
}
2021-03-31 12:18:16 +02:00
template<class KeyType>
Node *splay(Node *r, KeyType&& key)
2020-04-12 15:47:19 +02:00
{
if (r == nullptr || !(r->pair.key < key || key < r->pair.key))
{
return r;
}
if (key < r->pair.key)
{
if (r->leftChild == nullptr)
return r;
if (key < r->leftChild->pair.key)
2020-04-01 02:17:49 +02:00
{
2020-04-12 15:47:19 +02:00
r->leftChild->leftChild = splay(r->leftChild->leftChild, key);
r = rotateRight(r);
2020-04-01 02:17:49 +02:00
}
2020-04-12 15:47:19 +02:00
else if (r->leftChild->pair.key < key)
2020-04-01 02:17:49 +02:00
{
2020-04-12 15:47:19 +02:00
r->leftChild->rightChild = splay(r->leftChild->rightChild, key);
if (r->leftChild->rightChild != nullptr)
2020-04-01 02:17:49 +02:00
{
2020-04-12 15:47:19 +02:00
r->leftChild = rotateLeft(r->leftChild);
2020-04-01 02:17:49 +02:00
}
}
2020-04-12 15:47:19 +02:00
return (r->leftChild == nullptr) ? r : rotateRight(r);
}
2020-04-12 15:47:19 +02:00
else
{
2020-04-12 15:47:19 +02:00
if (r->rightChild == nullptr)
return r;
2020-04-12 15:47:19 +02:00
if (key < r->rightChild->pair.key)
{
2020-04-12 15:47:19 +02:00
r->rightChild->leftChild = splay(r->rightChild->leftChild, key);
2020-04-12 15:47:19 +02:00
if (r->rightChild->leftChild != nullptr)
{
2020-04-12 15:47:19 +02:00
r->rightChild = rotateRight(r->rightChild);
}
}
2020-04-12 15:47:19 +02:00
else if (r->rightChild->pair.key < key)
{
2020-04-12 15:47:19 +02:00
r->rightChild->rightChild = splay(r->rightChild->rightChild, key);
r = rotateLeft(r);
}
2020-04-12 15:47:19 +02:00
return (r->rightChild == nullptr) ? r : rotateLeft(r);
}
2020-04-12 15:47:19 +02:00
}
};
} // namespace Seele