tlibc/include/tlibc/collections/List.h
2025-07-21 17:57:28 +03:00

28 lines
1.1 KiB
C
Executable File

#pragma once
#include "../std.h"
typedef struct List {
void* data;
u32 size;
u32 allocated_size;
} List;
#define List_construct(DATA_PTR, T, OCCUPIED_COUNT, ALLOCATED_COUNT) \
List_construct_size(DATA_PTR, (OCCUPIED_COUNT) * sizeof(T), (ALLOCATED_COUNT) * sizeof(T))
static inline List List_construct_size(void* data_ptr, u32 occupied_size, u32 allocated_size) {
return (List){ .data = data_ptr, .size = occupied_size, .allocated_size = allocated_size };
}
#define List_alloc(T, INITIAL_COUNT) List_alloc_size((INITIAL_COUNT) * sizeof(T))
List List_alloc_size(u32 initial_size);
void* List_expand(List* ptr, u32 expansion_size);
#define List_push(L, T, VALUE) *(T*)(List_expand(L, sizeof(T))) = VALUE
#define List_pushMany(L, T, VALUES_PTR, COUNT) List_push_size(L, VALUES_PTR, (COUNT) * sizeof(T))
void List_push_size(List* ptr, void* values, u32 size);
#define List_removeAt(L, T, I, COUNT) List_removeAt_size(L, (I)*sizeof(T), (COUNT) * sizeof(T))
bool List_removeAt_size(List* ptr, u32 i, u32 remove_size);
#define List_len(L, T) ((L)->size / sizeof(T))