From 958cb269b2c2cb4e7fca293c53b46053fccab5f0 Mon Sep 17 00:00:00 2001 From: Timerix Date: Mon, 10 Nov 2025 07:04:09 +0500 Subject: [PATCH] renamed first argument of all methods to 'self' --- include/tlibc/collections/Array.h | 32 ++++----- include/tlibc/collections/HashMap.h | 18 ++--- include/tlibc/collections/List.h | 16 ++--- src/collections/HashMap.c | 100 ++++++++++++++-------------- src/collections/List.c | 34 +++++----- src/errors.c | 2 +- 6 files changed, 101 insertions(+), 101 deletions(-) diff --git a/include/tlibc/collections/Array.h b/include/tlibc/collections/Array.h index 33857ca..5b72572 100755 --- a/include/tlibc/collections/Array.h +++ b/include/tlibc/collections/Array.h @@ -8,7 +8,7 @@ typedef struct Array_ { u32 size; } Array_; -/// creates Array_ from a const array +/// creates Array_ from self 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)) @@ -20,31 +20,31 @@ 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)) +#define Array_realloc(SELF, T, COUNT) Array_realloc_size(SELF, (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; +static inline void Array_realloc_size(Array_* self, u32 new_size){ + self->data = realloc(self->data, new_size); + self->size = new_size; } -static inline Array_ Array_copy(const Array_ src){ - Array_ copy = Array_alloc_size(src.size); +static inline Array_ Array_copy(const Array_ self){ + Array_ copy = Array_alloc_size(self.size); if(copy.data != NULL) - memcpy(copy.data, src.data, src.size); + memcpy(copy.data, self.data, self.size); return copy; } -#define Array_len(A, T) (A.size / sizeof(T)) -#define Array_memset(A, VAL) memset(A.data, VAL, A.size) +#define Array_len(SELF, T) (SELF.size / sizeof(T)) +#define Array_memset(SELF, VAL) memset(SELF.data, VAL, SELF.size) #define struct_castTo_Array(STRUCT_PTR) Array_construct_size((STRUCT_PTR), sizeof(*STRUCT_PTR)) -///@return a[0..i-1] -static inline Array(u8) Array_sliceTo(const Array(u8) a, u32 i){ - return Array_construct_size(a.data, i); +///@return self[0..i-1] +static inline Array(u8) Array_sliceTo(const Array(u8) self, u32 i){ + return Array_construct_size(self.data, i); } -///@return a[i...] -static inline Array(u8) Array_sliceFrom(const Array(u8) a, u32 i){ - return Array_construct_size((u8*)a.data + i, a.size - i); +///@return self[i...] +static inline Array(u8) Array_sliceFrom(const Array(u8) self, u32 i){ + return Array_construct_size((u8*)self.data + i, self.size - i); } diff --git a/include/tlibc/collections/HashMap.h b/include/tlibc/collections/HashMap.h index 2a68b9b..81de8b0 100755 --- a/include/tlibc/collections/HashMap.h +++ b/include/tlibc/collections/HashMap.h @@ -19,18 +19,18 @@ typedef struct HashMapBucket { #define HashMap(T) HashMap_ typedef struct HashMap_ { - HashMapBucket* table; - FreeFunction NULLABLE(value_destructor); + NULLABLE(HashMapBucket*) table; + NULLABLE(FreeFunction) value_destructor; u32 value_t_size; u32 height; u16 height_n; } HashMap_; -#define HashMap_construct(PTR, T, VALUE_DESTRUCTOR) HashMap_construct_size(PTR, sizeof(T), VALUE_DESTRUCTOR) -void HashMap_construct_size(HashMap_* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor)); -void HashMap_destroy(HashMap_* ptr); +#define HashMap_construct(SELF, T, VALUE_DESTRUCTOR) HashMap_construct_size(SELF, sizeof(T), VALUE_DESTRUCTOR) +void HashMap_construct_size(HashMap_* self, u32 value_t_size, FreeFunction NULLABLE(value_destructor)); +void HashMap_destroy(HashMap_* self); -bool HashMap_tryPush(HashMap_* ptr, const str key, void* value_ptr); -void HashMap_pushOrUpdate(HashMap_* ptr, const str key, void* value_ptr); -NULLABLE(void*) HashMap_tryGetPtr(const HashMap_* ptr, const str key); -bool HashMap_tryDelete(HashMap_* ptr, const str key); +bool HashMap_tryPush(HashMap_* self, const str key, void* value_ptr); +void HashMap_pushOrUpdate(HashMap_* self, const str key, void* value_ptr); +NULLABLE(void*) HashMap_tryGetPtr(const HashMap_* self, const str key); +bool HashMap_tryDelete(HashMap_* self, const str key); diff --git a/include/tlibc/collections/List.h b/include/tlibc/collections/List.h index fc4370d..2908a66 100755 --- a/include/tlibc/collections/List.h +++ b/include/tlibc/collections/List.h @@ -21,13 +21,13 @@ List_ List_alloc_size(u32 initial_size); List_ List_copy(List_ src); -void* List_expand_size(List_* ptr, u32 size_to_add); -#define List_push(L, T, VALUE) *(T*)(List_expand_size(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_expand_size(List_* self, u32 size_to_add); +#define List_push(SELF, T, VALUE) *(T*)(List_expand_size(SELF, sizeof(T))) = VALUE +#define List_pushMany(SELF, T, VALUES_PTR, COUNT) List_push_size(SELF, VALUES_PTR, (COUNT) * sizeof(T)) +void List_push_size(List_* self, 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_removeAt(SELF, T, I, COUNT) List_removeAt_size(SELF, (I)*sizeof(T), (COUNT) * sizeof(T)) +bool List_removeAt_size(List_* self, u32 i, u32 remove_size); -#define List_len(L, T) ((L)->size / sizeof(T)) -#define List_castTo_Array(l) Array_construct_size(l.data, l.size) +#define List_len(SELF, T) (SELF.size / sizeof(T)) +#define List_castTo_Array(SELF) Array_construct_size(SELF.data, SELF.size) diff --git a/src/collections/HashMap.c b/src/collections/HashMap.c index 54ae1a0..9303cae 100755 --- a/src/collections/HashMap.c +++ b/src/collections/HashMap.c @@ -13,21 +13,21 @@ static const Array(u32) __HashMap_heights = ARRAY(u32, { }); -void HashMap_construct_size(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; - ptr->height = 0; - ptr->table = NULL; +void HashMap_construct_size(HashMap_* self, u32 value_t_size, FreeFunction NULLABLE(value_destructor)){ + self->value_t_size = value_t_size; + self->value_destructor = value_destructor; + self->height_n = 0; + self->height = 0; + self->table = NULL; } -void HashMap_destroy(HashMap_* ptr){ - if(!ptr) +void HashMap_destroy(HashMap_* self){ + if(!self) return; - for(u32 i = 0; i < ptr->height; i++){ - HashMapBucket* bu = &ptr->table[i]; - u32 len = List_len(&bu->key_hash_list, KeyHash); + for(u32 i = 0; i < self->height; i++){ + HashMapBucket* bu = &self->table[i]; + u32 len = List_len(bu->key_hash_list, KeyHash); // free key strings for(u32 j = 0; j < len; j++){ @@ -36,12 +36,12 @@ void HashMap_destroy(HashMap_* ptr){ } // destroy values - if(ptr->value_destructor){ + if(self->value_destructor){ u8* value_ptr = (u8*)bu->value_list.data; u8* end = value_ptr + bu->value_list.size; while(value_ptr < end){ - ptr->value_destructor(value_ptr); - value_ptr += ptr->value_t_size; + self->value_destructor(value_ptr); + value_ptr += self->value_t_size; } } @@ -49,7 +49,7 @@ void HashMap_destroy(HashMap_* ptr){ free(bu->value_list.data); } - free(ptr->table); + free(self->table); } typedef struct BucketAndIndex { @@ -62,13 +62,13 @@ typedef struct BucketAndIndex { ///@returns `HashMapBucket*` for specified key or NULL, if table hasn't been allocated yet; /// Index of existing item with the same key or -1 if no item is present. -static BucketAndIndex __HashMap_search(const HashMap_* ptr, const str key, u32 hash){ - if(ptr->height == 0) +static BucketAndIndex __HashMap_search(const HashMap_* self, const str key, u32 hash){ + if(self->height == 0) return BucketAndIndex_null; 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++){ + r.bu = &self->table[hash % self->height]; + for(r.i = 0; r.i < (i32)List_len(r.bu->key_hash_list, KeyHash); r.i++){ KeyHash* kh = (KeyHash*)r.bu->key_hash_list.data + r.i; if(kh->hash == hash && str_equals(kh->key, key)){ return r; @@ -79,18 +79,18 @@ static BucketAndIndex __HashMap_search(const HashMap_* ptr, const str key, u32 h return r; } -void* HashMap_tryGetPtr(const HashMap_* ptr, const str key){ +void* HashMap_tryGetPtr(const HashMap_* self, const str key){ u32 hash = __HashMap_HASH_FUNC(key); - BucketAndIndex r = __HashMap_search(ptr, key, hash); + BucketAndIndex r = __HashMap_search(self, key, hash); // key not found if(r.i == -1) return NULL; - return ((u8*)r.bu->value_list.data) + r.i * ptr->value_t_size; + return ((u8*)r.bu->value_list.data) + r.i * self->value_t_size; } -static void __HashMap_expand(HashMap_* ptr){ - u32 height_expanded_n = ptr->height_n + 1; +static void __HashMap_expand(HashMap_* self){ + u32 height_expanded_n = self->height_n + 1; assert(height_expanded_n < Array_len(__HashMap_heights, u32) && "HashMap IS FULL! Fix your code."); // alloc new HashMapBucket array @@ -100,74 +100,74 @@ static void __HashMap_expand(HashMap_* ptr){ memset(table_expanded, 0, table_expanded_size); // copy values from old buckets to new - for(u32 i = 0; i < ptr->height; i++){ - HashMapBucket* old_bucket = &ptr->table[i]; - u32 len = List_len(&old_bucket->key_hash_list, KeyHash); + for(u32 i = 0; i < self->height; i++){ + HashMapBucket* old_bucket = &self->table[i]; + u32 len = List_len(old_bucket->key_hash_list, KeyHash); for(u32 j = 0; j < len; j++){ KeyHash kh = ((KeyHash*)old_bucket->key_hash_list.data)[j]; HashMapBucket* new_bucket = &table_expanded[kh.hash % height_expanded]; List_push(&new_bucket->key_hash_list, KeyHash, kh); - void* old_value_ptr = (u8*)old_bucket->value_list.data + j * ptr->value_t_size; - List_push_size(&new_bucket->value_list, old_value_ptr, ptr->value_t_size); + void* old_value_ptr = (u8*)old_bucket->value_list.data + j * self->value_t_size; + List_push_size(&new_bucket->value_list, old_value_ptr, self->value_t_size); } free(old_bucket->key_hash_list.data); free(old_bucket->value_list.data); } - free(ptr->table); - ptr->table = table_expanded; - ptr->height = height_expanded; - ptr->height_n = height_expanded_n; + free(self->table); + self->table = table_expanded; + self->height = height_expanded; + self->height_n = height_expanded_n; } -bool HashMap_tryPush(HashMap_* ptr, const str key, void* value_ptr){ +bool HashMap_tryPush(HashMap_* self, const str key, void* value_ptr){ u32 hash = __HashMap_HASH_FUNC(key); - BucketAndIndex r = __HashMap_search(ptr, key, hash); + BucketAndIndex r = __HashMap_search(self, key, hash); // found existing item with the same key if(r.i != -1) return false; HashMapBucket* bu = r.bu; - if(bu == NULL || List_len(&bu->key_hash_list, KeyHash) >= __HashMapBucket_MAX_LEN){ - __HashMap_expand(ptr); - bu = &ptr->table[hash % ptr->height]; + if(bu == NULL || List_len(bu->key_hash_list, KeyHash) >= __HashMapBucket_MAX_LEN){ + __HashMap_expand(self); + bu = &self->table[hash % self->height]; } KeyHash kh = { .key = str_copy(key), .hash = hash }; List_push(&bu->key_hash_list, KeyHash, kh); - List_push_size(&bu->value_list, value_ptr, ptr->value_t_size); + List_push_size(&bu->value_list, value_ptr, self->value_t_size); return true; } -void HashMap_pushOrUpdate(HashMap_* ptr, const str key, void* value_ptr){ +void HashMap_pushOrUpdate(HashMap_* self, const str key, void* value_ptr){ u32 hash = __HashMap_HASH_FUNC(key); - BucketAndIndex r = __HashMap_search(ptr, key, hash); + BucketAndIndex r = __HashMap_search(self, key, hash); // found existing item with the same key if(r.i != -1){ - void* existing_item = ((u8*)r.bu->value_list.data) + r.i * ptr->value_t_size; - memcpy(existing_item, value_ptr, ptr->value_t_size); + void* existing_item = ((u8*)r.bu->value_list.data) + r.i * self->value_t_size; + memcpy(existing_item, value_ptr, self->value_t_size); } HashMapBucket* bu = r.bu; - if(bu == NULL || List_len(&bu->key_hash_list, KeyHash) >= __HashMapBucket_MAX_LEN){ - __HashMap_expand(ptr); - bu = &ptr->table[hash % ptr->height]; + if(bu == NULL || List_len(bu->key_hash_list, KeyHash) >= __HashMapBucket_MAX_LEN){ + __HashMap_expand(self); + bu = &self->table[hash % self->height]; } KeyHash kh = { .key = str_copy(key), .hash = hash }; List_push(&bu->key_hash_list, KeyHash, kh); - List_push_size(&bu->value_list, value_ptr, ptr->value_t_size); + List_push_size(&bu->value_list, value_ptr, self->value_t_size); } -bool HashMap_tryDelete(HashMap_* ptr, const str key){ +bool HashMap_tryDelete(HashMap_* self, const str key){ u32 hash = __HashMap_HASH_FUNC(key); - BucketAndIndex r = __HashMap_search(ptr, key, hash); + BucketAndIndex r = __HashMap_search(self, key, hash); // key not found if(r.i == -1) return false; List_removeAt(&r.bu->key_hash_list, KeyHash, r.i, 1); - List_removeAt_size(&r.bu->value_list, r.i, ptr->value_t_size); + List_removeAt_size(&r.bu->value_list, r.i, self->value_t_size); return true; } diff --git a/src/collections/List.c b/src/collections/List.c index c04f242..94a9b4b 100755 --- a/src/collections/List.c +++ b/src/collections/List.c @@ -14,33 +14,33 @@ List_ List_copy(const List_ src){ return copy; } -void* List_expand_size(List_* ptr, u32 size_to_add){ - u32 occupied_size = ptr->size; - u32 expanded_alloc_size = ptr->allocated_size; +void* List_expand_size(List_* self, u32 size_to_add){ + u32 occupied_size = self->size; + u32 expanded_alloc_size = self->allocated_size; if(expanded_alloc_size == 0) expanded_alloc_size = 64; - ptr->size += size_to_add; - while(ptr->size > expanded_alloc_size){ + self->size += size_to_add; + while(self->size > expanded_alloc_size){ expanded_alloc_size *= 2; } - // if ptr->data is null, realloc acts like malloc - ptr->data = realloc(ptr->data, expanded_alloc_size); - ptr->allocated_size = expanded_alloc_size; - return (u8*)(ptr->data) + occupied_size; + // if self->data is null, realloc acts like malloc + self->data = realloc(self->data, expanded_alloc_size); + self->allocated_size = expanded_alloc_size; + return (u8*)(self->data) + occupied_size; } -void List_push_size(List_* ptr, void* values, u32 size){ - void* empty_cell_ptr = List_expand_size(ptr, size); +void List_push_size(List_* self, void* values, u32 size){ + void* empty_cell_ptr = List_expand_size(self, size); memcpy(empty_cell_ptr, values, size); } -bool List_removeAt_size(List_* ptr, u32 i, u32 remove_size){ - if(i + remove_size >= ptr->size) +bool List_removeAt_size(List_* self, u32 i, u32 remove_size){ + if(i + remove_size >= self->size) return false; - ptr->size -= remove_size; - u8* src = (u8*)ptr->data + i + remove_size; - u8* dst = (u8*)ptr->data + i; - memmove(dst, src, ptr->size - i - remove_size); + self->size -= remove_size; + u8* src = (u8*)self->data + i + remove_size; + u8* dst = (u8*)self->data + i; + memmove(dst, src, self->size - i - remove_size); return true; } diff --git a/src/errors.c b/src/errors.c index 96995e7..147d2c0 100755 --- a/src/errors.c +++ b/src/errors.c @@ -24,7 +24,7 @@ void Error_addCallPos(Error* e, ErrorCallPos p){ } str Error_toStr(Error* e){ - u32 len = List_len(&e->call_stack, ErrorCallPos); + u32 len = List_len(e->call_stack, ErrorCallPos); StringBuilder b = StringBuilder_alloc(e->msg.size + 80 * len); StringBuilder_append_str(&b, STR("Catched Error: "));