Compare commits
No commits in common. "51980ebb0b5d220c0e2c7bffe853d4d3e2628a56" and "a9fa42c23f8419ef80f7b74074030d54388cf410" have entirely different histories.
51980ebb0b
...
a9fa42c23f
@ -1,28 +1,26 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
#define Array(T) Array_
|
||||
|
||||
typedef struct 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;
|
||||
}
|
||||
|
||||
@ -12,24 +12,20 @@ typedef struct KeyHash {
|
||||
} KeyHash;
|
||||
|
||||
typedef struct HashMapBucket {
|
||||
List(KeyHash) key_hash_list;
|
||||
List(T) value_list;
|
||||
List key_hash_list;
|
||||
List value_list;
|
||||
} HashMapBucket;
|
||||
|
||||
#define HashMap(T) HashMap_
|
||||
|
||||
typedef struct HashMap_ {
|
||||
typedef struct HashMap {
|
||||
HashMapBucket* table;
|
||||
FreeFunction NULLABLE(value_destructor);
|
||||
u32 value_t_size;
|
||||
u32 height;
|
||||
u16 height_n;
|
||||
} HashMap_;
|
||||
} HashMap;
|
||||
|
||||
//TODO: standartize constructor function names across tlibc
|
||||
#define HashMap_create(PTR, T, VALUE_DESTRUCTOR) HashMap_create_size(PTR, sizeof(T), VALUE_DESTRUCTOR)
|
||||
void HashMap_create_size(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);
|
||||
|
||||
@ -1,30 +1,28 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
#define List(T) List_
|
||||
|
||||
typedef struct List_ {
|
||||
typedef struct List {
|
||||
void* data;
|
||||
u32 size;
|
||||
u32 allocated_size;
|
||||
} List_;
|
||||
} List;
|
||||
|
||||
#define List_construct(T, DATA_PTR, OCCUPIED_COUNT, ALLOCATED_COUNT) \
|
||||
#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_size(List_* ptr, u32 expansion_size);
|
||||
#define List_push(L, T, VALUE) *(T*)(List_expand_size(L, sizeof(T))) = VALUE
|
||||
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))
|
||||
@ -17,7 +17,8 @@ typedef struct ErrorCallPos {
|
||||
typedef struct Error {
|
||||
str msg;
|
||||
bool is_msg_on_heap;
|
||||
List(ErrorCallPos) call_stack;
|
||||
/* List<ErrorCallPos> */
|
||||
List call_stack;
|
||||
} Error;
|
||||
|
||||
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p);
|
||||
|
||||
@ -23,7 +23,7 @@ typedef float f32;
|
||||
typedef double f64;
|
||||
typedef void* ptr;
|
||||
|
||||
#if !__cplusplus && !defined(bool)
|
||||
#if !__cplusplus
|
||||
typedef u8 bool;
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include "str.h"
|
||||
|
||||
typedef struct StringBuilder {
|
||||
List(char) buffer;
|
||||
List 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(u8) mem, bool uppercase);
|
||||
void StringBuilder_append_memory(StringBuilder* b, Array mem, bool uppercase);
|
||||
|
||||
// adds '\0' to the buffer and returns pointer to buffer content
|
||||
str StringBuilder_getStr(StringBuilder* b);
|
||||
|
||||
@ -43,4 +43,4 @@ u32 str_hash32(str s);
|
||||
str str_toUpper(str src);
|
||||
str str_toLower(str src);
|
||||
|
||||
str hex_to_str(Array(u8) buf, bool uppercase);
|
||||
str hex_to_str(Array buf, bool uppercase);
|
||||
@ -6,14 +6,13 @@
|
||||
#define __HashMap_HASH_FUNC str_hash32
|
||||
#define __HashMapBucket_MAX_LEN 16
|
||||
|
||||
static const Array(u32) __HashMap_heights = ARRAY(u32, {
|
||||
static const Array __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_size(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;
|
||||
@ -23,7 +22,7 @@ void HashMap_create_size(HashMap_* ptr, u32 value_t_size, FreeFunction NULLABLE(
|
||||
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);
|
||||
@ -58,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++){
|
||||
@ -72,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
|
||||
@ -82,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.");
|
||||
|
||||
@ -114,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
|
||||
@ -133,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
|
||||
|
||||
@ -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_size(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_size(List_* ptr, u32 expansion_size){
|
||||
return (u8*)(ptr->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* 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;
|
||||
|
||||
|
||||
@ -55,7 +55,7 @@ void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
|
||||
void StringBuilder_append_memory(StringBuilder* b, Array(u8) mem, bool uppercase) {
|
||||
void StringBuilder_append_memory(StringBuilder* b, Array mem, bool uppercase) {
|
||||
if(mem.data == NULL)
|
||||
return;
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include "tlibc/string/cstr.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
char* _strcat_malloc(size_t n, cstr str0, ...){
|
||||
va_list argv;
|
||||
|
||||
@ -125,7 +125,7 @@ str str_toLower(str src){
|
||||
return r;
|
||||
}
|
||||
|
||||
str hex_to_str(Array(u8) buf, bool uppercase){
|
||||
str hex_to_str(Array buf, bool uppercase){
|
||||
StringBuilder sb = StringBuilder_alloc(buf.size * 2 + 1);
|
||||
StringBuilder_append_memory(&sb, buf, uppercase);
|
||||
return StringBuilder_getStr(&sb);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user