Formatted EVERYTHING
This commit is contained in:
+205
-498
File diff suppressed because it is too large
Load Diff
+109
-276
@@ -1,124 +1,77 @@
|
||||
#pragma once
|
||||
#include <memory_resource>
|
||||
#include <assert.h>
|
||||
#include <memory_resource>
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>>
|
||||
class List
|
||||
{
|
||||
private:
|
||||
struct Node
|
||||
{
|
||||
Node(Node* prev, Node* next, const T& data)
|
||||
: prev(prev)
|
||||
, next(next)
|
||||
, data(data)
|
||||
{}
|
||||
Node(Node* prev, Node* next, T&& data)
|
||||
: prev(prev)
|
||||
, next(next)
|
||||
, data(std::move(data))
|
||||
{}
|
||||
Node *prev;
|
||||
Node *next;
|
||||
|
||||
namespace Seele {
|
||||
template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>> class List {
|
||||
private:
|
||||
struct Node {
|
||||
Node(Node* prev, Node* next, const T& data) : prev(prev), next(next), data(data) {}
|
||||
Node(Node* prev, Node* next, T&& data) : prev(prev), next(next), data(std::move(data)) {}
|
||||
Node* prev;
|
||||
Node* next;
|
||||
T data;
|
||||
};
|
||||
using NodeAllocator = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
|
||||
|
||||
public:
|
||||
template <typename X>
|
||||
class IteratorBase
|
||||
{
|
||||
public:
|
||||
public:
|
||||
template <typename X> class IteratorBase {
|
||||
public:
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = X;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using reference = X&;
|
||||
using pointer = X*;
|
||||
|
||||
IteratorBase(Node *x = nullptr)
|
||||
: node(x)
|
||||
{
|
||||
}
|
||||
IteratorBase(const IteratorBase &i)
|
||||
: node(i.node)
|
||||
{
|
||||
}
|
||||
IteratorBase(IteratorBase&& i) noexcept
|
||||
: node(std::move(i.node))
|
||||
{
|
||||
}
|
||||
~IteratorBase()
|
||||
{
|
||||
}
|
||||
IteratorBase& operator=(const IteratorBase& other)
|
||||
{
|
||||
if(this != &other)
|
||||
{
|
||||
IteratorBase(Node* x = nullptr) : node(x) {}
|
||||
IteratorBase(const IteratorBase& i) : node(i.node) {}
|
||||
IteratorBase(IteratorBase&& i) noexcept : node(std::move(i.node)) {}
|
||||
~IteratorBase() {}
|
||||
IteratorBase& operator=(const IteratorBase& other) {
|
||||
if (this != &other) {
|
||||
node = other.node;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
IteratorBase& operator=(IteratorBase&& other) noexcept
|
||||
{
|
||||
if(this != &other)
|
||||
{
|
||||
IteratorBase& operator=(IteratorBase&& other) noexcept {
|
||||
if (this != &other) {
|
||||
node = std::move(other.node);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr reference operator*() const
|
||||
{
|
||||
return node->data;
|
||||
}
|
||||
constexpr pointer operator->() const
|
||||
{
|
||||
return &node->data;
|
||||
}
|
||||
constexpr bool operator!=(const IteratorBase &other)
|
||||
{
|
||||
return node != other.node;
|
||||
}
|
||||
constexpr bool operator==(const IteratorBase &other)
|
||||
{
|
||||
return node == other.node;
|
||||
}
|
||||
constexpr std::strong_ordering operator<=>(const IteratorBase& other)
|
||||
{
|
||||
return node <=> other.node;
|
||||
}
|
||||
constexpr IteratorBase &operator--()
|
||||
{
|
||||
constexpr reference operator*() const { return node->data; }
|
||||
constexpr pointer operator->() const { return &node->data; }
|
||||
constexpr bool operator!=(const IteratorBase& other) { return node != other.node; }
|
||||
constexpr bool operator==(const IteratorBase& other) { return node == other.node; }
|
||||
constexpr std::strong_ordering operator<=>(const IteratorBase& other) { return node <=> other.node; }
|
||||
constexpr IteratorBase& operator--() {
|
||||
node = node->prev;
|
||||
return *this;
|
||||
}
|
||||
constexpr IteratorBase operator--(int)
|
||||
{
|
||||
constexpr IteratorBase operator--(int) {
|
||||
IteratorBase tmp(*this);
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
constexpr IteratorBase &operator++()
|
||||
{
|
||||
constexpr IteratorBase& operator++() {
|
||||
node = node->next;
|
||||
return *this;
|
||||
}
|
||||
constexpr IteratorBase operator++(int)
|
||||
{
|
||||
constexpr IteratorBase operator++(int) {
|
||||
IteratorBase tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
private:
|
||||
Node *node;
|
||||
private:
|
||||
Node* node;
|
||||
friend class List<T>;
|
||||
};
|
||||
|
||||
|
||||
using Iterator = IteratorBase<T>;
|
||||
using ConstIterator = IteratorBase<const T>;
|
||||
|
||||
|
||||
using value_type = T;
|
||||
using allocator_type = Allocator;
|
||||
using size_type = std::size_t;
|
||||
@@ -132,118 +85,72 @@ public:
|
||||
using const_iterator = ConstIterator;
|
||||
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||
|
||||
constexpr List()
|
||||
: allocator(Allocator())
|
||||
, root(allocateNode())
|
||||
, tail(root)
|
||||
, beginIt(Iterator(root))
|
||||
, endIt(Iterator(tail))
|
||||
, _size(0)
|
||||
{
|
||||
}
|
||||
|
||||
constexpr List() : allocator(Allocator()), root(allocateNode()), tail(root), beginIt(Iterator(root)), endIt(Iterator(tail)), _size(0) {}
|
||||
constexpr explicit List(const Allocator& alloc)
|
||||
: allocator(alloc)
|
||||
, root(allocateNode())
|
||||
, tail(root)
|
||||
, beginIt(Iterator(root))
|
||||
, endIt(Iterator(tail))
|
||||
, _size(0)
|
||||
{
|
||||
}
|
||||
constexpr List(size_type count, const T& value = T(), const Allocator& alloc = Allocator())
|
||||
: List(alloc)
|
||||
{
|
||||
for(size_type i = 0; i < count; ++i)
|
||||
{
|
||||
: allocator(alloc), root(allocateNode()), tail(root), beginIt(Iterator(root)), endIt(Iterator(tail)), _size(0) {}
|
||||
constexpr List(size_type count, const T& value = T(), const Allocator& alloc = Allocator()) : List(alloc) {
|
||||
for (size_type i = 0; i < count; ++i) {
|
||||
add(value);
|
||||
}
|
||||
}
|
||||
constexpr List(size_type count, const Allocator& alloc = Allocator())
|
||||
: List(alloc)
|
||||
{
|
||||
for(size_type i = 0; i < count; ++i)
|
||||
{
|
||||
constexpr List(size_type count, const Allocator& alloc = Allocator()) : List(alloc) {
|
||||
for (size_type i = 0; i < count; ++i) {
|
||||
add(T());
|
||||
}
|
||||
}
|
||||
constexpr List(const List& other)
|
||||
: List(std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.allocator))
|
||||
{
|
||||
//TODO: improve
|
||||
for(const auto& it : other)
|
||||
{
|
||||
: List(std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.allocator)) {
|
||||
// TODO: improve
|
||||
for (const auto& it : other) {
|
||||
add(it);
|
||||
}
|
||||
}
|
||||
|
||||
constexpr List(const List& other, const Allocator& alloc)
|
||||
: List(alloc)
|
||||
{
|
||||
//TODO: improve
|
||||
for(const auto& it : other)
|
||||
{
|
||||
|
||||
constexpr List(const List& other, const Allocator& alloc) : List(alloc) {
|
||||
// TODO: improve
|
||||
for (const auto& it : other) {
|
||||
add(it);
|
||||
}
|
||||
}
|
||||
constexpr List(List&& other)
|
||||
: allocator(std::move(other.allocator))
|
||||
, root(std::move(other.root))
|
||||
, tail(std::move(other.tail))
|
||||
, beginIt(std::move(other.beginIt))
|
||||
, endIt(std::move(other.endIt))
|
||||
, _size(std::move(other._size))
|
||||
{
|
||||
: allocator(std::move(other.allocator)), root(std::move(other.root)), tail(std::move(other.tail)),
|
||||
beginIt(std::move(other.beginIt)), endIt(std::move(other.endIt)), _size(std::move(other._size)) {
|
||||
other.root = nullptr;
|
||||
other.tail = nullptr;
|
||||
other._size = 0;
|
||||
}
|
||||
constexpr List(List&& other, const Allocator& alloc)
|
||||
: allocator(alloc)
|
||||
, root(std::move(other.root))
|
||||
, tail(std::move(other.tail))
|
||||
, beginIt(std::move(other.beginIt))
|
||||
, endIt(std::move(other.endIt))
|
||||
, _size(std::move(other._size))
|
||||
{
|
||||
: allocator(alloc), root(std::move(other.root)), tail(std::move(other.tail)), beginIt(std::move(other.beginIt)),
|
||||
endIt(std::move(other.endIt)), _size(std::move(other._size)) {
|
||||
other.root = nullptr;
|
||||
other.tail = nullptr;
|
||||
other._size = 0;
|
||||
}
|
||||
constexpr ~List()
|
||||
{
|
||||
constexpr ~List() {
|
||||
clear();
|
||||
deallocateNode(tail);
|
||||
}
|
||||
constexpr List& operator=(const List& other)
|
||||
{
|
||||
if(this != &other)
|
||||
{
|
||||
constexpr List& operator=(const List& other) {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value )
|
||||
{
|
||||
if (!std::allocator_traits<allocator_type>::is_always_equal::value
|
||||
&& allocator != other.allocator)
|
||||
{
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value) {
|
||||
if (!std::allocator_traits<allocator_type>::is_always_equal::value && allocator != other.allocator) {
|
||||
clear();
|
||||
}
|
||||
allocator = other.allocator;
|
||||
}
|
||||
for(const auto& it : other)
|
||||
{
|
||||
for (const auto& it : other) {
|
||||
add(it);
|
||||
}
|
||||
markIteratorDirty();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr List& operator=(List&& other)
|
||||
{
|
||||
if(this != &other)
|
||||
{
|
||||
constexpr List& operator=(List&& other) {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
|
||||
{
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value) {
|
||||
allocator = std::move(other.allocator);
|
||||
}
|
||||
root = other.root;
|
||||
@@ -258,23 +165,14 @@ public:
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr reference front()
|
||||
{
|
||||
return root->data;
|
||||
}
|
||||
constexpr reference back()
|
||||
{
|
||||
return tail->prev->data;
|
||||
}
|
||||
constexpr void clear()
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
|
||||
constexpr reference front() { return root->data; }
|
||||
constexpr reference back() { return tail->prev->data; }
|
||||
constexpr void clear() {
|
||||
if (empty()) {
|
||||
return;
|
||||
}
|
||||
for (Node *tmp = root; tmp != tail;)
|
||||
{
|
||||
for (Node* tmp = root; tmp != tail;) {
|
||||
tmp = tmp->next;
|
||||
destroyNode(tmp->prev);
|
||||
deallocateNode(tmp->prev);
|
||||
@@ -283,24 +181,12 @@ public:
|
||||
markIteratorDirty();
|
||||
_size = 0;
|
||||
}
|
||||
//Insert at the end
|
||||
constexpr iterator add(const T &value)
|
||||
{
|
||||
return addInternal(value);
|
||||
}
|
||||
constexpr iterator add(T&& value)
|
||||
{
|
||||
return addInternal(std::move(value));
|
||||
}
|
||||
template<typename... args>
|
||||
constexpr reference emplace(args... arguments)
|
||||
{
|
||||
std::allocator_traits<NodeAllocator>::construct(allocator,
|
||||
tail,
|
||||
tail->prev,
|
||||
tail->next,
|
||||
arguments...);
|
||||
Node *newTail = allocateNode();
|
||||
// Insert at the end
|
||||
constexpr iterator add(const T& value) { return addInternal(value); }
|
||||
constexpr iterator add(T&& value) { return addInternal(std::move(value)); }
|
||||
template <typename... args> constexpr reference emplace(args... arguments) {
|
||||
std::allocator_traits<NodeAllocator>::construct(allocator, tail, tail->prev, tail->next, arguments...);
|
||||
Node* newTail = allocateNode();
|
||||
newTail->prev = tail;
|
||||
newTail->next = nullptr;
|
||||
tail->next = newTail;
|
||||
@@ -311,31 +197,23 @@ public:
|
||||
return insertedElement;
|
||||
}
|
||||
// front + popFront
|
||||
constexpr value_type retrieve()
|
||||
{
|
||||
constexpr value_type retrieve() {
|
||||
value_type temp = std::move(root->data);
|
||||
popFront();
|
||||
return temp;
|
||||
}
|
||||
constexpr iterator remove(iterator pos)
|
||||
{
|
||||
constexpr iterator remove(iterator pos) {
|
||||
_size--;
|
||||
Node *prev = pos.node->prev;
|
||||
Node *next = pos.node->next;
|
||||
if (prev == nullptr)
|
||||
{
|
||||
Node* prev = pos.node->prev;
|
||||
Node* next = pos.node->next;
|
||||
if (prev == nullptr) {
|
||||
root = next;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
prev->next = next;
|
||||
}
|
||||
if(next == nullptr)
|
||||
{
|
||||
if (next == nullptr) {
|
||||
tail = prev;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
next->prev = prev;
|
||||
}
|
||||
destroyNode(pos.node);
|
||||
@@ -343,111 +221,67 @@ public:
|
||||
markIteratorDirty();
|
||||
return Iterator(next);
|
||||
}
|
||||
constexpr void popBack()
|
||||
{
|
||||
constexpr void popBack() {
|
||||
assert(_size > 0);
|
||||
remove(Iterator(tail->prev));
|
||||
}
|
||||
constexpr void pop_back()
|
||||
{
|
||||
constexpr void pop_back() {
|
||||
assert(_size > 0);
|
||||
remove(Iterator(tail->prev));
|
||||
}
|
||||
constexpr void popFront()
|
||||
{
|
||||
constexpr void popFront() {
|
||||
assert(_size > 0);
|
||||
remove(Iterator(root));
|
||||
}
|
||||
constexpr void pop_front()
|
||||
{
|
||||
constexpr void pop_front() {
|
||||
assert(_size > 0);
|
||||
remove(Iterator(root));
|
||||
}
|
||||
constexpr iterator insert(iterator pos, const T &value)
|
||||
{
|
||||
constexpr iterator insert(iterator pos, const T& value) {
|
||||
_size++;
|
||||
Node *newNode = allocateNode();
|
||||
Node* newNode = allocateNode();
|
||||
initializeNode(newNode, value);
|
||||
newNode->next = pos.node;
|
||||
pos.node->prev = newNode;
|
||||
Node *tmp = pos.node->prev;
|
||||
if (tmp != nullptr)
|
||||
{
|
||||
Node* tmp = pos.node->prev;
|
||||
if (tmp != nullptr) {
|
||||
tmp->next = newNode;
|
||||
newNode->prev = tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
root = newNode;
|
||||
}
|
||||
markIteratorDirty();
|
||||
return Iterator(newNode);
|
||||
}
|
||||
constexpr iterator find(const T &value)
|
||||
{
|
||||
for (Node *i = root; i != tail; i = i->next)
|
||||
{
|
||||
if (!(i->data < value) && !(value < i->data))
|
||||
{
|
||||
constexpr iterator find(const T& value) {
|
||||
for (Node* i = root; i != tail; i = i->next) {
|
||||
if (!(i->data < value) && !(value < i->data)) {
|
||||
return iterator(i);
|
||||
}
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
constexpr bool empty() const
|
||||
{
|
||||
return _size == 0;
|
||||
}
|
||||
constexpr size_type size() const
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
constexpr iterator begin()
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
constexpr const_iterator begin() const
|
||||
{
|
||||
return cbeginIt;
|
||||
}
|
||||
constexpr iterator end()
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
constexpr const_iterator end() const
|
||||
{
|
||||
return cendIt;
|
||||
}
|
||||
constexpr bool empty() const { return _size == 0; }
|
||||
constexpr size_type size() const { return _size; }
|
||||
constexpr iterator begin() { return beginIt; }
|
||||
constexpr const_iterator begin() const { return cbeginIt; }
|
||||
constexpr iterator end() { return endIt; }
|
||||
constexpr const_iterator end() const { return cendIt; }
|
||||
|
||||
private:
|
||||
constexpr Node* allocateNode()
|
||||
{
|
||||
private:
|
||||
constexpr Node* allocateNode() {
|
||||
Node* node = allocator.allocate(1);
|
||||
assert(node != nullptr);
|
||||
node->prev = nullptr;
|
||||
node->next = nullptr;
|
||||
return node;
|
||||
}
|
||||
template<typename Type>
|
||||
constexpr void initializeNode(Node* node, Type&& data)
|
||||
{
|
||||
std::allocator_traits<NodeAllocator>::construct(allocator,
|
||||
node,
|
||||
node->prev,
|
||||
node->next,
|
||||
std::forward<Type>(data));
|
||||
template <typename Type> constexpr void initializeNode(Node* node, Type&& data) {
|
||||
std::allocator_traits<NodeAllocator>::construct(allocator, node, node->prev, node->next, std::forward<Type>(data));
|
||||
}
|
||||
constexpr void destroyNode(Node* node)
|
||||
{
|
||||
std::allocator_traits<NodeAllocator>::destroy(allocator, node);
|
||||
}
|
||||
constexpr void deallocateNode(Node* node)
|
||||
{
|
||||
allocator.deallocate(node, 1);
|
||||
}
|
||||
template<typename ValueType>
|
||||
constexpr iterator addInternal(ValueType&& value)
|
||||
{
|
||||
constexpr void destroyNode(Node* node) { std::allocator_traits<NodeAllocator>::destroy(allocator, node); }
|
||||
constexpr void deallocateNode(Node* node) { allocator.deallocate(node, 1); }
|
||||
template <typename ValueType> constexpr iterator addInternal(ValueType&& value) {
|
||||
initializeNode(tail, std::forward<ValueType>(value));
|
||||
Node* newTail = allocateNode();
|
||||
newTail->prev = tail;
|
||||
@@ -459,16 +293,15 @@ private:
|
||||
_size++;
|
||||
return insertedElement;
|
||||
}
|
||||
constexpr void markIteratorDirty()
|
||||
{
|
||||
constexpr void markIteratorDirty() {
|
||||
beginIt = Iterator(root);
|
||||
endIt = Iterator(tail);
|
||||
cbeginIt = ConstIterator(root);
|
||||
cendIt = ConstIterator(tail);
|
||||
}
|
||||
NodeAllocator allocator;
|
||||
Node *root;
|
||||
Node *tail;
|
||||
Node* root;
|
||||
Node* tail;
|
||||
iterator beginIt;
|
||||
iterator endIt;
|
||||
const_iterator cbeginIt;
|
||||
|
||||
+27
-78
@@ -1,26 +1,17 @@
|
||||
#pragma once
|
||||
#include <utility>
|
||||
#include "Array.h"
|
||||
#include "Pair.h"
|
||||
#include "Tree.h"
|
||||
#include "Serialization/Serialization.h"
|
||||
#include "Tree.h"
|
||||
#include <utility>
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
template<typename KeyType, typename PairType>
|
||||
struct _KeyFun
|
||||
{
|
||||
const KeyType& operator()(const PairType& pair) const
|
||||
{
|
||||
return pair.key;
|
||||
}
|
||||
|
||||
namespace Seele {
|
||||
template <typename KeyType, typename PairType> struct _KeyFun {
|
||||
const KeyType& operator()(const PairType& pair) const { return pair.key; }
|
||||
};
|
||||
template <typename K,
|
||||
typename V,
|
||||
typename Compare = std::less<K>,
|
||||
typename Allocator = std::pmr::polymorphic_allocator<Pair<const K, V>>>
|
||||
struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator>
|
||||
{
|
||||
template <typename K, typename V, typename Compare = std::less<K>, typename Allocator = std::pmr::polymorphic_allocator<Pair<const K, V>>>
|
||||
struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator> {
|
||||
using Super = Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator>;
|
||||
using key_type = Super::key_type;
|
||||
using mapped_type = V;
|
||||
@@ -39,84 +30,42 @@ struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocat
|
||||
using reverse_iterator = Super::reverse_iterator;
|
||||
using const_reverse_iterator = Super::const_reverse_iterator;
|
||||
|
||||
constexpr Map() noexcept
|
||||
: Super()
|
||||
{
|
||||
}
|
||||
constexpr Map() noexcept : Super() {}
|
||||
constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
|
||||
: Super(comp, alloc)
|
||||
{
|
||||
}
|
||||
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare()))
|
||||
: Super(alloc)
|
||||
{
|
||||
}
|
||||
constexpr mapped_type& operator[](const key_type& key)
|
||||
{
|
||||
: Super(comp, alloc) {}
|
||||
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare())) : Super(alloc) {}
|
||||
constexpr mapped_type& operator[](const key_type& key) {
|
||||
auto [it, inserted] = Super::insert(Pair<K, V>(key, V()));
|
||||
return it->value;
|
||||
}
|
||||
constexpr const mapped_type& operator[](const key_type& key) const
|
||||
{
|
||||
return Super::find(key)->value;
|
||||
}
|
||||
constexpr mapped_type& at(const key_type& key)
|
||||
{
|
||||
constexpr const mapped_type& operator[](const key_type& key) const { return Super::find(key)->value; }
|
||||
constexpr mapped_type& at(const key_type& key) {
|
||||
iterator elem = Super::find(key);
|
||||
if (elem == Super::end())
|
||||
{
|
||||
if (elem == Super::end()) {
|
||||
throw std::logic_error("Key not found");
|
||||
}
|
||||
return elem->value;
|
||||
}
|
||||
constexpr const mapped_type& at(const key_type& key) const
|
||||
{
|
||||
return Super::find(key)->value;
|
||||
}
|
||||
constexpr iterator find(const key_type& key)
|
||||
{
|
||||
return Super::find(key);
|
||||
}
|
||||
constexpr iterator erase(const key_type& key)
|
||||
{
|
||||
return Super::remove(key);
|
||||
}
|
||||
constexpr bool exists(const key_type& key) const
|
||||
{
|
||||
return Super::find(key) != Super::end();
|
||||
}
|
||||
constexpr bool exists(const key_type& key)
|
||||
{
|
||||
return Super::find(key) != Super::end();
|
||||
}
|
||||
constexpr bool contains(const key_type& key) const
|
||||
{
|
||||
return exists(key);
|
||||
}
|
||||
constexpr bool contains(const key_type& key)
|
||||
{
|
||||
return exists(key);
|
||||
}
|
||||
constexpr Pair<iterator, bool> insert(const value_type& val)
|
||||
{
|
||||
return Super::insert(val);
|
||||
}
|
||||
void save(ArchiveBuffer& buffer) const
|
||||
{
|
||||
constexpr const mapped_type& at(const key_type& key) const { return Super::find(key)->value; }
|
||||
constexpr iterator find(const key_type& key) { return Super::find(key); }
|
||||
constexpr iterator erase(const key_type& key) { return Super::remove(key); }
|
||||
constexpr bool exists(const key_type& key) const { return Super::find(key) != Super::end(); }
|
||||
constexpr bool exists(const key_type& key) { return Super::find(key) != Super::end(); }
|
||||
constexpr bool contains(const key_type& key) const { return exists(key); }
|
||||
constexpr bool contains(const key_type& key) { return exists(key); }
|
||||
constexpr Pair<iterator, bool> insert(const value_type& val) { return Super::insert(val); }
|
||||
void save(ArchiveBuffer& buffer) const {
|
||||
size_t s = Super::size();
|
||||
buffer.writeBytes(&s, sizeof(uint64));
|
||||
for(const auto& [k, v] : *this)
|
||||
{
|
||||
for (const auto& [k, v] : *this) {
|
||||
Serialization::save(buffer, k);
|
||||
Serialization::save(buffer, v);
|
||||
}
|
||||
}
|
||||
void load(ArchiveBuffer& buffer)
|
||||
{
|
||||
void load(ArchiveBuffer& buffer) {
|
||||
uint64 len = 0;
|
||||
buffer.readBytes(&len, sizeof(uint64));
|
||||
for(uint64 i = 0; i < len; ++i)
|
||||
{
|
||||
for (uint64 i = 0; i < len; ++i) {
|
||||
K k = K();
|
||||
V v = V();
|
||||
Serialization::load(buffer, k);
|
||||
|
||||
@@ -1,30 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
template <typename K, typename V>
|
||||
struct Pair
|
||||
{
|
||||
public:
|
||||
constexpr Pair()
|
||||
: key(K()), value(V())
|
||||
{}
|
||||
constexpr Pair(const K & key, const V & value)
|
||||
: key(key), value(value)
|
||||
{}
|
||||
template<class U1 = K, class U2 = V>
|
||||
constexpr Pair(U1&& x, U2&& y)
|
||||
: key(std::forward<U1>(x))
|
||||
, value(std::forward<U2>(y))
|
||||
{
|
||||
}
|
||||
namespace Seele {
|
||||
template <typename K, typename V> struct Pair {
|
||||
public:
|
||||
constexpr Pair() : key(K()), value(V()) {}
|
||||
constexpr Pair(const K& key, const V& value) : key(key), value(value) {}
|
||||
template <class U1 = K, class U2 = V> constexpr Pair(U1&& x, U2&& y) : key(std::forward<U1>(x)), value(std::forward<U2>(y)) {}
|
||||
Pair(const Pair& other) = default;
|
||||
Pair(Pair&& other) = default;
|
||||
~Pair(){}
|
||||
~Pair() {}
|
||||
Pair& operator=(const Pair& other) = default;
|
||||
Pair& operator=(Pair&& other) = default;
|
||||
constexpr friend bool operator<(const Pair& left, const Pair& right)
|
||||
{
|
||||
constexpr friend bool operator<(const Pair& left, const Pair& right) {
|
||||
return left.key < right.key || (!(right.key < left.key) && left.value < right.value);
|
||||
}
|
||||
K key;
|
||||
|
||||
+10
-23
@@ -1,14 +1,13 @@
|
||||
#pragma once
|
||||
#include <utility>
|
||||
#include "Array.h"
|
||||
#include "Tree.h"
|
||||
#include <utility>
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
template<class Key, class Compare = std::less<Key>, class Allocator = std::pmr::polymorphic_allocator<Key>>
|
||||
class Set : public Tree<Key, Key, std::identity, Compare, Allocator>
|
||||
{
|
||||
public:
|
||||
|
||||
namespace Seele {
|
||||
template <class Key, class Compare = std::less<Key>, class Allocator = std::pmr::polymorphic_allocator<Key>>
|
||||
class Set : public Tree<Key, Key, std::identity, Compare, Allocator> {
|
||||
public:
|
||||
using Super = Tree<Key, Key, std::identity, Compare, Allocator>;
|
||||
using key_type = Key;
|
||||
using value_type = Key;
|
||||
@@ -26,21 +25,9 @@ public:
|
||||
using reverse_iterator = Super::reverse_iterator;
|
||||
using const_reverse_iterator = Super::const_reverse_iterator;
|
||||
|
||||
Set()
|
||||
: Set(Compare())
|
||||
{
|
||||
}
|
||||
explicit Set(const Compare& comp, const Allocator alloc = Allocator())
|
||||
: Super(comp, alloc)
|
||||
{
|
||||
}
|
||||
explicit Set(const Allocator alloc)
|
||||
: Super(alloc)
|
||||
{
|
||||
}
|
||||
constexpr Pair<iterator, bool> insert(const value_type& value)
|
||||
{
|
||||
return Super::insert(value);
|
||||
}
|
||||
Set() : Set(Compare()) {}
|
||||
explicit Set(const Compare& comp, const Allocator alloc = Allocator()) : Super(comp, alloc) {}
|
||||
explicit Set(const Allocator alloc) : Super(alloc) {}
|
||||
constexpr Pair<iterator, bool> insert(const value_type& value) { return Super::insert(value); }
|
||||
};
|
||||
} // namespace Seele
|
||||
+105
-276
@@ -1,147 +1,88 @@
|
||||
#pragma once
|
||||
#include <memory_resource>
|
||||
#include "Pair.h"
|
||||
#include <memory_resource>
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
template <
|
||||
typename KeyType,
|
||||
typename NodeData,
|
||||
typename KeyFun,
|
||||
typename Compare,
|
||||
typename Allocator>
|
||||
struct Tree
|
||||
{
|
||||
protected:
|
||||
struct Node
|
||||
{
|
||||
Node(Node* left, Node* right, const NodeData& nodeData)
|
||||
: leftChild(left)
|
||||
, rightChild(right)
|
||||
, data(nodeData)
|
||||
{}
|
||||
Node(Node* left, Node* right, NodeData&& nodeData)
|
||||
: leftChild(left)
|
||||
, rightChild(right)
|
||||
, data(std::move(nodeData))
|
||||
{}
|
||||
|
||||
namespace Seele {
|
||||
template <typename KeyType, typename NodeData, typename KeyFun, typename Compare, typename Allocator> struct Tree {
|
||||
protected:
|
||||
struct Node {
|
||||
Node(Node* left, Node* right, const NodeData& nodeData) : leftChild(left), rightChild(right), data(nodeData) {}
|
||||
Node(Node* left, Node* right, NodeData&& nodeData) : leftChild(left), rightChild(right), data(std::move(nodeData)) {}
|
||||
Node* leftChild;
|
||||
Node* rightChild;
|
||||
NodeData data;
|
||||
};
|
||||
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
|
||||
public:
|
||||
template<typename IterType>
|
||||
class IteratorBase
|
||||
{
|
||||
public:
|
||||
|
||||
public:
|
||||
template <typename IterType> class IteratorBase {
|
||||
public:
|
||||
using iterator_category = std::bidirectional_iterator_tag;
|
||||
using value_type = IterType;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using reference = IterType&;
|
||||
using pointer = IterType*;
|
||||
|
||||
constexpr IteratorBase(Node* x = nullptr, Array<Node*>&& beginIt = Array<Node*>())
|
||||
: node(x), traversal(std::move(beginIt))
|
||||
{
|
||||
}
|
||||
constexpr IteratorBase(const IteratorBase& i)
|
||||
: node(i.node), traversal(i.traversal)
|
||||
{
|
||||
}
|
||||
constexpr IteratorBase(IteratorBase&& i) noexcept
|
||||
: node(std::move(i.node)), traversal(std::move(i.traversal))
|
||||
{
|
||||
}
|
||||
constexpr IteratorBase& operator=(const IteratorBase& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
constexpr IteratorBase(Node* x = nullptr, Array<Node*>&& beginIt = Array<Node*>()) : node(x), traversal(std::move(beginIt)) {}
|
||||
constexpr IteratorBase(const IteratorBase& i) : node(i.node), traversal(i.traversal) {}
|
||||
constexpr IteratorBase(IteratorBase&& i) noexcept : node(std::move(i.node)), traversal(std::move(i.traversal)) {}
|
||||
constexpr IteratorBase& operator=(const IteratorBase& other) {
|
||||
if (this != &other) {
|
||||
node = other.node;
|
||||
traversal = other.traversal;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr IteratorBase& operator=(IteratorBase&& other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
constexpr IteratorBase& operator=(IteratorBase&& other) noexcept {
|
||||
if (this != &other) {
|
||||
node = std::move(other.node);
|
||||
traversal = std::move(other.traversal);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr reference operator*() const
|
||||
{
|
||||
return node->data;
|
||||
}
|
||||
constexpr pointer operator->() const
|
||||
{
|
||||
return &(node->data);
|
||||
}
|
||||
constexpr bool operator!=(const IteratorBase& other)
|
||||
{
|
||||
return node != other.node;
|
||||
}
|
||||
constexpr bool operator==(const IteratorBase& other)
|
||||
{
|
||||
return node == other.node;
|
||||
}
|
||||
constexpr std::strong_ordering operator<=>(const IteratorBase& other)
|
||||
{
|
||||
return node <=> other.node;
|
||||
}
|
||||
constexpr IteratorBase& operator++()
|
||||
{
|
||||
constexpr reference operator*() const { return node->data; }
|
||||
constexpr pointer operator->() const { return &(node->data); }
|
||||
constexpr bool operator!=(const IteratorBase& other) { return node != other.node; }
|
||||
constexpr bool operator==(const IteratorBase& other) { return node == other.node; }
|
||||
constexpr std::strong_ordering operator<=>(const IteratorBase& other) { return node <=> other.node; }
|
||||
constexpr IteratorBase& operator++() {
|
||||
node = node->rightChild;
|
||||
while (node != nullptr
|
||||
&& node->leftChild != nullptr)
|
||||
{
|
||||
while (node != nullptr && node->leftChild != nullptr) {
|
||||
traversal.add(node);
|
||||
node = node->leftChild;
|
||||
}
|
||||
if (node == nullptr
|
||||
&& traversal.size() > 0)
|
||||
{
|
||||
if (node == nullptr && traversal.size() > 0) {
|
||||
node = traversal.back();
|
||||
traversal.pop();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr IteratorBase& operator--()
|
||||
{
|
||||
constexpr IteratorBase& operator--() {
|
||||
node = node->leftChild;
|
||||
while (node != nullptr
|
||||
&& node->rightChild != nullptr)
|
||||
{
|
||||
while (node != nullptr && node->rightChild != nullptr) {
|
||||
traversal.add(node);
|
||||
node = node->rightchild;
|
||||
}
|
||||
if (node == nullptr
|
||||
&& traversal.size() > 0)
|
||||
{
|
||||
if (node == nullptr && traversal.size() > 0) {
|
||||
node = traversal.back();
|
||||
traversal.pop();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr IteratorBase operator--(int)
|
||||
{
|
||||
constexpr IteratorBase operator--(int) {
|
||||
IteratorBase tmp(*this);
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
constexpr IteratorBase operator++(int)
|
||||
{
|
||||
constexpr IteratorBase operator++(int) {
|
||||
IteratorBase tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
Node* getNode()
|
||||
{
|
||||
return node;
|
||||
}
|
||||
private:
|
||||
Node* getNode() { return node; }
|
||||
|
||||
private:
|
||||
Node* node;
|
||||
Array<Node*> traversal;
|
||||
};
|
||||
@@ -164,84 +105,39 @@ public:
|
||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||
|
||||
constexpr Tree() noexcept
|
||||
: alloc(NodeAlloc())
|
||||
, root(nullptr)
|
||||
, beginIt(nullptr)
|
||||
, endIt(nullptr)
|
||||
, iteratorsDirty(true)
|
||||
, _size(0)
|
||||
, comp(Compare())
|
||||
{
|
||||
}
|
||||
: alloc(NodeAlloc()), root(nullptr), beginIt(nullptr), endIt(nullptr), iteratorsDirty(true), _size(0), comp(Compare()) {}
|
||||
constexpr explicit Tree(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
|
||||
: alloc(alloc)
|
||||
, root(nullptr)
|
||||
, beginIt(nullptr)
|
||||
, endIt(nullptr)
|
||||
, iteratorsDirty(true)
|
||||
, _size(0)
|
||||
, comp(comp)
|
||||
{
|
||||
}
|
||||
: alloc(alloc), root(nullptr), beginIt(nullptr), endIt(nullptr), iteratorsDirty(true), _size(0), comp(comp) {}
|
||||
constexpr explicit Tree(const Allocator& alloc) noexcept(noexcept(Compare()))
|
||||
: alloc(alloc)
|
||||
, root(nullptr)
|
||||
, beginIt(nullptr)
|
||||
, endIt(nullptr)
|
||||
, iteratorsDirty(true)
|
||||
, _size(0)
|
||||
, comp(Compare())
|
||||
{
|
||||
}
|
||||
constexpr Tree(const Tree& other)
|
||||
: alloc(other.alloc)
|
||||
, root(nullptr)
|
||||
, iteratorsDirty(true)
|
||||
, _size()
|
||||
, comp(other.comp)
|
||||
{
|
||||
for (const auto& elem : other)
|
||||
{
|
||||
: alloc(alloc), root(nullptr), beginIt(nullptr), endIt(nullptr), iteratorsDirty(true), _size(0), comp(Compare()) {}
|
||||
constexpr Tree(const Tree& other) : alloc(other.alloc), root(nullptr), iteratorsDirty(true), _size(), comp(other.comp) {
|
||||
for (const auto& elem : other) {
|
||||
insert(elem);
|
||||
}
|
||||
}
|
||||
constexpr Tree(Tree&& other) noexcept
|
||||
: alloc(std::move(other.alloc))
|
||||
, root(std::move(other.root))
|
||||
, iteratorsDirty(true)
|
||||
, _size(std::move(other._size))
|
||||
, comp(std::move(other.comp))
|
||||
{
|
||||
: alloc(std::move(other.alloc)), root(std::move(other.root)), iteratorsDirty(true), _size(std::move(other._size)),
|
||||
comp(std::move(other.comp)) {
|
||||
other._size = 0;
|
||||
}
|
||||
constexpr ~Tree() noexcept
|
||||
{
|
||||
clear();
|
||||
}
|
||||
constexpr Tree& operator=(const Tree& other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
constexpr ~Tree() noexcept { clear(); }
|
||||
constexpr Tree& operator=(const Tree& other) {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value)
|
||||
{
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value) {
|
||||
alloc = other.alloc;
|
||||
}
|
||||
for (const auto& elem : other)
|
||||
{
|
||||
for (const auto& elem : other) {
|
||||
insert(elem);
|
||||
}
|
||||
markIteratorsDirty();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr Tree& operator=(Tree&& other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
constexpr Tree& operator=(Tree&& other) noexcept {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
|
||||
{
|
||||
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value) {
|
||||
alloc = std::move(other.alloc);
|
||||
}
|
||||
root = std::move(other.root);
|
||||
@@ -252,144 +148,108 @@ public:
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
constexpr void clear()
|
||||
{
|
||||
while (_size > 0)
|
||||
{
|
||||
constexpr void clear() {
|
||||
while (_size > 0) {
|
||||
root = _remove(root, keyFun(root->data));
|
||||
}
|
||||
root = nullptr;
|
||||
markIteratorsDirty();
|
||||
}
|
||||
constexpr iterator begin()
|
||||
{
|
||||
if (iteratorsDirty)
|
||||
{
|
||||
constexpr iterator begin() {
|
||||
if (iteratorsDirty) {
|
||||
refreshIterators();
|
||||
}
|
||||
return beginIt;
|
||||
}
|
||||
constexpr iterator end()
|
||||
{
|
||||
if (iteratorsDirty)
|
||||
{
|
||||
constexpr iterator end() {
|
||||
if (iteratorsDirty) {
|
||||
refreshIterators();
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
constexpr iterator begin() const
|
||||
{
|
||||
if (iteratorsDirty)
|
||||
{
|
||||
constexpr iterator begin() const {
|
||||
if (iteratorsDirty) {
|
||||
return calcBeginIterator();
|
||||
}
|
||||
return beginIt;
|
||||
}
|
||||
constexpr iterator end() const
|
||||
{
|
||||
if (iteratorsDirty)
|
||||
{
|
||||
constexpr iterator end() const {
|
||||
if (iteratorsDirty) {
|
||||
return calcEndIterator();
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
constexpr bool empty() const
|
||||
{
|
||||
return _size == 0;
|
||||
}
|
||||
constexpr size_type size() const
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
protected:
|
||||
constexpr iterator find(const key_type& key)
|
||||
{
|
||||
constexpr bool empty() const { return _size == 0; }
|
||||
constexpr size_type size() const { return _size; }
|
||||
|
||||
protected:
|
||||
constexpr iterator find(const key_type& key) {
|
||||
root = _splay(root, key);
|
||||
if (root == nullptr || !equal(root->data, key))
|
||||
{
|
||||
if (root == nullptr || !equal(root->data, key)) {
|
||||
return end();
|
||||
}
|
||||
return iterator(root);
|
||||
}
|
||||
constexpr iterator find(const key_type& key) const
|
||||
{
|
||||
constexpr iterator find(const key_type& key) const {
|
||||
Node* it = root;
|
||||
while (it != nullptr && !equal(it->data, key))
|
||||
{
|
||||
if (comp(key, keyFun(it->data)))
|
||||
{
|
||||
while (it != nullptr && !equal(it->data, key)) {
|
||||
if (comp(key, keyFun(it->data))) {
|
||||
it = it->leftChild;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
it = it->rightChild;
|
||||
}
|
||||
}
|
||||
if (it == nullptr)
|
||||
{
|
||||
if (it == nullptr) {
|
||||
return end();
|
||||
}
|
||||
return iterator(it);
|
||||
}
|
||||
constexpr Pair<iterator, bool> insert(const NodeData& data)
|
||||
{
|
||||
constexpr Pair<iterator, bool> insert(const NodeData& data) {
|
||||
auto [r, inserted] = _insert(root, data);
|
||||
root = r;
|
||||
return Pair<iterator, bool>(iterator(root), inserted);
|
||||
}
|
||||
constexpr Pair<iterator, bool> insert(NodeData&& data)
|
||||
{
|
||||
constexpr Pair<iterator, bool> insert(NodeData&& data) {
|
||||
auto [r, inserted] = _insert(root, std::move(data));
|
||||
root = r;
|
||||
return Pair<iterator, bool>(iterator(root), inserted);
|
||||
}
|
||||
constexpr iterator remove(const key_type& key)
|
||||
{
|
||||
constexpr iterator remove(const key_type& key) {
|
||||
root = _remove(root, key);
|
||||
return iterator(root);
|
||||
}
|
||||
private:
|
||||
void verifyTree()
|
||||
{
|
||||
|
||||
private:
|
||||
void verifyTree() {
|
||||
size_t numElems = 0;
|
||||
for (const auto& it : *this)
|
||||
{
|
||||
for (const auto& it : *this) {
|
||||
numElems++;
|
||||
}
|
||||
assert(numElems == _size);
|
||||
}
|
||||
void markIteratorsDirty()
|
||||
{
|
||||
iteratorsDirty = true;
|
||||
}
|
||||
void refreshIterators()
|
||||
{
|
||||
void markIteratorsDirty() { iteratorsDirty = true; }
|
||||
void refreshIterators() {
|
||||
beginIt = calcBeginIterator();
|
||||
endIt = calcEndIterator();
|
||||
iteratorsDirty = false;
|
||||
}
|
||||
constexpr Iterator calcBeginIterator() const
|
||||
{
|
||||
constexpr Iterator calcBeginIterator() const {
|
||||
Node* begin = root;
|
||||
Array<Node*> beginTraversal;
|
||||
while (begin != nullptr)
|
||||
{
|
||||
while (begin != nullptr) {
|
||||
beginTraversal.add(begin);
|
||||
begin = begin->leftChild;
|
||||
}
|
||||
if (!beginTraversal.empty())
|
||||
{
|
||||
if (!beginTraversal.empty()) {
|
||||
begin = beginTraversal.back();
|
||||
beginTraversal.pop();
|
||||
}
|
||||
return Iterator(begin, std::move(beginTraversal));
|
||||
}
|
||||
constexpr Iterator calcEndIterator() const
|
||||
{
|
||||
constexpr Iterator calcEndIterator() const {
|
||||
Node* endIndex = root;
|
||||
Array<Node*> endTraversal;
|
||||
while (endIndex != nullptr)
|
||||
{
|
||||
while (endIndex != nullptr) {
|
||||
endTraversal.add(endIndex);
|
||||
endIndex = endIndex->rightChild;
|
||||
}
|
||||
@@ -403,26 +263,21 @@ private:
|
||||
size_type _size;
|
||||
Compare comp;
|
||||
KeyFun keyFun = KeyFun();
|
||||
Node* rotateLeft(Node* x)
|
||||
{
|
||||
Node* rotateLeft(Node* x) {
|
||||
Node* y = x->rightChild;
|
||||
x->rightChild = y->leftChild;
|
||||
y->leftChild = x;
|
||||
return y;
|
||||
}
|
||||
Node* rotateRight(Node* x)
|
||||
{
|
||||
Node* rotateRight(Node* x) {
|
||||
Node* y = x->leftChild;
|
||||
x->leftChild = y->rightChild;
|
||||
y->rightChild = x;
|
||||
return y;
|
||||
}
|
||||
template<class NodeDataType>
|
||||
Pair<Node*, bool> _insert(Node* r, NodeDataType&& data)
|
||||
{
|
||||
template <class NodeDataType> Pair<Node*, bool> _insert(Node* r, NodeDataType&& data) {
|
||||
markIteratorsDirty();
|
||||
if (r == nullptr)
|
||||
{
|
||||
if (r == nullptr) {
|
||||
root = alloc.allocate(1);
|
||||
std::allocator_traits<NodeAlloc>::construct(alloc, root, nullptr, nullptr, std::forward<NodeDataType>(data));
|
||||
_size++;
|
||||
@@ -436,14 +291,11 @@ private:
|
||||
Node* newNode = alloc.allocate(1);
|
||||
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, nullptr, nullptr, std::forward<NodeDataType>(data));
|
||||
|
||||
if (comp(keyFun(newNode->data), keyFun(r->data)))
|
||||
{
|
||||
if (comp(keyFun(newNode->data), keyFun(r->data))) {
|
||||
newNode->rightChild = r;
|
||||
newNode->leftChild = r->leftChild;
|
||||
r->leftChild = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
newNode->leftChild = r;
|
||||
newNode->rightChild = r->rightChild;
|
||||
r->rightChild = nullptr;
|
||||
@@ -451,9 +303,7 @@ private:
|
||||
_size++;
|
||||
return Pair<Node*, bool>(newNode, true);
|
||||
}
|
||||
template<class K>
|
||||
Node* _remove(Node* r, K&& key)
|
||||
{
|
||||
template <class K> Node* _remove(Node* r, K&& key) {
|
||||
markIteratorsDirty();
|
||||
Node* temp;
|
||||
if (r == nullptr)
|
||||
@@ -464,13 +314,10 @@ private:
|
||||
if (!equal(r->data, key))
|
||||
return r;
|
||||
|
||||
if (r->leftChild == nullptr)
|
||||
{
|
||||
if (r->leftChild == nullptr) {
|
||||
temp = r;
|
||||
r = r->rightChild;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
temp = r;
|
||||
|
||||
r = _splay(r->leftChild, key);
|
||||
@@ -481,52 +328,38 @@ private:
|
||||
_size--;
|
||||
return r;
|
||||
}
|
||||
template<class K>
|
||||
Node* _splay(Node* r, K&& key)
|
||||
{
|
||||
template <class K> Node* _splay(Node* r, K&& key) {
|
||||
markIteratorsDirty();
|
||||
if (r == nullptr || equal(r->data, key))
|
||||
{
|
||||
if (r == nullptr || equal(r->data, key)) {
|
||||
return r;
|
||||
}
|
||||
if (comp(key, keyFun(r->data)))
|
||||
{
|
||||
if (comp(key, keyFun(r->data))) {
|
||||
if (r->leftChild == nullptr)
|
||||
return r;
|
||||
|
||||
if (comp(key, keyFun(r->leftChild->data)))
|
||||
{
|
||||
if (comp(key, keyFun(r->leftChild->data))) {
|
||||
r->leftChild->leftChild = _splay(r->leftChild->leftChild, key);
|
||||
|
||||
r = rotateRight(r);
|
||||
}
|
||||
else if (comp(keyFun(r->leftChild->data), key))
|
||||
{
|
||||
} else if (comp(keyFun(r->leftChild->data), key)) {
|
||||
r->leftChild->rightChild = _splay(r->leftChild->rightChild, key);
|
||||
|
||||
if (r->leftChild->rightChild != nullptr)
|
||||
{
|
||||
if (r->leftChild->rightChild != nullptr) {
|
||||
r->leftChild = rotateLeft(r->leftChild);
|
||||
}
|
||||
}
|
||||
return (r->leftChild == nullptr) ? r : rotateRight(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
if (r->rightChild == nullptr)
|
||||
return r;
|
||||
|
||||
if (comp(key, keyFun(r->rightChild->data)))
|
||||
{
|
||||
if (comp(key, keyFun(r->rightChild->data))) {
|
||||
r->rightChild->leftChild = _splay(r->rightChild->leftChild, key);
|
||||
|
||||
if (r->rightChild->leftChild != nullptr)
|
||||
{
|
||||
if (r->rightChild->leftChild != nullptr) {
|
||||
r->rightChild = rotateRight(r->rightChild);
|
||||
}
|
||||
}
|
||||
else if (comp(keyFun(r->rightChild->data), key))
|
||||
{
|
||||
} else if (comp(keyFun(r->rightChild->data), key)) {
|
||||
r->rightChild->rightChild = _splay(r->rightChild->rightChild, key);
|
||||
|
||||
r = rotateLeft(r);
|
||||
@@ -534,10 +367,6 @@ private:
|
||||
return (r->rightChild == nullptr) ? r : rotateLeft(r);
|
||||
}
|
||||
}
|
||||
template<typename K>
|
||||
bool equal(const NodeData& a, K&& b) const
|
||||
{
|
||||
return !comp(keyFun(a), b) && !comp(b, keyFun(a));
|
||||
}
|
||||
template <typename K> bool equal(const NodeData& a, K&& b) const { return !comp(keyFun(a), b) && !comp(b, keyFun(a)); }
|
||||
};
|
||||
} // namespace Seele
|
||||
Reference in New Issue
Block a user