-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque.h
49 lines (43 loc) · 1.22 KB
/
deque.h
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
#pragma once
typedef struct Deque {
size_t capacity;
size_t count;
ptrdiff_t first;
intptr_t * values;
} Deque_t;
void deque_init (struct Deque * deque, size_t capacity) {
deque->capacity = capacity;
deque->count = 0;
deque->first = 0;
if (capacity) {
deque->values = malloc(sizeof(intptr_t)*capacity);
} else {
deque->values = NULL;
}
}
void deque_copy (struct Deque * dest, struct Deque const * src) {
deque_init(dest, src->capacity);
dest->count = src->count;
dest->first = src->first;
memcpy(dest->values, src->values, sizeof(intptr_t)*src->capacity);
}
void deque_free (struct Deque * deque) {
free(deque->values);
deque->capacity = 0;
deque->count = 0;
deque->first = 0;
deque->values = NULL;
}
void deque_push (struct Deque * deque, intptr_t value) {
ASSERT(deque->count < deque->capacity);
ptrdiff_t index = (deque->first + deque->count) % deque->capacity;
deque->values[index] = value;
deque->count += 1;
}
intptr_t deque_pop (struct Deque * deque) {
ASSERT(deque->count);
intptr_t value = deque->values[deque->first];
deque->first = (deque->first + 1) % deque->capacity;
deque->count -= 1;
return value;
}