-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
226 lines (168 loc) · 4.32 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Author: Kazuya Suzuki
*
* Copyright (C) 2012 NEC Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "heap.h"
#include "wrapper.h"
static void move_up( heap_t *heap, int index );
static void move_down( heap_t *heap, int index );
static int parent_of( int c_index );
static int min_child_of( heap_t *heap, int p_index );
static void swap_in_heap( heap_t *heap, int i, int j );
heap_t *
create_heap( const compare_heap_function compare, const size_t size ) {
heap_t *heap = xmalloc( sizeof( heap_t ) );
if ( heap == NULL ) {
return NULL;
}
heap->size = size;
heap->element_num = 0;
heap->elements = xcalloc( size, sizeof( void * ) );
heap->compare = compare;
if ( heap->elements == NULL ) {
xfree( heap );
return NULL;
}
return heap;
}
void
destroy_heap( heap_t *heap ) {
if ( heap == NULL ) {
return;
}
xfree( heap->elements );
xfree( heap );
return;
}
bool
push_to_heap( heap_t *heap, void *value ) {
if ( heap == NULL || value == NULL ) {
return false;
}
size_t element_num = heap->element_num;
if ( element_num == heap->size ) {
return false;
}
heap->elements[ element_num ] = value;
heap->element_num++;
move_up( heap, ( int )element_num );
return true;
}
void *
pop_from_heap( heap_t *heap ) {
return remove_from_heap( heap, 0 );
}
void *
remove_from_heap( heap_t *heap, int index ) {
if ( index < 0 || heap->element_num <= ( size_t )index ) {
return NULL;
}
void *value = heap->elements[ index ];
heap->element_num--;
if ( ( size_t )index != heap->element_num ) {
heap->elements[ index ] = heap->elements[ heap->element_num ];
move_down( heap, index );
}
heap->elements[ heap->element_num ] = NULL;
return value;
}
bool
check_heap( heap_t *heap ) {
int c = ( int )heap->element_num - 1;
for ( ; c > 0; c-- ) {
int p = ( c - 1 ) / 2;
void *parent = heap->elements[ p ];
void *child = heap->elements[ c ];
if ( ( *heap->compare )( parent, child ) < 0 ) {
return false;
}
}
return true;
}
static void
move_up( heap_t *heap, int index ) {
int c_index = index;
for (;;) {
int p_index = parent_of( c_index );
if ( p_index < 0 ) return;
void *child = heap->elements[ c_index ];
void *parent = heap->elements[ p_index ];
if ( ( *heap->compare )( parent, child ) <= 0 ) {
break;
}
swap_in_heap( heap, p_index, c_index );
c_index = p_index;
}
}
static void
move_down( heap_t *heap, int index ) {
int p_index = index;
for (;;) {
int c_index = min_child_of( heap, p_index );
if ( c_index < 0 ) return;
void *child = heap->elements[ c_index ];
void *parent = heap->elements[ p_index ];
if ( ( *heap->compare )( parent, child ) <= 0 ) {
break;
}
swap_in_heap( heap, p_index, c_index );
p_index = c_index;
}
}
static int
parent_of( int c_index ) {
if ( c_index <= 0 ) {
return -1;
}
return ( c_index - 1 ) / 2;
}
static int
min_child_of( heap_t *heap, int p_index ) {
if ( p_index < 0 ) {
return -1;
}
int l_index = p_index * 2 + 1;
int r_index = l_index + 1;
if ( l_index < 0 ) {
return -1;
}
if ( ( size_t )l_index >= heap->element_num ) {
return -1;
}
if ( ( size_t )r_index >= heap->element_num ) {
return l_index;
}
void *left = heap->elements[ l_index ];
void *right = heap->elements[ r_index ];
if ( ( *heap->compare )( left, right ) <= 0 ) {
return l_index;
} else {
return r_index;
}
}
static void
swap_in_heap( heap_t *heap, int i, int j ) {
void *temp = heap->elements[ i ];
heap->elements[ i ] = heap->elements[ j ];
heap->elements[ j ] = temp;
}
/*
* Local variables:
* c-basic-offset: 2
* indent-tabs-mode: nil
* End:
*/