refactored code from tcpu
This commit is contained in:
28
include/collections/Array.h
Executable file
28
include/collections/Array.h
Executable file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
typedef struct Array {
|
||||
void* data;
|
||||
u32 size;
|
||||
} Array;
|
||||
|
||||
/// creates Array from a const array
|
||||
#define Array_CONST(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))
|
||||
|
||||
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){
|
||||
ar->data = realloc(ar->data, new_size);
|
||||
ar->size = new_size;
|
||||
}
|
||||
|
||||
#define Array_len(AR, T) ((AR)->size / sizeof(T))
|
||||
31
include/collections/HashMap.h
Executable file
31
include/collections/HashMap.h
Executable file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
#include "../string/str.h"
|
||||
#include "Array.h"
|
||||
#include "List.h"
|
||||
|
||||
typedef void (*FreeFunction)(void*);
|
||||
|
||||
typedef struct KeyHash {
|
||||
str key;
|
||||
u32 hash;
|
||||
} KeyHash;
|
||||
|
||||
typedef struct HashMapBucket {
|
||||
List key_hash_list;
|
||||
List value_list;
|
||||
} HashMapBucket;
|
||||
|
||||
typedef struct HashMap {
|
||||
HashMapBucket* table;
|
||||
FreeFunction NULLABLE(value_destructor);
|
||||
u32 value_t_size;
|
||||
u32 height;
|
||||
u16 height_n;
|
||||
} HashMap;
|
||||
|
||||
void HashMap_alloc(HashMap* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor));
|
||||
void HashMap_free(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);
|
||||
28
include/collections/List.h
Executable file
28
include/collections/List.h
Executable file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
typedef struct List {
|
||||
void* data;
|
||||
u32 size;
|
||||
u32 allocated_size;
|
||||
} List;
|
||||
|
||||
#define List_construct(L, T, OCCUPIED_COUNT, ALLOCATED_COUNT) \
|
||||
List_construct_size(L, (OCCUPIED_COUNT) * sizeof(T), (ALLOCATED_COUNT) * sizeof(T))
|
||||
|
||||
static inline List List_construct_size(void* data_ptr, u32 size, u32 allocated_size) {
|
||||
return (List){ .data = data_ptr, .size = size, .allocated_size = allocated_size };
|
||||
}
|
||||
|
||||
#define List_alloc(L, T, INITIAL_COUNT) List_alloc_size(L, (INITIAL_COUNT) * sizeof(T))
|
||||
List List_alloc_size(u32 initial_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);
|
||||
|
||||
#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_len(L, T) ((L)->size / sizeof(T))
|
||||
67
include/std.h
Executable file
67
include/std.h
Executable file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef uint8_t u8;
|
||||
typedef int16_t i16;
|
||||
typedef uint16_t u16;
|
||||
typedef int32_t i32;
|
||||
typedef uint32_t u32;
|
||||
typedef int64_t i64;
|
||||
typedef uint64_t u64;
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
|
||||
#if !__cplusplus
|
||||
typedef u8 bool;
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif
|
||||
|
||||
typedef const char* cstr;
|
||||
|
||||
#define dbg(N) printf("\e[95m%d\n",N)
|
||||
|
||||
#define nameof(V) #V
|
||||
|
||||
#define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
|
||||
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
||||
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#define IFWIN(YES, NO) YES
|
||||
#else
|
||||
#define IFWIN(YES, NO) NO
|
||||
#endif
|
||||
|
||||
#define __count_args( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7 , a8, a9, a10,a11,a12,a13,a14,a15, \
|
||||
a16,a17,a18,a19,a20,a21,a22,a23, a24,a25,a26,a27,a28,a29,a30,a31, \
|
||||
a32,a33,a34,a35,a36,a37,a38,a39, a40,a41,a42,a43,a44,a45,a46,a47, \
|
||||
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
|
||||
a64,...) a64
|
||||
// Macro for counting variadic arguments (max 64)
|
||||
// (see usage in kprint.h)
|
||||
#define count_args(ARGS...) __count_args(ARGS, \
|
||||
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
|
||||
48,47,46,45,44,43,42,41, 40,39,38,37,36,35,34,33, \
|
||||
32,31,30,29,28,27,26,25, 24,23,22,21,20,19,18,17, \
|
||||
16,15,14,13,12,11,10,9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
|
||||
|
||||
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
||||
|
||||
/// @warning pointer can be null
|
||||
#define NULLABLE(NAME) NAME
|
||||
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
25
include/string/StringBuilder.h
Executable file
25
include/string/StringBuilder.h
Executable file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "../collections/List.h"
|
||||
#include "str.h"
|
||||
|
||||
typedef struct StringBuilder {
|
||||
List buffer;
|
||||
} StringBuilder;
|
||||
|
||||
static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
|
||||
return (StringBuilder){ .buffer = List_alloc_size(initial_size) };
|
||||
}
|
||||
void StringBuilder_free(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_i64(StringBuilder* b, i64 a);
|
||||
void StringBuilder_append_u64(StringBuilder* b, u64 a);
|
||||
void StringBuilder_append_f64(StringBuilder* b, f64 a);
|
||||
|
||||
// adds '\0' to the buffer and returns pointer to buffer content
|
||||
str StringBuilder_getStr(StringBuilder* b);
|
||||
6
include/string/char.h
Executable file
6
include/string/char.h
Executable file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
static inline bool isAlphabeticalLower(char c) { return 'a' <= c && c <= 'z'; }
|
||||
static inline bool isAlphabeticalUpper(char c) { return 'A' <= c && c <= 'Z'; }
|
||||
static inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
|
||||
9
include/string/cstr.h
Executable file
9
include/string/cstr.h
Executable file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
#define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__)
|
||||
char* _strcat_malloc(size_t n, cstr str0, ...);
|
||||
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv);
|
||||
|
||||
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...) __attribute__((__format__(__printf__, 2, 3)));
|
||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv);
|
||||
43
include/string/str.h
Executable file
43
include/string/str.h
Executable file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "../std.h"
|
||||
#include "char.h"
|
||||
#include "cstr.h"
|
||||
|
||||
typedef struct str {
|
||||
char* data;
|
||||
u32 size;
|
||||
bool isZeroTerminated;
|
||||
} str;
|
||||
|
||||
/// creates str from a string literal
|
||||
#define STR(LITERAL) str_construct(LITERAL, ARRAY_SIZE(LITERAL) - 1, true)
|
||||
|
||||
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
||||
|
||||
static const str str_null = str_construct(NULL, 0, 0);
|
||||
|
||||
/// copies src content to new string and adds \0 at the end
|
||||
str str_copy(str src);
|
||||
|
||||
/// compares two strings, NullPtr-friendly
|
||||
bool str_equals(str str0, str str1);
|
||||
|
||||
/// allocates new string which is reversed variant of <s>
|
||||
str str_reverse(str s);
|
||||
|
||||
i32 str_seek(str src, str fragment, u32 startIndex);
|
||||
i32 str_seekReverse(str src, str fragment, u32 startIndex);
|
||||
|
||||
i32 str_seekChar(str src, char c, u32 startIndex);
|
||||
i32 str_seekCharReverse(str src, char c, u32 startIndex);
|
||||
|
||||
bool str_startsWith(str src, str fragment);
|
||||
bool str_endsWith(str src, str fragment);
|
||||
|
||||
/// @brief calculates string hash using sdbm32 algorythm (something like lightweight crc32)
|
||||
/// @return non-cryptografic hash of the string
|
||||
u32 str_hash32(str s);
|
||||
|
||||
str str_toUpper(str src);
|
||||
str str_toLower(str src);
|
||||
Reference in New Issue
Block a user