still working on remove, pushing to be able to work on laptop, started recursive move back, still broke

This commit is contained in:
herbglitch 2025-02-19 03:06:18 -07:00
parent c175278416
commit c173519f14
2 changed files with 84 additions and 61 deletions

View file

@ -113,6 +113,7 @@ void ARC_HashtableNode_SetNearestNodeToArray(ARC_HashtableNode *nodes, uint32_t
while(nodes[index].nextIndex != index){
index = nodes[index].nextIndex;
}
index = nodes[index].nextIndex;
//init variable for found node
uint32_t nextIndex = index;
@ -195,6 +196,38 @@ 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;
//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 end has been reached, set index to the end
if(nodes->nextIndex == 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;
*index = nextIndex;
return;
}
//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;
ARC_Hashtable_MoveNextBack(nodes, capacity, index);
}
void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
//get the index from a hashvalue
uint32_t initialIndex = hashtable->hashFn(key) % hashtable->currentCapacity;
@ -231,27 +264,8 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
(*(hashtable->destroyKeyValueFn))(node.key, node.value);
}
//while the current node needs to be moved back becuase it is offset to the initial index
while(hashtable->nodes[index].nextIndex != index){
//get the currently used next index
uint32_t nextIndex = hashtable->nodes[index].nextIndex;
//move the current node back one
hashtable->nodes[index] = hashtable->nodes[nextIndex];
//check if the next index is an end index and set this index to end
if(nextIndex == hashtable->nodes[nextIndex].nextIndex){
hashtable->nodes[index].nextIndex = index;
index = nextIndex;
break;
}
//moves the next index into the next used slot
hashtable->nodes[index].nextIndex = nextIndex;
//get the next index to move back
index = nextIndex;
}
//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 };
@ -281,6 +295,11 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
//add the old nodes into the new array
for(uint32_t index = 0; index < oldCapacity; index++){
//null values do not need to be copied
if(oldNodes[index].key == NULL){
continue;
}
ARC_HashtableNode_SetNearestNodeToArray(hashtable->nodes, hashtable->currentCapacity, oldNodes[index]);
}