-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpriority_queue.cc
165 lines (128 loc) · 3.19 KB
/
priority_queue.cc
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
#include "priority_queue.h"
#include "erl_nif.h"
#include <stdlib.h>
#include <string.h>
// Algorithm described at : http://robin-thomas.github.io/min-heap/
#define left(x) 2 * x + 1
#define right(x) 2 * x + 2
#define parent(x) (x - 1) / 2
#define less(a, b) less_(heap_[a], heap_[b])
PriorityQueue::PriorityQueue(LessFun ls, UpdatePositionFun upd, DestroyElementFun dtor) :
capacity_(0),
length_(0),
heap_(NULL),
less_(ls),
update_pos_fun_(upd),
item_dtor_(dtor) { }
PriorityQueue::~PriorityQueue()
{
if(heap_)
{
for(int i = 0; i < length_; i++)
item_dtor_(heap_[i]);
enif_free(heap_);
}
}
bool PriorityQueue::insert(void* item)
{
int pos;
if (length_ == capacity_)
{
int new_capacity = (length_+1) * 2;
void** new_heap = reinterpret_cast<void**>(enif_alloc(sizeof(void*) * new_capacity));
if (!new_heap)
return false;
memcpy(new_heap, heap_, sizeof(void*)*length_);
enif_free(heap_);
heap_ = new_heap;
capacity_ = new_capacity;
}
pos = (length_)++;
set(pos, item);
bubble_down(pos);
return true;
}
bool PriorityQueue::remove(void* item, int pos)
{
if (pos >= length_)
return false;
if(heap_[pos] != item)
return false;
return remove(pos) == item;
}
void* PriorityQueue::remove(int pos)
{
if (pos >= length_)
return NULL;
void* item = heap_[pos];
length_--;
int ls = less(pos, length_);
set(pos, heap_[length_]);
if(ls)
bubble_up(pos);
else
bubble_down(pos);
// todo: resize down the heap in case we have a lot of empty slots
update_pos_fun_(item, -1);
return item;
}
void* PriorityQueue::peek()
{
if(length_ == 0)
return NULL;
return heap_[0];
}
// private
void PriorityQueue::set(int pos, void* item)
{
heap_[pos] = item;
update_pos_fun_(item, pos);
}
void PriorityQueue::pos_swap(int pos1, int pos2)
{
void* tmp = heap_[pos1];
set(pos1, heap_[pos2]);
set(pos2, tmp);
}
void PriorityQueue::bubble_down(int pos)
{
while(true)
{
int parent = parent(pos);
if (pos == 0 || less(parent, pos))
return;
pos_swap(pos, parent);
pos = parent;
}
}
int PriorityQueue::rank_r(int target_index, int cur_node_index)
{
// https://rafal.io/posts/heap-rank.html
if (cur_node_index >= length_)
return 0;
// if the current node priority is <= target_node, it is part of the rank count
if (less(cur_node_index, target_index))
{
int left_count = rank_r(target_index, left(cur_node_index));
int right_count = rank_r(target_index, right(cur_node_index));
return 1 + left_count + right_count;
}
return 0;
}
void PriorityQueue::bubble_up(int pos)
{
while (true)
{
int left = left(pos);
int right = right(pos);
int smallest = pos;
if (left < length_ && less(left, smallest))
smallest = left;
if (right < length_ && less(right, smallest))
smallest = right;
if (smallest == pos)
return;
pos_swap(pos, smallest);
pos = smallest;
}
}