collection structs name replaced with macro
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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))
|
||||
@@ -17,8 +17,7 @@ typedef struct ErrorCallPos {
|
||||
typedef struct Error {
|
||||
str msg;
|
||||
bool is_msg_on_heap;
|
||||
/* List<ErrorCallPos> */
|
||||
List call_stack;
|
||||
List(ErrorCallPos) call_stack;
|
||||
} Error;
|
||||
|
||||
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
str hex_to_str(Array(u8) buf, bool uppercase);
|
||||
|
||||
Reference in New Issue
Block a user