2016-06-09 19:11:20 +00:00
|
|
|
#ifndef QUEUE_H
|
|
|
|
#define QUEUE_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-11-11 16:07:45 +00:00
|
|
|
#define QUEUE_SIZE 5000
|
2016-06-09 19:11:20 +00:00
|
|
|
|
|
|
|
#define ERROR(condition, msg, ...) if(condition){fprintf(stderr, msg, ##__VA_ARGS__); exit(1);}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int start;
|
|
|
|
unsigned int end;
|
|
|
|
int data[QUEUE_SIZE];
|
|
|
|
} queue_t;
|
|
|
|
|
|
|
|
static void queue_init(queue_t *q)
|
|
|
|
{
|
|
|
|
q->start = q->end = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void queue_put(queue_t *q, int x)
|
|
|
|
{
|
|
|
|
q->data[q->end++] = x;
|
|
|
|
q->end %= QUEUE_SIZE;
|
|
|
|
|
|
|
|
ERROR(q->start == q->end, "The queue is full! Increase QUEUE_SIZE\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int queue_get(queue_t *q)
|
|
|
|
{
|
|
|
|
if(q->start == q->end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int result = q->data[q->start++];
|
|
|
|
q->start %= QUEUE_SIZE;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|