-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.c
130 lines (110 loc) · 3.47 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Generic queue, implemented as a circular buffer.
Author: David De Potter
LICENSE: MIT, see LICENSE file in repository root folder
*/
#include <stdio.h>
#include "queue.h"
#include "../../lib/clib.h"
//===================================================================
// Creates a new queue with given capacity
queue *newQueue(size_t cap) {
queue *Q = safeCalloc(1, sizeof(queue));
Q->buffer = safeCalloc(cap, sizeof(void *));
Q->capacity = cap;
Q->delim = ", ";
return Q;
}
//===================================================================
// Sets queue to own the input data, deallocating
// what is still in the queue when it is destroyed
void setQueueOwner(queue *Q, freeQueueItem free) {
Q->free = free;
}
//===================================================================
// Makes the queue operate on copies of the input data
void setQueueCopy(queue *Q, copyQueueItem copy,
freeQueueItem free) {
Q->copy = copy;
Q->free = free;
}
//===================================================================
// Deallocates the queue
void freeQueue(queue *Q) {
if (!Q) return;
if (Q->free) {
// free all items stored in the circular buffer
for (size_t i = Q->head; i != Q->tail;
i = (i + 1) % Q->capacity)
Q->free(Q->buffer[i]);
}
free(Q->buffer);
free(Q);
}
//===================================================================
// Sets the show function for the queue
void setQueueShow(queue *Q, showQueueItem show) {
Q->show = show;
}
//===================================================================
// Sets the string delimiter for showing items
void setQueueDelim(queue *Q, char *delim) {
Q->delim = delim;
}
//===================================================================
// Doubles the capacity of the queue
static void checkCapacity(queue *Q) {
if (Q->tail != Q->head) return;
Q->buffer = safeRealloc(Q->buffer,
2 * Q->capacity * sizeof(void *));
for (size_t i = 0; i < Q->tail; ++i)
Q->buffer[i + Q->capacity] = Q->buffer[i];
Q->tail += Q->capacity;
Q->capacity *= 2;
}
//===================================================================
// Enqueues an item in the queue
void enqueue (queue *Q, void *item) {
if (Q->copy)
item = Q->copy(item);
Q->buffer[Q->tail] = item;
Q->tail = (Q->tail + 1) % Q->capacity;
checkCapacity(Q);
Q->size++;
}
//===================================================================
// Dequeues an item from the queue and returns it
void *dequeue (queue *Q) {
if (isEmptyQueue(Q))
return NULL;
void *item = Q->buffer[Q->head];
Q->head = (Q->head + 1) % Q->capacity;
Q->size--;
return item;
}
//===================================================================
// Peeks at the front of the queue
void *peekHead (queue *Q) {
if (isEmptyQueue(Q))
return NULL;
return Q->buffer[Q->head];
}
//===================================================================
// Peeks at the back of the queue
void *peekTail (queue *Q) {
if (isEmptyQueue(Q))
return NULL;
return Q->buffer[(Q->tail - 1) % Q->capacity];
}
//===================================================================
// Shows the entire queue
void showQueue(queue *Q) {
if (! Q->show) {
fprintf(stderr, "ShowQueue error: show function not set\n");
return;
}
for (size_t i = Q->head; i != Q->tail; i = (i + 1) % Q->capacity) {
Q->show(Q->buffer[i]);
printf("%s", (i + 1) % Q->capacity == Q->tail ? "\n" : Q->delim);
}
}