probably some progress on hashtable, still breaks though

This commit is contained in:
herbglitch 2025-02-22 18:05:34 -07:00
parent fc06ed85e3
commit b3453d92e0
2 changed files with 163 additions and 167 deletions

View file

@ -196,42 +196,42 @@ void ARC_Hashtable_Add(ARC_Hashtable *hashtable, void *key, void *value){
hashtable->currentSize++;
}
void ARC_Hashtable_MoveNextBack(ARC_HashtableNode *nodes, uint32_t capacity, uint32_t *index){
//get the currently used next index
uint32_t nextIndex = nodes[*index].nextIndex;
void ARC_Hashtable_UnsetNodeAtIndexFromArray(ARC_HashtableNode *nodes, uint32_t capacity, uint32_t index){
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;
//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);
printf("%02u -> %2u\t%2u\n", index, nextIndex, hashIndex);
//if the end has been reached, set index to the end
if(*index == nextIndex){
//if the next index is already in the right place return out
if(nodes->nextIndex == hashIndex){
return;
//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;
}
//set the last index
*index = nextIndex;
return;
//move set the current index to the end
if(nodes[index].nextIndex == nextIndex){
nodes[index].nextIndex = index;
index = nextIndex;
break;
}
//moves the next index into the next used slot
nodes[index].nextIndex = nextIndex;
//get the next index to move back
index = 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;
//get the next index to move back
*index = nextIndex;
//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){
@ -251,7 +251,7 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
}
//check the last index if the others could not find the node
if(nodeFound == ARC_False && hashtable->keyCompareFn(hashtable->nodes[index].key, key) == ARC_True){
if(nodeFound == ARC_False && hashtable->nodes[index].key != NULL && hashtable->keyCompareFn(hashtable->nodes[index].key, key) == ARC_True){
nodeFound = ARC_True;
}
@ -272,10 +272,7 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
printf("MOVE:\n");
//move all next items back
ARC_Hashtable_MoveNextBack(hashtable->nodes, hashtable->currentCapacity, &index);
//set the current value to an empty node
hashtable->nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
ARC_Hashtable_UnsetNodeAtIndexFromArray(hashtable->nodes, hashtable->currentCapacity, index);
//we have removed the item so we can decrease the current size
hashtable->currentSize--;
@ -350,24 +347,16 @@ void *ARC_Hashtable_Get(ARC_Hashtable *hashtable, void *key){
ARC_HashtableNode node = hashtable->nodes[index];
//check each available node for a match
for(uint32_t nextIndex = index; node.key != NULL; node = hashtable->nodes[nextIndex]){
while(node.nextIndex != index){
//if the key is found, return its value
if(hashtable->keyCompareFn(node.key, key) == ARC_True){
return node.value;
}
//up the current index by one
nextIndex++;
//up the current index to the next available index
index = node.nextIndex;
//cycle back to the first index if it is above the array's capacity
if(nextIndex >= hashtable->currentCapacity){
nextIndex = 0;
}
//check if the loop has circled back to the starting index to stop checking
if(index == nextIndex){
break;
}
node = hashtable->nodes[index];
}
//if the key is found, return its value