From eb6432470caa211e32019426705a4b45b6dd1fe0 Mon Sep 17 00:00:00 2001 From: Timerix Date: Thu, 24 Jul 2025 12:56:53 +0300 Subject: [PATCH] collection structs name replaced with macro --- include/tlibc/collections/Array.h | 14 ++++++++------ include/tlibc/collections/HashMap.h | 20 +++++++++++--------- include/tlibc/collections/List.h | 18 ++++++++++-------- include/tlibc/errors.h | 3 +-- include/tlibc/string/StringBuilder.h | 4 ++-- include/tlibc/string/str.h | 2 +- src/collections/HashMap.c | 16 ++++++++-------- src/collections/List.c | 8 ++++---- src/string/StringBuilder.c | 2 +- src/string/cstr.c | 1 - src/string/str.c | 2 +- 11 files changed, 47 insertions(+), 43 deletions(-) diff --git a/include/tlibc/collections/Array.h b/include/tlibc/collections/Array.h index ec8fc08..a79b8d5 100755 --- a/include/tlibc/collections/Array.h +++ b/include/tlibc/collections/Array.h @@ -1,26 +1,28 @@ #pragma once #include "../std.h" -typedef struct Array { +#define Array(T) Array_ + +typedef struct Array_ { void* data; u32 size; -} Array; +} Array_; -/// creates Array from a const array +/// creates Array_ from a const array #define ARRAY(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_construct_size(DATA, LEN) ((Array_){ .data = (DATA), .size = (LEN) }) #define Array_alloc(T, COUNT) Array_alloc_size((COUNT) * sizeof(T)) -static inline Array Array_alloc_size(u32 size){ +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){ +static inline void Array_realloc_size(Array_* ar, u32 new_size){ ar->data = realloc(ar->data, new_size); ar->size = new_size; } diff --git a/include/tlibc/collections/HashMap.h b/include/tlibc/collections/HashMap.h index 4cb4bb8..850df38 100755 --- a/include/tlibc/collections/HashMap.h +++ b/include/tlibc/collections/HashMap.h @@ -12,20 +12,22 @@ typedef struct KeyHash { } KeyHash; typedef struct HashMapBucket { - List key_hash_list; - List value_list; + List(KeyHash) key_hash_list; + List(T) value_list; } HashMapBucket; -typedef struct HashMap { +#define HashMap(T) HashMap_ + +typedef struct HashMap_ { HashMapBucket* table; FreeFunction NULLABLE(value_destructor); u32 value_t_size; u32 height; u16 height_n; -} HashMap; +} HashMap_; -void HashMap_create(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)); -void HashMap_destroy(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); +void HashMap_create(HashMap_* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)); +void HashMap_destroy(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); diff --git a/include/tlibc/collections/List.h b/include/tlibc/collections/List.h index 8173cb4..84f7fca 100755 --- a/include/tlibc/collections/List.h +++ b/include/tlibc/collections/List.h @@ -1,28 +1,30 @@ #pragma once #include "../std.h" -typedef struct List { +#define List(T) List_ + +typedef struct List_ { void* data; u32 size; u32 allocated_size; -} List; +} 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 }; +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); +List_ List_alloc_size(u32 initial_size); -void* List_expand(List* ptr, u32 expansion_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); +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); +bool List_removeAt_size(List_* ptr, u32 i, u32 remove_size); #define List_len(L, T) ((L)->size / sizeof(T)) \ No newline at end of file diff --git a/include/tlibc/errors.h b/include/tlibc/errors.h index 2194b98..d6b2362 100755 --- a/include/tlibc/errors.h +++ b/include/tlibc/errors.h @@ -17,8 +17,7 @@ typedef struct ErrorCallPos { typedef struct Error { str msg; bool is_msg_on_heap; - /* List */ - List call_stack; + List(ErrorCallPos) call_stack; } Error; Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p); diff --git a/include/tlibc/string/StringBuilder.h b/include/tlibc/string/StringBuilder.h index 77bf40e..799dc78 100755 --- a/include/tlibc/string/StringBuilder.h +++ b/include/tlibc/string/StringBuilder.h @@ -5,7 +5,7 @@ #include "str.h" typedef struct StringBuilder { - List buffer; + List(char) buffer; } StringBuilder; static inline StringBuilder StringBuilder_alloc(u32 initial_size) { @@ -21,7 +21,7 @@ void StringBuilder_append_str(StringBuilder* b, str s); void StringBuilder_append_i64(StringBuilder* b, i64 a); void StringBuilder_append_u64(StringBuilder* b, u64 a); void StringBuilder_append_f64(StringBuilder* b, f64 a); -void StringBuilder_append_memory(StringBuilder* b, Array mem, bool uppercase); +void StringBuilder_append_memory(StringBuilder* b, Array(u8) mem, bool uppercase); // adds '\0' to the buffer and returns pointer to buffer content str StringBuilder_getStr(StringBuilder* b); diff --git a/include/tlibc/string/str.h b/include/tlibc/string/str.h index 4fe479e..e09e40a 100755 --- a/include/tlibc/string/str.h +++ b/include/tlibc/string/str.h @@ -43,4 +43,4 @@ u32 str_hash32(str s); str str_toUpper(str src); str str_toLower(str src); -str hex_to_str(Array buf, bool uppercase); \ No newline at end of file +str hex_to_str(Array(u8) buf, bool uppercase); diff --git a/src/collections/HashMap.c b/src/collections/HashMap.c index d722a20..2839f69 100755 --- a/src/collections/HashMap.c +++ b/src/collections/HashMap.c @@ -6,13 +6,13 @@ #define __HashMap_HASH_FUNC str_hash32 #define __HashMapBucket_MAX_LEN 16 -static const Array __HashMap_heights = ARRAY(u32, { +static const Array(u32) __HashMap_heights = ARRAY(u32, { 17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381, 32771, 65521, 131071, 262147, 524287, 1048583, 2097169, 4194319, 8388617, 16777213, 33554467, 67108859, 134217757, 268435493 }); -void HashMap_create(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)){ +void HashMap_create(HashMap_* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)){ ptr->value_t_size = value_t_size; ptr->value_destructor = value_destructor; ptr->height_n = 0; @@ -22,7 +22,7 @@ void HashMap_create(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_ memset(ptr->table, 0, alloc_size); } -void HashMap_destroy(HashMap* ptr){ +void HashMap_destroy(HashMap_* ptr){ for(u32 i = 0; i < ptr->height; i++){ HashMapBucket* bu = &ptr->table[i]; u32 len = List_len(&bu->key_hash_list, KeyHash); @@ -57,7 +57,7 @@ typedef struct BucketAndIndex { #define BucketAndIndex_null ((BucketAndIndex){ .bu = NULL, .i = -1 }) -static BucketAndIndex __HashMap_search(HashMap* ptr, str key, u32 hash){ +static BucketAndIndex __HashMap_search(HashMap_* ptr, str key, u32 hash){ BucketAndIndex r; r.bu = &ptr->table[hash % ptr->height]; for(r.i = 0; r.i < (i32)List_len(&r.bu->key_hash_list, KeyHash); r.i++){ @@ -71,7 +71,7 @@ static BucketAndIndex __HashMap_search(HashMap* ptr, str key, u32 hash){ return r; } -void* NULLABLE(HashMap_tryGetPtr)(HashMap* ptr, str key){ +void* NULLABLE(HashMap_tryGetPtr)(HashMap_* ptr, str key){ u32 hash = __HashMap_HASH_FUNC(key); BucketAndIndex r = __HashMap_search(ptr, key, hash); // key not found @@ -81,7 +81,7 @@ void* NULLABLE(HashMap_tryGetPtr)(HashMap* ptr, str key){ } -static void __HashMap_expand(HashMap* ptr){ +static void __HashMap_expand(HashMap_* ptr){ u32 height_expanded_n = ptr->height_n + 1; assert(height_expanded_n < Array_len(&__HashMap_heights, u32) && "HashMap IS FULL! Fix your code."); @@ -113,7 +113,7 @@ static void __HashMap_expand(HashMap* ptr){ ptr->height_n = height_expanded_n; } -bool HashMap_tryPush(HashMap* ptr, str key, void* value_ptr){ +bool HashMap_tryPush(HashMap_* ptr, str key, void* value_ptr){ u32 hash = __HashMap_HASH_FUNC(key); BucketAndIndex r = __HashMap_search(ptr, key, hash); // found existing item with the same key @@ -132,7 +132,7 @@ bool HashMap_tryPush(HashMap* ptr, str key, void* value_ptr){ return true; } -bool HashMap_tryDelete(HashMap* ptr, str key){ +bool HashMap_tryDelete(HashMap_* ptr, str key){ u32 hash = __HashMap_HASH_FUNC(key); BucketAndIndex r = __HashMap_search(ptr, key, hash); // key not found diff --git a/src/collections/List.c b/src/collections/List.c index 2c450d8..e6a92c0 100755 --- a/src/collections/List.c +++ b/src/collections/List.c @@ -1,13 +1,13 @@ #include "tlibc/collections/List.h" -List List_alloc_size(u32 initial_size){ +List_ List_alloc_size(u32 initial_size){ if(initial_size == 0) return List_construct_size(NULL, 0, 0); u32 allocated_size = ALIGN_TO(initial_size, sizeof(void*)); return List_construct_size(malloc(allocated_size), 0, allocated_size); } -void* List_expand(List* ptr, u32 expansion_size){ +void* List_expand(List_* ptr, u32 expansion_size){ u32 occupied_size = ptr->size; u32 expanded_alloc_size = ptr->allocated_size; if(expanded_alloc_size == 0) @@ -22,12 +22,12 @@ void* List_expand(List* ptr, u32 expansion_size){ return (u8*)(ptr->data) + occupied_size; } -void List_push_size(List* ptr, void* values, u32 size){ +void List_push_size(List_* ptr, void* values, u32 size){ void* empty_cell_ptr = List_expand(ptr, size); memcpy(empty_cell_ptr, values, size); } -bool List_removeAt_size(List* ptr, u32 i, u32 remove_size){ +bool List_removeAt_size(List_* ptr, u32 i, u32 remove_size){ if(i + remove_size >= ptr->size) return false; diff --git a/src/string/StringBuilder.c b/src/string/StringBuilder.c index 72f563a..b32bdd3 100755 --- a/src/string/StringBuilder.c +++ b/src/string/StringBuilder.c @@ -55,7 +55,7 @@ void StringBuilder_append_f64(StringBuilder* b, f64 n){ StringBuilder_append_cstr(b, buf); } -void StringBuilder_append_memory(StringBuilder* b, Array mem, bool uppercase) { +void StringBuilder_append_memory(StringBuilder* b, Array(u8) mem, bool uppercase) { if(mem.data == NULL) return; diff --git a/src/string/cstr.c b/src/string/cstr.c index bbd14ef..2498adc 100755 --- a/src/string/cstr.c +++ b/src/string/cstr.c @@ -1,5 +1,4 @@ #include "tlibc/string/cstr.h" -#include char* _strcat_malloc(size_t n, cstr str0, ...){ va_list argv; diff --git a/src/string/str.c b/src/string/str.c index 83c0ab3..22c418a 100755 --- a/src/string/str.c +++ b/src/string/str.c @@ -125,7 +125,7 @@ str str_toLower(str src){ return r; } -str hex_to_str(Array buf, bool uppercase){ +str hex_to_str(Array(u8) buf, bool uppercase){ StringBuilder sb = StringBuilder_alloc(buf.size * 2 + 1); StringBuilder_append_memory(&sb, buf, uppercase); return StringBuilder_getStr(&sb);