1#ifndef NEFORCE_CORE_CONTAINER_LIST_HPP__
2#define NEFORCE_CORE_CONTAINER_LIST_HPP__
18NEFORCE_BEGIN_NAMESPACE__
54 template <
typename... Args>
68template <
bool IsConst,
typename List>
69struct list_iterator :
iiterator<list_iterator<IsConst, List>> {
73 using size_type =
typename container_type::size_type;
77 typename container_type::reference>;
79 typename container_type::pointer>;
84 node_type* current_ =
nullptr;
88 list_iterator() noexcept = default;
89 ~list_iterator() = default;
91 list_iterator(const list_iterator&) noexcept = default;
92 list_iterator& operator=(const list_iterator&) noexcept = default;
93 list_iterator(list_iterator&&) noexcept = default;
94 list_iterator& operator=(list_iterator&&) noexcept = default;
110 NEFORCE_DEBUG_VERIFY(current_ && container_,
"Attempting to dereference on a null pointer");
111 NEFORCE_DEBUG_VERIFY(current_ != container_->head_,
"Attempting to dereference out of boundary");
112 return current_->data;
119 NEFORCE_DEBUG_VERIFY(current_ && container_,
"Attempting to increment a null pointer");
120 NEFORCE_DEBUG_VERIFY(current_ != container_->head_,
"Attempting to increment out of boundary");
121 current_ = current_->next;
128 NEFORCE_DEBUG_VERIFY(current_ && container_,
"Attempting to decrement a null pointer");
129 NEFORCE_DEBUG_VERIFY(current_->prev != container_->head_,
"Attempting to decrement out of boundary");
130 current_ = current_->prev;
139 NEFORCE_DEBUG_VERIFY(container_ == other.container_,
140 "Attempting distance_to on iterators from different containers");
142 const node_type* p = current_;
143 while (p != other.current_) {
144 NEFORCE_DEBUG_VERIFY(p != container_->head_,
"Attempting distance_to past end iterator");
156 NEFORCE_NODISCARD
bool equal_to(
const list_iterator& rhs)
const noexcept {
157 NEFORCE_DEBUG_VERIFY(container_ == rhs.container_,
"Attempting to equal to a different container");
158 return current_ == rhs.current_;
165 NEFORCE_NODISCARD node_type*
base() const noexcept {
return current_; }
184template <
typename T,
typename Alloc = allocator<list_node<T>>>
188 static_assert(
is_object_v<T>,
"list only contains object types.");
206 using link_type = node_type*;
208 link_type head_ =
nullptr;
211 template <
bool,
typename>
223 template <
typename... Args>
224 link_type create_node(Args&&... args) {
225 link_type p = pair_.
get_base().allocate();
229 pair_.get_base().deallocate(p);
243 pair_.get_base().deallocate(p);
252 head_ = pair_.
get_base().allocate();
256 pair_.get_base().deallocate(head_);
259 head_->prev = head_->next = head_;
268 list() { list::init_header(); }
285 while ((n--) != 0U) {
300 template <
typename Iterator, enable_if_t<is_iter_v<Iterator>,
int> = 0>
301 list(Iterator first, Iterator last) {
304 while (first != last) {
318 list(std::initializer_list<T> ilist) :
383 link_type p = head_->next;
387 list::destroy_node(q);
389 list::destroy_node(head_);
480 NEFORCE_NODISCARD
bool empty() const noexcept {
return head_->next == head_; }
487 NEFORCE_DEBUG_VERIFY(!
empty(),
"front called on empty list");
488 return head_->next->data;
496 NEFORCE_DEBUG_VERIFY(!
empty(),
"front called on empty list");
497 return head_->next->data;
505 NEFORCE_DEBUG_VERIFY(!
empty(),
"back called on empty list");
506 return head_->prev->data;
514 NEFORCE_DEBUG_VERIFY(!
empty(),
"back called on empty list");
515 return head_->prev->data;
525 template <
typename... Args>
527 link_type temp = list::create_node(_NEFORCE
forward<Args>(args)...);
542 template <
typename... Args>
553 template <
typename... Args>
608 template <
typename Iterator, enable_if_t<is_iter_v<Iterator>,
int> = 0>
609 void assign(Iterator first, Iterator last) {
646 template <
typename Iterator, enable_if_t<is_iter_v<Iterator>,
int> = 0>
652 link_type original_prev = position.
base()->
prev;
653 link_type current_prev = original_prev;
654 link_type first_inserted =
nullptr;
657 while (first != last) {
658 link_type temp = list::create_node(*first);
659 temp->
prev = current_prev;
661 current_prev->
next = temp;
663 if (first_inserted ==
nullptr) {
664 first_inserted = temp;
671 if (first_inserted !=
nullptr) {
672 link_type to_delete = first_inserted;
673 while (to_delete != position.
base()) {
675 list::destroy_node(to_delete);
679 original_prev->
next = position.
base();
680 position.
base()->
prev = original_prev;
708 link_type original_prev = position.
base()->
prev;
709 link_type current_prev = original_prev;
710 link_type first_inserted =
nullptr;
713 while ((n--) != 0U) {
714 link_type temp = list::create_node(value);
715 temp->
prev = current_prev;
717 current_prev->
next = temp;
719 if (first_inserted ==
nullptr) {
720 first_inserted = temp;
726 if (first_inserted !=
nullptr) {
727 link_type to_delete = first_inserted;
728 while (to_delete != position.
base()) {
730 list::destroy_node(to_delete);
734 original_prev->
next = position.
base();
735 position.
base()->
prev = original_prev;
750 link_type ret = position.base()->next;
751 position.base()->prev->next = position.base()->next;
752 position.base()->next->prev = position.base()->prev;
753 list::destroy_node(position.base());
765 while (first != last) {
777 link_type cur = head_->next;
778 while (cur != head_) {
779 link_type temp = cur;
781 list::destroy_node(temp);
793 _NEFORCE
swap(head_, other.head_);
794 _NEFORCE
swap(pair_, other.pair_);
807 if (position.
base() == last.base()) {
810 last.base()->prev->next = position.
base();
813 link_type tmp = position.
base()->
prev;
824 template <
typename Pred>
827 while (iter != last) {
841 return list::remove_if([&](
const T& other) ->
bool {
return other == value; });
852 if (!other.
empty()) {
856 other.pair_.
value = 0;
893 for (
iterator it = first; it != last; ++it) {
898 other.pair_.
value -= n;
910 template <
typename Pred>
915 while (first1 != last1 && first2 != last2) {
916 if (!pred(*first2, *first1)) {
928 if (first2 != last2) {
932 other.pair_.
value = 0;
949 link_type current = head_;
952 current = current->
prev;
953 }
while (current != head_);
963 template <
typename Pred>
971 if (pred(*current, *
next)) {
992 template <
typename Pred>
997 link_type p = head_->next->next;
1001 while (
prev != head_ && pred(temp,
prev->data)) {
1005 prev->next->data = temp;
1022 while ((position--) != 0U) {
1035 while ((position--) != 0U) {
1076#ifdef NEFORCE_STANDARD_17
1077template <
typename Iterator,
typename Alloc>
1083NEFORCE_END_NAMESPACE__
const_iterator cbegin() const noexcept
获取常量起始迭代器
const_reference operator[](const size_type position) const
常量下标访问操作符
void assign(Iterator first, Iterator last)
范围赋值
bool empty() const noexcept
检查是否为空
const_reference back() const noexcept
访问最后一个常量元素
void swap(list &other) noexcept(is_nothrow_swappable_v< allocator_type >)
交换两个链表的内容
void push_front(T &&value)
在开头移动插入元素
list & operator=(std::initializer_list< T > ilist)
初始化列表赋值运算符
iterator insert(iterator position, T &&value)
在指定位置移动插入元素
const_iterator cend() const noexcept
获取常量结束迭代器
const_reverse_iterator rbegin() const noexcept
获取常量反向起始迭代器
void splice(iterator position, list &other, iterator first, iterator last)
拼接范围内的元素
_NEFORCE reverse_iterator< const_iterator > const_reverse_iterator
常量反向迭代器类型
list(std::initializer_list< T > ilist)
初始化列表构造函数
iterator begin() noexcept
void pop_front() noexcept
移除开头元素
list & operator=(list &&other) noexcept(is_nothrow_swappable_v< compressed_pair< allocator_type, size_type > > &&is_nothrow_destructible_v< node_type >)
移动赋值运算符
void transfer(iterator position, iterator first, iterator last)
传输元素
iterator emplace_front(Args &&... args)
在开头构造元素
const_iterator begin() const noexcept
获取常量起始迭代器
iterator emplace_back(Args &&... args)
在末尾构造元素
reverse_iterator rend() noexcept
获取反向结束迭代器
void push_front(const T &value)
在开头拷贝插入元素
void sort()
对链表进行排序(默认使用小于比较)
void merge(list &other)
合并两个有序链表(默认使用小于比较)
const_iterator end() const noexcept
获取常量结束迭代器
void unique_if(Pred pred) noexcept
移除连续的重复元素
void sort_if(Pred pred)
对链表进行排序
void reverse() noexcept
反转链表
list(const list &other)
拷贝构造函数
ptrdiff_t difference_type
差值类型
void insert(iterator position, size_type n, const T &value)
插入n个指定值的元素
void push_back(T &&value)
在末尾移动插入元素
size_type max_size() const noexcept
获取最大可能大小
void pop_back() noexcept
移除末尾元素
void insert(iterator position, std::initializer_list< T > ilist)
初始化列表插入
void unique() noexcept
移除连续的重复元素(默认使用等于比较)
list(Iterator first, Iterator last)
范围构造函数
void push_back(const T &value)
在末尾拷贝插入元素
const_reverse_iterator rend() const noexcept
获取常量反向结束迭代器
bool less_than(const list &rhs) const noexcept(noexcept(_NEFORCE lexicographical_compare(cbegin(), cend(), rhs.cbegin(), rhs.cend())))
小于比较操作符
list(size_type n, const T &value)
构造包含n个指定值元素的链表
void assign(std::initializer_list< T > ilist)
初始化列表赋值
reference operator[](const size_type position)
下标访问操作符
reference at(size_type position)
索引访问
list & operator=(const list &other)
拷贝赋值运算符
void remove_if(Pred pred)
根据谓词移除元素
reference front() noexcept
访问第一个元素
list_iterator< true, list > const_iterator
常量迭代器类型
reverse_iterator rbegin() noexcept
获取反向起始迭代器
const_reference front() const noexcept
访问第一个常量元素
list(list &&other) noexcept(is_nothrow_swappable_v< compressed_pair< allocator_type, size_type > >)
移动构造函数
iterator insert(iterator position, const T &value)
在指定位置拷贝插入元素
void insert(iterator position, Iterator first, Iterator last)
范围插入
iterator erase(iterator first, iterator last) noexcept(is_nothrow_destructible_v< node_type >)
删除指定范围内的元素
Alloc allocator_type
分配器类型
reference back() noexcept
访问最后一个元素
void splice(iterator position, list &other, iterator iter)
拼接单个元素
void splice(iterator position, list &other)
拼接整个链表
const T * const_pointer
常量指针类型
list_iterator< false, list > iterator
迭代器类型
void remove(const T &value)
移除指定值的元素
iterator erase(iterator position) noexcept(is_nothrow_destructible_v< node_type >)
删除指定位置的元素
void assign(const size_type n, const T &value)
赋值n个指定值的元素
const_reverse_iterator crend() const noexcept
获取常量反向结束迭代器
const_reverse_iterator crbegin() const noexcept
获取常量反向起始迭代器
iterator emplace(iterator position, Args &&... args)
在指定位置构造元素
const T & const_reference
常量引用类型
bool equal_to(const list &rhs) const noexcept(noexcept(_NEFORCE equal(cbegin(), cend(), rhs.cbegin())))
_NEFORCE reverse_iterator< iterator > reverse_iterator
反向迭代器类型
void merge_if(list &other, Pred pred)
合并两个有序链表
const_reference at(size_type position) const
常量索引访问
size_type size() const noexcept
获取当前元素数量
void clear() noexcept(is_nothrow_destructible_v< node_type >)
清空链表
list(size_type n)
构造包含n个默认构造元素的链表
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr bool is_object_v
is_object的便捷变量模板
constexpr bool lexicographical_compare(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, Compare comp) noexcept(noexcept(++first1) &&noexcept(++first2) &&noexcept(comp(*first1, *first2)) &&noexcept(first1==last1 &&first2 !=last2))
字典序比较两个范围
constexpr bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, BinaryPredicate binary_pred) noexcept(noexcept(++first1) &&noexcept(++first2) &&noexcept(binary_pred(*first1, *first2)))
比较两个范围是否相等
constexpr void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
constexpr T * construct(T *ptr, Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
在指定内存位置构造对象
constexpr Iterator next(Iterator iter, iter_difference_t< Iterator > n=1)
获取迭代器的后一个位置
constexpr Iterator prev(Iterator iter, iter_difference_t< Iterator > n=1)
获取迭代器的前一个位置
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
void swap()=delete
删除无参数的swap重载
constexpr bool is_nothrow_swappable_v
is_nothrow_swappable的便捷变量模板
constexpr bool is_allocator_v
is_allocator的便捷变量模板
constexpr T initialize() noexcept(is_nothrow_default_constructible< T >::value)
返回类型T的默认初始化值
constexpr bool is_nothrow_default_constructible_v
is_nothrow_default_constructible的便捷变量模板
constexpr bool is_nothrow_constructible_v
is_nothrow_constructible的便捷变量模板
constexpr bool is_nothrow_destructible_v
is_nothrow_destructible的便捷变量模板
typename conditional< Test, T1, T2 >::type conditional_t
conditional的便捷别名
constexpr bool is_same_v
is_same的便捷变量模板
constexpr compressed_pair & get_base() &noexcept
获取基类引用
void decrement() noexcept
递减操作
reference dereference() const noexcept
解引用操作实现
void increment() noexcept
递增操作
node_type * base() const noexcept
获取底层节点指针
conditional_t< IsConst, typename container_type::const_pointer, typename container_type::pointer > pointer
指针类型
conditional_t< IsConst, typename container_type::const_reference, typename container_type::reference > reference
引用类型
typename container_type::value_type value_type
值类型
bidirectional_iterator_tag iterator_category
迭代器类别
typename container_type::difference_type difference_type
差值类型
bool equal_to(const list_iterator &rhs) const noexcept
相等比较
typename container_type::size_type size_type
大小类型
const container_type * container() const noexcept
获取关联容器
difference_type distance_to(const list_iterator &other) const noexcept
计算当前迭代器到另一个迭代器的距离
list_node() noexcept(is_nothrow_default_constructible_v< T >)
默认构造函数
list_node(Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
参数构造