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

pqueue: Cleanup

parent 1190192b
......@@ -13,6 +13,8 @@
#ifndef FIRM_ADT_PQUEUE_H
#define FIRM_ADT_PQUEUE_H
#include <stddef.h>
#include "../begin.h"
/**
......@@ -58,14 +60,14 @@ FIRM_API void *pqueue_pop_front(pqueue_t *q);
* @param q The priority 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.
* @param q The priority queue.
* @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 @@
/**
* @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.
*
* Implementation note: It might seem strange that we start indexing at 0
......@@ -27,7 +19,13 @@
* 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 :-/)
*
* @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 {
void *data;
......@@ -45,24 +43,19 @@ struct pqueue_t {
static void pqueue_heapify(pqueue_t *q, size_t pos)
{
size_t len = ARR_LEN(q->elems);
while (pos * 2 < len) {
pqueue_el_t tmp;
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;
}
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;
}
if (exchange == pos)
break;
tmp = q->elems[pos];
pqueue_el_t tmp = q->elems[pos];
q->elems[pos] = q->elems[exchange];
q->elems[exchange] = tmp;
......@@ -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)
{
while (q->elems[pos].priority > q->elems[pos / 2].priority) {
pqueue_el_t tmp;
tmp = q->elems[pos];
pqueue_el_t tmp = q->elems[pos];
q->elems[pos] = q->elems[pos / 2];
q->elems[pos / 2] = tmp;
......@@ -101,11 +92,10 @@ void del_pqueue(pqueue_t *q)
void pqueue_put(pqueue_t *q, void *data, int priority)
{
pqueue_el_t el;
el.data = data;
el.priority = priority;
pqueue_el_t el = {
.data = data,
.priority = priority
};
ARR_APP1(pqueue_el_t, q->elems, el);
pqueue_sift_up(q, ARR_LEN(q->elems) - 1);
......@@ -126,18 +116,17 @@ void *pqueue_pop_front(pqueue_t *q)
q->elems[0] = q->elems[len];
ARR_SHRINKLEN(q->elems, len);
pqueue_heapify(q, 0);
return data;
}
}
}
size_t pqueue_length(const pqueue_t *q)
size_t pqueue_length(pqueue_t const *q)
{
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;
}
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