2004-12-23 15:16:30 +02:00
|
|
|
#include "vqueue.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2005-01-18 14:18:51 +02:00
|
|
|
#include <stdlib.h>
|
2005-01-21 22:42:33 +02:00
|
|
|
struct __vqueue_t
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
cmph_uint32 * values;
|
|
|
|
cmph_uint32 beg, end, capacity;
|
2004-12-23 15:16:30 +02:00
|
|
|
};
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
vqueue_t * vqueue_new(cmph_uint32 capacity)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-21 22:42:33 +02:00
|
|
|
vqueue_t *q = (vqueue_t *)malloc(sizeof(vqueue_t));
|
2004-12-23 15:16:30 +02:00
|
|
|
assert(q);
|
2005-01-18 23:06:08 +02:00
|
|
|
q->values = (cmph_uint32 *)calloc(capacity+1, sizeof(cmph_uint32));
|
2004-12-23 15:16:30 +02:00
|
|
|
q->beg = q->end = 0;
|
|
|
|
q->capacity = capacity+1;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
cmph_uint8 vqueue_is_empty(vqueue_t * q)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
return (q->beg == q->end);
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void vqueue_insert(vqueue_t * q, cmph_uint32 val)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
assert((q->end + 1)%q->capacity != q->beg); // Is queue full?
|
|
|
|
q->end = (q->end + 1)%q->capacity;
|
|
|
|
q->values[q->end] = val;
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
cmph_uint32 vqueue_remove(vqueue_t * q)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-21 22:42:33 +02:00
|
|
|
assert(!vqueue_is_empty(q)); // Is queue empty?
|
2004-12-23 15:16:30 +02:00
|
|
|
q->beg = (q->beg + 1)%q->capacity;
|
|
|
|
return q->values[q->beg];
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void vqueue_print(vqueue_t * q)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
2005-01-18 23:06:08 +02:00
|
|
|
cmph_uint32 i;
|
2004-12-23 15:16:30 +02:00
|
|
|
for (i = q->beg; i != q->end; i = (i + 1)%q->capacity)
|
|
|
|
fprintf(stderr, "%u\n", q->values[(i + 1)%q->capacity]);
|
|
|
|
}
|
|
|
|
|
2005-01-21 22:42:33 +02:00
|
|
|
void vqueue_destroy(vqueue_t *q)
|
2004-12-23 15:16:30 +02:00
|
|
|
{
|
|
|
|
free(q->values); q->values = NULL;
|
|
|
|
}
|