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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef _heap_hpp_INCLUDED
#define _heap_hpp_INCLUDED
#include "util.hpp" // Alphabetically after 'heap.hpp'.
#include <cassert>
namespace CaDiCaL {
using namespace std;
// This is a priority queue with updates for unsigned integers implemented
// as binary heap. We need to map integer elements added (through
// 'push_back') to positions on the binary heap in 'array'. This map is
// stored in the 'pos' array. This approach is really wasteful (at least in
// terms of memory) if only few and a sparse set of integers is added. So
// it should not be used in this situation. A generic priority queue would
// implement the mapping externally provided by another template parameter.
// Since we use 'UINT_MAX' as 'not contained' flag, we can only have
// 'UINT_MAX - 1' elements in the heap.
const unsigned invalid_heap_position = UINT_MAX;
template <class C> class heap {
vector<unsigned> array; // actual binary heap
vector<unsigned> pos; // positions of elements in array
C less; // less-than for elements
// Map an element to its position entry in the 'pos' map.
//
unsigned &index (unsigned e) {
if (e >= pos.size ())
pos.resize (1 + (size_t) e, invalid_heap_position);
unsigned &res = pos[e];
assert (res == invalid_heap_position || (size_t) res < array.size ());
return res;
}
bool has_parent (unsigned e) { return index (e) > 0; }
bool has_left (unsigned e) {
return (size_t) 2 * index (e) + 1 < size ();
}
bool has_right (unsigned e) {
return (size_t) 2 * index (e) + 2 < size ();
}
unsigned parent (unsigned e) {
assert (has_parent (e));
return array[(index (e) - 1) / 2];
}
unsigned left (unsigned e) {
assert (has_left (e));
return array[2 * index (e) + 1];
}
unsigned right (unsigned e) {
assert (has_right (e));
return array[2 * index (e) + 2];
}
// Exchange elements 'a' and 'b' in 'array' and fix their positions.
//
void exchange (unsigned a, unsigned b) {
unsigned &i = index (a), &j = index (b);
swap (array[i], array[j]);
swap (i, j);
}
// Bubble up an element as far as necessary.
//
void up (unsigned e) {
unsigned p;
while (has_parent (e) && less ((p = parent (e)), e))
exchange (p, e);
}
// Bubble down an element as far as necessary.
//
void down (unsigned e) {
while (has_left (e)) {
unsigned c = left (e);
if (has_right (e)) {
unsigned r = right (e);
if (less (c, r))
c = r;
}
if (!less (e, c))
break;
exchange (e, c);
}
}
// Very expensive checker for the main 'heap' invariant. Can be enabled
// to find violations of antisymmetry in the client implementation of
// 'less' and as well of course bugs in this heap implementation. It
// should be enabled during testing applications of the heap.
//
void check () {
#if 0 // EXPENSIVE HEAP CHECKING IF ENABLED
#warning "expensive checking in heap enabled"
assert (array.size () <= invalid_heap_position);
for (size_t i = 0; i < array.size (); i++) {
size_t l = 2*i + 1, r = 2*i + 2;
if (l < array.size ()) assert (!less (array[i], array[l]));
if (r < array.size ()) assert (!less (array[i], array[r]));
assert (array[i] >= 0);
{
assert ((size_t) array[i] < pos.size ());
assert (i == (size_t) pos[array[i]]);
}
}
for (size_t i = 0; i < pos.size (); i++) {
if (pos[i] == invalid_heap_position) continue;
assert (pos[i] < array.size ());
assert (array[pos[i]] == (unsigned) i);
}
#endif
}
public:
heap (const C &c) : less (c) {}
// Number of elements in the heap.
//
size_t size () const { return array.size (); }
// Check if no more elements are in the heap.
//
bool empty () const { return array.empty (); }
// Check whether 'e' is already in the heap.
//
bool contains (unsigned e) const {
if ((size_t) e >= pos.size ())
return false;
return pos[e] != invalid_heap_position;
}
// Add a new (not contained) element 'e' to the heap.
//
void push_back (unsigned e) {
assert (!contains (e));
size_t i = array.size ();
assert (i < (size_t) invalid_heap_position);
array.push_back (e);
index (e) = (unsigned) i;
up (e);
down (e);
check ();
}
// Returns the maximum element in the heap.
//
unsigned front () const {
assert (!empty ());
return array[0];
}
// Removes the maximum element in the heap.
//
unsigned pop_front () {
assert (!empty ());
unsigned res = array[0], last = array.back ();
if (size () > 1)
exchange (res, last);
index (res) = invalid_heap_position;
array.pop_back ();
if (size () > 1)
down (last);
check ();
return res;
}
// Notify the heap, that evaluation of 'less' has changed for 'e'.
//
void update (unsigned e) {
assert (contains (e));
up (e);
down (e);
check ();
}
void clear () {
array.clear ();
pos.clear ();
}
void erase () {
erase_vector (array);
erase_vector (pos);
}
void shrink () {
shrink_vector (array);
shrink_vector (pos);
}
// Standard iterators 'inherited' from 'vector'.
//
typedef typename vector<unsigned>::iterator iterator;
typedef typename vector<unsigned>::const_iterator const_iterator;
iterator begin () { return array.begin (); }
iterator end () { return array.end (); }
const_iterator begin () const { return array.begin (); }
const_iterator end () const { return array.end (); }
};
} // namespace CaDiCaL
#endif