-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathpriority_queue.h
269 lines (221 loc) · 7.7 KB
/
priority_queue.h
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_PLATFORM_PRIORITY_QUEUE_H_
#define RUNTIME_PLATFORM_PRIORITY_QUEUE_H_
#include "platform/assert.h"
#include "platform/globals.h"
#include "platform/hashmap.h"
#include "platform/utils.h"
namespace dart {
// A min-priority queue with deletion support.
//
// The [PriorityQueue] allows insertion of entries with a priority [P] and a
// value [V]. The minimum element can be queried in O(1) time.
// Insertion/Deletion operations have O(N) time.
//
// In addition to the normal insert/minimum/remove-minimum operations this
// priority queue allows deletion-by-value. We have therefore an invariant
// is that the value must be unique amongst all entries.
template <typename P, typename V>
class PriorityQueue {
public:
static constexpr intptr_t kMinimumSize = 16;
struct Entry {
P priority;
V value;
};
PriorityQueue() : hashmap_(&MatchFun, kMinimumSize) {
min_heap_size_ = kMinimumSize;
min_heap_ =
reinterpret_cast<Entry*>(malloc(sizeof(Entry) * min_heap_size_));
if (min_heap_ == nullptr) FATAL("Cannot allocate memory.");
size_ = 0;
}
~PriorityQueue() { free(min_heap_); }
// Whether the queue is empty.
bool IsEmpty() const { return size_ == 0; }
// Inserts a new entry with [priority] and [value], requires there to be no
// existing entry with given [value].
void Insert(const P& priority, const V& value) {
ASSERT(!ContainsValue(value));
if (size_ == min_heap_size_) {
Resize(min_heap_size_ << 1);
}
Set(size_, {priority, value});
BubbleUp(size_);
size_++;
}
// Returns a reference to the minimum entry.
//
// The caller can access it's priority and value in read-only mode only.
const Entry& Minimum() const {
ASSERT(!IsEmpty());
return min_heap_[0];
}
// Removes the minimum entry.
void RemoveMinimum() {
ASSERT(!IsEmpty());
RemoveAt(0);
}
// Removes an existing entry with the given [value].
//
// Returns true if such an entry was removed.
bool RemoveByValue(const V& value) {
auto entry = FindMapEntry(value);
if (entry != nullptr) {
const intptr_t offset = ValueOfMapEntry(entry);
RemoveAt(offset);
ASSERT(hashmap_.size() == size_);
return true;
}
return false;
}
// Whether the priority queue contains an entry with the given [value].
bool ContainsValue(const V& value) { return FindMapEntry(value) != nullptr; }
// Changes the priority of an existing entry with given [value] or adds a
// new entry.
bool InsertOrChangePriority(const P& priority, const V& value) {
auto map_entry = FindMapEntry(value);
if (map_entry == nullptr) {
Insert(priority, value);
return true;
}
const intptr_t offset = ValueOfMapEntry(map_entry);
ASSERT(offset < size_);
Entry& entry = min_heap_[offset];
entry.priority = priority;
if (offset == 0) {
BubbleDown(offset);
} else {
intptr_t parent = (offset - 1) / 2;
intptr_t diff = entry.priority - min_heap_[parent].priority;
if (diff < 0) {
BubbleUp(offset);
} else if (diff > 0) {
BubbleDown(offset);
}
}
return false;
}
#ifdef TESTING
intptr_t min_heap_size() { return min_heap_size_; }
#endif // TESTING
private:
// Utility functions dealing with the SimpleHashMap interface.
static bool MatchFun(void* key1, void* key2) { return key1 == key2; }
SimpleHashMap::Entry* FindMapEntry(const V& key, bool insert = false) {
return hashmap_.Lookup(CastKey(key), HashKey(key), insert);
}
void RemoveMapEntry(const V& key) {
ASSERT(FindMapEntry(key) != nullptr);
hashmap_.Remove(CastKey(key), HashKey(key));
}
void SetMapEntry(const V& key, intptr_t value) {
FindMapEntry(key, /*insert=*/true)->value = reinterpret_cast<void*>(value);
}
static uint32_t HashKey(const V& key) {
return static_cast<uint32_t>(reinterpret_cast<intptr_t>(CastKey(key)));
}
static intptr_t ValueOfMapEntry(SimpleHashMap::Entry* entry) {
return reinterpret_cast<intptr_t>(entry->value);
}
static void* CastKey(const V& key) {
return reinterpret_cast<void*>((const_cast<V&>(key)));
}
void RemoveAt(intptr_t offset) {
ASSERT(offset < size_);
size_--;
if (offset == size_) {
RemoveMapEntry(min_heap_[offset].value);
} else {
Replace(offset, size_);
BubbleDown(offset);
}
if (size_ <= (min_heap_size_ >> 2) &&
kMinimumSize <= (min_heap_size_ >> 1)) {
Resize(min_heap_size_ >> 1);
}
}
void BubbleUp(intptr_t offset) {
while (true) {
if (offset == 0) return;
intptr_t parent = (offset - 1) / 2;
if (min_heap_[parent].priority > min_heap_[offset].priority) {
Swap(parent, offset);
}
offset = parent;
}
}
void BubbleDown(intptr_t offset) {
while (true) {
intptr_t left_child_index = 2 * offset + 1;
bool has_left_child = left_child_index < size_;
if (!has_left_child) return;
intptr_t smallest_index = offset;
if (min_heap_[left_child_index].priority < min_heap_[offset].priority) {
smallest_index = left_child_index;
}
intptr_t right_child_index = left_child_index + 1;
bool has_right_child = right_child_index < size_;
if (has_right_child) {
if (min_heap_[right_child_index].priority <
min_heap_[smallest_index].priority) {
smallest_index = right_child_index;
}
}
if (offset == smallest_index) {
return;
}
Swap(offset, smallest_index);
offset = smallest_index;
}
}
void Set(intptr_t offset1, const Entry& entry) {
min_heap_[offset1] = entry;
SetMapEntry(entry.value, offset1);
}
void Swap(intptr_t offset1, intptr_t offset2) {
Entry temp = min_heap_[offset1];
min_heap_[offset1] = min_heap_[offset2];
min_heap_[offset2] = temp;
SetMapEntry(min_heap_[offset1].value, offset1);
SetMapEntry(min_heap_[offset2].value, offset2);
}
void Replace(intptr_t index, intptr_t with_other) {
RemoveMapEntry(min_heap_[index].value);
const Entry& entry = min_heap_[with_other];
SetMapEntry(entry.value, index);
min_heap_[index] = entry;
}
void Resize(intptr_t new_min_heap_size) {
ASSERT(size_ < new_min_heap_size);
ASSERT(new_min_heap_size != min_heap_size_);
Entry* new_backing = reinterpret_cast<Entry*>(
realloc(min_heap_, sizeof(Entry) * new_min_heap_size));
if (new_backing == nullptr) FATAL("Cannot allocate memory.");
min_heap_ = new_backing;
min_heap_size_ = new_min_heap_size;
}
// The array is representing a tree structure with guaranteed log(n) height.
// It has the property that the value of node N is always equal or smaller
// than the value of N's children. Furthermore it is a "dense" tree in the
// sense that all rows/layers of the tree are fully occupied except the last
// one. The way to represent such "dense" trees is via an array that allows
// finding left/right children by <2*index+1><2*index+2> and the parent by
// <(index-1)/2>.
//
// Insertion operations can be performed by adding one more entry at the end
// (bottom right) and bubbling it up until the tree invariant is satisfied
// again.
//
// Deletion operations can be performed by replacing the minimum element
// (first entry) by the last entry (bottom right) and bubbling it down until
// the tree invariant is satisfied again.
Entry* min_heap_;
intptr_t min_heap_size_;
intptr_t size_;
SimpleHashMap hashmap_;
};
} // namespace dart
#endif // RUNTIME_PLATFORM_PRIORITY_QUEUE_H_