commit 514603cebfc39f08086f3c2ea6d73cbd1763fa69
parent f4b9941e7f71686cb59f4aaa8e4f1cbd2b8bfb67
Author: Jake Koroman <jake@jakekoroman.com>
Date: Sun, 4 Jan 2026 15:40:54 -0500
code cleanup.
Diffstat:
| M | jrk.h | | | 207 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 101 insertions(+), 106 deletions(-)
diff --git a/jrk.h b/jrk.h
@@ -1,17 +1,13 @@
/*
- * NOTES:
- * - i think i've finally landed on dynamic array api
- * that i like.
- *
* TODO:
- * - remove strnlen/strncmp from hm impl. should probably be using
- * jrk_StringView's
+ * - remove strnlen/strncmp from hm impl. should probably be using
+ * jrk_StringView's
*
- * - add a log interface to give user control of logging.
- * disable completely, log to file, log format, etc.
+ * - add a log interface to give user control of logging.
+ * disable completely, log to file, log format, etc.
*
- * - look at at the jrk_sv_chop_delim_loop stuff. there must
- * must be a better way of handling things.
+ * - look at at the jrk_sv_chop_delim_loop stuff. there must
+ * must be a better way of handling things.
*/
#include <stdbool.h>
@@ -284,105 +280,104 @@ bool jrk_sb_write_entire_file(jrk_StringBuilder *, char *);
bool jrk_sb_fd_read_all(jrk_StringBuilder*, i32);
bool jrk_sb_read_entire_file(jrk_StringBuilder*, char*);
-
// http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
// for 64 bit hash
-#define FNV1A_64_OFFSET_BASIS 0xCBF29CE484222325
-#define FNV1A_64_PRIME 0x100000001B3
-uint64_t jrk_fnv1a_64(uint8_t *data, uint64_t size);
+#define JRK_FNV1A_64_OFFSET_BASIS 0xCBF29CE484222325
+#define JRK_FNV1A_64_PRIME 0x100000001B3
+u64 jrk_fnv1a_64(u8*, u64);
#define JRK_HM_MAX_KEY_SIZE 16
-#define jrk_hm_prototype(type) \
- typedef struct { \
- char key[JRK_HM_MAX_KEY_SIZE]; \
- type val; \
- } jrk_HashMapItem_##type; \
- \
- typedef struct { \
- jrk_HashMapItem_##type *items; \
- uint64_t capacity; \
- } jrk_HashMap_##type; \
- \
- bool jrk_hm_##type##_init(jrk_HashMap_##type *map, jrk_HashMapItem_##type *items, uint64_t items_sz); \
- bool jrk_hm_##type##_push(jrk_HashMap_##type *map, char *key, type val); \
+#define jrk_hm_prototype(type) \
+ typedef struct { \
+ char key[JRK_HM_MAX_KEY_SIZE]; \
+ type val; \
+ } jrk_HashMapItem_##type; \
+ \
+ typedef struct { \
+ jrk_HashMapItem_##type *items; \
+ u64 capacity; \
+ } jrk_HashMap_##type; \
+ \
+ bool jrk_hm_##type##_init(jrk_HashMap_##type *map, jrk_HashMapItem_##type *items, u64 items_cap); \
+ bool jrk_hm_##type##_push(jrk_HashMap_##type *map, char *key, type val); \
bool jrk_hm_##type##_get(jrk_HashMap_##type *map, char *key, type *outval);
-#define jrk_hm_impl(type) \
- bool \
- jrk_hm_##type##_init(jrk_HashMap_##type *map, jrk_HashMapItem_##type *items, uint64_t items_cap) \
- { \
- if (!map) { \
- fprintf(stderr, "error: failed to init hashmap: map pointer is NULL\n"); \
- return false; \
- } \
- if (!items) { \
- fprintf(stderr, "error: failed to init hashmap: buf pointer is NULL\n"); \
- return false; \
- } \
- map->items = items; \
- map->capacity = items_cap; \
- return true; \
- } \
- \
- bool \
- jrk_hm_##type##_push(jrk_HashMap_##type *map, char *key, type val) \
- { \
- if (!key) { \
- fprintf(stderr, "error: failed to push key into hashmap: key pointer is NULL\n"); \
- return false; \
- } \
- if (!map) { \
- fprintf(stderr, "error: failed to push key '%s' into hashmap: map pointer is NULL\n", key); \
- return false; \
- } \
- uint64_t idx = jrk_fnv1a_64((uint8_t *) key, strnlen(key, JRK_HM_MAX_KEY_SIZE)) % map->capacity; \
- jrk_HashMapItem_##type *item = &map->items[idx]; \
- if (item->key[0] != 0 && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE)) \
- printf("%s collision with %s\n", key, item->key); \
- for (; item < &map->items[map->capacity] && item->key[0] != 0 && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE); ++item); \
- if (item == &map->items[map->capacity]) { \
- for (item = &map->items[0]; item < &map->items[idx] && item->key[0] != 0; ++item); \
- if (item == &map->items[idx]) { \
- fprintf(stderr, "error: no space found for key '%s': hashmap is full\n", key); \
- return false; \
- } \
- } \
- memcpy(item->key, key, strlen(key)); \
- item->val = val; \
- return true; \
- } \
- \
- bool \
- jrk_hm_##type##_get(jrk_HashMap_##type *map, char *key, type *outval) \
- { \
- if (!key) { \
- fprintf(stderr, "error: failed to get key from hashmap: key pointer is NULL\n"); \
- return false; \
- } \
- if (!map) { \
- fprintf(stderr, "error: failed to get key '%s' from hashmap: map pointer is NULL\n", key); \
- return false; \
- } \
- if (!outval) { \
- fprintf(stderr, "error: failed to get key '%s' from hashmap: outval pointer is NULL\n", key); \
- return false; \
- } \
- uint64_t idx = jrk_fnv1a_64((uint8_t *) key, strnlen(key, JRK_HM_MAX_KEY_SIZE)) % map->capacity; \
- jrk_HashMapItem_##type *item = &map->items[idx]; \
- if (0 == strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE)) { \
- *outval = item->val; \
- return true; \
- } \
- for (; item < &map->items[map->capacity] && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE); ++item) \
- if (item->key[0] == 0) \
- return false; \
- if (item == &map->items[map->capacity]) { \
- for (item = &map->items[0]; item < &map->items[idx] && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE); ++item); \
- if (item == &map->items[idx]) \
- return false; \
- } \
- *outval = item->val; \
- return true; \
+#define jrk_hm_impl(type) \
+ bool \
+ jrk_hm_##type##_init(jrk_HashMap_##type *map, jrk_HashMapItem_##type *items, u64 items_cap) \
+ { \
+ if (!map) { \
+ fprintf(stderr, "error: failed to init hashmap: map pointer is NULL\n"); \
+ return false; \
+ } \
+ if (!items) { \
+ fprintf(stderr, "error: failed to init hashmap: buf pointer is NULL\n"); \
+ return false; \
+ } \
+ map->items = items; \
+ map->capacity = items_cap; \
+ return true; \
+ } \
+ \
+ bool \
+ jrk_hm_##type##_push(jrk_HashMap_##type *map, char *key, type val) \
+ { \
+ if (!key) { \
+ fprintf(stderr, "error: failed to push key into hashmap: key pointer is NULL\n"); \
+ return false; \
+ } \
+ if (!map) { \
+ fprintf(stderr, "error: failed to push key '%s' into hashmap: map pointer is NULL\n", key); \
+ return false; \
+ } \
+ u64 idx = jrk_fnv1a_64((u8 *) key, strnlen(key, JRK_HM_MAX_KEY_SIZE)) % map->capacity; \
+ jrk_HashMapItem_##type *item = &map->items[idx]; \
+ if (item->key[0] != 0 && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE)) \
+ printf("%s collision with %s\n", key, item->key); \
+ for (; item < &map->items[map->capacity] && item->key[0] != 0 && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE); ++item); \
+ if (item == &map->items[map->capacity]) { \
+ for (item = &map->items[0]; item < &map->items[idx] && item->key[0] != 0; ++item); \
+ if (item == &map->items[idx]) { \
+ fprintf(stderr, "error: no space found for key '%s': hashmap is full\n", key); \
+ return false; \
+ } \
+ } \
+ memcpy(item->key, key, strlen(key)); \
+ item->val = val; \
+ return true; \
+ } \
+ \
+ bool \
+ jrk_hm_##type##_get(jrk_HashMap_##type *map, char *key, type *outval) \
+ { \
+ if (!key) { \
+ fprintf(stderr, "error: failed to get key from hashmap: key pointer is NULL\n"); \
+ return false; \
+ } \
+ if (!map) { \
+ fprintf(stderr, "error: failed to get key '%s' from hashmap: map pointer is NULL\n", key); \
+ return false; \
+ } \
+ if (!outval) { \
+ fprintf(stderr, "error: failed to get key '%s' from hashmap: outval pointer is NULL\n", key); \
+ return false; \
+ } \
+ u64 idx = jrk_fnv1a_64((u8 *) key, strnlen(key, JRK_HM_MAX_KEY_SIZE)) % map->capacity; \
+ jrk_HashMapItem_##type *item = &map->items[idx]; \
+ if (0 == strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE)) { \
+ *outval = item->val; \
+ return true; \
+ } \
+ for (; item < &map->items[map->capacity] && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE); ++item) \
+ if (item->key[0] == 0) \
+ return false; \
+ if (item == &map->items[map->capacity]) { \
+ for (item = &map->items[0]; item < &map->items[idx] && 0 != strncmp(item->key, key, JRK_HM_MAX_KEY_SIZE); ++item); \
+ if (item == &map->items[idx]) \
+ return false; \
+ } \
+ *outval = item->val; \
+ return true; \
}
#ifdef JRK_IMPLEMENTATION
@@ -819,12 +814,12 @@ jrk_sv_equals_exact(jrk_StringView *a, jrk_StringView *b)
return true;
}
-uint64_t jrk_fnv1a_64(uint8_t *data, uint64_t size)
+u64 jrk_fnv1a_64(u8 *data, u64 size)
{
- uint64_t result = FNV1A_64_OFFSET_BASIS;
+ u64 result = JRK_FNV1A_64_OFFSET_BASIS;
for (u64 i = 0; i < size; ++i) {
result ^= data[i];
- result *= FNV1A_64_PRIME;
+ result *= JRK_FNV1A_64_PRIME;
}
return result;
}