forked from OSchip/llvm-project
Fix PR#202520 - predicate called too many times in list::remove_if. Add tests for list, forward_list, and the std::remove_if algorithm
llvm-svn: 214736
This commit is contained in:
parent
f6fabcc27b
commit
90ba0533cd
|
@ -2046,6 +2046,8 @@ list<_Tp, _Alloc>::remove(const value_type& __x)
|
|||
for (; __j != __e && *__j == __x; ++__j)
|
||||
;
|
||||
__i = erase(__i, __j);
|
||||
if (__i != __e)
|
||||
__i = _VSTD::next(__i);
|
||||
}
|
||||
else
|
||||
++__i;
|
||||
|
@ -2065,6 +2067,8 @@ list<_Tp, _Alloc>::remove_if(_Pred __pred)
|
|||
for (; __j != __e && __pred(*__j); ++__j)
|
||||
;
|
||||
__i = erase(__i, __j);
|
||||
if (__i != __e)
|
||||
__i = _VSTD::next(__i);
|
||||
}
|
||||
else
|
||||
++__i;
|
||||
|
|
|
@ -23,6 +23,9 @@
|
|||
#endif
|
||||
|
||||
#include "test_iterators.h"
|
||||
#include "counting_predicates.hpp"
|
||||
|
||||
bool equal2 ( int i ) { return i == 2; }
|
||||
|
||||
template <class Iter>
|
||||
void
|
||||
|
@ -30,7 +33,9 @@ test()
|
|||
{
|
||||
int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
|
||||
const unsigned sa = sizeof(ia)/sizeof(ia[0]);
|
||||
int* r = std::remove_if(ia, ia+sa, std::bind2nd(std::equal_to<int>(), 2));
|
||||
// int* r = std::remove_if(ia, ia+sa, std::bind2nd(std::equal_to<int>(), 2));
|
||||
unary_counting_predicate<bool(*)(int), int> cp(equal2);
|
||||
int* r = std::remove_if(ia, ia+sa, std::ref(cp));
|
||||
assert(r == ia + sa-3);
|
||||
assert(ia[0] == 0);
|
||||
assert(ia[1] == 1);
|
||||
|
@ -38,6 +43,7 @@ test()
|
|||
assert(ia[3] == 4);
|
||||
assert(ia[4] == 3);
|
||||
assert(ia[5] == 4);
|
||||
assert(cp.count() == sa);
|
||||
}
|
||||
|
||||
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
#include <cassert>
|
||||
|
||||
#include "min_allocator.h"
|
||||
#include "counting_predicates.hpp"
|
||||
|
||||
|
||||
bool g(int i)
|
||||
{
|
||||
|
@ -26,98 +28,128 @@ int main()
|
|||
{
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T> C;
|
||||
const T t1[] = {0, 5, 5, 0, 0, 0, 5};
|
||||
const T t2[] = {5, 5, 5};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2(std::begin(t2), std::end(t2));
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T> C;
|
||||
const T t1[] = {0, 0, 0, 0};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2;
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T> C;
|
||||
const T t1[] = {5, 5, 5};
|
||||
const T t2[] = {5, 5, 5};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2(std::begin(t2), std::end(t2));
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T> C;
|
||||
C c1;
|
||||
C c2;
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == 0);
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T> C;
|
||||
const T t1[] = {5, 5, 5, 0};
|
||||
const T t2[] = {5, 5, 5};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2(std::begin(t2), std::end(t2));
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T, min_allocator<T>> C;
|
||||
const T t1[] = {0, 5, 5, 0, 0, 0, 5};
|
||||
const T t2[] = {5, 5, 5};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2(std::begin(t2), std::end(t2));
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T, min_allocator<T>> C;
|
||||
const T t1[] = {0, 0, 0, 0};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2;
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T, min_allocator<T>> C;
|
||||
const T t1[] = {5, 5, 5};
|
||||
const T t2[] = {5, 5, 5};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2(std::begin(t2), std::end(t2));
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T, min_allocator<T>> C;
|
||||
C c1;
|
||||
C c2;
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == 0);
|
||||
}
|
||||
{
|
||||
typedef int T;
|
||||
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
|
||||
typedef std::forward_list<T, min_allocator<T>> C;
|
||||
const T t1[] = {5, 5, 5, 0};
|
||||
const T t2[] = {5, 5, 5};
|
||||
C c1(std::begin(t1), std::end(t1));
|
||||
C c2(std::begin(t2), std::end(t2));
|
||||
c1.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c1.remove_if(std::ref(cp));
|
||||
assert(c1 == c2);
|
||||
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -16,28 +16,49 @@
|
|||
#include <functional>
|
||||
|
||||
#include "min_allocator.h"
|
||||
#include "counting_predicates.hpp"
|
||||
|
||||
bool even(int i)
|
||||
{
|
||||
return i % 2 == 0;
|
||||
}
|
||||
|
||||
bool g(int i)
|
||||
{
|
||||
return i < 3;
|
||||
}
|
||||
|
||||
typedef unary_counting_predicate<bool(*)(int), int> Predicate;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
int a1[] = {1, 2, 3, 4};
|
||||
int a2[] = {3, 4};
|
||||
std::list<int> c(a1, a1+4);
|
||||
c.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c.remove_if(std::ref(cp));
|
||||
assert(c == std::list<int>(a2, a2+2));
|
||||
assert(cp.count() == 4);
|
||||
}
|
||||
{
|
||||
int a1[] = {1, 2, 3, 4};
|
||||
int a2[] = {1, 3};
|
||||
std::list<int> c(a1, a1+4);
|
||||
Predicate cp(even);
|
||||
c.remove_if(std::ref(cp));
|
||||
assert(c == std::list<int>(a2, a2+2));
|
||||
assert(cp.count() == 4);
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
{
|
||||
int a1[] = {1, 2, 3, 4};
|
||||
int a2[] = {3, 4};
|
||||
std::list<int, min_allocator<int>> c(a1, a1+4);
|
||||
c.remove_if(g);
|
||||
Predicate cp(g);
|
||||
c.remove_if(std::ref(cp));
|
||||
assert((c == std::list<int, min_allocator<int>>(a2, a2+2)));
|
||||
assert(cp.count() == 4);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -11,16 +11,13 @@
|
|||
#define __COUNTING_PREDICATES_H
|
||||
|
||||
|
||||
template <typename Predicate>
|
||||
struct unary_counting_predicate {
|
||||
template <typename Predicate, typename Arg>
|
||||
struct unary_counting_predicate : public std::unary_function<Arg, bool> {
|
||||
public:
|
||||
unary_counting_predicate(Predicate p) : p_(p), count_(0) {}
|
||||
~unary_counting_predicate() {}
|
||||
|
||||
typedef typename Predicate::argument_type argument_type;
|
||||
typedef bool result_type;
|
||||
|
||||
bool operator () (const argument_type &a) const { ++count_; return p_(a); }
|
||||
bool operator () (const Arg &a) const { ++count_; return p_(a); }
|
||||
size_t count() const { return count_; }
|
||||
void reset() { count_ = 0; }
|
||||
|
||||
|
|
Loading…
Reference in New Issue