array
This commit is contained in:
@@ -3,11 +3,14 @@
|
||||
#elif PLATFORM_WINDOWS
|
||||
#pragma comment(lib, "user32.lib")
|
||||
#pragma comment(lib, "DbgHelp.lib")
|
||||
#pragma comment(lib, "Shell32.lib")
|
||||
#pragma comment(lib, "Ole32.lib")
|
||||
#define NOMINMAX
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <TlHelp32.h>
|
||||
#include <DbgHelp.h>
|
||||
#include <Shlobj.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
@@ -59,6 +62,7 @@
|
||||
#include "core_type_info.c"
|
||||
#include "core_hash.c"
|
||||
#include "core_hash_table.c"
|
||||
#include "core_array.c"
|
||||
#ifndef DONT_INCLUDE_GENERATED_MATH
|
||||
#include "core_math.gen.c"
|
||||
#endif
|
||||
|
||||
98
src/core/core_array.c
Normal file
98
src/core/core_array.c
Normal file
@@ -0,0 +1,98 @@
|
||||
typedef struct array_header_t array_header_t;
|
||||
struct array_header_t {
|
||||
ma_arena_t *arena;
|
||||
i64 len;
|
||||
i64 cap;
|
||||
};
|
||||
|
||||
#define array_header(arr) ((array_header_t *)(arr) - 1)
|
||||
#define array_len(arr) (array_header(arr)->len)
|
||||
#define array_cap(arr) (array_header(arr)->cap)
|
||||
#define array_add(arr, x) (array_grow(&(arr), sizeof(*(arr)), 1), (arr)[array_len(arr)++] = (x))
|
||||
#define array_addn(arr, count) (array_grow(&(arr), sizeof(*(arr)), (count)), array_len(arr) += (count), &(arr)[array_len(arr) - (count)])
|
||||
#define array_set_len(arr, x) (array_len(arr) = (x))
|
||||
#define array_set_cap(arr, x) (array__set_cap(&(arr), sizeof(*(arr)), (x)))
|
||||
#define array_pop(arr) ((arr)[--array_len(arr)])
|
||||
#define array_swapdel(arr, i) (assert_expr((i) < array_len(arr)), (arr)[(i)] = (arr)[--array_len(arr)])
|
||||
#define array_create(arena, type, count) (type *)(array__create((arena), sizeof(type), (count)) + 1)
|
||||
|
||||
fn array_header_t *array__create(ma_arena_t *arena, i64 item_size, i64 item_count) {
|
||||
array_header_t *hdr = (array_header_t *)ma_push_size(arena, item_size * item_count + sizeof(array_header_t));
|
||||
hdr->arena = arena;
|
||||
hdr->cap = item_count;
|
||||
return hdr;
|
||||
}
|
||||
|
||||
fn void array__set_cap(void **arr, i64 item_size, i64 item_count) {
|
||||
array_header_t *hdr = array_header(*arr);
|
||||
array_header_t *new_hdr = array__create(hdr->arena, item_size, item_count);
|
||||
new_hdr->len = CLAMP(hdr->len, 0, item_count);
|
||||
memory_copy(new_hdr + 1, hdr + 1, new_hdr->len * item_size);
|
||||
*arr = new_hdr + 1;
|
||||
}
|
||||
|
||||
fn void array_grow(void **arr, i64 item_size, i64 increment) {
|
||||
assert(increment >= 1);
|
||||
array_header_t *hdr = array_header(*arr);
|
||||
if (hdr->len + increment > hdr->cap) {
|
||||
array_header_t *new_hdr = array__create(hdr->arena, item_size, (hdr->cap + increment - 1) * 2);
|
||||
memory_copy(new_hdr + 1, hdr + 1, hdr->len * item_size);
|
||||
new_hdr->len = hdr->len;
|
||||
*arr = new_hdr + 1;
|
||||
// dealloc(hdr);
|
||||
}
|
||||
}
|
||||
|
||||
fn_test void test_array(void) {
|
||||
ma_temp_t scratch = ma_begin_scratch();
|
||||
int *arr = array_create(scratch.arena, int, 2);
|
||||
for (int i = 0; i < 512; i += 1) {
|
||||
array_add(arr, i);
|
||||
}
|
||||
for (int i = 0; i < 512; i += 1) {
|
||||
assert(arr[i] == i);
|
||||
}
|
||||
assert(array_len(arr) == 512);
|
||||
assert(array_cap(arr) == 512);
|
||||
for (int i = 511; i >= 0; i -= 1) {
|
||||
int a = array_pop(arr);
|
||||
assert(a == i);
|
||||
}
|
||||
assert(array_len(arr) == 0);
|
||||
|
||||
{
|
||||
int *a = array_addn(arr, 4);
|
||||
assert(arr == a);
|
||||
assert(array_header(arr)->len == 4);
|
||||
|
||||
int *b = array_addn(arr, 12);
|
||||
assert(arr + 4 == b);
|
||||
assert(array_header(arr)->len == 16);
|
||||
|
||||
array_set_len(arr, 0);
|
||||
}
|
||||
|
||||
{
|
||||
for (int i = 0; i < 512; i += 1) {
|
||||
array_add(arr, i);
|
||||
}
|
||||
|
||||
array_swapdel(arr, 3);
|
||||
assert(arr[3] == 511);
|
||||
assert(array_len(arr) == 511);
|
||||
|
||||
array_set_len(arr, 0);
|
||||
}
|
||||
|
||||
{
|
||||
for (int i = 0; i < 512; i += 1) {
|
||||
array_add(arr, i);
|
||||
}
|
||||
array_set_cap(arr, 256);
|
||||
assert(array_len(arr) == 256);
|
||||
assert(array_cap(arr) == 256);
|
||||
}
|
||||
|
||||
|
||||
ma_end_scratch(scratch);
|
||||
}
|
||||
@@ -432,7 +432,7 @@ fn void parser_panicf(b32 print_only_file, lex_t *token, const char *str, ...) {
|
||||
if (print_only_file) {
|
||||
fatalf("%s: error: %S", token->file, str8);
|
||||
} else {
|
||||
fatalf("%S(%d): error: %S", token->file, token->line, str8);
|
||||
fatalf("%s(%d): error: %S", token->file, token->line, str8);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user