forked from arminbiere/gimsatul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
111 lines (100 loc) · 2.34 KB
/
heap.c
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
#include "heap.h"
static struct node *merge_nodes (struct node *a, struct node *b) {
if (!a)
return b;
if (!b)
return a;
assert (a != b);
struct node *parent, *child;
if (b->score > a->score)
parent = b, child = a;
else
parent = a, child = b;
struct node *parent_child = parent->child;
child->next = parent_child;
if (parent_child)
parent_child->prev = child;
child->prev = parent;
parent->child = child;
parent->prev = parent->next = 0;
return parent;
}
static struct node *collapse_node (struct node *node) {
if (!node)
return 0;
struct node *next = node, *tail = 0;
do {
struct node *a = next;
assert (a);
struct node *b = a->next;
if (b) {
next = b->next;
struct node *tmp = merge_nodes (a, b);
assert (tmp);
tmp->prev = tail;
tail = tmp;
} else {
a->prev = tail;
tail = a;
break;
}
} while (next);
struct node *res = 0;
while (tail) {
struct node *prev = tail->prev;
res = merge_nodes (res, tail);
tail = prev;
}
return res;
}
static void deheap_node (struct node *node) {
assert (node);
struct node *prev = node->prev;
struct node *next = node->next;
assert (prev);
node->prev = 0;
if (prev->child == node)
prev->child = next;
else
prev->next = next;
if (next)
next->prev = prev;
}
void pop_heap (struct heap *heap) {
struct node *root = heap->root;
struct node *child = root->child;
heap->root = collapse_node (child);
assert (!heap_contains (heap, root));
#ifndef NDEBUG
assert (heap->last >= root->score);
heap->last = root->score;
#endif
}
void push_heap (struct heap *heap, struct node *node) {
assert (!heap_contains (heap, node));
node->child = 0;
heap->root = merge_nodes (heap->root, node);
assert (heap_contains (heap, node));
#ifndef NDEBUG
if (heap->last < node->score)
heap->last = node->score;
#endif
}
void update_heap (struct heap *heap, struct node *node, double new_score) {
double old_score = node->score;
assert (old_score <= new_score);
if (old_score == new_score)
return;
node->score = new_score;
#ifndef NDEBUG
if (heap->last < new_score)
heap->last = new_score;
#endif
struct node *root = heap->root;
if (root == node)
return;
if (!node->prev)
return;
deheap_node (node);
heap->root = merge_nodes (root, node);
}