2020-03-13 12:44:33 +01:00
|
|
|
#pragma once
|
|
|
|
|
#include "MinimalEngine.h"
|
|
|
|
|
namespace Seele
|
|
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
template <typename T>
|
|
|
|
|
class List
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
struct Node
|
|
|
|
|
{
|
|
|
|
|
Node *prev;
|
|
|
|
|
Node *next;
|
|
|
|
|
T data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
List()
|
|
|
|
|
{
|
|
|
|
|
root = nullptr;
|
|
|
|
|
tail = nullptr;
|
|
|
|
|
beginIt = Iterator(root);
|
|
|
|
|
endIt = Iterator(tail);
|
2020-10-14 01:49:43 +02:00
|
|
|
_size = 0;
|
|
|
|
|
}
|
|
|
|
|
List(const List& other)
|
|
|
|
|
{
|
|
|
|
|
//TODO: improve
|
|
|
|
|
for(const auto& it : other)
|
|
|
|
|
{
|
|
|
|
|
add(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List(List&& other)
|
|
|
|
|
: 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))
|
|
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
~List()
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
List& operator=(const List& other)
|
|
|
|
|
{
|
|
|
|
|
if(this != &other)
|
|
|
|
|
{
|
|
|
|
|
if(root != nullptr)
|
|
|
|
|
{
|
|
|
|
|
delete root;
|
|
|
|
|
}
|
|
|
|
|
if(tail != nullptr)
|
|
|
|
|
{
|
|
|
|
|
delete tail;
|
|
|
|
|
}
|
|
|
|
|
_size = 0;
|
|
|
|
|
for(const auto& it : other)
|
|
|
|
|
{
|
|
|
|
|
add(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
List& operator=(List&& other)
|
|
|
|
|
{
|
|
|
|
|
if(this != &other)
|
|
|
|
|
{
|
|
|
|
|
if(root != nullptr)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
root = other.root;
|
|
|
|
|
tail = other.tail;
|
|
|
|
|
beginIt = other.beginIt;
|
|
|
|
|
endIt = other.endIt;
|
|
|
|
|
_size = other._size;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
template <typename X>
|
|
|
|
|
class IteratorBase
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2020-04-12 15:47:19 +02:00
|
|
|
typedef std::forward_iterator_tag iterator_category;
|
|
|
|
|
typedef X value_type;
|
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
|
typedef X &reference;
|
|
|
|
|
typedef X *pointer;
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
IteratorBase(Node *x = nullptr)
|
|
|
|
|
: node(x)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
IteratorBase(const IteratorBase &i)
|
|
|
|
|
: node(i.node)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
IteratorBase(IteratorBase&& i)
|
|
|
|
|
: node(std::move(i.node))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
~IteratorBase()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
IteratorBase& operator=(const IteratorBase& other)
|
|
|
|
|
{
|
|
|
|
|
if(this != &other)
|
|
|
|
|
{
|
|
|
|
|
node = other.node;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase& operator=(IteratorBase&& other)
|
|
|
|
|
{
|
|
|
|
|
if(this != &other)
|
|
|
|
|
{
|
|
|
|
|
node = std::move(other.node);
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
reference operator*() const
|
|
|
|
|
{
|
|
|
|
|
return node->data;
|
|
|
|
|
}
|
|
|
|
|
pointer operator->() const
|
|
|
|
|
{
|
|
|
|
|
return &node->data;
|
|
|
|
|
}
|
|
|
|
|
inline bool operator!=(const IteratorBase &other)
|
|
|
|
|
{
|
|
|
|
|
return node != other.node;
|
|
|
|
|
}
|
|
|
|
|
inline bool operator==(const IteratorBase &other)
|
|
|
|
|
{
|
|
|
|
|
return node == other.node;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase &operator--()
|
|
|
|
|
{
|
|
|
|
|
node = node->prev;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase operator--(int)
|
|
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
--*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase &operator++()
|
|
|
|
|
{
|
|
|
|
|
node = node->next;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
IteratorBase operator++(int)
|
|
|
|
|
{
|
|
|
|
|
IteratorBase tmp(*this);
|
|
|
|
|
++*this;
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
private:
|
|
|
|
|
Node *node;
|
|
|
|
|
friend class List<T>;
|
|
|
|
|
};
|
|
|
|
|
typedef IteratorBase<T> Iterator;
|
|
|
|
|
typedef IteratorBase<const T> ConstIterator;
|
|
|
|
|
|
|
|
|
|
T &front()
|
|
|
|
|
{
|
|
|
|
|
return root->data;
|
|
|
|
|
}
|
|
|
|
|
T &back()
|
|
|
|
|
{
|
|
|
|
|
return tail->prev->data;
|
|
|
|
|
}
|
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
if (empty())
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
return;
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
for (Node *tmp = root; tmp != tail;)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
tmp = tmp->next;
|
|
|
|
|
delete tmp->prev;
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
delete tail;
|
|
|
|
|
tail = nullptr;
|
|
|
|
|
root = nullptr;
|
|
|
|
|
}
|
|
|
|
|
//Insert at the end
|
|
|
|
|
Iterator add(const T &value)
|
|
|
|
|
{
|
|
|
|
|
if (root == nullptr)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
root = new Node();
|
|
|
|
|
tail = root;
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
tail->data = value;
|
|
|
|
|
Node *newTail = new Node();
|
|
|
|
|
newTail->prev = tail;
|
|
|
|
|
newTail->next = nullptr;
|
|
|
|
|
tail->next = newTail;
|
|
|
|
|
Iterator insertedElement(tail);
|
|
|
|
|
tail = newTail;
|
2021-04-13 23:09:16 +02:00
|
|
|
markIteratorDirty();
|
2020-10-14 01:49:43 +02:00
|
|
|
_size++;
|
2020-04-12 15:47:19 +02:00
|
|
|
return insertedElement;
|
|
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
Iterator add(T&& value)
|
|
|
|
|
{
|
|
|
|
|
if (root == nullptr)
|
|
|
|
|
{
|
|
|
|
|
root = new Node();
|
|
|
|
|
tail = root;
|
|
|
|
|
}
|
|
|
|
|
tail->data = std::move(value);
|
|
|
|
|
Node *newTail = new Node();
|
|
|
|
|
newTail->prev = tail;
|
|
|
|
|
newTail->next = nullptr;
|
|
|
|
|
tail->next = newTail;
|
|
|
|
|
Iterator insertedElement(tail);
|
|
|
|
|
tail = newTail;
|
2021-04-13 23:09:16 +02:00
|
|
|
markIteratorDirty();
|
2020-10-14 01:49:43 +02:00
|
|
|
_size++;
|
2020-06-02 11:46:18 +02:00
|
|
|
return insertedElement;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
Iterator remove(Iterator pos)
|
|
|
|
|
{
|
2020-10-14 01:49:43 +02:00
|
|
|
_size--;
|
2020-04-12 15:47:19 +02:00
|
|
|
Node *prev = pos.node->prev;
|
|
|
|
|
Node *next = pos.node->next;
|
|
|
|
|
if (prev == nullptr)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
root = next;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
prev->next = next;
|
|
|
|
|
}
|
2020-09-19 14:36:50 +02:00
|
|
|
if(next == nullptr)
|
|
|
|
|
{
|
|
|
|
|
root = prev;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
next->prev = prev;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
delete pos.node;
|
2021-04-13 23:09:16 +02:00
|
|
|
markIteratorDirty();
|
2020-04-12 15:47:19 +02:00
|
|
|
return Iterator(next);
|
|
|
|
|
}
|
2021-03-31 12:18:16 +02:00
|
|
|
void popBack()
|
|
|
|
|
{
|
|
|
|
|
remove(Iterator(tail->prev));
|
|
|
|
|
}
|
|
|
|
|
void popFront()
|
|
|
|
|
{
|
|
|
|
|
remove(Iterator(root));
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
Iterator insert(Iterator pos, const T &value)
|
|
|
|
|
{
|
2020-10-14 01:49:43 +02:00
|
|
|
_size++;
|
2020-04-12 15:47:19 +02:00
|
|
|
if (root == nullptr)
|
|
|
|
|
{
|
|
|
|
|
root = new Node();
|
|
|
|
|
root->data = value;
|
|
|
|
|
tail = new Node();
|
|
|
|
|
root->next = tail;
|
|
|
|
|
root->prev = nullptr;
|
|
|
|
|
tail->prev = root;
|
|
|
|
|
tail->next = nullptr;
|
2021-04-13 23:09:16 +02:00
|
|
|
markIteratorDirty();
|
2020-03-13 12:44:33 +01:00
|
|
|
return beginIt;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
Node *tmp = pos.node->prev;
|
|
|
|
|
Node *newNode = new Node();
|
|
|
|
|
newNode->data = value;
|
|
|
|
|
tmp->next = newNode;
|
|
|
|
|
newNode->prev = tmp;
|
|
|
|
|
newNode->next = pos.node;
|
|
|
|
|
pos.node->prev = newNode;
|
|
|
|
|
return Iterator(newNode);
|
|
|
|
|
}
|
|
|
|
|
Iterator find(const T &value)
|
|
|
|
|
{
|
|
|
|
|
for (Node *i = root; i != tail; i = i->next)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
if (!(i->data < value) && !(value < i->data))
|
|
|
|
|
{
|
|
|
|
|
return Iterator(i);
|
|
|
|
|
}
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
return endIt;
|
|
|
|
|
}
|
|
|
|
|
bool empty()
|
|
|
|
|
{
|
2020-10-14 01:49:43 +02:00
|
|
|
return _size == 0;
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
uint32 size()
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
2020-10-14 01:49:43 +02:00
|
|
|
return _size;
|
2020-04-12 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
Iterator begin()
|
|
|
|
|
{
|
|
|
|
|
return beginIt;
|
|
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
const Iterator begin() const
|
|
|
|
|
{
|
|
|
|
|
return beginIt;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
Iterator end()
|
|
|
|
|
{
|
|
|
|
|
return endIt;
|
|
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
const Iterator end() const
|
|
|
|
|
{
|
|
|
|
|
return endIt;
|
|
|
|
|
}
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
private:
|
2021-04-13 23:09:16 +02:00
|
|
|
void markIteratorDirty()
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
|
|
|
|
beginIt = Iterator(root);
|
|
|
|
|
endIt = Iterator(tail);
|
|
|
|
|
}
|
|
|
|
|
Node *root;
|
|
|
|
|
Node *tail;
|
|
|
|
|
Iterator beginIt;
|
|
|
|
|
Iterator endIt;
|
2020-10-14 01:49:43 +02:00
|
|
|
uint32 _size;
|
2020-04-12 15:47:19 +02:00
|
|
|
};
|
|
|
|
|
} // namespace Seele
|