hashtable 'fixed' and tested with 1 - 10000 entries adding and removing, might have some very nitch edge cases, but idk what else to test for

This commit is contained in:
herbglitch 2025-03-06 01:44:18 -07:00
parent 262e6ef9de
commit d79f9bb2ec
2 changed files with 131 additions and 193 deletions

View file

@ -1,5 +1,4 @@
#include "arc/std/hashtable.h"
#include "arc/std/errno.h"
#include <stdlib.h>
#include <stdio.h>
@ -51,6 +50,7 @@ uint32_t CRC32Fn(void *key){
return hashvalue;
}
//private function that will just check compare void pointers directly used as default key compare
ARC_Bool ARC_Hashtable_DefaultKeyCompareFn(void *key1, void *key2){
return (ARC_Bool)(key1 == key2);
}
@ -105,6 +105,7 @@ void ARC_Hashtable_Destroy(ARC_Hashtable *hashtable){
free(hashtable);
}
//private function to add a node as close to its hashed index as possible
void ARC_HashtableNode_SetNearestNodeToArray(ARC_HashtableNode *nodes, uint32_t capacity, ARC_HashtableNode node){
//get the first possible index based on the node's hashvalue
uint32_t index = node.hashvalue % capacity;
@ -196,10 +197,7 @@ void ARC_Hashtable_Add(ARC_Hashtable *hashtable, void *key, void *value){
hashtable->currentSize++;
}
void ARC_Hashtable_VectorDestroyHashtableNodeFn(void *data){
free((ARC_HashtableNode *)data);
}
//private function used to remove a node at an index (moving next node values back)
void ARC_Hashtable_UnsetNodeAtIndexFromArray(ARC_HashtableNode *nodes, uint32_t capacity, uint32_t index, uint32_t previousIndex){
//if the first index is the end index just set it to null and return
if(nodes[index].nextIndex == index && nodes[previousIndex].nextIndex == index){
@ -218,7 +216,7 @@ void ARC_Hashtable_UnsetNodeAtIndexFromArray(ARC_HashtableNode *nodes, uint32_t
//if the next index will be moved into the correct spot
if(index == nodes[nextIndex].hashvalue % capacity){
//NOTE: I couldn't figure out an elegant way of handling this, so for now we remove then readd clashing nodes
//NOTE: I couldn't figure out an elegant way of handling this, so for now we remove then re-add clashing nodes
//reset the previous index's next index
nodes[previousIndex].nextIndex = previousIndex;
@ -233,14 +231,12 @@ void ARC_Hashtable_UnsetNodeAtIndexFromArray(ARC_HashtableNode *nodes, uint32_t
//clear the last moved node
nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
printf("move: %u -> %u\t%u\t%s\n", index, nodeCopy.nextIndex, nodeCopy.hashvalue % capacity, (char *)nodeCopy.key);
//loop through remaining next nodes adding them to a temporary vector and clearing them from the nodes
while(nodeCopy.nextIndex != index){
//move to the next node
index = nodeCopy.nextIndex;
nodeCopy = nodes[index];
printf("next: %u -> %u\t%u\t%s\n", index, nodeCopy.nextIndex, nodeCopy.hashvalue % capacity, (char *)nodeCopy.key);
//copy and clear node
nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
@ -262,62 +258,13 @@ void ARC_Hashtable_UnsetNodeAtIndexFromArray(ARC_HashtableNode *nodes, uint32_t
index = nextIndex;
}
//the previous index will be the last moved node, so set its next index to itself
nodes[previousIndex].nextIndex = previousIndex;
//set the current value to an empty node
nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
}
//void ARC_Hashtable_UnsetNodeAtIndexFromArray(ARC_HashtableNode *nodes, uint32_t capacity, uint32_t index, uint32_t previousIndex){
// while(nodes[index].nextIndex != index){
// //get the currently used next index
// uint32_t nextIndex = nodes[index].nextIndex;
//
// //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);
//
// //move the current node back one
// nodes[index] = nodes[nextIndex];
//
// //if the end has been reached, set index to the end
// if(nodes[nextIndex].nextIndex == hashIndex){
// //fix if moved back but next index not updated
// nodes[nextIndex].nextIndex = hashIndex;
// break;
// }
//
// //move set the current index to the end
// if(nodes[index].nextIndex == nextIndex){
// nodes[index].nextIndex = index;
//
// //check to see if the previous index is pointing to something it shouldn't be
// if(nodes[index].nextIndex == hashIndex){
// nodes[previousIndex].nextIndex = previousIndex;
// }
//
// index = nextIndex;
// continue;
// }
//
// //moves the next index into the next used slot
// nodes[index].nextIndex = nextIndex;
//
// //set the previous index
// previousIndex = index;
//
// //get the next index to move back
// index = nextIndex;
// }
//
// //fix pointing at a nonexistant node if the index was the first node found
// if(previousIndex != index && nodes[previousIndex].nextIndex == index){
// nodes[previousIndex].nextIndex = previousIndex;
// }
//
// //set the current value to an empty node
// nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
//}
void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
//get the index from a hashvalue
uint32_t initialIndex = hashtable->hashFn(key) % hashtable->currentCapacity;
@ -356,7 +303,6 @@ 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_UnsetNodeAtIndexFromArray(hashtable->nodes, hashtable->currentCapacity, index, previousIndex);
@ -368,7 +314,6 @@ 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;
@ -465,8 +410,6 @@ 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);
}