adding fixed, working on debugging infinite loop

This commit is contained in:
herbglitch 2025-02-16 23:45:27 -07:00
parent fc5bf78919
commit 6a9f914ffb
5 changed files with 144 additions and 64 deletions

View file

@ -13,7 +13,7 @@ struct ARC_HashtableNode {
uint32_t hashvalue;
//will be set if next slot is searched for, to be used to remove elements faster
uint32_t initialIndex;
uint32_t nextIndex;
};
struct ARC_Hashtable {
@ -109,34 +109,36 @@ void ARC_HashtableNode_SetNearestNodeToArray(ARC_HashtableNode *nodes, uint32_t
//get the first possible index based on the node's hashvalue
uint32_t index = node.hashvalue % capacity;
//get the first possible node
ARC_HashtableNode foundNode = nodes[index];
//go to last added
while(nodes[index].nextIndex != index){
index = nodes[index].nextIndex;
}
//init variable for found node
uint32_t nextIndex = index;
//check each available node for a free slot
while(foundNode.key != NULL){
while(nodes[nextIndex].key != NULL){
//up the current index by one
nextIndex++;
//cycle back to the first index if it is above the array's capacity
if(nextIndex >= capacity){
index = 0;
nextIndex = 0;
}
//check if the loop has circled back to the starting index to stop checking
if(index == nextIndex){
break;
}
//get the next possible node
foundNode = nodes[index];
}
//set the next index of the last added index
nodes[index].nextIndex = nextIndex;
//set the foundNode and next index
nodes[nextIndex] = node;
nodes[nextIndex].initialIndex = index;
nodes[nextIndex] = node;
nodes[nextIndex].nextIndex = nextIndex;
}
void ARC_Hashtable_Add(ARC_Hashtable *hashtable, void *key, void *value){
@ -171,15 +173,18 @@ void ARC_Hashtable_Add(ARC_Hashtable *hashtable, void *key, void *value){
//resize the hashtable's array and copy the contents at the same time
hashtable->nodes = (ARC_HashtableNode *)malloc(sizeof(ARC_HashtableNode) * hashtable->currentCapacity);
//set keys to null
//set nodes to null
for(uint32_t index = 0; index < hashtable->currentCapacity; index++){
hashtable->nodes[index].key = NULL;
hashtable->nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
}
//add the old nodes into the new array
for(uint32_t index = 0; index < oldCapacity; index++){
ARC_HashtableNode_SetNearestNodeToArray(hashtable->nodes, hashtable->currentCapacity, oldNodes[index]);
}
//free the old array
free(oldNodes);
}
//get the hashvalue
@ -192,34 +197,35 @@ void ARC_Hashtable_Add(ARC_Hashtable *hashtable, void *key, void *value){
void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
//get the index from a hashvalue
uint32_t index = hashtable->hashFn(key) % hashtable->currentCapacity;
uint32_t initialIndex = hashtable->hashFn(key) % hashtable->currentCapacity;
uint32_t index = initialIndex;
//get the first possible node
ARC_HashtableNode node = hashtable->nodes[index];
//check each available node for a match and break if the current nodes doesn't hold anything
ARC_Bool nodeFound = ARC_False;
for(uint32_t nextIndex = index; node.key != NULL;){
while(node.key != NULL){
if(hashtable->keyCompareFn(node.key, key) == ARC_True){
index = nextIndex;
nodeFound = ARC_True;
break;
}
//up the current index by one
nextIndex++;
index++;
//cycle back to the first index if it is above the array's capacity
if(nextIndex >= hashtable->currentCapacity){
nextIndex = 0;
if(index >= hashtable->currentCapacity){
index = 0;
}
//check if the loop has circled back to the starting index to stop checking
if(index == nextIndex){
if(index == initialIndex){
break;
}
//get the next possible node
node = hashtable->nodes[nextIndex];
node = hashtable->nodes[index];
}
//error if the node was not found
@ -234,41 +240,28 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
(*(hashtable->destroyKeyValueFn))(node.key, node.value);
}
//set the current index to a starting index
uint32_t currentIndex = index;
//cycle back to the first index if it is above the array's capacity
if(index >= hashtable->currentCapacity){
index = 0;
}
//get the next possible node
node = hashtable->nodes[currentIndex];
//while the current node
while(node.initialIndex == index){
//set the last index to move a offset node back to
uint32_t lastIndex = index;
//up the current index by one
currentIndex++;
//cycle back to the first index if it is above the array's capacity
if(currentIndex >= hashtable->currentCapacity){
currentIndex = 0;
}
//check if the loop has circled back to the starting index to stop checking and throw an error
if(index == currentIndex){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_Hashtable_Remove(hashtable, key), removed index matched initalIndex of node, this should never happen");
return;
}
//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 node
node = hashtable->nodes[index];
//move the current node back one
hashtable->nodes[lastIndex] = hashtable->nodes[currentIndex];
hashtable->nodes[index] = hashtable->nodes[node.nextIndex];
//get the next possible node
node = hashtable->nodes[currentIndex];
//get the next index to move back
index = hashtable->nodes[index].nextIndex;
//moves the next index into the next used slot
hashtable->nodes[index].nextIndex = node.nextIndex;
}
//set the current value to an empty node
hashtable->nodes[currentIndex] = (ARC_HashtableNode){ NULL, NULL, 0, currentIndex };
hashtable->nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
//we have removed the item so we can decrease the current size
hashtable->currentSize--;
@ -290,13 +283,16 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
//set keys to null
for(uint32_t index = 0; index < hashtable->currentCapacity; index++){
hashtable->nodes[index].key = NULL;
hashtable->nodes[index] = (ARC_HashtableNode){ NULL, NULL, 0, index };
}
//add the old nodes into the new array
for(uint32_t index = 0; index < oldCapacity; index++){
ARC_HashtableNode_SetNearestNodeToArray(hashtable->nodes, hashtable->currentCapacity, oldNodes[index]);
}
//free the old array
free(oldNodes);
}
void ARC_Hashtable_Clear(ARC_Hashtable *hashtable){
@ -315,6 +311,13 @@ void ARC_Hashtable_Clear(ARC_Hashtable *hashtable){
}
void *ARC_Hashtable_Get(ARC_Hashtable *hashtable, void *key){
//check to make sure key is not NULL
if(key == NULL){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR("ARC_Hashtable_Get(hashtable, key), NULL was passed in for the key, this function cannot handle that");
return NULL;
}
//get the index from a hashvalue
uint32_t index = hashtable->hashFn(key) % hashtable->currentCapacity;
@ -322,7 +325,7 @@ 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;){
for(uint32_t nextIndex = index; node.key != NULL; node = hashtable->nodes[nextIndex]){
//if the key is found, return its value
if(hashtable->keyCompareFn(node.key, key) == ARC_True){
return node.value;
@ -340,9 +343,11 @@ void *ARC_Hashtable_Get(ARC_Hashtable *hashtable, void *key){
if(index == nextIndex){
break;
}
}
//get the next possible node
node = hashtable->nodes[nextIndex];
//if the key is found, return its value
if(node.key != NULL && hashtable->keyCompareFn(node.key, key) == ARC_True){
return node.value;
}
//could not find node, so return NULL
@ -361,6 +366,6 @@ void ARC_Hashtable_RunIteration(ARC_Hashtable *hashtable, ARC_Hashtable_Iterator
}
//passes current iteration into the callback function
iteratorFn(node.key, node.value);
iteratorFn(node.key, node.value, index, node.nextIndex);
}
}