git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@111745 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2010-08-21 20:58:44 +00:00
parent 98e5d97400
commit 7a2523b201
3 changed files with 35 additions and 20 deletions

View File

@ -97,8 +97,8 @@ public:
InputIterator first, InputIterator last); InputIterator first, InputIterator last);
iterator insert_after(const_iterator p, initializer_list<value_type> il); iterator insert_after(const_iterator p, initializer_list<value_type> il);
void erase_after(const_iterator p); iterator erase_after(const_iterator p);
void erase_after(const_iterator first, const_iterator last); iterator erase_after(const_iterator first, const_iterator last);
void swap(forward_list& x); void swap(forward_list& x);
@ -568,8 +568,8 @@ public:
iterator insert_after(const_iterator __p, initializer_list<value_type> __il) iterator insert_after(const_iterator __p, initializer_list<value_type> __il)
{return insert_after(__p, __il.begin(), __il.end());} {return insert_after(__p, __il.begin(), __il.end());}
void erase_after(const_iterator __p); iterator erase_after(const_iterator __p);
void erase_after(const_iterator __f, const_iterator __l); iterator erase_after(const_iterator __f, const_iterator __l);
void swap(forward_list& __x) {base::swap(__x);} void swap(forward_list& __x) {base::swap(__x);}
@ -1033,7 +1033,7 @@ forward_list<_Tp, _Alloc>::insert_after(const_iterator __p,
} }
template <class _Tp, class _Alloc> template <class _Tp, class _Alloc>
void typename forward_list<_Tp, _Alloc>::iterator
forward_list<_Tp, _Alloc>::erase_after(const_iterator __f) forward_list<_Tp, _Alloc>::erase_after(const_iterator __f)
{ {
__node_pointer __p = const_cast<__node_pointer>(__f.__ptr_); __node_pointer __p = const_cast<__node_pointer>(__f.__ptr_);
@ -1042,17 +1042,18 @@ forward_list<_Tp, _Alloc>::erase_after(const_iterator __f)
__node_allocator& __a = base::__alloc(); __node_allocator& __a = base::__alloc();
__node_traits::destroy(__a, addressof(__n->__value_)); __node_traits::destroy(__a, addressof(__n->__value_));
__node_traits::deallocate(__a, __n, 1); __node_traits::deallocate(__a, __n, 1);
return iterator(__p->__next_);
} }
template <class _Tp, class _Alloc> template <class _Tp, class _Alloc>
void typename forward_list<_Tp, _Alloc>::iterator
forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l) forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l)
{ {
__node_pointer __e = const_cast<__node_pointer>(__l.__ptr_);
if (__f != __l) if (__f != __l)
{ {
__node_pointer __p = const_cast<__node_pointer>(__f.__ptr_); __node_pointer __p = const_cast<__node_pointer>(__f.__ptr_);
__node_pointer __n = __p->__next_; __node_pointer __n = __p->__next_;
__node_pointer __e = const_cast<__node_pointer>(__l.__ptr_);
if (__n != __e) if (__n != __e)
{ {
__p->__next_ = __e; __p->__next_ = __e;
@ -1066,6 +1067,7 @@ forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l)
} while (__n != __e); } while (__n != __e);
} }
} }
return iterator(__e);
} }
template <class _Tp, class _Alloc> template <class _Tp, class _Alloc>

View File

@ -9,7 +9,7 @@
// <forward_list> // <forward_list>
// void erase_after(const_iterator first, const_iterator last); // iterator erase_after(const_iterator first, const_iterator last);
#include <forward_list> #include <forward_list>
#include <cassert> #include <cassert>
@ -22,7 +22,8 @@ int main()
const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
C c(std::begin(t), std::end(t)); C c(std::begin(t), std::end(t));
c.erase_after(next(c.cbefore_begin(), 4), next(c.cbefore_begin(), 4)); C::iterator i = c.erase_after(next(c.cbefore_begin(), 4), next(c.cbefore_begin(), 4));
assert(i == next(c.cbefore_begin(), 4));
assert(distance(c.begin(), c.end()) == 10); assert(distance(c.begin(), c.end()) == 10);
assert(*next(c.begin(), 0) == 0); assert(*next(c.begin(), 0) == 0);
assert(*next(c.begin(), 1) == 1); assert(*next(c.begin(), 1) == 1);
@ -35,7 +36,8 @@ int main()
assert(*next(c.begin(), 8) == 8); assert(*next(c.begin(), 8) == 8);
assert(*next(c.begin(), 9) == 9); assert(*next(c.begin(), 9) == 9);
c.erase_after(next(c.cbefore_begin(), 2), next(c.cbefore_begin(), 5)); i = c.erase_after(next(c.cbefore_begin(), 2), next(c.cbefore_begin(), 5));
assert(i == next(c.begin(), 2));
assert(distance(c.begin(), c.end()) == 8); assert(distance(c.begin(), c.end()) == 8);
assert(*next(c.begin(), 0) == 0); assert(*next(c.begin(), 0) == 0);
assert(*next(c.begin(), 1) == 1); assert(*next(c.begin(), 1) == 1);
@ -46,7 +48,8 @@ int main()
assert(*next(c.begin(), 6) == 8); assert(*next(c.begin(), 6) == 8);
assert(*next(c.begin(), 7) == 9); assert(*next(c.begin(), 7) == 9);
c.erase_after(next(c.cbefore_begin(), 2), next(c.cbefore_begin(), 3)); i = c.erase_after(next(c.cbefore_begin(), 2), next(c.cbefore_begin(), 3));
assert(i == next(c.begin(), 2));
assert(distance(c.begin(), c.end()) == 8); assert(distance(c.begin(), c.end()) == 8);
assert(*next(c.begin(), 0) == 0); assert(*next(c.begin(), 0) == 0);
assert(*next(c.begin(), 1) == 1); assert(*next(c.begin(), 1) == 1);
@ -57,7 +60,8 @@ int main()
assert(*next(c.begin(), 6) == 8); assert(*next(c.begin(), 6) == 8);
assert(*next(c.begin(), 7) == 9); assert(*next(c.begin(), 7) == 9);
c.erase_after(next(c.cbefore_begin(), 5), next(c.cbefore_begin(), 9)); i = c.erase_after(next(c.cbefore_begin(), 5), next(c.cbefore_begin(), 9));
assert(i == c.end());
assert(distance(c.begin(), c.end()) == 5); assert(distance(c.begin(), c.end()) == 5);
assert(*next(c.begin(), 0) == 0); assert(*next(c.begin(), 0) == 0);
assert(*next(c.begin(), 1) == 1); assert(*next(c.begin(), 1) == 1);
@ -65,14 +69,17 @@ int main()
assert(*next(c.begin(), 3) == 5); assert(*next(c.begin(), 3) == 5);
assert(*next(c.begin(), 4) == 6); assert(*next(c.begin(), 4) == 6);
c.erase_after(next(c.cbefore_begin(), 0), next(c.cbefore_begin(), 2)); i = c.erase_after(next(c.cbefore_begin(), 0), next(c.cbefore_begin(), 2));
assert(i == c.begin());
assert(distance(c.begin(), c.end()) == 4); assert(distance(c.begin(), c.end()) == 4);
assert(*next(c.begin(), 0) == 1); assert(*next(c.begin(), 0) == 1);
assert(*next(c.begin(), 1) == 4); assert(*next(c.begin(), 1) == 4);
assert(*next(c.begin(), 2) == 5); assert(*next(c.begin(), 2) == 5);
assert(*next(c.begin(), 3) == 6); assert(*next(c.begin(), 3) == 6);
c.erase_after(next(c.cbefore_begin(), 0), next(c.cbefore_begin(), 5)); i = c.erase_after(next(c.cbefore_begin(), 0), next(c.cbefore_begin(), 5));
assert(i == c.begin());
assert(i == c.end());
assert(distance(c.begin(), c.end()) == 0); assert(distance(c.begin(), c.end()) == 0);
} }
} }

View File

@ -9,7 +9,7 @@
// <forward_list> // <forward_list>
// void erase_after(const_iterator p); // iterator erase_after(const_iterator p);
#include <forward_list> #include <forward_list>
#include <cassert> #include <cassert>
@ -22,29 +22,35 @@ int main()
const T t[] = {0, 1, 2, 3, 4}; const T t[] = {0, 1, 2, 3, 4};
C c(std::begin(t), std::end(t)); C c(std::begin(t), std::end(t));
c.erase_after(next(c.cbefore_begin(), 4)); C::iterator i = c.erase_after(next(c.cbefore_begin(), 4));
assert(i == c.end());
assert(distance(c.begin(), c.end()) == 4); assert(distance(c.begin(), c.end()) == 4);
assert(*next(c.begin(), 0) == 0); assert(*next(c.begin(), 0) == 0);
assert(*next(c.begin(), 1) == 1); assert(*next(c.begin(), 1) == 1);
assert(*next(c.begin(), 2) == 2); assert(*next(c.begin(), 2) == 2);
assert(*next(c.begin(), 3) == 3); assert(*next(c.begin(), 3) == 3);
c.erase_after(next(c.cbefore_begin(), 0)); i = c.erase_after(next(c.cbefore_begin(), 0));
assert(i == c.begin());
assert(distance(c.begin(), c.end()) == 3); assert(distance(c.begin(), c.end()) == 3);
assert(*next(c.begin(), 0) == 1); assert(*next(c.begin(), 0) == 1);
assert(*next(c.begin(), 1) == 2); assert(*next(c.begin(), 1) == 2);
assert(*next(c.begin(), 2) == 3); assert(*next(c.begin(), 2) == 3);
c.erase_after(next(c.cbefore_begin(), 1)); i = c.erase_after(next(c.cbefore_begin(), 1));
assert(i == next(c.begin()));
assert(distance(c.begin(), c.end()) == 2); assert(distance(c.begin(), c.end()) == 2);
assert(*next(c.begin(), 0) == 1); assert(*next(c.begin(), 0) == 1);
assert(*next(c.begin(), 1) == 3); assert(*next(c.begin(), 1) == 3);
c.erase_after(next(c.cbefore_begin(), 1)); i = c.erase_after(next(c.cbefore_begin(), 1));
assert(i == c.end());
assert(distance(c.begin(), c.end()) == 1); assert(distance(c.begin(), c.end()) == 1);
assert(*next(c.begin(), 0) == 1); assert(*next(c.begin(), 0) == 1);
c.erase_after(next(c.cbefore_begin(), 0)); i = c.erase_after(next(c.cbefore_begin(), 0));
assert(i == c.begin());
assert(i == c.end());
assert(distance(c.begin(), c.end()) == 0); assert(distance(c.begin(), c.end()) == 0);
} }
} }