Mercurial
comparison dowa/dowa.h @ 21:09def63429b9
[Dowa] Updated the naming scheme and tests.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 10:57:30 -0700 |
| parents | 0a9e67c7039a |
| children | 947b81010aba |
comparison
equal
deleted
inserted
replaced
| 20:0a9e67c7039a | 21:09def63429b9 |
|---|---|
| 2 #define DOWA | 2 #define DOWA |
| 3 | 3 |
| 4 #include <stdio.h> | 4 #include <stdio.h> |
| 5 #include <string.h> // stdup | 5 #include <string.h> // stdup |
| 6 #include <stdlib.h> // only for malloc, free, stuff | 6 #include <stdlib.h> // only for malloc, free, stuff |
| 7 #include <assert.h> | 7 #include <assert.h> // mostly for TODO |
| 8 #include <dirent.h> | 8 #include <dirent.h> // some functions loop through files |
| 9 | 9 |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| 11 #include <limits.h> | 11 #include <limits.h> |
| 12 | 12 |
| 13 #include "dowa_internal.h" | 13 #include "dowa_internal.h" |
| 15 #define HASH_KEY_NUMBER 5381 // DJD hash number | 15 #define HASH_KEY_NUMBER 5381 // DJD hash number |
| 16 #define ONE_MEGA_BYTE 1048576 | 16 #define ONE_MEGA_BYTE 1048576 |
| 17 #define TRUE 1 | 17 #define TRUE 1 |
| 18 #define FALSE 0 | 18 #define FALSE 0 |
| 19 | 19 |
| 20 typedef unsigned int uint32; | 20 // Fixed-width integer types |
| 21 typedef int int32; | 21 typedef unsigned int uint32; // 32-bit unsigned integer |
| 22 typedef unsigned short uint16; | 22 typedef int int32; // 32-bit signed integer |
| 23 typedef short int16; | 23 typedef unsigned short uint16; // 16-bit unsigned integer |
| 24 typedef unsigned char uint8; | 24 typedef short int16; // 16-bit signed integer |
| 25 typedef char int8; | 25 typedef unsigned char uint8; // 8-bit unsigned integer |
| 26 typedef char boolean; | 26 typedef char int8; // 8-bit signed integer |
| 27 typedef char boolean; // Boolean type (0 = false, nonzero = true) | |
| 27 | 28 |
| 28 // --- Misc --- // | |
| 29 char *Dowa_Int32ToString(uint32 int32, char *buffer); | |
| 30 | 29 |
| 31 // --- Arena --- // | 30 // --- Miscellaneous --- // |
| 31 /* Just use atoid lmao*/ | |
| 32 char *Dowa_Int32ToString(uint32 value, char *buffer); | |
| 33 | |
| 34 | |
| 35 // --- Arena Allocator --- // | |
| 32 typedef struct { | 36 typedef struct { |
| 33 char *buffer; | 37 char *buffer; |
| 34 size_t offset; | 38 size_t offset; |
| 35 size_t capacity; | 39 size_t capacity; |
| 36 } Dowa_Arena, *Dowa_PArena; | 40 } Dowa_Arena, *Dowa_PArena; |
| 37 | 41 |
| 38 extern Dowa_PArena Dowa_Arena_Create(size_t capacity); | 42 /* Creates a new arena with the specified capacity (in bytes). Returns a pointer to the arena, or NULL on failure. */ |
| 39 extern void *Dowa_Arena_Allocate(Dowa_PArena p_arena, size_t size); | 43 extern Dowa_PArena Dowa_Arena_Create(size_t capacity); |
| 40 extern void Dowa_Arena_Destory(Dowa_PArena p_arena); | 44 /* Allocates a block of memory of the given size from the arena. Returns pointer to the allocated memory, or NULL if there is insufficient space. */ |
| 45 extern void *Dowa_Arena_Allocate(Dowa_PArena arena, size_t size); | |
| 46 /* Destroys the arena and frees its underlying memory block.*/ | |
| 47 extern void Dowa_Arena_Destroy(Dowa_PArena arena); | |
| 48 | |
| 41 | 49 |
| 42 // --- HashMap --- // | 50 // --- HashMap --- // |
| 43 typedef enum { | 51 typedef enum { |
| 44 DOWA_HASH_MAP_TYPE_BUFFER, | 52 DOWA_HASH_MAP_TYPE_BUFFER, // Raw byte buffer |
| 45 DOWA_HASH_MAP_TYPE_STRING, | 53 DOWA_HASH_MAP_TYPE_STRING, // Null-terminated string |
| 46 DOWA_HASH_MAP_TYPE_HASHMAP, | 54 DOWA_HASH_MAP_TYPE_HASHMAP, // Nested hashmap |
| 47 DOWA_HASH_MAP_TYPE_INT, | 55 DOWA_HASH_MAP_TYPE_INT // Integer value |
| 48 } Dowa_HashMap_ValueType; | 56 } Dowa_HashMap_ValueType; |
| 49 | 57 |
| 50 typedef struct { | 58 typedef struct { |
| 51 char *key; | 59 char *key; |
| 52 void *buffer; | 60 void *buffer; |
| 53 size_t capacity; | 61 size_t capacity; |
| 54 Dowa_HashMap_ValueType type; | 62 Dowa_HashMap_ValueType type; |
| 55 } Dowa_HashEntry, *Dowa_PHashEntry; | 63 } Dowa_HashEntry, *Dowa_PHashEntry; |
| 56 | 64 |
| 57 typedef struct { | 65 typedef struct { |
| 58 Dowa_PHashEntry *entries; | 66 Dowa_PHashEntry *entries; |
| 59 size_t capacity; | 67 size_t capacity; |
| 60 uint32 current_capacity; | 68 uint32 current_capacity; |
| 61 Dowa_PArena p_arena; | 69 Dowa_PArena p_arena; |
| 62 } Dowa_HashMap, *Dowa_PHashMap; | 70 } Dowa_HashMap, *Dowa_PHashMap; |
| 63 | 71 |
| 64 extern Dowa_PHashMap Dowa_HashMap_Create(size_t capacity); | 72 /* Creates a new hashmap with the given initial capacity. Returns pointer to the hashmap, or NULL on failure. */ |
| 65 extern Dowa_PHashMap Dowa_HashMap_Create(size_t capacity, Dowa_PArena p_arena); | 73 extern Dowa_PHashMap Dowa_HashMap_Create(size_t capacity); |
| 66 extern void Dowa_HashMap_Destory(Dowa_PHashMap p_hash_map); | 74 /* Creates a new hashmap with the given initial capacity, using the provided arena for all internal allocations. Returns pointer to the hashmap, or NULL on failure. */ |
| 67 extern int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, char *key); | 75 extern Dowa_PHashMap Dowa_HashMap_Create_With_Arena(size_t capacity, Dowa_PArena arena); |
| 68 extern void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, char *key); | 76 /* Destroys the hashmap and frees all associated memory */ |
| 69 extern void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size); | 77 extern void Dowa_HashMap_Destroy(Dowa_PHashMap p_hash_map); |
| 70 extern int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); | 78 /* Returns the index of the entry for the specified key, or -1 if the key is not found. */ |
| 71 extern int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); | 79 extern int32 Dowa_HashMap_Get_Position(Dowa_PHashMap p_hash_map, const char *key); |
| 72 extern void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, char *key); | 80 /* Retrieves the value buffer for the specified key, or NULL if the key does not exist. */ |
| 81 extern void *Dowa_HashMap_Get(Dowa_PHashMap p_hash_map, const char *key); | |
| 82 /* Inserts a copy of the given value into the hashmap under the specified key. Uses DOWA_HASH_MAP_TYPE_BUFFER as the entry type. */ | |
| 83 extern void Dowa_HashMap_Push_Value(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size); | |
| 84 /* Inserts a copy of the given value with the specified type into the hashmap under the key. Returns the index of the new entry, or -1 on failure. */ | |
| 85 extern int32 Dowa_HashMap_Push_Value_With_Type(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); | |
| 86 /* Inserts a value pointer into the hashmap under the specified key without copying data. The caller retains ownership of the pointer. Returns the entry index, or -1 on failure. */ | |
| 87 extern int32 Dowa_HashMap_Push_Value_With_Type_NoCopy(Dowa_PHashMap p_hash_map, const char *key, void *value, size_t value_size, Dowa_HashMap_ValueType type); | |
| 88 /* Removes the entry with the specified key from the hashmap and frees its data if owned. */ | |
| 89 extern void Dowa_HashMap_Pop_Key(Dowa_PHashMap p_hash_map, const char *key); | |
| 73 | 90 |
| 74 // --- Maybe Useful --- // | 91 // --- Utility Functions --- // |
| 75 extern void Dowa_HashMap_Print(Dowa_PHashMap map); | 92 /* Prints all entries in the hashmap to stdout for debugging purposes. */ |
| 76 // 0 for success, -1 for failure. Get all files in the folder into key and vlaues. | 93 extern void Dowa_HashMap_Print(Dowa_PHashMap map); |
| 77 extern int Dowa_HashMap_Cache_Folder(Dowa_PHashMap map, const char *folder_path); | 94 /* Loads all files from the specified folder into the hashmap. Uses file names as keys and file contents as values. Returns 0 on success, or -1 on failure. */ |
| 95 extern int Dowa_HashMap_Cache_Folder(Dowa_PHashMap map, const char *folder_path); | |
| 78 | 96 |
| 79 | 97 |
| 80 #endif | 98 #endif |