summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libcxx/include/list26
-rw-r--r--libcxx/test/containers/sequences/list/list.ops/remove.pass.cpp32
2 files changed, 46 insertions, 12 deletions
diff --git a/libcxx/include/list b/libcxx/include/list
index 1d21fd6f118..c36786dd49a 100644
--- a/libcxx/include/list
+++ b/libcxx/include/list
@@ -219,7 +219,7 @@ struct __list_node_base
_LIBCPP_INLINE_VISIBILITY
pointer __self()
{
- return static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this));
+ return static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this));
}
};
@@ -1086,10 +1086,10 @@ 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;
+ __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
@@ -1098,10 +1098,10 @@ 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;
+ __l->__next_ = base::__end_.__self();
+ __f->__prev_ = base::__end_.__prev_;
+ __f->__prev_->__next_ = __f;
+ base::__end_.__prev_ = __l;
}
@@ -2058,14 +2058,16 @@ 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;
}
diff --git a/libcxx/test/containers/sequences/list/list.ops/remove.pass.cpp b/libcxx/test/containers/sequences/list/list.ops/remove.pass.cpp
index 76d878d02c3..106c0527f5f 100644
--- a/libcxx/test/containers/sequences/list/list.ops/remove.pass.cpp
+++ b/libcxx/test/containers/sequences/list/list.ops/remove.pass.cpp
@@ -16,6 +16,17 @@
#include "min_allocator.h"
+struct S {
+ S(int i) : i_(new int(i)) {}
+ S(const S &rhs) : i_(new int(*rhs.i_)) {}
+ S& operator = (const S &rhs) { *i_ = *rhs.i_; return *this; }
+ ~S () { delete i_; i_ = NULL; }
+ bool operator == (const S &rhs) const { return *i_ == *rhs.i_; }
+ int get () const { return *i_; }
+ int *i_;
+ };
+
+
int main()
{
{
@@ -25,6 +36,27 @@ int main()
c.remove(3);
assert(c == std::list<int>(a2, a2+3));
}
+ { // LWG issue #526
+ int a1[] = {1, 2, 1, 3, 5, 8, 11};
+ int a2[] = { 2, 3, 5, 8, 11};
+ std::list<int> c(a1, a1+7);
+ c.remove(c.front());
+ assert(c == std::list<int>(a2, a2+5));
+ }
+ {
+ int a1[] = {1, 2, 1, 3, 5, 8, 11, 1};
+ int a2[] = { 2, 3, 5, 8, 11 };
+ std::list<S> c;
+ for(int *ip = a1; ip < a1+8; ++ip)
+ c.push_back(S(*ip));
+ c.remove(c.front());
+ std::list<S>::const_iterator it = c.begin();
+ for(int *ip = a2; ip < a2+5; ++ip, ++it) {
+ assert ( it != c.end());
+ assert ( *ip == it->get());
+ }
+ assert ( it == c.end ());
+ }
#if __cplusplus >= 201103L
{
int a1[] = {1, 2, 3, 4};
OpenPOWER on IntegriCloud