Compare commits
4 Commits
921bada09a
...
ebe6e58ef3
| Author | SHA1 | Date | |
|---|---|---|---|
| ebe6e58ef3 | |||
| 3fb4d03f1b | |||
| c0c845bee8 | |||
| f7b0b53d05 |
@@ -7,12 +7,12 @@ typedef struct Array {
|
||||
} Array;
|
||||
|
||||
/// creates Array from a const array
|
||||
#define Array_CONST(T, A...) Array_construct_size(((T[])A), sizeof((T[])A))
|
||||
#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_alloc(T, COUNT) Array_alloc((COUNT) * sizeof(T))
|
||||
#define Array_alloc(T, COUNT) Array_alloc_size((COUNT) * sizeof(T))
|
||||
|
||||
static inline Array Array_alloc_size(u32 size){
|
||||
return Array_construct_size(malloc(size), size);
|
||||
@@ -25,4 +25,8 @@ static inline void Array_realloc_size(Array* ar, u32 new_size){
|
||||
ar->size = new_size;
|
||||
}
|
||||
|
||||
#define Array_len(AR, T) ((AR)->size / sizeof(T))
|
||||
#define Array_len(AR, T) ((AR)->size / sizeof(T))
|
||||
#define Array_memset(A, VAL) memset((A)->data, VAL, (A)->size)
|
||||
|
||||
#define str_castTo_Array(S) Array_construct_size((S).data, (S).size)
|
||||
#define Array_castTo_str(S, IS_ZERO_TERMINATED) str_construct((S).data, (S).size, IS_ZERO_TERMINATED)
|
||||
|
||||
@@ -24,8 +24,8 @@ typedef struct HashMap {
|
||||
u16 height_n;
|
||||
} HashMap;
|
||||
|
||||
void HashMap_alloc(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor));
|
||||
void HashMap_free(HashMap* ptr);
|
||||
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,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../collections/List.h"
|
||||
#include "../collections/Array.h"
|
||||
#include "str.h"
|
||||
|
||||
typedef struct StringBuilder {
|
||||
@@ -10,16 +11,17 @@ typedef struct StringBuilder {
|
||||
static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
|
||||
return (StringBuilder){ .buffer = List_alloc_size(initial_size) };
|
||||
}
|
||||
void StringBuilder_free(StringBuilder* b);
|
||||
void StringBuilder_destroy(StringBuilder* b);
|
||||
|
||||
/// @param count set to -1 to clear StringBuilder
|
||||
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
|
||||
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||
void StringBuilder_append_cstr(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_string(StringBuilder* b, str s);
|
||||
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);
|
||||
|
||||
// adds '\0' to the buffer and returns pointer to buffer content
|
||||
str StringBuilder_getStr(StringBuilder* b);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
typedef struct str {
|
||||
char* data;
|
||||
u32 size;
|
||||
u32 size; // size of data in bytes without \0
|
||||
bool isZeroTerminated;
|
||||
} str;
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
#define __HashMap_HASH_FUNC str_hash32
|
||||
#define __HashMapBucket_MAX_LEN 16
|
||||
|
||||
static const Array __HashMap_heights = Array_CONST(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_alloc(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_alloc(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_d
|
||||
memset(ptr->table, 0, alloc_size);
|
||||
}
|
||||
|
||||
void HashMap_free(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);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "string/StringBuilder.h"
|
||||
|
||||
void StringBuilder_free(StringBuilder* b){
|
||||
void StringBuilder_destroy(StringBuilder* b){
|
||||
free(b->buffer.data);
|
||||
b->buffer = List_construct_size(NULL, 0, 0);
|
||||
}
|
||||
@@ -26,12 +26,15 @@ void StringBuilder_append_char(StringBuilder* b, char c){
|
||||
}
|
||||
|
||||
|
||||
void StringBuilder_append_string(StringBuilder* b, str s){
|
||||
void StringBuilder_append_str(StringBuilder* b, str s){
|
||||
if(s.data == NULL)
|
||||
return;
|
||||
|
||||
List_push_size(&b->buffer, s.data, s.size);
|
||||
}
|
||||
|
||||
void StringBuilder_append_cstr(StringBuilder* b, char* s){
|
||||
StringBuilder_append_string(b, str_construct(s, strlen(s), true));
|
||||
StringBuilder_append_str(b, str_construct(s, strlen(s), true));
|
||||
}
|
||||
|
||||
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
||||
@@ -51,3 +54,14 @@ void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
||||
sprintf(buf, "%lf", n);
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
|
||||
void StringBuilder_append_memory(StringBuilder* b, Array mem) {
|
||||
if(mem.data == NULL)
|
||||
return;
|
||||
|
||||
char buf[8];
|
||||
for (u32 i=0; i < mem.size; i++) {
|
||||
sprintf(buf, "%02x", ((u8*)mem.data)[i]);
|
||||
StringBuilder_append_str(b, str_construct(buf, 2, true));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user