215 lines
8.1 KiB
C++
215 lines
8.1 KiB
C++
/**
|
|
This file is a part of rexy's general purpose library
|
|
Copyright (C) 2022 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef REXY_LLIST_HPP
|
|
#define REXY_LLIST_HPP
|
|
|
|
#include <cstddef> //size_t, ptrdiff_t
|
|
#include "allocator.hpp" //TODO
|
|
#include "detail/hasallocator.hpp"
|
|
#include <iterator> //bidirectional_iterator_tag, reverse_iterator
|
|
#include <memory> //allocator_traits
|
|
#include <initializer_list>
|
|
#include <utility> //pair
|
|
|
|
namespace rexy{
|
|
|
|
template<class T, class Alloc>
|
|
class list;
|
|
|
|
namespace detail{
|
|
struct node_base{
|
|
node_base* next = nullptr;
|
|
node_base* prev = nullptr;
|
|
};
|
|
|
|
template<class T>
|
|
struct list_iterator;
|
|
template<class T>
|
|
struct const_list_iterator;
|
|
template<class T>
|
|
struct node;
|
|
}
|
|
|
|
template<class T, class Alloc = rexy::allocator<T>>
|
|
class list : protected detail::hasallocator<typename std::allocator_traits<Alloc>::template rebind_alloc<detail::node<T>>>
|
|
{
|
|
private:
|
|
using node_allocator_type = detail::hasallocator<typename std::allocator_traits<Alloc>::template rebind_alloc<detail::node<T>>>;
|
|
|
|
public:
|
|
using value_type = T;
|
|
using allocator_type = Alloc;
|
|
using size_type = std::size_t;
|
|
using difference_type = std::ptrdiff_t;
|
|
using reference = value_type&;
|
|
using const_reference = const value_type&;
|
|
using pointer = value_type*;
|
|
using const_pointer = const value_type*;
|
|
using iterator = detail::list_iterator<T>;
|
|
using const_iterator = detail::const_list_iterator<T>;
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
public:
|
|
using node = detail::node<T>;
|
|
|
|
private:
|
|
detail::node_base m_sentinel = {&m_sentinel, &m_sentinel};
|
|
size_type m_size = 0;
|
|
|
|
public:
|
|
constexpr list(void) = default;
|
|
constexpr explicit list(const allocator_type& alloc);
|
|
REXY_CPP20_CONSTEXPR list(size_type count, const_reference value = value_type(), const allocator_type& = allocator_type());
|
|
REXY_CPP20_CONSTEXPR explicit list(size_type count, const allocator_type& alloc = allocator_type());
|
|
template<class InputIt>
|
|
REXY_CPP20_CONSTEXPR list(InputIt first, InputIt last, const allocator_type& alloc = allocator_type());
|
|
REXY_CPP20_CONSTEXPR list(const list& other, const allocator_type& alloc = allocator_type());
|
|
REXY_CPP20_CONSTEXPR list(list&& other, const allocator_type& alloc = allocator_type());
|
|
REXY_CPP20_CONSTEXPR list(std::initializer_list<value_type> l, const allocator_type& alloc = allocator_type());
|
|
|
|
REXY_CPP20_CONSTEXPR ~list(void);
|
|
|
|
REXY_CPP20_CONSTEXPR list& operator=(const list& other);
|
|
REXY_CPP20_CONSTEXPR list& operator=(list&& other);
|
|
REXY_CPP20_CONSTEXPR list& operator=(std::initializer_list<value_type> l);
|
|
|
|
constexpr bool operator==(const list& other)const noexcept;
|
|
#if __cpp_impl_three_way_comparison
|
|
constexpr auto operator<=>(const list& other)const noexcept;
|
|
#else
|
|
constexpr bool operator!=(const list& other)const noexcept;
|
|
constexpr bool operator<(const list& other)const noexcept;
|
|
constexpr bool operator<=(const list& other)const noexcept;
|
|
constexpr bool operator>(const list& other)const noexcept;
|
|
constexpr bool operator>=(const list& other)const noexcept;
|
|
#endif
|
|
|
|
REXY_CPP20_CONSTEXPR void assign(size_type count, const_reference value);
|
|
template<class InputIt>
|
|
REXY_CPP20_CONSTEXPR void assign(InputIt first, InputIt last);
|
|
REXY_CPP20_CONSTEXPR void assign(std::initializer_list<value_type> l);
|
|
|
|
constexpr allocator_type get_allocator(void)const noexcept;
|
|
|
|
constexpr reference front(void);
|
|
constexpr const_reference front(void)const;
|
|
constexpr reference back(void);
|
|
constexpr const_reference back(void)const;
|
|
|
|
constexpr iterator begin(void)noexcept;
|
|
constexpr const_iterator begin(void)const noexcept;
|
|
constexpr const_iterator cbegin(void)const noexcept;
|
|
constexpr iterator end(void)noexcept;
|
|
constexpr const_iterator end(void)const noexcept;
|
|
constexpr const_iterator cend(void)const noexcept;
|
|
|
|
constexpr iterator next(iterator i)noexcept;
|
|
constexpr const_iterator next(const_iterator i)noexcept;
|
|
|
|
constexpr iterator prev(iterator i)noexcept;
|
|
constexpr const_iterator prev(const_iterator i)noexcept;
|
|
|
|
constexpr reverse_iterator rbegin(void)noexcept;
|
|
constexpr const_reverse_iterator rbegin(void)const noexcept;
|
|
constexpr const_reverse_iterator crbegin(void)const noexcept;
|
|
constexpr reverse_iterator rend(void)noexcept;
|
|
constexpr const_reverse_iterator rend(void)const noexcept;
|
|
constexpr const_reverse_iterator crend(void)const noexcept;
|
|
|
|
constexpr bool empty(void)const noexcept;
|
|
constexpr size_type size(void)const noexcept;
|
|
constexpr size_type max_size(void)const noexcept;
|
|
|
|
REXY_CPP20_CONSTEXPR void clear(void)noexcept;
|
|
|
|
REXY_CPP20_CONSTEXPR iterator insert(const_iterator pos, const_reference value);
|
|
REXY_CPP20_CONSTEXPR iterator insert(const_iterator pos, value_type&& value);
|
|
REXY_CPP20_CONSTEXPR iterator insert(const_iterator pos, size_type count, const_reference value);
|
|
template<class InputIt>
|
|
REXY_CPP20_CONSTEXPR iterator insert(const_iterator pos, InputIt first, InputIt last);
|
|
REXY_CPP20_CONSTEXPR iterator insert(const_iterator pos, std::initializer_list<value_type> l);
|
|
|
|
template<class... Args>
|
|
REXY_CPP20_CONSTEXPR iterator emplace(const_iterator pos, Args&&... args);
|
|
|
|
REXY_CPP20_CONSTEXPR iterator erase(const_iterator pos);
|
|
REXY_CPP20_CONSTEXPR iterator erase(const_iterator first, const_iterator last);
|
|
|
|
REXY_CPP20_CONSTEXPR reference push_back(const_reference value);
|
|
REXY_CPP20_CONSTEXPR reference push_back(value_type&& value);
|
|
template<class... Args>
|
|
REXY_CPP20_CONSTEXPR reference emplace_back(Args&&... args);
|
|
|
|
REXY_CPP20_CONSTEXPR void pop_back(void);
|
|
|
|
REXY_CPP20_CONSTEXPR reference push_front(const_reference value);
|
|
REXY_CPP20_CONSTEXPR reference push_front(value_type&& value);
|
|
template<class... Args>
|
|
REXY_CPP20_CONSTEXPR reference emplace_front(Args&&... args);
|
|
|
|
REXY_CPP20_CONSTEXPR void pop_front(void);
|
|
|
|
REXY_CPP20_CONSTEXPR void resize(size_type count);
|
|
REXY_CPP20_CONSTEXPR void resize(size_type count, value_type value);
|
|
|
|
REXY_CPP20_CONSTEXPR void swap(list& other);
|
|
|
|
REXY_CPP20_CONSTEXPR void merge(list&& other);
|
|
template<class Comp>
|
|
REXY_CPP20_CONSTEXPR void merge(list&& other, Comp comp);
|
|
|
|
REXY_CPP20_CONSTEXPR void splice(const_iterator pos, list&& other);
|
|
REXY_CPP20_CONSTEXPR void splice(const_iterator pos, list&& other, const_iterator it);
|
|
REXY_CPP20_CONSTEXPR void splice(const_iterator pos, list&& other, const_iterator first, const_iterator last);
|
|
|
|
REXY_CPP20_CONSTEXPR size_type remove(const_reference value);
|
|
template<class UnaryPred>
|
|
REXY_CPP20_CONSTEXPR size_type remove_if(UnaryPred p);
|
|
|
|
REXY_CPP20_CONSTEXPR void reverse(void)noexcept;
|
|
|
|
REXY_CPP20_CONSTEXPR size_type unique(void);
|
|
template<class BinaryPred>
|
|
REXY_CPP20_CONSTEXPR size_type unique(BinaryPred p);
|
|
|
|
REXY_CPP20_CONSTEXPR void sort(void);
|
|
template<class Comp>
|
|
REXY_CPP20_CONSTEXPR void sort(Comp comp);
|
|
private:
|
|
template<class InputIt>
|
|
REXY_CPP20_CONSTEXPR void iterator_initialize_(InputIt first, InputIt last);
|
|
REXY_CPP20_CONSTEXPR void constant_initialize_(size_type count, const_reference value);
|
|
REXY_CPP20_CONSTEXPR void default_initialize_(size_type count);
|
|
|
|
template<class Comp>
|
|
static iterator mergesort(iterator first, size_type firstlen, Comp comp);
|
|
static std::pair<iterator,size_type> ms_split(iterator it, size_type len);
|
|
|
|
static void insert_node_(detail::node_base* prev, detail::node_base* n);
|
|
static void remove_node_(detail::node_base* rm);
|
|
static detail::node_base* get_next_then_move_node_(detail::node_base* dest, detail::node_base* n);
|
|
};
|
|
|
|
}
|
|
|
|
#include "list.tpp"
|
|
|
|
#endif
|