From 7d39b21c98cbe3bebe5ea351277dfe9f243bada7 Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Fri, 17 Apr 2026 14:54:10 +0200 Subject: [PATCH] some progress, but broken --- cig.h | 21 ++++++++++++++------- foobar.c | 8 +++++--- map.c | 11 ++++++----- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/cig.h b/cig.h index 5e7af07..7046595 100644 --- a/cig.h +++ b/cig.h @@ -280,7 +280,7 @@ void *map_create_func(map_create_func_args_t args); }); \ } while(0) -#define init_set(PTR, ...) do { \ +#define set_init(PTR, ...) do { \ (*(PTR)) = map_create_func((map_create_func_args_t) { \ .item_size=sizeof(*(*(PTR))), \ .key_size=sizeof(*(*(PTR))), \ @@ -291,8 +291,12 @@ void *map_create_func(map_create_func_args_t args); }); \ } while(0) -int map_len(void *this); -int map_cap(void *this); +int map_len_func(void *this); +#define map_len(THIS) map_len_func((void*) THIS) + +int map_cap_func(void *this); +#define map_cap(THIS) map_cap_func((void*) THIS) + uint32_t fnv_1a(const uint8_t *bytes, const unsigned int size); // Used for debugging the mapping array. Prints out nerdfonts icons to indicate // the state of each mapping slot. @@ -314,10 +318,13 @@ typedef struct index_pair { */ index_pair_t map_pair_hash(void *this, void *pair); -// TODO: NOT DONE -#define map_add(THIS, PAIR) do { \ - map_assure_growable_by_1(THIS, __FILE__, __LINE__); \ - index_pair_t idx_pair = map_pair_hash((*(THIS)), pair); \ +// TODO: NOT DONE!!! FIRST try to get the existing in +#define set_add(THIS, VALUE) do { \ + map_assure_growable_by_1((void**)THIS, __FILE__, __LINE__); \ + unsigned int len = map_len(*(THIS)); \ + (*(THIS))[len] = VALUE; \ + index_pair_t idx_pair = map_pair_hash((*(THIS)), &(*(THIS))[len]); \ + (*(THIS))[map_place((*(THIS)), idx_pair)] = VALUE; \ } while (0) // CLI ///////////////////////////////////////////////////////////////////////// diff --git a/foobar.c b/foobar.c index 218c802..3038f4a 100644 --- a/foobar.c +++ b/foobar.c @@ -4,9 +4,11 @@ int main() { with_borrow(allocator) { int *ints; - init_set(&ints, allocator, .initial_capacity=20); - map_print_mapping_state(ints, stdout); - set_add(ints, 10); + set_init(&ints, allocator, .initial_capacity=20); map_print_mapping_state(ints, stdout); + for ( int i = 0; i < 100; i++ ) { + set_add(&ints, i); + map_print_mapping_state(ints, stdout); + } } } diff --git a/map.c b/map.c index de93db7..2cb9edd 100644 --- a/map.c +++ b/map.c @@ -117,7 +117,7 @@ bool map_key_equals(void *this, const void *key1, const void *key2) { return true; } -int map_place(void *this, index_pair_t idx_pair) { +unsigned int map_place(void *this, index_pair_t idx_pair) { map_header_t *header = PTR_FROM_FIELD_PTR(map_header_t, bytes, this); if (idx_pair.has_old_index) { header->mapping_arr[idx_pair.new_index] = header->mapping_arr[idx_pair.old_index]; @@ -129,10 +129,11 @@ int map_place(void *this, index_pair_t idx_pair) { break; } } + } else { + header->mapping_arr[idx_pair.new_index] = header->n_items; } - // TODO WRONG!!! - return idx_pair.new_index; + return header->mapping_arr[idx_pair.new_index]; } index_pair_t map_pair_hash(void *this, void *pair) { @@ -249,7 +250,7 @@ uint32_t fnv_1a(const uint8_t *bytes, const unsigned int size) { return hash; } -int map_len(void *this) { +int map_len_func(void *this) { if (this == NULL) { return 0; } @@ -257,7 +258,7 @@ int map_len(void *this) { return header->n_items; } -int map_cap(void *this) { +int map_cap_func(void *this) { if (this == NULL) { return 0; }