Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <algorithm> |
| 11 | |
Howard Hinnant | eb564e7 | 2010-08-22 00:08:10 +0000 | [diff] [blame] | 12 | // template<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred> |
| 13 | // requires OutputIterator<Iter, RvalueOf<Iter::reference>::type> |
| 14 | // && CopyConstructible<Pred> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 15 | // Iter |
| 16 | // remove_if(Iter first, Iter last, Pred pred); |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <functional> |
| 20 | #include <cassert> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 21 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | #include <memory> |
| 23 | #endif |
| 24 | |
| 25 | #include "../../iterators.h" |
| 26 | |
| 27 | template <class Iter> |
| 28 | void |
| 29 | test() |
| 30 | { |
| 31 | int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2}; |
| 32 | const unsigned sa = sizeof(ia)/sizeof(ia[0]); |
| 33 | int* r = std::remove_if(ia, ia+sa, std::bind2nd(std::equal_to<int>(), 2)); |
| 34 | assert(r == ia + sa-3); |
| 35 | assert(ia[0] == 0); |
| 36 | assert(ia[1] == 1); |
| 37 | assert(ia[2] == 3); |
| 38 | assert(ia[3] == 4); |
| 39 | assert(ia[4] == 3); |
| 40 | assert(ia[5] == 4); |
| 41 | } |
| 42 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 43 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | |
| 45 | struct pred |
| 46 | { |
| 47 | bool operator()(const std::unique_ptr<int>& i) {return *i == 2;} |
| 48 | }; |
| 49 | |
| 50 | template <class Iter> |
| 51 | void |
| 52 | test1() |
| 53 | { |
| 54 | const unsigned sa = 9; |
| 55 | std::unique_ptr<int> ia[sa]; |
| 56 | ia[0].reset(new int(0)); |
| 57 | ia[1].reset(new int(1)); |
| 58 | ia[2].reset(new int(2)); |
| 59 | ia[3].reset(new int(3)); |
| 60 | ia[4].reset(new int(4)); |
| 61 | ia[5].reset(new int(2)); |
| 62 | ia[6].reset(new int(3)); |
| 63 | ia[7].reset(new int(4)); |
| 64 | ia[8].reset(new int(2)); |
| 65 | Iter r = std::remove_if(Iter(ia), Iter(ia+sa), pred()); |
| 66 | assert(base(r) == ia + sa-3); |
| 67 | assert(*ia[0] == 0); |
| 68 | assert(*ia[1] == 1); |
| 69 | assert(*ia[2] == 3); |
| 70 | assert(*ia[3] == 4); |
| 71 | assert(*ia[4] == 3); |
| 72 | assert(*ia[5] == 4); |
| 73 | } |
| 74 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 75 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | |
| 77 | int main() |
| 78 | { |
| 79 | test<forward_iterator<int*> >(); |
| 80 | test<bidirectional_iterator<int*> >(); |
| 81 | test<random_access_iterator<int*> >(); |
| 82 | test<int*>(); |
| 83 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 84 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | |
| 86 | test1<forward_iterator<std::unique_ptr<int>*> >(); |
| 87 | test1<bidirectional_iterator<std::unique_ptr<int>*> >(); |
| 88 | test1<random_access_iterator<std::unique_ptr<int>*> >(); |
| 89 | test1<std::unique_ptr<int>*>(); |
| 90 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 91 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | } |