blob: aeae64f25f49535f8d13078ff954ce50f999b90e [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
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 Hinnanteb564e72010-08-22 00:08:10 +000012// template<RandomAccessIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare>
13// requires ShuffleIterator<Iter> && CopyConstructible<Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000014// void
15// pop_heap(Iter first, Iter last, Compare comp);
16
17#include <algorithm>
18#include <functional>
19#include <cassert>
Howard Hinnant73d21a42010-09-04 23:28:19 +000020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#include <memory>
22
23struct indirect_less
24{
25 template <class P>
26 bool operator()(const P& x, const P& y)
27 {return *x < *y;}
28};
29
Howard Hinnant73d21a42010-09-04 23:28:19 +000030#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031
32void 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
48int main()
49{
50 test(1000);
51
Howard Hinnant73d21a42010-09-04 23:28:19 +000052#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000053 {
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 Hinnant73d21a42010-09-04 23:28:19 +000067#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068}