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
|
/**
* @file pqueue.c
* @brief Implements a priority queue.
* @note Copyright (c) 2015 Richard Cochran <richardcochran@gmail.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* 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-1335 USA.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "pqueue.h"
#define parent(x) (((1 + (x)) >> 1) - 1)
#define left(x) (((1 + (x)) << 1) - 1)
#define right(x) (((1 + (x)) << 1))
struct pqueue {
int len;
int max;
int (*cmp)(void *a, void *b);
void **data;
};
static int pq_greater(struct pqueue *q, int a, int b)
{
return q->cmp(q->data[a], q->data[b]) > 0 ? 1 : 0;
}
static void heapify(struct pqueue *q, int index)
{
int i_max = index;
int left = left(index);
int right = right(index);
if (left < q->len) {
if (pq_greater(q, left, i_max))
i_max = left;
}
if (right < q->len && pq_greater(q, right, i_max)) {
i_max = right;
}
if (i_max != index) {
void *tmp = q->data[index];
q->data[index] = q->data[i_max];
q->data[i_max] = tmp;
heapify(q, i_max);
}
}
/* public methods */
struct pqueue *pqueue_create(int max_length,
int (*compare)(void *a, void *b))
{
struct pqueue *q = calloc(1, sizeof(*q));
if (!q) {
return NULL;
}
q->len = 0;
q->max = max_length;
q->cmp = compare;
q->data = calloc(max_length, sizeof(void *));
if (!q->data) {
free(q);
return NULL;
}
return q;
}
void pqueue_destroy(struct pqueue *q)
{
free(q->data);
free(q);
}
void *pqueue_extract(struct pqueue *q)
{
void *data;
if (!q->len) {
return NULL;
}
data = q->data[0];
q->data[0] = q->data[q->len - 1];
q->len--;
heapify(q, 0);
return data;
}
int pqueue_insert(struct pqueue *q, void *d)
{
int index;
if (q->len >= q->max) {
void **buf = realloc(q->data, 2 * q->max * sizeof(void *));
if (buf) {
q->data = buf;
q->max *= 2;
} else {
return -ENOMEM;
}
}
index = q->len;
q->len++;
while (index && (q->cmp(q->data[parent(index)], d) < 0)) {
q->data[index] = q->data[parent(index)];
index = parent(index);
}
q->data[index] = d;
return 0;
}
int pqueue_length(struct pqueue *q)
{
return q->len;
}
void *pqueue_peek(struct pqueue *q)
{
return q->len ? q->data[0] : (void *) 0;
}
|