diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-09-01 22:00:02 +0300 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-09-01 22:00:02 +0300 |
commit | e68480f2fc508ce73cad331ce4e8439e07d25702 (patch) | |
tree | bfa99b6f20402ba46230771a0d42172fc2169481 /example_hashmap/new | |
parent | 20ce6fa81cd9f55dca3212699d5949f8ebe7d95b (diff) | |
download | ngc-e68480f2fc508ce73cad331ce4e8439e07d25702.tar.gz ngc-e68480f2fc508ce73cad331ce4e8439e07d25702.zip |
+ I kind of like the 'symmetry' of <> being 'self' and <param> being
some parameter, but having to type lots of extra <*> is a bit
annoying, and might also be an issue for C++. Still, enough
functionality for a hashmap example
Diffstat (limited to 'example_hashmap/new')
-rw-r--r-- | example_hashmap/new/main.c | 5 | ||||
-rw-r--r-- | example_hashmap/new/map.h | 22 |
2 files changed, 15 insertions, 12 deletions
diff --git a/example_hashmap/new/main.c b/example_hashmap/new/main.c index 4846ab5..db575c6 100644 --- a/example_hashmap/new/main.c +++ b/example_hashmap/new/main.c @@ -1,6 +1,9 @@ #include <assert.h> #include <stdio.h> +/* provide generic any that matches anything/everything */ +typedef any {} + #include "map.h" #define foreach(name, i, s) \ @@ -9,7 +12,7 @@ /* int wrapper, provides cmp/hash for a generic integer type * (here a trait like is_integer would be ideal but oh well) */ typedef intw[any I]() { - typedef I <>; + typedef <I> <>; size_t <>_hash(<> *a) { diff --git a/example_hashmap/new/map.h b/example_hashmap/new/map.h index e993139..e892011 100644 --- a/example_hashmap/new/map.h +++ b/example_hashmap/new/map.h @@ -17,8 +17,8 @@ * supported so just go with this for now */ typedef map[any K, any D]() { struct <>_tuple { - K key; - D data; + <K> key; + <D> data; }; typedef struct <>_tuple *<>_iter; @@ -64,9 +64,9 @@ typedef map[any K, any D]() { free(root->buckets); } - D *<>_insert(<>* root, K key, D data) + <D> *<>_insert(<>* root, <K> key, <D> data) { - size_t hash = <key>_hash(&key); + size_t hash = <K>_hash(&key); /* look through buckets in order */ for (size_t b = 0; b < root->count; ++b) { struct <>_bucket *bucket = root->buckets[b]; @@ -84,7 +84,7 @@ typedef map[any K, any D]() { } /* there already exists a node like this */ - if (node->hash == hash && <key>_cmp(&node->t.key, &key) == 0) + if (node->hash == hash && <K>_cmp(&node->t.key, &key) == 0) return &node->t.data; } @@ -122,12 +122,12 @@ typedef map[any K, any D]() { return &node->t.data; } - D *<>_find(<> *root, K key) + <D> *<>_find(<> *root, <K> key) { if (root->len == 0) return NULL; - size_t hash = <key>_hash(&key); + size_t hash = <K>_hash(&key); for (size_t b = 0; b < root->count; ++b) { struct <>_bucket *bucket = root->buckets[b]; size_t idx = NGC_BUCKET_IDX(hash, root->pow2, b); @@ -136,7 +136,7 @@ typedef map[any K, any D]() { if (node->hash != hash) continue; - if (<key>_cmp(&node->t.key, key) != 0) + if (<K>_cmp(&node->t.key, &key) != 0) continue; return &node->t.data; @@ -145,7 +145,7 @@ typedef map[any K, any D]() { return NULL; } - void <>_remove_found(<> *root, D *data) + void <>_remove_found(<> *root, <D> *data) { struct <>_tuple *tuple = NGC_CONTAINER_OF(data, struct <>_tuple, data); struct <>_node *node = NGC_CONTAINER_OF(tuple, struct <>_node, t); @@ -153,9 +153,9 @@ typedef map[any K, any D]() { root->len--; } - void <>_remove(<> *root, K key) + void <>_remove(<> *root, <K> key) { - D *found = <>_find(root, key); + <D> *found = <>_find(root, key); if (!found) return; |