-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
52 lines (36 loc) · 1.39 KB
/
Copy pathstack.h
File metadata and controls
52 lines (36 loc) · 1.39 KB
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
#ifndef STACK_H
#define STACK_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "slice.h"
#define STACK_DEFAULT_CAPACITY 4
// First-in, last-out collection of bytes.
struct Stack {
// Underlying buffer.
struct Slice slice;
// Current capacity of the internal buffer.
size_t capacity;
};
// Initializes an empty stack without allocating yet.
void stack_init(struct Stack * self);
// Creates an empty stack without allocating yet.
struct Stack stack_create(void);
// Creates a empty heap-allocated stack with the given capacity.
void stack_init_with_capacity(struct Stack * self, size_t cap);
// Creates a empty heap-allocated stack with the given capacity.
struct Stack stack_create_with_capacity(size_t cap);
// Increase the capacity of the internal buffer by `len` elements.
void stack_increase_capacity(struct Stack * self, size_t len);
// Conditionally allocates if the capacity is zero.
void stack_ensure_has_capacity(struct Stack * self);
bool stack_has_vacancy(const struct Stack * self);
void stack_ensure_has_vacancy(struct Stack * self);
void stack_free(struct Stack * self);
// Peek at the top of the stack.
uint8_t stack_peek(struct Stack const * self);
// Push an element to the back of the stack.
void stack_push(struct Stack * self, uint8_t item);
// Pop an element off the top of the stack.
uint8_t stack_pop(struct Stack * self);
#endif // !STACK_H