comparison dowa/dowa.h @ 92:655ea0b661fd

[Seobeo] Added few endpoints for handling files. [Dowa] Added few functions for random number and generating uuids
author June Park <parkjune1995@gmail.com>
date Fri, 02 Jan 2026 17:47:10 -0800
parents 4b96794c8d59
children 70401cf61e97
comparison
equal deleted inserted replaced
91:19cccf6e866a 92:655ea0b661fd
1 #ifndef DOWA 1 #ifndef DOWA
2 #define DOWA 2 #define DOWA
3 3
4 #include <stdio.h> 4 #include <stdio.h> // printfs...
5 #include <string.h> // strdup, strlen, memcmp, memcpy 5 #include <string.h> // strdup, strlen, memcmp, memcpy
6 #include <stdlib.h> // malloc, free, realloc 6 #include <stdlib.h> // malloc, free, realloc
7 #include <assert.h> // mostly for TODO 7 #include <assert.h> // mostly for TODO
8 #include <stddef.h> // size_t 8 #include <stddef.h> // size_t
9
10 #ifdef DIRECTORY
11 // Only for linux and mac since window do not support.
12 // Maybe move this into seobeo file directly?
13 #include <dirent.h> // some functions loop through files
14 #endif
15 9
16 #include <math.h> // I am not re-writing stuff I guess... 10 #include <math.h> // I am not re-writing stuff I guess...
17 11
18 #include <sys/stat.h> 12 #include <sys/stat.h>
19 #include <limits.h> 13 #include <limits.h>
45 free(p); \ 39 free(p); \
46 (p) = NULL; \ 40 (p) = NULL; \
47 } \ 41 } \
48 } while (0) 42 } while (0)
49 43
50 // Fixed-width integer types
51 typedef unsigned long long uint64; 44 typedef unsigned long long uint64;
52 typedef long long int64; 45 typedef long long int64;
53 typedef unsigned int uint32; 46 typedef unsigned int uint32;
54 typedef int int32; 47 typedef int int32;
55 typedef unsigned short uint16; 48 typedef unsigned short uint16;
72 DLAPI void *Dowa_Arena_Copy(Dowa_Arena *p_arena, const void *p_src, size_t size); 65 DLAPI void *Dowa_Arena_Copy(Dowa_Arena *p_arena, const void *p_src, size_t size);
73 DLAPI void Dowa_Arena_Reset(Dowa_Arena *p_arena); 66 DLAPI void Dowa_Arena_Reset(Dowa_Arena *p_arena);
74 DLAPI size_t Dowa_Arena_Get_Used(Dowa_Arena *p_arena); 67 DLAPI size_t Dowa_Arena_Get_Used(Dowa_Arena *p_arena);
75 DLAPI size_t Dowa_Arena_Get_Remaining(Dowa_Arena *p_arena); 68 DLAPI size_t Dowa_Arena_Get_Remaining(Dowa_Arena *p_arena);
76 69
77 // --- New stb_ds-style Data Structures --- // 70 // --- Array and HashMap --- //
78 71
79 // Allocator type enum
80 typedef enum { 72 typedef enum {
81 DOWA_ALLOCATOR_MALLOC = 0, 73 DOWA_ALLOCATOR_MALLOC = 0,
82 DOWA_ALLOCATOR_ARENA = 1 74 DOWA_ALLOCATOR_ARENA = 1
83 } Dowa_Allocator_Type; 75 } Dowa_Allocator_Type;
84 76
85 // Array header (prefix to actual array data)
86 typedef struct { 77 typedef struct {
87 size_t length; 78 size_t length;
88 size_t capacity; 79 size_t capacity;
89 uint8 allocator_type; 80 uint8 allocator_type;
90 Dowa_Arena* p_arena; 81 Dowa_Arena *p_arena;
91 void* p_hash; 82 void *p_hash;
92 } Dowa_Array_Header; 83 } Dowa_Array_Header;
93 84
94 // Hash bucket (64 bytes for cache line alignment)
95 typedef struct { 85 typedef struct {
96 uint32 hash[DOWA_HASH_BUCKET_SIZE]; 86 uint32 hash[DOWA_HASH_BUCKET_SIZE];
97 uint32 index[DOWA_HASH_BUCKET_SIZE]; 87 uint32 index[DOWA_HASH_BUCKET_SIZE];
98 } Dowa_Hash_Bucket; 88 } Dowa_Hash_Bucket;
99 89
100 // Hash index structure (separate from value array)
101 typedef struct { 90 typedef struct {
102 size_t bucket_count; 91 size_t bucket_count;
103 size_t item_count; 92 size_t item_count;
104 size_t tombstone_count; 93 size_t tombstone_count;
105 uint8 allocator_type; 94 uint8 allocator_type;
106 Dowa_Arena* p_arena; 95 Dowa_Arena* p_arena;
107 Dowa_Hash_Bucket* p_buckets; 96 Dowa_Hash_Bucket* p_buckets;
108 } Dowa_Hash_Index; 97 } Dowa_Hash_Index;
109 98
110 // Key-Value pair declaration macro
111 #define Dowa_KV(K, V) struct { K key; V value; } 99 #define Dowa_KV(K, V) struct { K key; V value; }
112 100
113 // Internal header accessor
114 #define dowa__header(a) ((Dowa_Array_Header*)(a) - 1) 101 #define dowa__header(a) ((Dowa_Array_Header*)(a) - 1)
115 102
116 // --- Array Macros --- // 103 // --- Array Macros --- //
117 104
118 #define Dowa_Array_Length(a) ((a) ? dowa__header(a)->length : 0) 105 #define Dowa_Array_Length(a) ((a) ? dowa__header(a)->length : 0)
147 dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), strlen(k) + 1) 134 dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), strlen(k) + 1)
148 135
149 #define Dowa_HashMap_Push(m, k, v) \ 136 #define Dowa_HashMap_Push(m, k, v) \
150 do { \ 137 do { \
151 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), strlen(k) + 1, NULL); \ 138 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), strlen(k) + 1, NULL); \
152 void* p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), strlen(k) + 1); \ 139 void *p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), strlen(k) + 1); \
153 if (p_kv_temp) \ 140 if (p_kv_temp) \
154 ((typeof(m))p_kv_temp)->value = (v); \ 141 ((typeof(m))p_kv_temp)->value = (v); \
155 } while (0) 142 } while (0)
156 143
157 #define Dowa_HashMap_Push_Arena(m, k, v, arena) \ 144 #define Dowa_HashMap_Push_Arena(m, k, v, arena) \
158 do { \ 145 do { \
159 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), strlen(k) + 1, (arena)); \ 146 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), strlen(k) + 1, (arena)); \
160 void* p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), strlen(k) + 1); \ 147 void *p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), strlen(k) + 1); \
161 if (p_kv_temp) \ 148 if (p_kv_temp) \
162 ((typeof(m))p_kv_temp)->value = (v); \ 149 ((typeof(m))p_kv_temp)->value = (v); \
163 } while (0) 150 } while (0)
164 151
165 #define Dowa_HashMap_Has_Key(m, k) \ 152 #define Dowa_HashMap_Has_Key(m, k) \
181 dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), (ksize)) 168 dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), (ksize))
182 169
183 #define Dowa_HashMap_Push_Binary(m, k, ksize, v) \ 170 #define Dowa_HashMap_Push_Binary(m, k, ksize, v) \
184 do { \ 171 do { \
185 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), (ksize), NULL); \ 172 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), (ksize), NULL); \
186 void* p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), (ksize)); \ 173 void *p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), (ksize)); \
187 if (p_kv_temp) \ 174 if (p_kv_temp) \
188 ((typeof(m))p_kv_temp)->value = (v); \ 175 ((typeof(m))p_kv_temp)->value = (v); \
189 } while (0) 176 } while (0)
190 177
191 #define Dowa_HashMap_Push_Binary_Arena(m, k, ksize, v, arena) \ 178 #define Dowa_HashMap_Push_Binary_Arena(m, k, ksize, v, arena) \
192 do { \ 179 do { \
193 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), (ksize), (arena)); \ 180 (m) = dowa__hashmap_push((m), sizeof(*(m)), (k), (ksize), (arena)); \
194 void* p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), (ksize)); \ 181 void *p_kv_temp = dowa__hashmap_get_ptr((m), sizeof(*(m)), (k), (ksize)); \
195 if (p_kv_temp) \ 182 if (p_kv_temp) \
196 ((typeof(m))p_kv_temp)->value = (v); \ 183 ((typeof(m))p_kv_temp)->value = (v); \
197 } while (0) 184 } while (0)
198 185
199 // --- Array Core Functions --- // 186 // --- Array Core Functions --- //
200 187
201 DLAPI void* dowa__array_grow(void* p_array, size_t element_size, size_t minimum_capacity, Dowa_Arena* p_arena); 188 DLAPI void *dowa__array_grow(void *p_array, size_t element_size, size_t minimum_capacity, Dowa_Arena* p_arena);
202 DLAPI void dowa__array_free(void* p_array); 189 DLAPI void dowa__array_free(void *p_array);
203 190
204 // --- HashMap Core Functions --- // 191 // --- HashMap Core Functions --- //
205 192
206 DLAPI uint32 dowa__hash_bytes(void* p_key, size_t key_size); 193 DLAPI uint32 dowa__hash_bytes(void *p_key, size_t key_size);
207 DLAPI void* dowa__hashmap_get(void* p_map, size_t element_size, void* p_key, size_t key_size); 194 DLAPI void *dowa__hashmap_get(void *p_map, size_t element_size, void *p_key, size_t key_size);
208 DLAPI void* dowa__hashmap_get_ptr(void* p_map, size_t element_size, void* p_key, size_t key_size); 195 DLAPI void *dowa__hashmap_get_ptr(void *p_map, size_t element_size, void *p_key, size_t key_size);
209 DLAPI void* dowa__hashmap_push(void* p_map, size_t element_size, void* p_key, size_t key_size, Dowa_Arena* p_arena); 196 DLAPI void *dowa__hashmap_push(void *p_map, size_t element_size, void *p_key, size_t key_size, Dowa_Arena* p_arena);
210 DLAPI boolean dowa__hashmap_has_key(void* p_map, size_t element_size, void* p_key, size_t key_size); 197 DLAPI boolean dowa__hashmap_has_key(void *p_map, size_t element_size, void *p_key, size_t key_size);
211 DLAPI void dowa__hashmap_delete(void* p_map, size_t element_size, void* p_key, size_t key_size); 198 DLAPI void dowa__hashmap_delete(void *p_map, size_t element_size, void *p_key, size_t key_size);
212 DLAPI void dowa__hashmap_clear(void* p_map, size_t element_size); 199 DLAPI void dowa__hashmap_clear(void *p_map, size_t element_size);
213 DLAPI void dowa__hashmap_free(void* p_map); 200 DLAPI void dowa__hashmap_free(void *p_map);
214 DLAPI size_t dowa__hashmap_count(void* p_map); 201 DLAPI size_t dowa__hashmap_count(void *p_map);
215 202
216 // --- String Manipulation --- // 203 // --- String Manipulation --- //
217 204
218 DLAPI char *Dowa_String_Slice(char *from, size_t start, size_t end, Dowa_Arena *p_arena); 205 DLAPI char *Dowa_String_Slice(char *from, size_t start, size_t end, Dowa_Arena *p_arena);
219 DLAPI char **Dowa_String_Split(char *from, char *token, int32 from_length, int32 token_length, Dowa_Arena *p_arena); 206 DLAPI char **Dowa_String_Split(char *from, char *token, int32 from_length, int32 token_length, Dowa_Arena *p_arena);
220 DLAPI char *Dowa_String_Copy_Arena(char *from, Dowa_Arena *p_arena); 207 DLAPI char *Dowa_String_Copy_Arena(char *from, Dowa_Arena *p_arena);
221 DLAPI int32 Dowa_String_Pos_Find(const char *p_from, const char *p_value, const size_t from_length, const size_t value_length); 208 DLAPI int32 Dowa_String_Pos_Find(const char *p_from, const char *p_value, const size_t from_length, const size_t value_length);
222 DLAPI char *Dowa_String_Find(const char *p_from, const char *p_value, const size_t from_length, const size_t value_length); 209 DLAPI char *Dowa_String_Find(const char *p_from, const char *p_value, const size_t from_length, const size_t value_length);
223 DLAPI int32 Dowa_String_Pos_Find_Char(const char *p_from, int c, int32 from_length); 210 DLAPI int32 Dowa_String_Pos_Find_Char(const char *p_from, int c, int32 from_length);
224 DLAPI char *Dowa_String_Find_Char(const char *p_from, int c, int32 from_length); 211 DLAPI char *Dowa_String_Find_Char(const char *p_from, int c, int32 from_length);
225 212 DLAPI char *Dowa_String_UUID(uint32 seed, void *buffer);
213
214 // --- Math --- //
215 DLAPI uint32 Dowa_Math_Random_Uint32(uint32 seed_number);
226 216
227 #endif 217 #endif