Compare commits
1 Commits
ad406e9647
...
57de76ca09
| Author | SHA1 | Date |
|---|---|---|
|
|
57de76ca09 |
|
|
@ -8,6 +8,8 @@
|
||||||
#include <concepts>
|
#include <concepts>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include "allocator.h"
|
||||||
|
#include "CompileOptional.h"
|
||||||
|
|
||||||
namespace cc {
|
namespace cc {
|
||||||
|
|
||||||
|
|
@ -15,12 +17,13 @@ namespace cc {
|
||||||
class const_list_node;
|
class const_list_node;
|
||||||
|
|
||||||
template<typename N>
|
template<typename N>
|
||||||
|
requires std::derived_from<N, const_list_node>
|
||||||
class _const_list_iterator_base {
|
class _const_list_iterator_base {
|
||||||
protected:
|
protected:
|
||||||
N *_curr = nullptr;
|
N *_curr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr _const_list_iterator_base() noexcept = default;
|
constexpr _const_list_iterator_base() noexcept : _curr() {}
|
||||||
constexpr explicit _const_list_iterator_base(N *curr) noexcept : _curr(curr) {}
|
constexpr explicit _const_list_iterator_base(N *curr) noexcept : _curr(curr) {}
|
||||||
constexpr _const_list_iterator_base(const _const_list_iterator_base& other) noexcept : _curr(other._curr) {}
|
constexpr _const_list_iterator_base(const _const_list_iterator_base& other) noexcept : _curr(other._curr) {}
|
||||||
|
|
||||||
|
|
@ -29,8 +32,8 @@ namespace cc {
|
||||||
|
|
||||||
constexpr virtual _const_list_iterator_base &operator++() noexcept = 0;
|
constexpr virtual _const_list_iterator_base &operator++() noexcept = 0;
|
||||||
constexpr virtual _const_list_iterator_base &operator--() noexcept = 0;
|
constexpr virtual _const_list_iterator_base &operator--() noexcept = 0;
|
||||||
constexpr virtual _const_list_iterator_base &operator++(int) & noexcept = 0;
|
constexpr virtual _const_list_iterator_base operator++(int) & noexcept = 0;
|
||||||
constexpr virtual _const_list_iterator_base &operator--(int) & noexcept = 0;
|
constexpr virtual _const_list_iterator_base operator--(int) & noexcept = 0;
|
||||||
|
|
||||||
constexpr bool operator==(const _const_list_iterator_base &other) { return _curr == other._curr; }
|
constexpr bool operator==(const _const_list_iterator_base &other) { return _curr == other._curr; }
|
||||||
};
|
};
|
||||||
|
|
@ -66,44 +69,45 @@ namespace cc {
|
||||||
constexpr _const_const_list_iterator operator--(int) & noexcept override { auto tmp = *this; _Base::_curr == _Base::_curr->_prev; return tmp; }
|
constexpr _const_const_list_iterator operator--(int) & noexcept override { auto tmp = *this; _Base::_curr == _Base::_curr->_prev; return tmp; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Node>
|
template<typename N>
|
||||||
|
requires std::derived_from<N, const_list_node>
|
||||||
class const_list {
|
class const_list {
|
||||||
private:
|
|
||||||
static consteval void asserts() {
|
|
||||||
static_assert(std::is_base_of_v<const_list_node<Node>, Node>, "Can only create const_list with elements derived from const_list_node");
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using value_type = Node;
|
using value_type = N;
|
||||||
using size_type = std::size_t;
|
using size_type = std::size_t;
|
||||||
using difference_type = std::ptrdiff_t;
|
using difference_type = std::ptrdiff_t;
|
||||||
using reference = value_type&;
|
using reference = value_type&;
|
||||||
using const_reference = const value_type&;
|
using const_reference = const value_type&;
|
||||||
using pointer = value_type *;
|
using pointer = value_type *;
|
||||||
using const_pointer = const value_type *;
|
using const_pointer = const value_type *;
|
||||||
using iterator = _const_list_iterator<Node>;
|
using iterator = _const_list_iterator<N>;
|
||||||
using const_iterator = _const_const_list_iterator<Node>;
|
using const_iterator = _const_const_list_iterator<N>;
|
||||||
using reverse_iterator = std::reverse_iterator<iterator>;
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Node *_start = nullptr;
|
const_list_node *_start;
|
||||||
Node *_end = nullptr;
|
const_list_node *_end;
|
||||||
std::size_t _size = 0;
|
std::size_t _size;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
constexpr const_list() noexcept { asserts(); };
|
constexpr const_list() noexcept = default;
|
||||||
constexpr const_list(const const_list&) = delete;
|
constexpr explicit const_list(size_type count) noexcept;
|
||||||
|
constexpr const_list(size_type count, const value_type& value) noexcept;
|
||||||
|
template<std::input_iterator InputIt>
|
||||||
|
constexpr const_list(InputIt first, InputIt last);
|
||||||
|
constexpr const_list(const const_list& other) noexcept;
|
||||||
constexpr const_list(const_list&& other) noexcept;
|
constexpr const_list(const_list&& other) noexcept;
|
||||||
constexpr const_list(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept;
|
constexpr const_list(std::initializer_list<value_type> init) noexcept;
|
||||||
|
|
||||||
constexpr ~const_list();
|
constexpr ~const_list();
|
||||||
|
|
||||||
constexpr const_list& operator=(const const_list&) = delete;
|
constexpr const_list& operator=(const const_list& other);
|
||||||
constexpr const_list& operator=(const_list&& other) noexcept;
|
constexpr const_list& operator=(const_list&& other);
|
||||||
constexpr const_list& operator=(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept;
|
constexpr const_list& operator=(std::initializer_list<N> init);
|
||||||
|
|
||||||
constexpr void assign(size_type count, const value_type& value) noexcept;
|
constexpr void assign(size_type count, const value_type& value) noexcept;
|
||||||
template<std::input_iterator InputIt>
|
template<std::input_iterator InputIt>
|
||||||
|
|
@ -203,24 +207,20 @@ namespace cc {
|
||||||
|
|
||||||
template<typename D>
|
template<typename D>
|
||||||
class const_list_node {
|
class const_list_node {
|
||||||
|
static_assert(std::derived_from)
|
||||||
private:
|
private:
|
||||||
static consteval void asserts() {
|
const_list_node *_prev;
|
||||||
static_assert(std::is_base_of_v<const_list_node<D>, D>, "Template parameter has to be a subclass of const_list_node<*self*>");
|
const_list_node *_next;
|
||||||
}
|
const_list<D> *_owner;
|
||||||
|
|
||||||
private:
|
friend class const_list<const_list_node>;
|
||||||
const_list_node *_prev = nullptr;
|
friend class _const_list_iterator_base<const_list_node>;
|
||||||
const_list_node *_next = nullptr;
|
|
||||||
const_list<D> *_owner = nullptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
constexpr const_list_node() noexcept { asserts(); }
|
|
||||||
virtual constexpr ~const_list_node();
|
|
||||||
|
|
||||||
friend class const_list<D>;
|
|
||||||
friend class _const_list_iterator_base<D>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<std::input_iterator InputIt>
|
||||||
|
const_list(InputIt, InputIt) -> const_list<typename std::iterator_traits<InputIt>::value_type>;
|
||||||
|
|
||||||
template<typename N1, typename N2>
|
template<typename N1, typename N2>
|
||||||
constexpr bool operator==(const const_list<N1>& lhs,
|
constexpr bool operator==(const const_list<N1>& lhs,
|
||||||
const const_list<N2>& rhs)
|
const const_list<N2>& rhs)
|
||||||
|
|
@ -254,74 +254,6 @@ namespace cc {
|
||||||
c.remove_if(pred);
|
c.remove_if(pred);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Node>
|
|
||||||
constexpr const_list<Node>::const_list(const_list &&other) noexcept
|
|
||||||
: _start(other._start), _end(other._end), _size(other._size)
|
|
||||||
{
|
|
||||||
asserts();
|
|
||||||
|
|
||||||
for (auto node : other) {
|
|
||||||
node._owner = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
other._size = 0;
|
|
||||||
other._start = nullptr;
|
|
||||||
other._end = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Node>
|
|
||||||
constexpr const_list<Node>::const_list(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept
|
|
||||||
: _start(std::addressof(init.begin()->get())),
|
|
||||||
_end(std::addressof(std::prev(init.end())->get())),
|
|
||||||
_size(init.size())
|
|
||||||
{
|
|
||||||
asserts();
|
|
||||||
|
|
||||||
auto prev = init.begin();
|
|
||||||
prev->get()._owner = this;
|
|
||||||
|
|
||||||
for (auto it = std::next(init.begin()); it != init.end(); ++it) {
|
|
||||||
prev->get()._next = std::addressof(it->get());
|
|
||||||
it->get()._prev = std::addressof(prev->get());
|
|
||||||
it->get()._owner = this;
|
|
||||||
|
|
||||||
prev = it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Node>
|
|
||||||
constexpr const_list<Node>::~const_list()
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Node>
|
|
||||||
constexpr const_list<Node> &const_list<Node>::operator=(const_list &&other) noexcept
|
|
||||||
{
|
|
||||||
for (auto node : other) {
|
|
||||||
node._owner = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
other._size = 0;
|
|
||||||
other._start = nullptr;
|
|
||||||
other._end = nullptr;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Node>
|
|
||||||
constexpr const_list<Node> &const_list<Node>::operator=(std::initializer_list<std::reference_wrapper<value_type>> init) noexcept
|
|
||||||
{
|
|
||||||
*this = const_list(init);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename D>
|
|
||||||
constexpr const_list_node<D>::~const_list_node()
|
|
||||||
{
|
|
||||||
_owner->remove(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // cc
|
} // cc
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue