-
Notifications
You must be signed in to change notification settings - Fork 2
/
StdMinHeap.hpp
136 lines (117 loc) · 3.05 KB
/
StdMinHeap.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <algorithm>
#include <functional>
#include <limits>
#include <new>
#include <vector>
template<class V = uint16_t, class Compare = std::greater<V>>
class StdMinHeap {
public:
typedef V value_type;
private:
typedef std::vector<value_type> array_type;
public:
typedef typename array_type::size_type size_type;
StdMinHeap(const Compare& cmp = Compare()) : cmp_(cmp) { }
~StdMinHeap() = default;
StdMinHeap(const StdMinHeap&) = delete;
StdMinHeap& operator=(const StdMinHeap&) = delete;
size_type size() const { return array_.size(); }
value_type& operator[](size_type index) { return array_[index]; }
value_type const& operator[](size_type index) const { return array_[index]; }
value_type* extend(size_type n) {
size_type old_size = array_.size();
if (n > std::numeric_limits<size_type>::max() - old_size) throw_bad_alloc();
array_.resize(old_size + n);
return &array_[old_size];
}
template<class InputIterator>
void append(InputIterator begin, InputIterator end) {
array_.insert(array_.end(), begin, end);
}
void pull_up(value_type b, size_type q) {
assert(q < size());
while (q > 0) {
size_type p = parent(q);
value_type a = array_[p];
if (!cmp_(a, b)) break;
array_[q] = a;
q = p;
}
array_[q] = b;
}
void push_down(value_type a, size_type p) {
size_type sz = size();
assert(p < sz);
while (true) {
size_type q = children(p);
if (q + 1 > sz) break;
value_type b = array_[q];
if (q + 1 < sz) {
value_type c = array_[q + 1];
if (cmp_(b, c)) {
q += 1;
b = c;
}
}
if (!cmp_(a, b)) break;
array_[p] = b;
p = q;
}
array_[p] = a;
}
void heapify() {
std::make_heap(array_.begin(), array_.end(), cmp_);
}
bool is_heap() const {
return std::is_heap(array_.begin(), array_.end(), cmp_);
}
void push(value_type b) {
array_.push_back(b);
#ifdef STD_PUSH_HEAP
std::push_heap(array_.begin(), array_.end(), cmp_);
#else
pull_up(b, size() - 1);
#endif
}
value_type top() const {
assert(size() > 0);
return array_[0];
}
value_type pop() {
value_type a = top();
#ifdef STD_POP_HEAP
std::pop_heap(array_.begin(), array_.end(), cmp_);
array_.pop_back();
#else
value_type b = array_.back();
array_.pop_back();
if (size() > 0) {
push_down(b, 0);
}
#endif
return a;
}
void sort() {
for (size_type i = size(); i > 0; --i) array_[i - 1] = pop();
}
bool is_sorted(size_type sz) const {
return std::is_sorted(array_.begin(), array_.begin() + sz, cmp_);
}
void clear() {
array_.clear();
array_.shrink_to_fit(); // to match heap_clear(heap*)
}
private:
static size_type parent(size_type q) { return (q - 1) / 2; }
static size_type children(size_type p) { return (p * 2) + 1; }
[[noreturn]] static void throw_bad_alloc() {
std::bad_alloc exception;
throw exception;
}
Compare cmp_;
array_type array_;
};