From 634a40c638204828330accdf69cb61099e30c14e Mon Sep 17 00:00:00 2001 From: herbglitch Date: Wed, 19 Feb 2025 18:28:49 -0700 Subject: [PATCH] still thinking through hashtable --- src/std/hashtable.c | 22 ++++++++++++++++------ tests/std/hashtable.c | 5 +++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/std/hashtable.c b/src/std/hashtable.c index f2e3d16..74351bc 100644 --- a/src/std/hashtable.c +++ b/src/std/hashtable.c @@ -203,21 +203,28 @@ void ARC_Hashtable_MoveNextBack(ARC_HashtableNode *nodes, uint32_t capacity, uin //if the next index is an end, but it is in the right place this should break uint32_t hashIndex = nodes[nextIndex].hashvalue % capacity; + printf("%02u -> %2u\t%2u\n", *index, nextIndex, hashIndex); + //if the end has been reached, set index to the end - if(nodes->nextIndex == nextIndex){ + if(*index == nextIndex){ //if the next index is already in the right place return out if(nodes->nextIndex == hashIndex){ return; } - //set the index to the end - nodes[*index].nextIndex = *index; + //set the last index *index = nextIndex; return; } - //move the current node back one - nodes[*index] = nodes[nextIndex]; + //run a next index to be used if the last index is this index + uint32_t testIndex = nextIndex; + ARC_Hashtable_MoveNextBack(nodes, capacity, &testIndex); + + if(testIndex != nextIndex){ + //move the current node back one + nodes[*index] = nodes[nextIndex]; + } //moves the next index into the next used slot nodes[*index].nextIndex = nextIndex; @@ -225,7 +232,6 @@ void ARC_Hashtable_MoveNextBack(ARC_HashtableNode *nodes, uint32_t capacity, uin //get the next index to move back *index = nextIndex; - ARC_Hashtable_MoveNextBack(nodes, capacity, index); } void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){ @@ -264,6 +270,7 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){ (*(hashtable->destroyKeyValueFn))(node.key, node.value); } + printf("MOVE:\n"); //move all next items back ARC_Hashtable_MoveNextBack(hashtable->nodes, hashtable->currentCapacity, &index); @@ -278,6 +285,7 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){ return; } + printf("RESIZE:\n"); //move the current nodes into a temporary variable to move into a resized array uint64_t oldCapacity = hashtable->currentCapacity; ARC_HashtableNode *oldNodes = hashtable->nodes; @@ -382,6 +390,8 @@ void ARC_Hashtable_RunIteration(ARC_Hashtable *hashtable, ARC_Hashtable_Iterator continue; } + printf("%02u -> %02u\t%02u\t%s\n", index, node.nextIndex, node.hashvalue % hashtable->currentCapacity, (char *)node.key); + //passes current iteration into the callback function iteratorFn(node.key, node.value); } diff --git a/tests/std/hashtable.c b/tests/std/hashtable.c index f2542e7..9cff201 100644 --- a/tests/std/hashtable.c +++ b/tests/std/hashtable.c @@ -10,7 +10,7 @@ //TODO: add hash function for testing void TEST_Hashtable_PrintIter(void *key, void *value){ - printf("%s, %d\n", (char *)key, *(int32_t *)value); + //printf("%s, %d\n", (char *)key, *(int32_t *)value); } void TEST_Hashtable_Print(void *hashtable){ @@ -147,7 +147,7 @@ ARC_TEST(Hashtable_Add_Get_Remove_100){ ARC_Hashtable_Create(&hashtable, NULL, &keyCompareFn, &destroyKeyValueFn); const char *keyCStr = "key%03u"; - uint32_t maxVal = 1000; + uint32_t maxVal = 15; for(uint32_t index = 0; index < maxVal; index++){ char *key = (char *)malloc(strlen(keyCStr)); @@ -172,6 +172,7 @@ ARC_TEST(Hashtable_Add_Get_Remove_100){ char *key = (char *)malloc(strlen(keyCStr)); sprintf(key, keyCStr, index); + TEST_Hashtable_Print(hashtable); ARC_Hashtable_Remove(hashtable, key); free(key);