libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
cqueue.c File Reference
#include <stdlib.h>
#include "queue.h"
#include "types/err.h"
Include dependency graph for cqueue.c:

Go to the source code of this file.

Macros

#define MIN(a, b)
 
#define MAX(a, b)
 

Functions

void cqueue_init (cqueue_t *queue, unsigned int max_length)
 Initializes a circular queue.
 
error_code_t cqueue_add (cqueue_t *queue, int item)
 Add a value to the circular queue.
 
error_code_t cqueue_get (cqueue_t *queue, int *item)
 Get the current value from the circular queue, removing it from the queue.
 
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.
 
int cqueue_isempty (const cqueue_t *queue)
 Check if the given circular queue is empty.
 
int cqueue_isfull (const cqueue_t *queue)
 Check if the given circular queue is full.
 
void cqueue_close (cqueue_t *queue)
 Close the given circular queue.
 

Macro Definition Documentation

◆ MAX

#define MAX ( a,
b )
Value:
(a > b ? a : b)

Definition at line 7 of file cqueue.c.

◆ MIN

#define MIN ( a,
b )
Value:
(a < b ? a : b)

Definition at line 6 of file cqueue.c.

Function Documentation

◆ cqueue_add()

error_code_t cqueue_add ( cqueue_t * queue,
int item )

Add a value to the circular queue.

Parameters
queueThe queue where to add the item.
itemThe value to add.
Returns
ERR_MAX_SIZE_EXCEEDED on queue is full.
ERR_OK on added successful.

Definition at line 20 of file cqueue.c.

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}
int cqueue_isfull(const cqueue_t *queue)
Check if the given circular queue is full.
Definition cqueue.c:74
#define MIN(a, b)
Definition cqueue.c:6
@ ERR_OK
Definition err.h:15
@ ERR_MAX_SIZE_EXCEEDED
Definition err.h:19
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

◆ cqueue_close()

void cqueue_close ( cqueue_t * queue)

Close the given circular queue.

Parameters
Thequeue to be closed.

Definition at line 80 of file cqueue.c.

81{
82 free(queue->data);
83 queue->data = NULL;
84}

◆ cqueue_get()

error_code_t cqueue_get ( cqueue_t * queue,
int * item )

Get the current value from the circular queue, removing it from the queue.

Parameters
queueThe queue where to remove the value.
itemThe pointer where the value will be saved.
Returns
ERR_EMPTY on queue is empty.
ERR_OK on value as get successful.

Definition at line 35 of file cqueue.c.

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}
int cqueue_isempty(const cqueue_t *queue)
Check if the given circular queue is empty.
Definition cqueue.c:68
@ ERR_EMPTY
Definition err.h:16
int _current
The index for the current item.
Definition queue.h:10

◆ cqueue_init()

void cqueue_init ( cqueue_t * queue,
unsigned int max_length )

Initializes a circular queue.

Parameters
queueThe queue to be initialized.
max_lengthThe maximum number of items to fit inside the queue.

Definition at line 10 of file cqueue.c.

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}

◆ cqueue_isempty()

int cqueue_isempty ( const cqueue_t * queue)

Check if the given circular queue is empty.

Parameters
queueThe queue to check if it's empty.
Returns
1 on the queue is empty.
0 on the queue has items.

Definition at line 68 of file cqueue.c.

69{
70 return queue->length == 0;
71}

◆ cqueue_isfull()

int cqueue_isfull ( const cqueue_t * queue)

Check if the given circular queue is full.

Parameters
queueThe queue to check if it's full.
Returns
1 on the queue is full.
0 on the queue is not full.

Definition at line 74 of file cqueue.c.

75{
76 return queue->length >= queue->_max_length;
77}

◆ cqueue_lookup()

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.

Parameters
queueThe queue where to get the value.
itemThe pointer where the value will be saved.
seekThe index of the value to get (0 means the current, 1 the next and so one).
Returns
ERR_EMPTY on queue is empty.
ERR_INVALID_INDEX on the seek is invalid.
ERR_OK on value as get successful.

Definition at line 50 of file cqueue.c.

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}
@ ERR_INVALID_INDEX
Definition err.h:18