No more arraymap
This commit is contained in:
+158
-217
@@ -15,12 +15,12 @@ struct Map
|
|||||||
private:
|
private:
|
||||||
struct Node
|
struct Node
|
||||||
{
|
{
|
||||||
size_t leftChild;
|
Node* leftChild;
|
||||||
size_t rightChild;
|
Node* rightChild;
|
||||||
Pair<K, V> pair;
|
Pair<K, V> pair;
|
||||||
Node()
|
Node()
|
||||||
: leftChild(SIZE_MAX)
|
: leftChild(nullptr)
|
||||||
, rightChild(SIZE_MAX)
|
, rightChild(nullptr)
|
||||||
, pair()
|
, pair()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -29,8 +29,8 @@ private:
|
|||||||
Node& operator=(const Node& other) = default;
|
Node& operator=(const Node& other) = default;
|
||||||
Node& operator=(Node&& other) = default;
|
Node& operator=(Node&& other) = default;
|
||||||
Node(K key)
|
Node(K key)
|
||||||
: leftChild(SIZE_MAX)
|
: leftChild(nullptr)
|
||||||
, rightChild(SIZE_MAX)
|
, rightChild(nullptr)
|
||||||
, pair(std::move(key))
|
, pair(std::move(key))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -50,68 +50,62 @@ public:
|
|||||||
using reference = PairType&;
|
using reference = PairType&;
|
||||||
using pointer = PairType*;
|
using pointer = PairType*;
|
||||||
|
|
||||||
IteratorBase(size_t x = SIZE_MAX)
|
constexpr IteratorBase(Node* x = nullptr, Array<Node*> &&beginIt = Array<Node*>())
|
||||||
: node(x)
|
: node(x), traversal(std::move(beginIt))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
IteratorBase(size_t x, const Array<Node, NodeAlloc>* nodeContainer, Array<size_t> &&beginIt = Array<size_t>())
|
constexpr IteratorBase(const IteratorBase &i)
|
||||||
: node(x), traversal(std::move(beginIt)), nodeContainer(nodeContainer)
|
: node(i.node), traversal(i.traversal)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
IteratorBase(const IteratorBase &i)
|
constexpr IteratorBase(IteratorBase&& i)
|
||||||
: node(i.node), traversal(i.traversal), nodeContainer(i.nodeContainer)
|
: node(std::move(i.node)), traversal(std::move(i.traversal))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
IteratorBase(IteratorBase&& i)
|
constexpr IteratorBase& operator=(const IteratorBase& other)
|
||||||
: node(std::move(i.node)), traversal(std::move(i.traversal)), nodeContainer(i.nodeContainer)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
IteratorBase& operator=(const IteratorBase& other)
|
|
||||||
{
|
{
|
||||||
if(this != &other)
|
if(this != &other)
|
||||||
{
|
{
|
||||||
node = other.node;
|
node = other.node;
|
||||||
nodeContainer = other.nodeContainer;
|
|
||||||
traversal = other.traversal;
|
traversal = other.traversal;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
IteratorBase& operator=(IteratorBase&& other)
|
constexpr IteratorBase& operator=(IteratorBase&& other)
|
||||||
{
|
{
|
||||||
if(this != &other)
|
if(this != &other)
|
||||||
{
|
{
|
||||||
node = std::move(other.node);
|
node = std::move(other.node);
|
||||||
nodeContainer = std::move(other.nodeContainer);
|
|
||||||
traversal = std::move(other.traversal);
|
traversal = std::move(other.traversal);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
reference operator*() const
|
constexpr reference operator*() const
|
||||||
{
|
{
|
||||||
return getNode()->pair;
|
return node->pair;
|
||||||
}
|
}
|
||||||
pointer operator->() const
|
constexpr pointer operator->() const
|
||||||
{
|
{
|
||||||
return &(getNode()->pair);
|
return &(node->pair);
|
||||||
}
|
}
|
||||||
inline bool operator!=(const IteratorBase &other)
|
constexpr bool operator!=(const IteratorBase &other)
|
||||||
{
|
{
|
||||||
return node != other.node;
|
return node != other.node;
|
||||||
}
|
}
|
||||||
inline bool operator==(const IteratorBase &other)
|
constexpr bool operator==(const IteratorBase &other)
|
||||||
{
|
{
|
||||||
return node == other.node;
|
return node == other.node;
|
||||||
}
|
}
|
||||||
IteratorBase &operator++()
|
constexpr IteratorBase &operator++()
|
||||||
{
|
{
|
||||||
node = getNode()->rightChild;
|
node = node->rightChild;
|
||||||
while (node < nodeContainer->size()
|
while (node != nullptr
|
||||||
&& getNode()->leftChild < nodeContainer->size())
|
&& node->leftChild != nullptr)
|
||||||
{
|
{
|
||||||
traversal.add(node);
|
traversal.add(node);
|
||||||
node = getNode()->leftChild;
|
node = node->leftChild;
|
||||||
}
|
}
|
||||||
if (node >= nodeContainer->size()
|
if (node == nullptr
|
||||||
&& traversal.size() > 0)
|
&& traversal.size() > 0)
|
||||||
{
|
{
|
||||||
node = traversal.back();
|
node = traversal.back();
|
||||||
@@ -119,16 +113,16 @@ public:
|
|||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
IteratorBase &operator--()
|
constexpr IteratorBase &operator--()
|
||||||
{
|
{
|
||||||
node = getNode()->leftChild;
|
node = node->leftChild;
|
||||||
while (node < nodeContainer->size()
|
while (node != nullptr
|
||||||
&& getNode()->rightChild < nodeContainer->size())
|
&& node->rightChild != nullptr)
|
||||||
{
|
{
|
||||||
traversal.add(node);
|
traversal.add(node);
|
||||||
node = getNode()->rightchild;
|
node = node->rightchild;
|
||||||
}
|
}
|
||||||
if (node >= nodeContainer->size()
|
if (node == nullptr
|
||||||
&& traversal.size() > 0)
|
&& traversal.size() > 0)
|
||||||
{
|
{
|
||||||
node = traversal.back();
|
node = traversal.back();
|
||||||
@@ -136,13 +130,13 @@ public:
|
|||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
IteratorBase operator--(int)
|
constexpr IteratorBase operator--(int)
|
||||||
{
|
{
|
||||||
IteratorBase tmp(*this);
|
IteratorBase tmp(*this);
|
||||||
--*this;
|
--*this;
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
IteratorBase operator++(int)
|
constexpr IteratorBase operator++(int)
|
||||||
{
|
{
|
||||||
IteratorBase tmp(*this);
|
IteratorBase tmp(*this);
|
||||||
++*this;
|
++*this;
|
||||||
@@ -150,13 +144,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Node* getNode() const
|
Node* node;
|
||||||
{
|
Array<Node*> traversal;
|
||||||
return &(*nodeContainer)[node];
|
|
||||||
}
|
|
||||||
size_t node;
|
|
||||||
Array<size_t> traversal;
|
|
||||||
const Array<Node, NodeAlloc>* nodeContainer;
|
|
||||||
};
|
};
|
||||||
using Iterator = IteratorBase<Pair<K,V>>;
|
using Iterator = IteratorBase<Pair<K,V>>;
|
||||||
using ConstIterator = IteratorBase<const Pair<K,V>>;
|
using ConstIterator = IteratorBase<const Pair<K,V>>;
|
||||||
@@ -178,36 +167,36 @@ public:
|
|||||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||||
|
|
||||||
constexpr Map() noexcept
|
constexpr Map() noexcept
|
||||||
: root(SIZE_MAX)
|
: root(nullptr)
|
||||||
, beginIt(SIZE_MAX)
|
, beginIt(nullptr)
|
||||||
, endIt(SIZE_MAX)
|
, endIt(nullptr)
|
||||||
, iteratorsDirty(true)
|
, iteratorsDirty(true)
|
||||||
, _size(0)
|
, _size(0)
|
||||||
, comp(Compare())
|
, comp(Compare())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
|
constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
|
||||||
: nodeContainer(alloc)
|
: alloc(alloc)
|
||||||
, root(SIZE_MAX)
|
, root(nullptr)
|
||||||
, beginIt(SIZE_MAX)
|
, beginIt(nullptr)
|
||||||
, endIt(SIZE_MAX)
|
, endIt(nullptr)
|
||||||
, iteratorsDirty(true)
|
, iteratorsDirty(true)
|
||||||
, _size(0)
|
, _size(0)
|
||||||
, comp(comp)
|
, comp(comp)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare()))
|
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare()))
|
||||||
: nodeContainer(alloc)
|
: alloc(alloc)
|
||||||
, root(SIZE_MAX)
|
, root(nullptr)
|
||||||
, beginIt(SIZE_MAX)
|
, beginIt(nullptr)
|
||||||
, endIt(SIZE_MAX)
|
, endIt(nullptr)
|
||||||
, iteratorsDirty(true)
|
, iteratorsDirty(true)
|
||||||
, _size(0)
|
, _size(0)
|
||||||
, comp(Compare())
|
, comp(Compare())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
constexpr Map(const Map& other)
|
constexpr Map(const Map& other)
|
||||||
: nodeContainer(other.nodeContainer)
|
: alloc(other.alloc)
|
||||||
, root(other.root)
|
, root(other.root)
|
||||||
, iteratorsDirty(true)
|
, iteratorsDirty(true)
|
||||||
, _size(other._size)
|
, _size(other._size)
|
||||||
@@ -215,7 +204,7 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
constexpr Map(Map&& other) noexcept
|
constexpr Map(Map&& other) noexcept
|
||||||
: nodeContainer(std::move(other.nodeContainer))
|
: alloc(std::move(other.alloc))
|
||||||
, root(std::move(other.root))
|
, root(std::move(other.root))
|
||||||
, iteratorsDirty(true)
|
, iteratorsDirty(true)
|
||||||
, _size(std::move(other._size))
|
, _size(std::move(other._size))
|
||||||
@@ -229,7 +218,7 @@ public:
|
|||||||
{
|
{
|
||||||
if(this != &other)
|
if(this != &other)
|
||||||
{
|
{
|
||||||
nodeContainer = other.nodeContainer;
|
alloc = other.alloc;
|
||||||
root = other.root;
|
root = other.root;
|
||||||
_size = other._size;
|
_size = other._size;
|
||||||
comp = other.comp;
|
comp = other.comp;
|
||||||
@@ -241,7 +230,7 @@ public:
|
|||||||
{
|
{
|
||||||
if(this != &other)
|
if(this != &other)
|
||||||
{
|
{
|
||||||
nodeContainer = std::move(other.nodeContainer);
|
alloc = std::move(other.alloc);
|
||||||
root = std::move(other.root);
|
root = std::move(other.root);
|
||||||
_size = std::move(other._size);
|
_size = std::move(other._size);
|
||||||
comp = std::move(other.comp);
|
comp = std::move(other.comp);
|
||||||
@@ -252,100 +241,107 @@ public:
|
|||||||
constexpr mapped_type& operator[](const key_type& key)
|
constexpr mapped_type& operator[](const key_type& key)
|
||||||
{
|
{
|
||||||
root = splay(root, key);
|
root = splay(root, key);
|
||||||
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
|
if (root == nullptr || !equal(root->pair.key, key))
|
||||||
{
|
{
|
||||||
root = insert(root, key);
|
root = insert(root, key);
|
||||||
_size++;
|
_size++;
|
||||||
}
|
}
|
||||||
markIteratorsDirty();
|
markIteratorsDirty();
|
||||||
return getNode(root)->pair.value;
|
return root->pair.value;
|
||||||
}
|
}
|
||||||
constexpr const mapped_type& operator[](const key_type& key) const
|
constexpr const mapped_type& operator[](const key_type& key) const
|
||||||
{
|
{
|
||||||
size_t it = root;
|
Node* it = root;
|
||||||
while (!equal(getNode(it)->pair.key, key))
|
while (!equal(it->pair.key, key))
|
||||||
{
|
{
|
||||||
if (comp(key, getNode(it)->pair.key))
|
if (comp(key, it->pair.key))
|
||||||
{
|
{
|
||||||
it = getNode(it)->leftChild;
|
it = it->leftChild;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
it = getNode(it)->rightChild;
|
it = it->rightChild;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return getNode(it)->pair.value;
|
return it->pair.value;
|
||||||
}
|
}
|
||||||
constexpr mapped_type& at(const key_type& key)
|
constexpr mapped_type& at(const key_type& key)
|
||||||
{
|
{
|
||||||
root = splay(root, key);
|
root = splay(root, key);
|
||||||
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
|
if (root == nullptr || !equal(root->pair.key, key))
|
||||||
{
|
{
|
||||||
throw std::logic_error("Key not found");
|
throw std::logic_error("Key not found");
|
||||||
}
|
}
|
||||||
markIteratorsDirty();
|
markIteratorsDirty();
|
||||||
return getNode(root)->pair.value;
|
return root->pair.value;
|
||||||
}
|
}
|
||||||
constexpr mapped_type& at(key_type&& key)
|
constexpr mapped_type& at(key_type&& key)
|
||||||
{
|
{
|
||||||
root = splay(root, std::move(key));
|
root = splay(root, std::move(key));
|
||||||
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
|
if (root == nullptr || !equal(root->pair.key, key))
|
||||||
{
|
{
|
||||||
throw std::logic_error("Key not found");
|
throw std::logic_error("Key not found");
|
||||||
}
|
}
|
||||||
markIteratorsDirty();
|
markIteratorsDirty();
|
||||||
return getNode(root)->pair.value;
|
return root->pair.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const mapped_type& at(const key_type& key) const
|
constexpr const mapped_type& at(const key_type& key) const
|
||||||
{
|
{
|
||||||
for(const auto& node : nodeContainer)
|
Node* it = root;
|
||||||
|
while (!equal(it->pair.key, key))
|
||||||
{
|
{
|
||||||
if(equal(node.pair.key, key))
|
if (comp(key, it->pair.key))
|
||||||
{
|
{
|
||||||
return node.pair.value;
|
it = it->leftChild;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
it = it->rightChild;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw std::logic_error("Key not found");
|
return it->pair.value;
|
||||||
}
|
}
|
||||||
constexpr iterator find(const key_type& key)
|
constexpr iterator find(const key_type& key)
|
||||||
{
|
{
|
||||||
root = splay(root, key);
|
root = splay(root, key);
|
||||||
refreshIterators();
|
refreshIterators();
|
||||||
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
|
if (root == nullptr || !equal(root->pair.key, key))
|
||||||
{
|
{
|
||||||
return endIt;
|
return endIt;
|
||||||
}
|
}
|
||||||
return iterator(root, &nodeContainer);
|
return iterator(root);
|
||||||
}
|
}
|
||||||
constexpr iterator find(key_type&& key)
|
constexpr iterator find(key_type&& key)
|
||||||
{
|
{
|
||||||
root = splay(root, std::move(key));
|
root = splay(root, std::move(key));
|
||||||
refreshIterators();
|
refreshIterators();
|
||||||
if (!isValid(root) || !equal(getNode(root)->pair.key, key))
|
if (root == nullptr || !equal(root->pair.key, key))
|
||||||
{
|
{
|
||||||
return endIt;
|
return endIt;
|
||||||
}
|
}
|
||||||
return iterator(root, &nodeContainer);
|
return iterator(root);
|
||||||
}
|
}
|
||||||
constexpr iterator erase(const key_type& key)
|
constexpr iterator erase(const key_type& key)
|
||||||
{
|
{
|
||||||
root = remove(root, key);
|
root = remove(root, key);
|
||||||
refreshIterators();
|
refreshIterators();
|
||||||
assert(root != SIZE_MAX || _size == 0);
|
assert(root != nullptr || _size == 0);
|
||||||
return iterator(root, &nodeContainer);
|
return iterator(root);
|
||||||
}
|
}
|
||||||
constexpr iterator erase(key_type&& key)
|
constexpr iterator erase(key_type&& key)
|
||||||
{
|
{
|
||||||
root = remove(root, std::move(key));
|
root = remove(root, std::move(key));
|
||||||
refreshIterators();
|
refreshIterators();
|
||||||
return iterator(root, &nodeContainer);
|
return iterator(root);
|
||||||
}
|
}
|
||||||
constexpr void clear()
|
constexpr void clear()
|
||||||
{
|
{
|
||||||
nodeContainer.clear();
|
while (_size > 0)
|
||||||
root = SIZE_MAX;
|
{
|
||||||
_size = 0;
|
root = remove(root, root->pair.key);
|
||||||
|
}
|
||||||
|
root = nullptr;
|
||||||
markIteratorsDirty();
|
markIteratorsDirty();
|
||||||
}
|
}
|
||||||
constexpr bool exists(const key_type& key)
|
constexpr bool exists(const key_type& key)
|
||||||
@@ -398,7 +394,7 @@ public:
|
|||||||
}
|
}
|
||||||
constexpr bool empty() const
|
constexpr bool empty() const
|
||||||
{
|
{
|
||||||
return nodeContainer.empty();
|
return _size == 0;
|
||||||
}
|
}
|
||||||
constexpr size_type size() const
|
constexpr size_type size() const
|
||||||
{
|
{
|
||||||
@@ -429,40 +425,12 @@ public:
|
|||||||
private:
|
private:
|
||||||
void verifyTree()
|
void verifyTree()
|
||||||
{
|
{
|
||||||
for(size_t i = 0; i < nodeContainer.size(); ++i)
|
size_t numElems = 0;
|
||||||
|
for (const auto& [_, _] : *this)
|
||||||
{
|
{
|
||||||
bool found = false;
|
numElems++;
|
||||||
for(auto it : nodeContainer)
|
|
||||||
{
|
|
||||||
if(it.leftChild == i)
|
|
||||||
{
|
|
||||||
assert(!found);
|
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
if(it.rightChild == i)
|
assert(numElems == _size);
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Node* getNode(size_t index) const
|
|
||||||
{
|
|
||||||
if(!isValid(index)) return nullptr;
|
|
||||||
return &nodeContainer[index];
|
|
||||||
}
|
|
||||||
inline bool isValid(size_t index) const
|
|
||||||
{
|
|
||||||
return index < nodeContainer.size();
|
|
||||||
}
|
}
|
||||||
void markIteratorsDirty()
|
void markIteratorsDirty()
|
||||||
{
|
{
|
||||||
@@ -476,189 +444,162 @@ private:
|
|||||||
}
|
}
|
||||||
constexpr Iterator calcBeginIterator() const
|
constexpr Iterator calcBeginIterator() const
|
||||||
{
|
{
|
||||||
size_t beginIndex = root;
|
Node* begin = root;
|
||||||
Array<size_t> beginTraversal;
|
Array<Node*> beginTraversal;
|
||||||
while (isValid(beginIndex))
|
while (begin != nullptr)
|
||||||
{
|
{
|
||||||
beginTraversal.add(beginIndex);
|
beginTraversal.add(begin);
|
||||||
beginIndex = getNode(beginIndex)->leftChild;
|
begin = begin->leftChild;
|
||||||
}
|
}
|
||||||
if(!beginTraversal.empty())
|
if(!beginTraversal.empty())
|
||||||
{
|
{
|
||||||
beginIndex = beginTraversal.back();
|
begin = beginTraversal.back();
|
||||||
beginTraversal.pop();
|
beginTraversal.pop();
|
||||||
}
|
}
|
||||||
return Iterator(beginIndex, &nodeContainer, std::move(beginTraversal));
|
return Iterator(begin, std::move(beginTraversal));
|
||||||
}
|
}
|
||||||
constexpr Iterator calcEndIterator() const
|
constexpr Iterator calcEndIterator() const
|
||||||
{
|
{
|
||||||
size_t endIndex = root;
|
Node* endIndex = root;
|
||||||
Array<size_t> endTraversal;
|
Array<Node*> endTraversal;
|
||||||
while (isValid(endIndex))
|
while (endIndex != nullptr)
|
||||||
{
|
{
|
||||||
endTraversal.add(endIndex);
|
endTraversal.add(endIndex);
|
||||||
endIndex = getNode(endIndex)->rightChild;
|
endIndex = endIndex->rightChild;
|
||||||
}
|
}
|
||||||
return Iterator(endIndex, &nodeContainer, std::move(endTraversal));
|
return Iterator(endIndex, std::move(endTraversal));
|
||||||
}
|
}
|
||||||
Array<Node, NodeAlloc> nodeContainer = Array<Node, NodeAlloc>();
|
NodeAlloc alloc;
|
||||||
size_t root;
|
Node* root;
|
||||||
Iterator beginIt;
|
Iterator beginIt;
|
||||||
Iterator endIt;
|
Iterator endIt;
|
||||||
bool iteratorsDirty;
|
bool iteratorsDirty;
|
||||||
size_type _size;
|
size_type _size;
|
||||||
Compare comp;
|
Compare comp;
|
||||||
size_t rotateRight(size_t node)
|
Node* rotateLeft(Node* x)
|
||||||
{
|
{
|
||||||
Node* x = getNode(node);
|
Node* y = x->rightChild;
|
||||||
size_t res = x->leftChild;
|
|
||||||
Node* y = getNode(x->leftChild);
|
|
||||||
x->leftChild = y->rightChild;
|
|
||||||
y->rightChild = node;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
size_t rotateLeft(size_t node)
|
|
||||||
{
|
|
||||||
Node* x = getNode(node);
|
|
||||||
size_t res = x->rightChild;
|
|
||||||
Node* y = getNode(x->rightChild);
|
|
||||||
x->rightChild = y->leftChild;
|
x->rightChild = y->leftChild;
|
||||||
y->leftChild = node;
|
y->leftChild = x;
|
||||||
return res;
|
return y;
|
||||||
|
}
|
||||||
|
Node* rotateRight(Node* x)
|
||||||
|
{
|
||||||
|
Node* y = x->leftChild;
|
||||||
|
x->leftChild = y->rightChild;
|
||||||
|
y->rightChild = x;
|
||||||
|
return y;
|
||||||
}
|
}
|
||||||
template<class KeyType>
|
template<class KeyType>
|
||||||
size_t insert(size_t r, KeyType&& key)
|
Node* insert(Node* r, KeyType&& key)
|
||||||
{
|
{
|
||||||
if (!isValid(r))
|
if (r == nullptr)
|
||||||
{
|
{
|
||||||
nodeContainer.emplace(std::forward<KeyType>(key));
|
root = alloc.allocate(1);
|
||||||
return nodeContainer.size() - 1;
|
std::allocator_traits<NodeAlloc>::construct(alloc, root, std::forward<KeyType>(key));
|
||||||
|
return root;
|
||||||
}
|
}
|
||||||
r = splay(r, key);
|
r = splay(r, key);
|
||||||
Node* node = getNode(r);
|
|
||||||
|
|
||||||
if (equal(node->pair.key, key))
|
if (equal(r->pair.key, key))
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
Node *newNode = &nodeContainer.emplace(std::forward<KeyType>(key));
|
Node *newNode = alloc.allocate(1);
|
||||||
node = getNode(r);
|
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, std::forward<KeyType>(key));
|
||||||
|
|
||||||
if (comp(newNode->pair.key, node->pair.key))
|
if (comp(newNode->pair.key, r->pair.key))
|
||||||
{
|
{
|
||||||
newNode->rightChild = r;
|
newNode->rightChild = r;
|
||||||
newNode->leftChild = node->leftChild;
|
newNode->leftChild = r->leftChild;
|
||||||
node->leftChild = SIZE_MAX;
|
r->leftChild = nullptr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
newNode->leftChild = r;
|
newNode->leftChild = r;
|
||||||
newNode->rightChild = node->rightChild;
|
newNode->rightChild = r->rightChild;
|
||||||
node->rightChild = SIZE_MAX;
|
r->rightChild = nullptr;
|
||||||
}
|
}
|
||||||
return nodeContainer.size() - 1;
|
return newNode;
|
||||||
}
|
}
|
||||||
template<class KeyType>
|
template<class KeyType>
|
||||||
size_t remove(size_t r, KeyType&& key)
|
Node* remove(Node* r, KeyType&& key)
|
||||||
{
|
{
|
||||||
size_t temp;
|
Node* temp;
|
||||||
if (!isValid(r))
|
if (r == nullptr)
|
||||||
return SIZE_MAX;
|
return nullptr;
|
||||||
|
|
||||||
r = splay(r, key);
|
r = splay(r, key);
|
||||||
Node* node = getNode(r);
|
|
||||||
|
|
||||||
if (!equal(node->pair.key, key))
|
if (!equal(r->pair.key, key))
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (!isValid(node->leftChild))
|
if (r->leftChild == nullptr)
|
||||||
{
|
{
|
||||||
temp = r;
|
temp = r;
|
||||||
r = node->rightChild;
|
r = r->rightChild;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
temp = r;
|
temp = r;
|
||||||
|
|
||||||
r = splay(node->leftChild, key);
|
r = splay(r->leftChild, key);
|
||||||
node = getNode(r);
|
r->rightChild = temp->rightChild;
|
||||||
node->rightChild = getNode(temp)->rightChild;
|
|
||||||
}
|
}
|
||||||
Node& lastNode = nodeContainer.back();
|
std::allocator_traits<NodeAlloc>::destroy(alloc, temp);
|
||||||
size_t lastIndex = nodeContainer.size() - 1;
|
alloc.deallocate(temp, 1);
|
||||||
size_t removedIndex = temp;
|
|
||||||
//Arrays can only pop back, so we need to move the last element to the deleted index
|
|
||||||
if(removedIndex != lastIndex)
|
|
||||||
{
|
|
||||||
nodeContainer[removedIndex] = std::move(lastNode);
|
|
||||||
for(auto& it : nodeContainer)
|
|
||||||
{
|
|
||||||
if(it.leftChild == lastIndex)
|
|
||||||
{
|
|
||||||
it.leftChild = removedIndex;
|
|
||||||
}
|
|
||||||
if(it.rightChild == lastIndex)
|
|
||||||
{
|
|
||||||
it.rightChild = removedIndex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nodeContainer.pop();
|
|
||||||
_size--;
|
_size--;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
template<class KeyType>
|
template<class KeyType>
|
||||||
size_t splay(size_t r, KeyType&& key)
|
Node* splay(Node* r, KeyType&& key)
|
||||||
{
|
{
|
||||||
Node* node = getNode(r);
|
if (r == nullptr || equal(r->pair.key, key))
|
||||||
if (node == nullptr || equal(node->pair.key, key))
|
|
||||||
{
|
{
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
if (comp(key, node->pair.key))
|
if (comp(key, r->pair.key))
|
||||||
{
|
{
|
||||||
if (!isValid(node->leftChild))
|
if (r->leftChild == nullptr)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (comp(key, getNode(node->leftChild)->pair.key))
|
if (comp(key, r->leftChild->pair.key))
|
||||||
{
|
{
|
||||||
getNode(node->leftChild)->leftChild = splay(getNode(node->leftChild)->leftChild, key);
|
r->leftChild->leftChild = splay(r->leftChild->leftChild, key);
|
||||||
|
|
||||||
r = rotateRight(r);
|
r = rotateRight(r);
|
||||||
node = getNode(r);
|
|
||||||
}
|
}
|
||||||
else if (comp(getNode(node->leftChild)->pair.key, key))
|
else if (comp(r->leftChild->pair.key, key))
|
||||||
{
|
{
|
||||||
getNode(node->leftChild)->rightChild = splay(getNode(node->leftChild)->rightChild, key);
|
r->leftChild->rightChild = splay(r->leftChild->rightChild, key);
|
||||||
|
|
||||||
if (isValid(getNode(node->leftChild)->rightChild))
|
if (r->leftChild->rightChild != nullptr)
|
||||||
{
|
{
|
||||||
node->leftChild = rotateLeft(node->leftChild);
|
r->leftChild = rotateLeft(r->leftChild);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (!isValid(node->leftChild)) ? r : rotateRight(r);
|
return (r->leftChild == nullptr) ? r : rotateRight(r);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!isValid(node->rightChild))
|
if (r->rightChild == nullptr)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
if (comp(key, getNode(node->rightChild)->pair.key))
|
if (comp(key, r->rightChild->pair.key))
|
||||||
{
|
{
|
||||||
getNode(node->rightChild)->leftChild = splay(getNode(node->rightChild)->leftChild, key);
|
r->rightChild->leftChild = splay(r->rightChild->leftChild, key);
|
||||||
|
|
||||||
if (isValid(getNode(node->rightChild)->leftChild))
|
if (r->rightChild->leftChild != nullptr)
|
||||||
{
|
{
|
||||||
node->rightChild = rotateRight(node->rightChild);
|
r->rightChild = rotateRight(r->rightChild);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (comp(getNode(node->rightChild)->pair.key, key))
|
else if (comp(r->rightChild->pair.key, key))
|
||||||
{
|
{
|
||||||
getNode(node->rightChild)->rightChild = splay(getNode(node->rightChild)->rightChild, key);
|
r->rightChild->rightChild = splay(r->rightChild->rightChild, key);
|
||||||
|
|
||||||
r = rotateLeft(r);
|
r = rotateLeft(r);
|
||||||
node = getNode(r);
|
|
||||||
}
|
}
|
||||||
return (!isValid(node->rightChild)) ? r : rotateLeft(r);
|
return (r->rightChild == nullptr) ? r : rotateLeft(r);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool equal(const key_type& a, const key_type& b) const
|
bool equal(const key_type& a, const key_type& b) const
|
||||||
|
|||||||
Reference in New Issue
Block a user