still thinking through hashtable

This commit is contained in:
herbglitch 2025-02-19 18:28:49 -07:00
parent 6f65e4b424
commit 634a40c638
2 changed files with 19 additions and 8 deletions

View file

@ -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;
}
//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);
}

View file

@ -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);