Formatted EVERYTHING

This commit is contained in:
Dynamitos
2024-06-09 12:20:53 +02:00
parent f18bf8acbe
commit d95dab850c
265 changed files with 8002 additions and 12310 deletions
+109 -276
View File
@@ -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;