collection structs name replaced with macro

This commit is contained in:
2025-07-24 12:56:53 +03:00
parent 2dd6316b7b
commit eb6432470c
11 changed files with 47 additions and 43 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -1,5 +1,4 @@
#include "tlibc/string/cstr.h"
#include <stdarg.h>
char* _strcat_malloc(size_t n, cstr str0, ...){
va_list argv;

View File

@@ -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);