added userdata to hastable to be able to do stuff in the iterator function callback

This commit is contained in:
herbglitch 2025-03-06 03:11:16 -07:00
parent d79f9bb2ec
commit 7316f7c779
5 changed files with 80 additions and 25 deletions

View file

@ -343,10 +343,17 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
free(oldNodes);
}
//private callback function to delete all the key value pairs in the hashtable
void ARC_Hashtable_DestroyKeyValueIteratorFn(void *key, void *value, void *userData){
ARC_Hashtable *hashtable = (ARC_Hashtable *)userData;
(*(hashtable->destroyKeyValueFn))(key, value);
}
void ARC_Hashtable_Clear(ARC_Hashtable *hashtable){
//NOTE: this recasts ARC_Hashtable_DestroyKeyValueFn because it matches ARC_Hashtable_IteratorFn, if either is changed, this needs updated
//if the destroyKeyValueFn exists, run iterations to clear the table
if(hashtable->destroyKeyValueFn != NULL){
ARC_Hashtable_RunIteration(hashtable, *(hashtable->destroyKeyValueFn));
ARC_Hashtable_RunIteration(hashtable, ARC_Hashtable_DestroyKeyValueIteratorFn, hashtable);
}
//delete the array holding all the nodes
@ -399,7 +406,7 @@ void *ARC_Hashtable_Get(ARC_Hashtable *hashtable, void *key){
return NULL;
}
void ARC_Hashtable_RunIteration(ARC_Hashtable *hashtable, ARC_Hashtable_IteratorFn iteratorFn){
void ARC_Hashtable_RunIteration(ARC_Hashtable *hashtable, ARC_Hashtable_IteratorFn iteratorFn, void *userData){
//pass each non NULL nodes into an iteratorFn callback
for(uint32_t index = 0; index < hashtable->currentCapacity; index++){
//get the current node
@ -411,6 +418,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, userData);
}
}