libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
cqueue.c
Go to the documentation of this file.
1#include <stdlib.h>
2
3#include "queue.h"
4#include "types/err.h"
5
6#define MIN(a, b) (a < b ? a : b)
7#define MAX(a, b) (a > b ? a : b)
8
9void
10cqueue_init(cqueue_t* queue, unsigned int max_length)
11{
12 queue->length = 0;
13 queue->_current = 0;
14 queue->_last = -1;
15 queue->_max_length = max_length;
16 queue->data = malloc(sizeof(int) * max_length);
17}
18
20cqueue_add(cqueue_t* queue, int item)
21{
22 if (cqueue_isfull(queue)) {
24 }
25
26 queue->_last = (queue->_last + 1) % queue->_max_length;
27 queue->data[queue->_last] = item;
28
29 queue->length = MIN(queue->length + 1, queue->_max_length);
30
31 return ERR_OK;
32}
33
35cqueue_get(cqueue_t* queue, int* item)
36{
37 if (cqueue_isempty(queue)) {
38 return ERR_EMPTY;
39 }
40
41 *item = queue->data[queue->_current];
42
43 queue->_current = (queue->_current + 1) % queue->_max_length;
44 queue->length--;
45
46 return ERR_OK;
47}
48
50cqueue_lookup(const cqueue_t* queue, int* item, unsigned int seek)
51{
52 if (cqueue_isempty(queue)) {
53 return ERR_EMPTY;
54 }
55
56 unsigned int index = (queue->_current + seek) % queue->_max_length;
57
58 if ((seek >= queue->length) || (queue->_last > queue->_current && index > queue->_last) ||
59 (queue->_last < queue->_current && index < queue->_current && index > queue->_last)) {
60 return ERR_INVALID_INDEX;
61 }
62
63 *item = queue->data[index];
64 return ERR_OK;
65}
66
67int
69{
70 return queue->length == 0;
71}
72
73int
75{
76 return queue->length >= queue->_max_length;
77}
78
79void
81{
82 free(queue->data);
83 queue->data = NULL;
84}
int cqueue_isfull(const cqueue_t *queue)
Check if the given circular queue is full.
Definition cqueue.c:74
void cqueue_close(cqueue_t *queue)
Close the given circular queue.
Definition cqueue.c:80
error_code_t cqueue_add(cqueue_t *queue, int item)
Add a value to the circular queue.
Definition cqueue.c:20
#define MIN(a, b)
Definition cqueue.c:6
error_code_t cqueue_get(cqueue_t *queue, int *item)
Get the current value from the circular queue, removing it from the queue.
Definition cqueue.c:35
error_code_t cqueue_lookup(const cqueue_t *queue, int *item, unsigned int seek)
Get a value from the circular queue, without removing it from the queue.
Definition cqueue.c:50
void cqueue_init(cqueue_t *queue, unsigned int max_length)
Initializes a circular queue.
Definition cqueue.c:10
int cqueue_isempty(const cqueue_t *queue)
Check if the given circular queue is empty.
Definition cqueue.c:68
enum error_code error_code_t
Enumeration of error codes.
@ ERR_INVALID_INDEX
Definition err.h:18
@ ERR_OK
Definition err.h:15
@ ERR_EMPTY
Definition err.h:16
@ ERR_MAX_SIZE_EXCEEDED
Definition err.h:19
int _current
The index for the current item.
Definition queue.h:10
unsigned int _max_length
The maximum number of items that fits on the queue.
Definition queue.h:9
int _last
The index of the last item.
Definition queue.h:11
int * data
Pointer to queue data with allocated space to fit max_size items.
Definition queue.h:12
unsigned int length
The current number of items on the queue.
Definition queue.h:8
struct cqueue cqueue_t
Circular queue of integers.