-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
30 lines (26 loc) · 741 Bytes
/
queue.cpp
File metadata and controls
30 lines (26 loc) · 741 Bytes
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
#include "queue.h"
#include <assert.h>
// Initialize a circular queue with specified size.
void *queue_initf(void *a, size_t elemsize, size_t len)
{
a = NULL; // Squelch error.
void *b = malloc((len + 1) * elemsize + sizeof(Queue));
b = (char *)b + sizeof(Queue);
queue_header(b)->cap = len + 1;
queue_header(b)->front = 0;
queue_header(b)->back = 0;
return b;
}
void queue_free(void *a) { free(queue_header(a)); }
long queue_len(const void *a)
{
Queue *q = queue_header(a);
return (q->back + q->cap - q->front) % q->cap;
}
// Pop an element from the front of the queue.
void queue_popf(void *a)
{
Queue *q = queue_header(a);
if (!queue_empty(a))
q->front = (q->front + 1) % q->cap;
}