-
Notifications
You must be signed in to change notification settings - Fork 3
/
priority_queue.c
396 lines (348 loc) · 10.6 KB
/
priority_queue.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Module priority_queue.c
* This module contains a set of functions that assures a
* maintenance of a priority queue. Its creating, inserting/deleting
* items, expanding queue, reducing, etc.
*
* Dialect : ANSI C
* Compiler: any ANSI C-compatible
*
* Copyright (c) 2014 Martin Váňa
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef PRIORITY_QUEUE_C
#define PRIORITY_QUEUE_C
/* C standard libraries */
#include <stdio.h>
#include <stdlib.h>
/* My libraries */
#include "priority_queue.h" /* Priority queue definition and routines */
#include "constants.h" /* Contains constants */
/* Minimum priority queue size */
const int MIN_PRIORITY_QUEUE_CAPACITY = 16;
/*
* Returns greater integer.
*/
static int imax( int a, int b )
{
return a > b ? a : b;
}
/*
* Creates priority queue with initial capacity.
* Returns priority queue on success, NULL otherwise.
*/
priority_queue *create_priority_queue( const int capacity, int ( *comparator )( const void *, const void * ) )
{
int i;
priority_queue *pq = NULL;
if ( capacity < MIN_PRIORITY_QUEUE_CAPACITY )
{
printf( "\nBad priority queue parameters. Minimum priority queue capacity is %d.\n", MIN_PRIORITY_QUEUE_CAPACITY );
return NULL;
}
else
{
pq = ( priority_queue * ) malloc( sizeof( priority_queue ) );
if ( pq == NULL )
{
printf( "\nPriority queue memory allocation failed.\n" );
return NULL;
}
pq->comparator = comparator;
pq->min_capacity = capacity;
pq->capacity = capacity;
pq->heap_size = 0;
pq->heap_array = malloc( capacity * sizeof( void * ) );
if ( pq->heap_array == NULL )
{
printf( "Priority queue heap memory allocation failed.\n" );
free( pq );
return NULL;
}
for ( i = 0; i < capacity; i++ )
{
pq->heap_array[ i ] = NULL;
}
return pq;
}
}
/*
* Performs swap in priority queue.
* Swaps pointers.
*/
static void swap( priority_queue *pq, const int key_1, const int key_2 )
{
void *tmp = NULL;
tmp = pq->heap_array[ key_1 ];
pq->heap_array[ key_1 ] = pq->heap_array[ key_2 ];
pq->heap_array[ key_2 ] = tmp;
}
/*
* Repairs properties of heap from the top.
*/
static void repair_top( priority_queue *pq, int key, int heap_size )
{
int j;
while( 2 * key <= heap_size )
{
j = 2 * key;
if( j < heap_size && pq->comparator( pq->heap_array[ j ], pq->heap_array[ j + 1 ] ) == GREATER )
{
j++;
}
if( pq->comparator( pq->heap_array[ key ], pq->heap_array[ j ] ) == SMALLER )
{
break;
}
swap( pq, key, j );
key = j;
}
}
/*
* Repairs properties of heap from the bottom.
*/
static void repair_bottom( priority_queue *pq, int key )
{
while( key > 1 && pq->comparator( pq->heap_array[ key / 2], pq->heap_array[ key ] ) == GREATER )
{
swap( pq, key, key / 2 );
key = key / 2;
}
}
/*
* Ensures sufficient capacity.
* Returns TRUE on success, FALSE otherwise.
*/
static int ensure_capacity( priority_queue *pq, const int min_capacity )
{
int old_capacity, new_capacity;
void **new_heap_array;
old_capacity = pq->capacity;
if( min_capacity > old_capacity )
{
/* New capacity formula took over Java Arraylist */
new_capacity = ( old_capacity * 3 ) / 2 + 1;
if( new_capacity < min_capacity )
{
new_capacity = min_capacity;
}
new_heap_array = ( void ** ) realloc( ( void * ) pq->heap_array, new_capacity * sizeof( void * ) );
if( new_heap_array == NULL )
{
return FALSE;
}
pq->heap_array = new_heap_array;
pq->capacity = new_capacity;
}
return TRUE;
}
/*
* Inserts item to the queue.
* Returns TRUE on success, FALSE otherwise.
*/
int priority_queue_insert( priority_queue *pq, void *item )
{
if ( pq == NULL )
{
printf( "Priority queue is uninitialized.\n" );
return FALSE;
}
else if ( item == NULL )
{
printf( "Item pointer is NULL.\n" );
return FALSE;
}
else
{
/* 2 = 1 for spare item in array + 1 for next item */
if( ensure_capacity( pq, pq->heap_size + 2 ) == FALSE )
{
printf( "\nPriority queue out of memory.\n" );
return FALSE;
}
pq->heap_size += 1;
pq->heap_array[ pq->heap_size ] = item;
repair_bottom( pq, pq->heap_size );
return TRUE;
}
return FALSE;
}
/*
* Returns pointer to an item on the peek.
* Does not remove item from queue.
*/
void *priority_queue_peek( priority_queue *pq )
{
if ( pq->heap_size > 0 )
{
return pq->heap_array[ 1 ];
}
return NULL;
}
/*
* Returns pointer to an item on the peek.
* Does remove item from queue.
*/
void *priority_queue_poll( priority_queue *pq )
{
if ( pq == NULL )
{
printf( "Priority queue is uninitialized.\n" );
return NULL;
}
else
{
if ( pq->heap_size != 0 )
{
swap( pq, 1, pq->heap_size );
repair_top( pq, 1, pq->heap_size - 1 );
pq->heap_size--;
return pq->heap_array[ pq->heap_size + 1 ];
}
return NULL;
}
}
/*
* Checks if the priority is queue empty.
* Returns TRUE if queue is empty, FALSE if queue is non empty,
* ERROR_VALUE if queue is uninitialized.
*/
int priority_queue_is_empty( const priority_queue *pq )
{
if ( pq == NULL )
{
printf( "Priority queue is uninitialized.\n" );
return ERROR_VALUE;
}
else
{
if ( pq->heap_size == 0 )
{
return TRUE;
}
else
{
return FALSE;
}
}
}
/*
* Returns number of items in the priority queue.
* Returns ERROR_VALUE if queue is uninitialized.
*/
int priority_queue_size( const priority_queue *pq )
{
if ( pq == NULL )
{
printf( "Priority queue is uninitialized.\n" );
return ERROR_VALUE;
}
else
{
return pq->heap_size;
}
}
/*
* Reduces priority queue size to number of inserted items.
* Returns TRUE on success, FALSE otherwise.
*/
int priority_queue_trim_to_size( priority_queue *pq )
{
int new_capacity;
void **new_heap_array;
if( pq == NULL )
{
printf( "Priority queue is uninitialized.\n" );
return FALSE;
}
else
{
new_capacity = imax( pq->heap_size + 1, pq->min_capacity );
if( pq->capacity > new_capacity )
{
new_heap_array = ( void ** ) realloc( pq->heap_array, new_capacity * sizeof( void * ) );
if( new_heap_array == NULL )
{
return FALSE;
}
pq->heap_array = new_heap_array;
pq->capacity = new_capacity;
}
/* Even no trimming is success */
return TRUE;
}
}
/*
* Frees data stores in priority queue using free function.
*/
void free_priority_queue_data( priority_queue **pq, void (*free_function)( void * ) )
{
int i;
for ( i = 1; i <= (*pq)->heap_size; i++ )
{
free_function( (*pq)->heap_array[ i ] );
}
(*pq)->heap_size = 0;
}
/*
* Frees priority queue.
*/
void free_priority_queue( priority_queue **pq )
{
if ( *pq == NULL )
{
printf( "Priority queue is uninitialized.\n" );
}
else
{
printf( "Frees priority queue memory..." );
free( (*pq)->heap_array );
free( (*pq) );
(*pq) = NULL;
printf( "OK\n" );
}
}
/*
* Prints content in priority queue as it is stored in array.
*/
void print_priority_queue( const priority_queue *pq, char* (*to_string)( void * ) )
{
int i;
char *item_string;
if ( pq == NULL )
{
printf( "Priority queue is uninitialized.\n" );
}
else
{
printf( "%d [", pq->heap_size );
for ( i = 1; i <= pq->heap_size; i++ )
{
item_string = to_string( pq->heap_array[ i ] );
printf( " %s ", item_string );
free( item_string );
}
printf( "]\n" );
}
}
#endif