-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_queue.c
62 lines (55 loc) · 1.77 KB
/
static_queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "static_queue.h"
#include <stdlib.h> // malloc
#include <string.h> // memset
static_queue_t* static_queue_create(int element_size, int capacity)
{
static_queue_t* queue = (static_queue_t*)malloc(sizeof(static_queue_t));
memset(queue, 0, sizeof(static_queue_t));
queue->capacity = capacity;
queue->element_size = element_size;
queue->buffer = malloc(queue->element_size * queue->capacity);
memset(queue->buffer, 0, queue->element_size * queue->capacity);
// points to the next vacant place at which new element can be pushed
queue->front = 0;
// points to the next filled place from which an element can be popped
queue->back = 0;
return queue;
}
void static_queue_destroy(static_queue_t* queue)
{
free(queue->buffer);
free(queue);
}
void static_queue_push(static_queue_t* queue, void* value)
{
if(queue->front >= queue->capacity)
{
// make a vacant place on the left most side
if(queue->back == 0)
queue->back = 1;
// shift all the elements to the left most to fill the vacant places on the back side
memmove(queue->buffer, queue->buffer + queue->back, queue->element_size * (queue->front - queue->back));
queue->front -= queue->back;
queue->back = 0;
}
memcpy(queue->buffer + queue->front * queue->element_size, value, queue->element_size);
queue->front += 1;
}
bool static_queue_pop(static_queue_t* queue, void* value)
{
if(static_queue_get_count(queue) > 0)
{
memcpy(value, queue->buffer + queue->back * queue->element_size, queue->element_size);
queue->back += 1;
return true;
}
return false;
}
bool static_queue_is_empty(static_queue_t* queue)
{
return static_queue_get_count(queue) <= 0;
}
int static_queue_get_count(static_queue_t* queue)
{
return queue->front - queue->back;
}