31#ifndef FENNEC_CONTAINERS_LIST_H
32#define FENNEC_CONTAINERS_LIST_H
66template<
class TypeT,
class Alloc = allocator<TypeT>>
80 static constexpr size_t npos = -1;
94 : _table(), _freed(), _root(
npos), _last(
npos), _size(0) {
112 : _table(fennec::move(l._table))
113 , _freed(fennec::move(l._freed))
122 for (
size_t i = 0; i <
capacity(); ++i) {
123 _table[i].value = nullopt;
154 _root = l._root; _last = l._last;
169 constexpr size_t size()
const {
182 return _root ==
npos;
200 assertd(i >= 0 &&
size_t(i) < _size,
"Index out of Bounds");
202 assertd(n !=
npos,
"Index out of Bounds");
203 return *_table[n].value;
213 assertd(i >= 0 &&
size_t(i) < _size,
"Index out of Bounds");
215 assertd(n !=
npos,
"Index out of Bounds");
216 return *_table[n].value;
223 return *_table[_root].value;
230 return *_table[_root].value;
237 return *_table[_last].value;
244 return *_table[_last].value;
262 return this->_insert(it._n, x);
272 return this->_insert(it._n, fennec::forward<value_t>(x));
282 assert(i <=
size(),
"Index out of Bounds");
284 size_t n = _walk(
min(i,
size_t(
size() - 1)));
285 return this->_insert(n, x);
295 assert(i <=
size(),
"Index out of Bounds");
297 size_t n = _walk(
min(i,
size_t(
size() - 1)));
298 return this->_insert(n, fennec::forward<value_t>(x));
308 template<
typename...ArgsT>
309 constexpr size_t emplace(
size_t i, ArgsT&&...args) {
310 assert(i <=
size(),
"Index out of Bounds");
312 size_t n = _walk(
min(i,
size_t(
size() - 1)));
313 return this->_insert(n, fennec::forward<ArgsT>(args)...);
320 return this->_insert(_root, x);
327 return this->_insert(_root, fennec::forward<value_t>(x));
334 template<
typename...ArgsT>
336 return this->_insert(_root, fennec::forward<ArgsT>(args)...);
343 return this->_insert(
npos, x);
350 return this->_insert(
npos, fennec::forward<value_t>(x));
357 template<
typename...ArgsT>
359 return this->_insert(
npos, fennec::forward<ArgsT>(args)...);
366 assert(i <
size(),
"Index out of Bounds!");
395 fennec::destruct(&_table[i]);
448 rhs._n = rhs._list->_next(rhs._n);
458 constexpr value_t& operator*() {
459 return *(_list->_table[_n].value);
462 constexpr value_t* operator->() {
463 return &*(_list->_table[_n].value);
466 constexpr bool operator==(
const iterator& it) {
467 return _list == it._list and _n == it._n;
470 constexpr bool operator!=(
const iterator& it) {
471 return _list != it._list or _n != it._n;
495 if (rhs._list->_next(rhs._n) < rhs._list->
capacity()) {
508 constexpr const value_t& operator*() {
509 return *(_list->_table[_n].value);
512 constexpr const value_t* operator->() {
513 return &*(_list->_table[_n].value);
517 return _list == it._list and _n == it._n;
521 return _list != it._list or _n != it._n;
538 size_t _root, _last, _size;
542 constexpr void _expand() {
556 constexpr node(
size_t p,
size_t n)
562 constexpr node(
size_t p,
size_t n,
value_t&& val)
568 constexpr ~node() =
default;
570 constexpr void clear() {
577 constexpr size_t _next(
size_t n)
const {
578 return _table[n].next;
581 constexpr size_t _prev(
size_t n)
const {
582 return _table[n].prev;
585 constexpr size_t _walk(
size_t i)
const {
587 if (n ==
npos)
return n;
588 while (i > 0 && n !=
npos) {
594 constexpr size_t _next_free() {
595 if (not _freed.
empty()) {
596 size_t n = _freed.
back();
603 template<
typename...ArgsT>
604 constexpr size_t _insert(
size_t n, ArgsT&&...args) {
609 size_t i = _next_free();
611 fennec::construct(&_table[i].value, fennec::forward<ArgsT>(args)...);
614 _table[i].prev =
npos;
615 _table[i].next =
npos;
621 _table[_last].next = i;
622 _table[i].prev = _last;
623 _table[i].next =
npos;
628 _table[i].prev = _prev(n);
631 _root = n == _root ? i : _root;
635 constexpr void _erase(
size_t n) {
636 if (n ==
npos)
return;
638 fennec::destruct(&_table[n].value);
642 size_t prev = _prev(n);
643 size_t next = _next(n);
646 _table[prev].next = next;
650 _table[next].prev = prev;
653 _root = (n == _root) ? next : _root;
654 _last = (n == _last) ? prev : _last;
This header contains structures and classes related to allocating blocks of memory.
Iterator Class for Const Access.
Definition list.h:487
Iterator Class.
Definition list.h:440
A header containing the definition for a dynamically allocated array.
A header containing the definition for a linked list of values.
constexpr genType min(genType x, genType y)
Returns if otherwise it returns .
Definition common.h:688
A header containing the definition for a container with an optionally present variable.
Container to hold a memory allocation.
Definition allocator.h:285
constexpr size_t capacity() const
Getter for the number of elements n of type T that the allocation can hold.
Definition allocator.h:617
constexpr void deallocate() noexcept
Release the block of memory.
Definition allocator.h:505
constexpr void creallocate(size_t n, align_t align=zero< align_t >()) noexcept
Reallocate the block with a new size. Contents are copied to the new allocation.
Definition allocator.h:541
typename _rebind< Alloc, TypeT >::type rebind
Rebinds the allocator type to produce an element type of type TypeT
Definition allocator.h:134
Wrapper for dynamically sized and allocated arrays.
Definition dynarray.h:64
constexpr bool empty() const
Definition dynarray.h:343
constexpr void pop_back()
Erase last element.
Definition dynarray.h:505
constexpr void push_back(const TypeT &val)
Push Back Copy.
Definition dynarray.h:483
constexpr TypeT & back()
Definition dynarray.h:387
Data Structure defining lists of elements.
Definition list.h:67
constexpr size_t capacity() const
Definition list.h:175
constexpr const_iterator begin() const
Const C++ Iterator Specification begin()
Definition list.h:425
constexpr size_t push_back(value_t &&x)
Push Back Move.
Definition list.h:349
constexpr size_t emplace_front(ArgsT &&...args)
Emplace Front.
Definition list.h:335
constexpr value_t & operator[](int i)
Array Access Operator.
Definition list.h:199
constexpr iterator begin()
C++ Iterator Specification begin()
Definition list.h:411
constexpr list & operator=(const list &l)
Copy Assignment Operator.
Definition list.h:138
constexpr void pop_front()
Pop Front, erases first element.
Definition list.h:380
static constexpr size_t npos
Constant representing a non-existant position.
Definition list.h:80
constexpr const value_t & back() const
Const Access Back Element.
Definition list.h:243
TypeT value_t
Alias for the value type.
Definition list.h:78
constexpr value_t & back()
Access Back Element.
Definition list.h:236
constexpr list(const list &l)
Copy Constructor, copies all elements in l with optimized layout.
Definition list.h:100
constexpr bool empty() const
Definition list.h:181
constexpr void clear()
Clears the list, destructing all elements in order.
Definition list.h:392
constexpr iterator end()
C++ Iterator Specification end()
Definition list.h:418
typename allocator_traits< Alloc >::template rebind< node > alloc_t
Alias for the allocator type, rebound to list nodes.
Definition list.h:76
constexpr list & operator=(list &&l) noexcept
Move Assignment Operator.
Definition list.h:150
constexpr const value_t & front() const
Const Access Front Element.
Definition list.h:229
constexpr size_t push_back(const value_t &x)
Push Back Copy.
Definition list.h:342
constexpr const_iterator end() const
Const C++ Iterator Specification end()
Definition list.h:432
constexpr size_t push_front(const value_t &x)
Push Front Copy.
Definition list.h:319
constexpr size_t insert(const iterator &it, value_t &&x)
Move Insertion.
Definition list.h:271
constexpr size_t insert(size_t i, value_t &&x)
Move Insertion.
Definition list.h:294
constexpr ~list()
Destructor, destructs all elements then releases the allocation.
Definition list.h:121
constexpr size_t size() const
Definition list.h:169
constexpr value_t & front()
Access Front Element.
Definition list.h:222
constexpr size_t push_front(value_t &&x)
Push Front Move.
Definition list.h:326
constexpr size_t insert(const iterator &it, const value_t &x)
Copy Insertion.
Definition list.h:261
constexpr list(list &&l) noexcept
Move Constructor, takes ownership of the list.
Definition list.h:111
constexpr size_t emplace(size_t i, ArgsT &&...args)
Emplace Insertion.
Definition list.h:309
constexpr list()
Default Constructor, initializes an empty list.
Definition list.h:93
constexpr void pop_back()
Pop Back, erases first element.
Definition list.h:386
constexpr void erase(size_t i)
Erase Element.
Definition list.h:365
constexpr size_t emplace_back(ArgsT &&...args)
Emplace Back.
Definition list.h:358
constexpr void erase(const iterator &it)
Erase Element.
Definition list.h:374
constexpr size_t insert(size_t i, const value_t &x)
Copy Insertion.
Definition list.h:281
constexpr const value_t & operator[](int i) const
Const Array Access Operator.
Definition list.h:212
Structure to hold an optional value.
Definition optional.h:51
constexpr T && forward(remove_reference_t< T > &x) noexcept
forwards reference types to extend their lifetime
Definition utility.h:75
constexpr remove_reference_t< T > && move(T &&x) noexcept
produces an x-value type to indicate x may be "moved"
Definition utility.h:92