Commit 86f02927 authored by Vitezslav Kriz's avatar Vitezslav Kriz

heap: typedef changed

parent 31e0b1b6
......@@ -36,10 +36,10 @@
#include <stdlib.h>
#include "contrib/ucw/heap.h"
static inline void heap_swap(heap_val_t *e1, heap_val_t *e2)
static inline void heap_swap(heap_val_t **e1, heap_val_t **e2)
{
if (e1 == e2) return; /* Stack tmp should be faster than tmpelem. */
heap_val_t tmp = *e1; /* Even faster than 2-XOR nowadays. */
heap_val_t *tmp = *e1; /* Even faster than 2-XOR nowadays. */
*e1 = *e2;
*e2 = tmp;
int pos = (*e1)->pos;
......@@ -54,7 +54,7 @@ int heap_init(struct heap *h, int (*cmp)(void *, void *), int init_size)
h->num = 0;
h->max_size = isize;
h->cmp = cmp;
h->data = malloc((isize + 1) * sizeof(heap_val_t)); /* Temp element unused. */
h->data = malloc((isize + 1) * sizeof(heap_val_t*)); /* Temp element unused. */
return h->data ? 1 : 0;
}
......@@ -86,21 +86,21 @@ static inline void _heap_bubble_up(struct heap *h, int e)
}
static void heap_increase(struct heap *h, int pos, heap_val_t e)
static void heap_increase(struct heap *h, int pos, heap_val_t *e)
{
*HELEMENT(h, pos) = e;
e->pos = pos;
_heap_bubble_down(h, pos);
}
static void heap_decrease(struct heap *h, int pos, heap_val_t e)
static void heap_decrease(struct heap *h, int pos, heap_val_t *e)
{
*HELEMENT(h, pos) = e;
e->pos = pos;
_heap_bubble_up(h, pos);
}
void heap_replace(struct heap *h, int pos, heap_val_t e)
void heap_replace(struct heap *h, int pos, heap_val_t *e)
{
if (h->cmp(*HELEMENT(h, pos),e) < 0) {
heap_increase(h, pos, e);
......@@ -121,12 +121,12 @@ void heap_delmin(struct heap *h)
_heap_bubble_down(h, 1);
}
int heap_insert(struct heap *h, heap_val_t e)
int heap_insert(struct heap *h, heap_val_t *e)
{
if(h->num == h->max_size)
{
h->max_size = h->max_size * HEAP_INCREASE_STEP;
h->data = realloc(h->data, (h->max_size + 1) * sizeof(heap_val_t));
h->data = realloc(h->data, (h->max_size + 1) * sizeof(heap_val_t*));
if (!h->data) {
return 0;
}
......@@ -139,7 +139,7 @@ int heap_insert(struct heap *h, heap_val_t e)
return 1;
}
int heap_find(struct heap *h, heap_val_t elm)
int heap_find(struct heap *h, heap_val_t *elm)
{
return ((struct heap_val *) elm)->pos;
}
......@@ -155,6 +155,6 @@ void heap_delete(struct heap *h, int e)
if ((h->num > INITIAL_HEAP_SIZE) && (h->num < h->max_size / HEAP_DECREASE_THRESHOLD))
{
h->max_size = h->max_size / HEAP_INCREASE_STEP;
h->data = realloc(h->data, (h->max_size + 1) * sizeof(heap_val_t));
h->data = realloc(h->data, (h->max_size + 1) * sizeof(heap_val_t*));
}
}
......@@ -20,13 +20,13 @@ struct heap_val {
int pos;
};
typedef struct heap_val* heap_val_t;
typedef struct heap_val heap_val_t;
struct heap {
int num; /* Number of elements */
int max_size; /* Size of allocated memory */
int (*cmp)(void *, void *);
heap_val_t *data;
heap_val_t **data;
}; /* Array follows */
#define INITIAL_HEAP_SIZE 512 /* initial heap size */
......@@ -38,7 +38,7 @@ struct heap {
int heap_init(struct heap *, int (*cmp)(), int);
void heap_delmin(struct heap *);
int heap_insert(struct heap *, heap_val_t);
int heap_find(struct heap *, heap_val_t);
int heap_insert(struct heap *, heap_val_t *);
int heap_find(struct heap *, heap_val_t *);
void heap_delete(struct heap *, int);
void heap_replace(struct heap *h, int pos, heap_val_t e);
void heap_replace(struct heap *h, int pos, heap_val_t *);
......@@ -193,11 +193,11 @@ int evsched_schedule(event_t *ev, uint32_t dt)
ev->tv = new_time;
/* Make sure it's not already enqueued. */
int found = heap_find(&sched->heap, (heap_val_t)ev);
int found = heap_find(&sched->heap, (heap_val_t *)ev);
if (found > 0) {
heap_replace(&sched->heap, found, (heap_val_t)ev);
heap_replace(&sched->heap, found, (heap_val_t *)ev);
} else {
heap_insert(&sched->heap, (heap_val_t)ev);
heap_insert(&sched->heap, (heap_val_t *)ev);
}
/* Unlock calendar. */
......@@ -218,7 +218,7 @@ int evsched_cancel(event_t *ev)
/* Lock calendar. */
pthread_mutex_lock(&sched->heap_lock);
int found = heap_find(&sched->heap, (heap_val_t)ev);
int found = heap_find(&sched->heap, (heap_val_t *)ev);
if (found > 0) {
heap_delete(&sched->heap, found);
}
......
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