-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheap.d
More file actions
75 lines (66 loc) · 1.73 KB
/
heap.d
File metadata and controls
75 lines (66 loc) · 1.73 KB
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
module stdex.heap;
import std.functional;
void pushHeap(alias compare = "a < b", T)(T[] heap)
{
pushHeap!compare(heap, heap.length - 1, 0, heap[$ - 1]);
}
void popHeap(alias compare = "a < b", T)(T[] heap)
{
if (heap.length > 1)
popHeap!compare(heap[0..$-1], heap[$-1]);
}
void makeHeap(alias compare = "a < b", T)(T[] heap)
{
if (heap.length < 2)
return;
size_t length = heap.length;
size_t parent = (length - 2) / 2;
while (true)
{
T value = heap[parent];
adjustHeap!compare(heap, parent, value);
if (parent == 0)
return;
--parent;
}
}
void adjustHeap(alias compare = "a < b", T)(T[] heap, size_t hole, T value)
{
alias binaryFun!compare comp;
const size_t top = hole;
size_t second = hole;
size_t length = heap.length;
while (second < (length - 1) / 2)
{
second = 2 * (second + 1);
if (comp(heap[second], heap[second - 1]))
second--;
heap[hole] = heap[second];
hole = second;
}
if ((length & 1) == 0 && second == (length - 2) / 2)
{
second = 2 * (second + 1);
heap[hole] = heap[second - 1];
hole = second - 1;
}
pushHeap!compare(heap, hole, top, value);
}
private void popHeap(alias compare = "a < b", T)(T[] heap, ref T result)
{
T value = result;
result = heap[0];
adjustHeap!compare(heap, 0, value);
}
private void pushHeap(alias compare = "a < b", T)(T[] heap, size_t hole, size_t top, T value)
{
alias binaryFun!compare comp;
size_t parent = (hole - 1) / 2;
while (hole > top && comp(heap[parent], value))
{
heap[hole] = heap[parent];
hole = parent;
parent = (hole - 1) / 2;
}
heap[hole] = value;
}