STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
is_heap.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/is_heap.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_COMMON_IS_HEAP_HEADER
14 #define STXXL_COMMON_IS_HEAP_HEADER
15 
16 #include <stxxl/bits/namespace.h>
17 #include <iterator>
18 #include <functional>
19 
21 
22 template <typename RandomAccessIterator, typename StrictWeakOrdering>
23 bool is_heap(
24  RandomAccessIterator first, RandomAccessIterator last,
25  StrictWeakOrdering comp =
26  std::less<typename std::iterator_traits<RandomAccessIterator>
27  ::value_type>())
28 {
29  typedef typename std::iterator_traits<RandomAccessIterator>
30  ::difference_type diff_type;
31 
32  if (first == last) return true;
33 
34  RandomAccessIterator parent = first;
35 
36  diff_type num = 1;
37  for (RandomAccessIterator child = first + 1; child != last; ++child, ++num)
38  {
39  if (comp(*parent, *child)) // parent < child -> max-heap violated
40  return false;
41 
42  if ((num & 1) == 0)
43  ++parent;
44  }
45 
46  return true;
47 }
48 
50 
51 #endif // !STXXL_COMMON_IS_HEAP_HEADER
bool is_heap(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp=std::less< typename std::iterator_traits< RandomAccessIterator >::value_type >())
Definition: is_heap.h:23
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define STXXL_END_NAMESPACE
Definition: namespace.h:17