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
|
/*
* include/ds/BloomFilter.h
*
* Copyright (C) 2023 Douglas Rumbaugh <drumbaugh@psu.edu>
* Dong Xie <dongx@psu.edu>
*
* All rights reserved. Published under the Modified BSD License.
*
*/
#pragma once
#include <vector>
#include <cassert>
namespace de {
template <typename R>
struct queue_record {
const R *data;
size_t version;
};
template <typename R>
class standard_minheap {
public:
inline bool operator()(const R* a, const R* b) {
return *a < *b;
}
};
template <typename R>
class standard_maxheap {
public:
inline bool operator()(const R* a, const R* b) {
return *a > *b;
}
};
template <typename R, typename CMP=standard_minheap<R>>
class PriorityQueue {
public:
PriorityQueue(size_t size) : data(size), tail(0) {}
~PriorityQueue() = default;
size_t size() const {
return tail;
}
void pop() {
assert(this->tail != 0);
// If there is only one element, just decrement the
// tail.
if (this->size() == 1) {
this->tail--;
return;
}
swap(0, --this->tail);
ssize_t idx = 0;
ssize_t child_idx;
while ((child_idx = min_child(idx)) != -1 && heap_cmp(child_idx, idx)) {
swap(idx, child_idx);
idx = child_idx;
}
}
void push(const R* record, size_t version=0) {
assert(tail != this->data.size());
size_t new_idx = this->tail++;
this->data[new_idx] = {record, version};
while (new_idx != 0 && !heap_cmp(parent(new_idx), new_idx)) {
swap(parent(new_idx), new_idx);
new_idx = parent(new_idx);
}
}
queue_record<R> peek(size_t depth=0) {
ssize_t idx = 0;
size_t cur_depth = 0;
while (cur_depth != depth) {
idx = min_child(idx);
assert(idx != -1);
cur_depth++;
}
return this->data[idx];
}
private:
std::vector<queue_record<R>> data;
CMP cmp;
size_t tail;
/*
* Swap the elements at position a and position
* b within the heap
*/
inline void swap(size_t a, size_t b) {
if (a == b) return;
queue_record temp = this->data[a];
this->data[a] = this->data[b];
this->data[b] = temp;
}
inline size_t left_child(size_t idx) {
return 2 * idx + 1;
}
inline size_t right_child(size_t idx) {
return 2 * idx + 2;
}
inline size_t parent(size_t idx) {
return (idx - 1) / 2;
}
inline ssize_t min_child(size_t idx) {
ssize_t left = left_child(idx) < tail ? left_child(idx) : -1;
ssize_t right = right_child(idx) < tail ? right_child(idx) : -1;
if (left == -1 && right == -1) {
return -1;
} else if (left == -1) {
return right;
} else if (right == -1) {
return left;
}
return (heap_cmp(left, right)) ? left : right;
}
inline bool heap_cmp(size_t a, size_t b) {
if (data[a].data != data[b].data) {
return cmp(data[a].data, data[b].data);
} else if (data[a].version != data[b].version) {
return data[a].version < data[b].version;
}
constexpr bool has_tombstone = requires(const R &r) {
r.is_tombstone();
};
if constexpr (has_tombstone) {
return data[a].data->is_tombstone() && data[b].data->is_tombstone();
} else {
return false;
}
}
};
}
|