-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_dynarray.h
38 lines (32 loc) · 904 Bytes
/
simple_dynarray.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
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
typedef struct {
uint64_t *buffer;
uint64_t size;
uint64_t capacity;
Arena *alloc;
} SimpleDynArray;
SimpleDynArray simple_dyn_init(Arena *a, uint64_t starting_capacity) {
SimpleDynArray arr;
arr.alloc = a;
arr.capacity = starting_capacity;
arr.buffer = arena_alloc(arr.alloc, arr.capacity * sizeof(uint64_t));
arr.size = 0;
return arr;
}
void simple_dyn_append(SimpleDynArray *arr, uint64_t elem) {
if (arr->size + 1 > arr->capacity) {
uint64_t old_capacity = arr->capacity;
arr->capacity *= 2;
arr->buffer = arena_realloc(arr->alloc, arr->buffer, old_capacity * sizeof(uint64_t), arr->capacity * sizeof(uint64_t));
}
arr->buffer[arr->size] = elem;
arr->size += 1;
}
uint64_t simple_dyn_get(SimpleDynArray *arr, uint64_t idx) {
assert(idx <= arr->size);
return arr->buffer[idx];
}