aboutsummaryrefslogtreecommitdiff
path: root/include/list
diff options
context:
space:
mode:
Diffstat (limited to 'include/list')
-rw-r--r--include/list92
1 files changed, 57 insertions, 35 deletions
diff --git a/include/list b/include/list
index 800a1a3f5aa8..13f8a53bf25f 100644
--- a/include/list
+++ b/include/list
@@ -179,11 +179,7 @@ template <class T, class Alloc>
#include <__undef_min_max>
-#ifdef _LIBCPP_DEBUG
-# include <__debug>
-#else
-# define _LIBCPP_ASSERT(x, m) ((void)0)
-#endif
+#include <__debug>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
@@ -214,10 +210,13 @@ struct __list_node_base
pointer __next_;
_LIBCPP_INLINE_VISIBILITY
- __list_node_base()
- : __prev_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this))),
- __next_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this)))
- {}
+ __list_node_base() : __prev_(__self()), __next_(__self()) {}
+
+ _LIBCPP_INLINE_VISIBILITY
+ pointer __self()
+ {
+ return static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this));
+ }
};
template <class _Tp, class _VoidPtr>
@@ -753,20 +752,14 @@ __list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
swap(__sz(), __c.__sz());
swap(__end_, __c.__end_);
if (__sz() == 0)
- __end_.__next_ = __end_.__prev_ = static_cast<__node_pointer>(
- pointer_traits<__node_base_pointer>::pointer_to(__end_));
+ __end_.__next_ = __end_.__prev_ = __end_.__self();
else
- __end_.__prev_->__next_ = __end_.__next_->__prev_
- = static_cast<__node_pointer>(
- pointer_traits<__node_base_pointer>::pointer_to(__end_));
+ __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_.__self();
if (__c.__sz() == 0)
- __c.__end_.__next_ = __c.__end_.__prev_
- = static_cast<__node_pointer>(
- pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
+ __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_.__self();
else
- __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_
- = static_cast<__node_pointer>(
- pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
+ __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_.__self();
+
#if _LIBCPP_DEBUG_LEVEL >= 2
__libcpp_db* __db = __get_db();
__c_node* __cn1 = __db->__find_c_and_lock(this);
@@ -1059,7 +1052,9 @@ public:
#endif // _LIBCPP_DEBUG_LEVEL >= 2
private:
- static void __link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l);
+ static void __link_nodes (__node_pointer __p, __node_pointer __f, __node_pointer __l);
+ void __link_nodes_at_front(__node_pointer __f, __node_pointer __l);
+ void __link_nodes_at_back (__node_pointer __f, __node_pointer __l);
iterator __iterator(size_type __n);
template <class _Comp>
static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
@@ -1081,6 +1076,31 @@ list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_p
__l->__next_ = __p;
}
+// Link in nodes [__f, __l] at the front of the list
+template <class _Tp, class _Alloc>
+inline _LIBCPP_INLINE_VISIBILITY
+void
+list<_Tp, _Alloc>::__link_nodes_at_front(__node_pointer __f, __node_pointer __l)
+{
+ __f->__prev_ = base::__end_.__self();
+ __l->__next_ = base::__end_.__next_;
+ __l->__next_->__prev_ = __l;
+ base::__end_.__next_ = __f;
+}
+
+// Link in nodes [__f, __l] at the front of the list
+template <class _Tp, class _Alloc>
+inline _LIBCPP_INLINE_VISIBILITY
+void
+list<_Tp, _Alloc>::__link_nodes_at_back(__node_pointer __f, __node_pointer __l)
+{
+ __l->__next_ = base::__end_.__self();
+ __f->__prev_ = base::__end_.__prev_;
+ __f->__prev_->__next_ = __f;
+ base::__end_.__prev_ = __l;
+}
+
+
template <class _Tp, class _Alloc>
inline _LIBCPP_INLINE_VISIBILITY
typename list<_Tp, _Alloc>::iterator
@@ -1502,7 +1522,7 @@ list<_Tp, _Alloc>::push_front(const value_type& __x)
typedef __allocator_destructor<__node_allocator> _Dp;
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
- __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
+ __link_nodes_at_front(__hold.get(), __hold.get());
++base::__sz();
__hold.release();
}
@@ -1515,8 +1535,7 @@ list<_Tp, _Alloc>::push_back(const value_type& __x)
typedef __allocator_destructor<__node_allocator> _Dp;
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
- __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
- pointer_to(base::__end_)), __hold.get(), __hold.get());
+ __link_nodes_at_back(__hold.get(), __hold.get());
++base::__sz();
__hold.release();
}
@@ -1531,7 +1550,7 @@ list<_Tp, _Alloc>::push_front(value_type&& __x)
typedef __allocator_destructor<__node_allocator> _Dp;
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
- __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
+ __link_nodes_at_front(__hold.get(), __hold.get());
++base::__sz();
__hold.release();
}
@@ -1544,8 +1563,7 @@ list<_Tp, _Alloc>::push_back(value_type&& __x)
typedef __allocator_destructor<__node_allocator> _Dp;
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
- __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
- pointer_to(base::__end_)), __hold.get(), __hold.get());
+ __link_nodes_at_back(__hold.get(), __hold.get());
++base::__sz();
__hold.release();
}
@@ -1561,7 +1579,7 @@ list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
typedef __allocator_destructor<__node_allocator> _Dp;
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
- __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
+ __link_nodes_at_front(__hold.get(), __hold.get());
++base::__sz();
__hold.release();
}
@@ -1575,8 +1593,7 @@ list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
typedef __allocator_destructor<__node_allocator> _Dp;
unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
__node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
- __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
- pointer_to(base::__end_)), __hold.get(), __hold.get());
+ __link_nodes_at_back(__hold.get(), __hold.get());
++base::__sz();
__hold.release();
}
@@ -1826,8 +1843,7 @@ list<_Tp, _Alloc>::resize(size_type __n)
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
- __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
- pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
+ __link_nodes_at_back(__r.__ptr_, __e.__ptr_);
base::__sz() += __ds;
}
}
@@ -2038,14 +2054,18 @@ template <class _Tp, class _Alloc>
void
list<_Tp, _Alloc>::remove(const value_type& __x)
{
- for (iterator __i = begin(), __e = end(); __i != __e;)
+ list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
+ for (const_iterator __i = begin(), __e = end(); __i != __e;)
{
if (*__i == __x)
{
- iterator __j = _VSTD::next(__i);
+ const_iterator __j = _VSTD::next(__i);
for (; __j != __e && *__j == __x; ++__j)
;
- __i = erase(__i, __j);
+ __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j);
+ __i = __j;
+ if (__i != __e)
+ ++__i;
}
else
++__i;
@@ -2065,6 +2085,8 @@ list<_Tp, _Alloc>::remove_if(_Pred __pred)
for (; __j != __e && __pred(*__j); ++__j)
;
__i = erase(__i, __j);
+ if (__i != __e)
+ ++__i;
}
else
++__i;