1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <algorithm> 11 12 // template<RandomAccessIterator Iter> 13 // requires ShuffleIterator<Iter> 14 // && LessThanComparable<Iter::value_type> 15 // void 16 // push_heap(Iter first, Iter last); 17 18 #include <algorithm> 19 #include <functional> 20 #include <cassert> 21 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 22 #include <memory> 23 24 struct indirect_less 25 { 26 template <class P> 27 bool operator()(const P& x, const P& y) 28 {return *x < *y;} 29 }; 30 31 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 32 33 void test(unsigned N) 34 { 35 int* ia = new int [N]; 36 for (int i = 0; i < N; ++i) 37 ia[i] = i; 38 std::random_shuffle(ia, ia+N); 39 for (int i = 0; i <= N; ++i) 40 { 41 std::push_heap(ia, ia+i, std::greater<int>()); 42 assert(std::is_heap(ia, ia+i, std::greater<int>())); 43 } 44 delete [] ia; 45 } 46 47 int main() 48 { 49 test(1000); 50 51 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 52 { 53 const int N = 1000; 54 std::unique_ptr<int>* ia = new std::unique_ptr<int> [N]; 55 for (int i = 0; i < N; ++i) 56 ia[i].reset(new int(i)); 57 std::random_shuffle(ia, ia+N); 58 for (int i = 0; i <= N; ++i) 59 { 60 std::push_heap(ia, ia+i, indirect_less()); 61 assert(std::is_heap(ia, ia+i, indirect_less())); 62 } 63 delete [] ia; 64 } 65 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 66 } 67