/
minheap.c
142 lines (118 loc) · 3.83 KB
/
minheap.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
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
/*
Copyright (c) 2017-2020 ByteBit
This file is part of BetterSpades.
BetterSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BetterSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <stdint.h>
#include "minheap.h"
static void nodes_swap(struct minheap* h, int a, int b) {
struct minheap_block tmp;
tmp = h->nodes[a];
h->nodes[a] = h->nodes[b];
h->nodes[b] = tmp;
}
void minheap_create(struct minheap* h) {
h->index = 0;
h->length = 256;
h->nodes = malloc(sizeof(struct minheap_block) * h->length);
}
void minheap_clear(struct minheap* h) {
h->index = 0;
h->length = 256;
h->nodes = realloc(h->nodes, sizeof(struct minheap_block) * h->length);
}
void minheap_destroy(struct minheap* h) {
free(h->nodes);
}
int minheap_isempty(struct minheap* h) {
return h->index <= 0;
}
struct minheap_block minheap_extract(struct minheap* h) {
struct minheap_block min = h->nodes[0];
h->nodes[0] = h->nodes[--h->index];
// now heapify at root node
int k = 0;
while(1) {
int smallest = k;
if(k * 2 + 1 < h->index
&& pos_keyy(h->nodes[k * 2 + 1].pos)
< pos_keyy(h->nodes[smallest].pos)) // does left child exist and is less than parent?
smallest = k * 2 + 1;
if(k * 2 + 2 < h->index
&& pos_keyy(h->nodes[k * 2 + 2].pos)
< pos_keyy(h->nodes[smallest].pos)) // does right child exist and is less than parent?
smallest = k * 2 + 2;
if(smallest == k) // parent is smallest, finished!
break;
nodes_swap(h, k, smallest);
k = smallest;
}
return min;
}
static void minheap_increase(struct minheap* h, struct minheap_block* b, int value) {
b->pos = pos_key(pos_keyx(b->pos), value, pos_keyy(b->pos));
int k = b - h->nodes;
while(1) {
int smallest = k;
if(k * 2 + 1 < h->index
&& pos_keyy(h->nodes[k * 2 + 1].pos)
< pos_keyy(h->nodes[smallest].pos)) // does left child exist and is less than parent?
smallest = k * 2 + 1;
if(k * 2 + 2 < h->index
&& pos_keyy(h->nodes[k * 2 + 2].pos)
< pos_keyy(h->nodes[smallest].pos)) // does right child exist and is less than parent?
smallest = k * 2 + 2;
if(smallest == k) // parent is smallest, finished!
break;
nodes_swap(h, k, smallest);
k = smallest;
}
}
static void minheap_decrease(struct minheap* h, struct minheap_block* b, int value) {
b->pos = pos_key(pos_keyx(b->pos), value, pos_keyy(b->pos));
int k = b - h->nodes;
while(k > 0) {
if(pos_keyy(h->nodes[k].pos) < pos_keyy(h->nodes[(k - 1) / 2].pos)) { // is child less than parent?
nodes_swap(h, k, (k - 1) / 2);
k = (k - 1) / 2; // continue at parent
} else {
break;
}
}
}
void minheap_set(struct minheap* h, struct minheap_block* b, int value) {
if(value > pos_keyy(b->pos))
minheap_increase(h, b, value);
else
minheap_decrease(h, b, value);
}
struct minheap_block* minheap_put(struct minheap* h, struct minheap_block* b) {
if(h->index >= h->length) { // grow buffer
h->length *= 2;
h->nodes = realloc(h->nodes, sizeof(struct minheap_block) * h->length);
}
h->nodes[h->index++] = *b; // place new node at end of heap
int k = h->index - 1;
while(k > 0) {
if(pos_keyy(h->nodes[k].pos) < pos_keyy(h->nodes[(k - 1) / 2].pos)) { // is child less than parent?
nodes_swap(h, k, (k - 1) / 2);
k = (k - 1) / 2; // continue at parent
} else {
break;
}
}
return h->nodes + k;
}