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<RandomAccessIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare> |
| 13 | // requires ShuffleIterator<Iter> && CopyConstructible<Compare> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | // void |
| 15 | // pop_heap(Iter first, Iter last, Compare comp); |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <functional> |
| 19 | #include <cassert> |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 20 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | #include <memory> |
| 22 | |
| 23 | struct indirect_less |
| 24 | { |
| 25 | template <class P> |
| 26 | bool operator()(const P& x, const P& y) |
| 27 | {return *x < *y;} |
| 28 | }; |
| 29 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 30 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | |
| 32 | void test(unsigned N) |
| 33 | { |
| 34 | int* ia = new int [N]; |
| 35 | for (int i = 0; i < N; ++i) |
| 36 | ia[i] = i; |
| 37 | std::random_shuffle(ia, ia+N); |
| 38 | std::make_heap(ia, ia+N, std::greater<int>()); |
| 39 | for (int i = N; i > 0; --i) |
| 40 | { |
| 41 | std::pop_heap(ia, ia+i, std::greater<int>()); |
| 42 | assert(std::is_heap(ia, ia+i-1, std::greater<int>())); |
| 43 | } |
| 44 | std::pop_heap(ia, ia, std::greater<int>()); |
| 45 | delete [] ia; |
| 46 | } |
| 47 | |
| 48 | int main() |
| 49 | { |
| 50 | test(1000); |
| 51 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 52 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | { |
| 54 | const int N = 1000; |
| 55 | std::unique_ptr<int>* ia = new std::unique_ptr<int> [N]; |
| 56 | for (int i = 0; i < N; ++i) |
| 57 | ia[i].reset(new int(i)); |
| 58 | std::random_shuffle(ia, ia+N); |
| 59 | std::make_heap(ia, ia+N, indirect_less()); |
| 60 | for (int i = N; i > 0; --i) |
| 61 | { |
| 62 | std::pop_heap(ia, ia+i, indirect_less()); |
| 63 | assert(std::is_heap(ia, ia+i-1, indirect_less())); |
| 64 | } |
| 65 | delete [] ia; |
| 66 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 67 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 68 | } |