While reading LWG#526, Ion Gaztañaga noticed that libc++ didn't correctly handle list::remove(const value_type &x), if x was an element of the list. Added a test for this, and a fix. Thanks to Ion for the report.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@215210 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2014-08-08 15:35:52 +00:00
parent f2e8c04540
commit fca038e133
2 changed files with 46 additions and 12 deletions

View File

@ -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;
}

View File

@ -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};