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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* @file DynamicHeap.hpp
* Defines class DynamicHeap.
*/
#ifndef __DynamicHeap__
#define __DynamicHeap__
#include <algorithm>
#include "Forwards.hpp"
#include "Debug/Assertion.hpp"
#include "Comparison.hpp"
#include "DArray.hpp"
#include "DHMap.hpp"
namespace Lib {
/**
* Minimum binary heap
*/
template<class T, class Comparator, class ElMap = DHMap<T,size_t>, class TArg = T >
class DynamicHeap {
public:
DynamicHeap(Comparator cmp=Comparator()) : _heap(0), _cmp(cmp) {}
/**
* Add @c obj to the end of the heap
* (heap property not being maintained).
*
* An object can appear in the heap at most once.
*/
void addToEnd(TArg obj)
{
ASS(!contains(obj));
size_t newIdx1 = size()+1;
_heap.expand(newIdx1);
getData1()[newIdx1] = obj;
_elMap.insert(obj, newIdx1);
}
/**
* Restore heap property (after a series of addToEnd calls
* and/or un-notified Increases/Decreases)
*/
void heapify()
{
for (size_t i = (size() >> 1); i > 0; i--) {
fixIncrease1(i);
}
}
/**
* Insert @c obj into the heap
*
* An object can appear in the heap at most once.
*/
void insert(TArg obj)
{
addToEnd(obj);
size_t newIdx1 = size();
fixDecrease1(newIdx1);
}
T pop()
{
T res = _heap[0];
T* data1 = getData1();
size_t backIdx1 = size();
data1[1] = data1[backIdx1];
_heap.expand(backIdx1-1);
_elMap.remove(res);
if(backIdx1!=1) {
//we still have some elements left
_elMap.set(data1[1], 1);
fixIncrease1(1);
}
return res;
}
void notifyIncrease(TArg obj)
{
size_t idx = _elMap.get(obj);
ASS(idx==1 || !isGreater1(idx/2, idx)); //check that we didn't decrease
fixIncrease1(idx);
}
void notifyDecrease(TArg obj)
{
size_t idx = _elMap.get(obj);
ASS(idx*2>size() || !isGreater1(idx, idx*2)); //check that we didn't increase
ASS(idx*2+1>size() || !isGreater1(idx, idx*2+1)); //check that we didn't increase
fixDecrease1(idx);
}
TArg top() const
{
ASS(!isEmpty());
return _heap[0];
}
bool contains(TArg obj)
{
return _elMap.find(obj);
}
size_t size() const { return _heap.size(); }
bool isEmpty() const { return size()==0; }
ElMap& elMap() { return _elMap; }
private:
//'1' appended to a function name means that it receives one-based indexes as arguments
/**
* Fix decrease of element at one-based index @c idx1.
*/
void fixDecrease1(size_t idx)
{
ASS_G(idx, 0);
ASS_LE(idx, size());
while(idx>1) {
size_t parent = idx/2;
if(!isGreater1(parent, idx)) {
break;
}
swapInHeap1(idx, parent);
idx = parent;
}
}
/**
* Fix increase of element at one-based index @c idx.
*/
void fixIncrease1(size_t idx)
{
ASS_G(idx, 0);
ASS_LE(idx, size());
size_t maxIdx = size();
for(;;) {
size_t child1 = idx*2;
if(child1>maxIdx) {
break;
}
if(child1==maxIdx) {
if(isGreater1(idx, child1)) {
swapInHeap1(idx, child1);
}
break;
}
size_t child2 = child1+1;
if(isGreater1(idx, child1)) {
if(isGreater1(child1, child2)) {
swapInHeap1(idx, child2);
idx = child2;
}
else {
swapInHeap1(idx, child1);
idx = child1;
}
}
else if(isGreater1(idx, child2)) {
swapInHeap1(idx, child2);
idx = child2;
}
else {
break;
}
}
}
/**
* Return true if element at one-based index @c idxA is greater than
* the one at @c idxB
*/
bool isGreater1(size_t idxA, size_t idxB)
{
ASS_G(idxA, 0);
ASS_LE(idxA, size());
ASS_G(idxB, 0);
ASS_LE(idxB, size());
T* data1 = getData1();
return _cmp.compare(data1[idxA], data1[idxB])==GREATER;
}
/**
* Swap elements at one-based indexes @c idxA and @c idxB.
*/
void swapInHeap1(size_t idxA, size_t idxB)
{
T* data1 = getData1();
using std::swap; //ADL
swap(data1[idxA], data1[idxB]);
_elMap.set(data1[idxA], idxA);
_elMap.set(data1[idxB], idxB);
}
/**
* Return pointer to heap data that can be accessed as an one-based array
*/
T* getData1() { return _heap.array()-1; }
DArray<T> _heap;
/**
* Maps elements to their position in the heap
*/
ElMap _elMap;
Comparator _cmp;
};
}
#endif // __DynamicHeap__