Commit 731fe730 authored by Matthias Braun's avatar Matthias Braun
Browse files

pqueue: Cleanup

parent 1190192b
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#ifndef FIRM_ADT_PQUEUE_H #ifndef FIRM_ADT_PQUEUE_H
#define FIRM_ADT_PQUEUE_H #define FIRM_ADT_PQUEUE_H
#include <stddef.h>
#include "../begin.h" #include "../begin.h"
/** /**
...@@ -58,14 +60,14 @@ FIRM_API void *pqueue_pop_front(pqueue_t *q); ...@@ -58,14 +60,14 @@ FIRM_API void *pqueue_pop_front(pqueue_t *q);
* @param q The priority queue. * @param q The priority queue.
* @return The length of the queue. * @return The length of the queue.
*/ */
FIRM_API size_t pqueue_length(const pqueue_t *q); FIRM_API size_t pqueue_length(pqueue_t const *q);
/** /**
* Returns true if queue is empty. * Returns true if queue is empty.
* @param q The priority queue. * @param q The priority queue.
* @return 1 if the queue is empty, 0 otherwise. * @return 1 if the queue is empty, 0 otherwise.
*/ */
FIRM_API int pqueue_empty(const pqueue_t *q); FIRM_API int pqueue_empty(pqueue_t const *q);
/** @} */ /** @} */
......
...@@ -5,14 +5,6 @@ ...@@ -5,14 +5,6 @@
/** /**
* @file * @file
* @author Christian Wuerdig, Matthias Braun
* @brief Priority Queue implementation based on the heap datastructure
*/
#include "array.h"
#include "pqueue.h"
#include "panic.h"
/*
* Implements a heap. * Implements a heap.
* *
* Implementation note: It might seem strange that we start indexing at 0 * Implementation note: It might seem strange that we start indexing at 0
...@@ -27,11 +19,17 @@ ...@@ -27,11 +19,17 @@
* left and right child. (At the expense that stuff easily breaks when you make * left and right child. (At the expense that stuff easily breaks when you make
* changes and don't think that the left child of 0 is 0 :-/) * changes and don't think that the left child of 0 is 0 :-/)
* *
* @author Christian Wuerdig, Matthias Braun
* @brief Priority Queue implementation based on the heap datastructure
*/ */
#include "pqueue.h"
#include "array.h"
#include "panic.h"
typedef struct pqueue_el_t { typedef struct pqueue_el_t {
void *data; void *data;
int priority; int priority;
} pqueue_el_t; } pqueue_el_t;
struct pqueue_t { struct pqueue_t {
...@@ -45,24 +43,19 @@ struct pqueue_t { ...@@ -45,24 +43,19 @@ struct pqueue_t {
static void pqueue_heapify(pqueue_t *q, size_t pos) static void pqueue_heapify(pqueue_t *q, size_t pos)
{ {
size_t len = ARR_LEN(q->elems); size_t len = ARR_LEN(q->elems);
while (pos * 2 < len) { while (pos * 2 < len) {
pqueue_el_t tmp; size_t exchange = pos;
size_t exchange = pos; if (q->elems[exchange].priority < q->elems[pos * 2].priority)
if (q->elems[exchange].priority < q->elems[pos * 2].priority) {
exchange = pos * 2; exchange = pos * 2;
}
if ((pos * 2 + 1) < len if ((pos * 2 + 1) < len
&& q->elems[exchange].priority < q->elems[pos * 2 + 1].priority) { && q->elems[exchange].priority < q->elems[pos * 2 + 1].priority)
exchange = pos * 2 + 1; exchange = pos * 2 + 1;
}
if (exchange == pos) if (exchange == pos)
break; break;
tmp = q->elems[pos]; pqueue_el_t tmp = q->elems[pos];
q->elems[pos] = q->elems[exchange]; q->elems[pos] = q->elems[exchange];
q->elems[exchange] = tmp; q->elems[exchange] = tmp;
...@@ -76,9 +69,7 @@ static void pqueue_heapify(pqueue_t *q, size_t pos) ...@@ -76,9 +69,7 @@ static void pqueue_heapify(pqueue_t *q, size_t pos)
static void pqueue_sift_up(pqueue_t *q, size_t pos) static void pqueue_sift_up(pqueue_t *q, size_t pos)
{ {
while (q->elems[pos].priority > q->elems[pos / 2].priority) { while (q->elems[pos].priority > q->elems[pos / 2].priority) {
pqueue_el_t tmp; pqueue_el_t tmp = q->elems[pos];
tmp = q->elems[pos];
q->elems[pos] = q->elems[pos / 2]; q->elems[pos] = q->elems[pos / 2];
q->elems[pos / 2] = tmp; q->elems[pos / 2] = tmp;
...@@ -101,11 +92,10 @@ void del_pqueue(pqueue_t *q) ...@@ -101,11 +92,10 @@ void del_pqueue(pqueue_t *q)
void pqueue_put(pqueue_t *q, void *data, int priority) void pqueue_put(pqueue_t *q, void *data, int priority)
{ {
pqueue_el_t el; pqueue_el_t el = {
.data = data,
el.data = data; .priority = priority
el.priority = priority; };
ARR_APP1(pqueue_el_t, q->elems, el); ARR_APP1(pqueue_el_t, q->elems, el);
pqueue_sift_up(q, ARR_LEN(q->elems) - 1); pqueue_sift_up(q, ARR_LEN(q->elems) - 1);
...@@ -114,30 +104,29 @@ void pqueue_put(pqueue_t *q, void *data, int priority) ...@@ -114,30 +104,29 @@ void pqueue_put(pqueue_t *q, void *data, int priority)
void *pqueue_pop_front(pqueue_t *q) void *pqueue_pop_front(pqueue_t *q)
{ {
switch (ARR_LEN(q->elems)) { switch (ARR_LEN(q->elems)) {
case 0: case 0:
panic("attempt to retrieve element from empty priority queue"); panic("attempt to retrieve element from empty priority queue");
case 1: case 1:
ARR_SHRINKLEN(q->elems, 0); ARR_SHRINKLEN(q->elems, 0);
return q->elems[0].data; return q->elems[0].data;
default: { default: {
void *data = q->elems[0].data; void *data = q->elems[0].data;
size_t len = ARR_LEN(q->elems) - 1; size_t len = ARR_LEN(q->elems) - 1;
q->elems[0] = q->elems[len]; q->elems[0] = q->elems[len];
ARR_SHRINKLEN(q->elems, len); ARR_SHRINKLEN(q->elems, len);
pqueue_heapify(q, 0); pqueue_heapify(q, 0);
return data;
return data; }
}
} }
} }
size_t pqueue_length(const pqueue_t *q) size_t pqueue_length(pqueue_t const *q)
{ {
return ARR_LEN(q->elems); return ARR_LEN(q->elems);
} }
int pqueue_empty(const pqueue_t *q) int pqueue_empty(pqueue_t const *q)
{ {
return ARR_LEN(q->elems) == 0; return ARR_LEN(q->elems) == 0;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment