-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
42 lines (32 loc) · 739 Bytes
/
stack.h
File metadata and controls
42 lines (32 loc) · 739 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
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include "dynarray.h"
typedef struct {
DynArray(void *) entries;
uint64_t len;
Arena *alloc;
} Stack;
Stack stack_init(Arena *a, uint64_t starting_capacity) {
Stack stack;
stack.alloc = a;
dyn_init(a, &stack.entries, starting_capacity);
stack.len = 0;
return stack;
}
void stack_push(Stack *stack, void *elem) {
if (stack->len >= stack->entries.capacity) {
dyn_resize(&stack->entries, stack->entries.capacity * 2);
}
stack->entries.arr[stack->len] = elem;
stack->len += 1;
}
void *stack_pop(Stack *stack) {
if (stack->len == 0) {
return NULL;
}
stack->len -= 1;
return stack->entries.arr[stack->len];
}