From e68480f2fc508ce73cad331ce4e8439e07d25702 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Mon, 1 Sep 2025 22:00:02 +0300 Subject: use <> as expansion characters + I kind of like the 'symmetry' of <> being 'self' and 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 --- example_hashmap/new/main.c | 5 ++++- example_hashmap/new/map.h | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) (limited to 'example_hashmap') 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 #include +/* 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 <>; 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; + key; + 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) + *<>_insert(<>* root, key, data) { - size_t hash = _hash(&key); + size_t hash = _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 && _cmp(&node->t.key, &key) == 0) + if (node->hash == hash && _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) + *<>_find(<> *root, key) { if (root->len == 0) return NULL; - size_t hash = _hash(&key); + size_t hash = _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 (_cmp(&node->t.key, key) != 0) + if (_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, *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, key) { - D *found = <>_find(root, key); + *found = <>_find(root, key); if (!found) return; -- cgit v1.2.3