refactored code from tcpu

This commit is contained in:
2025-07-18 22:07:30 +03:00
commit fbc209dda9
19 changed files with 937 additions and 0 deletions

28
include/collections/Array.h Executable file
View File

@@ -0,0 +1,28 @@
#pragma once
#include "../std.h"
typedef struct Array {
void* data;
u32 size;
} Array;
/// creates Array from a const array
#define Array_CONST(T, A...) Array_construct_size(((T[])A), sizeof((T[])A))
#define Array_construct(DATA, T, COUNT) Array_construct_size(DATA, (COUNT) * sizeof(T))
#define Array_construct_size(DATA, LEN) ((Array){ .data = (DATA), .size = (LEN) })
#define Array_alloc(T, COUNT) Array_alloc((COUNT) * sizeof(T))
static inline Array Array_alloc_size(u32 size){
return Array_construct_size(malloc(size), size);
}
#define Array_realloc(AR, T, COUNT) Array_realloc_size(AR, (COUNT) * sizeof(T))
static inline void Array_realloc_size(Array* ar, u32 new_size){
ar->data = realloc(ar->data, new_size);
ar->size = new_size;
}
#define Array_len(AR, T) ((AR)->size / sizeof(T))

31
include/collections/HashMap.h Executable file
View File

@@ -0,0 +1,31 @@
#pragma once
#include "../std.h"
#include "../string/str.h"
#include "Array.h"
#include "List.h"
typedef void (*FreeFunction)(void*);
typedef struct KeyHash {
str key;
u32 hash;
} KeyHash;
typedef struct HashMapBucket {
List key_hash_list;
List value_list;
} HashMapBucket;
typedef struct HashMap {
HashMapBucket* table;
FreeFunction NULLABLE(value_destructor);
u32 value_t_size;
u32 height;
u16 height_n;
} HashMap;
void HashMap_alloc(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor));
void HashMap_free(HashMap* ptr);
void* NULLABLE(HashMap_tryGetPtr)(HashMap* ptr, str key);
bool HashMap_tryPush(HashMap* ptr, str key, void* value_ptr);
bool HashMap_tryDelete(HashMap* ptr, str key);

28
include/collections/List.h Executable file
View File

@@ -0,0 +1,28 @@
#pragma once
#include "../std.h"
typedef struct List {
void* data;
u32 size;
u32 allocated_size;
} List;
#define List_construct(L, T, OCCUPIED_COUNT, ALLOCATED_COUNT) \
List_construct_size(L, (OCCUPIED_COUNT) * sizeof(T), (ALLOCATED_COUNT) * sizeof(T))
static inline List List_construct_size(void* data_ptr, u32 size, u32 allocated_size) {
return (List){ .data = data_ptr, .size = size, .allocated_size = allocated_size };
}
#define List_alloc(L, T, INITIAL_COUNT) List_alloc_size(L, (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))