basic hastable working, though stress testing breaks
This commit is contained in:
parent
6a9f914ffb
commit
c175278416
4 changed files with 114 additions and 57 deletions
|
|
@ -200,32 +200,20 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
|
|||
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
|
||||
//iterate through remaining possible nodes checking for a match
|
||||
ARC_Bool nodeFound = ARC_False;
|
||||
while(node.key != NULL){
|
||||
if(hashtable->keyCompareFn(node.key, key) == ARC_True){
|
||||
while(hashtable->nodes[index].nextIndex != index){
|
||||
if(hashtable->keyCompareFn(hashtable->nodes[index].key, key) == ARC_True){
|
||||
nodeFound = ARC_True;
|
||||
break;
|
||||
}
|
||||
|
||||
//up the current index by one
|
||||
index++;
|
||||
index = hashtable->nodes[index].nextIndex;
|
||||
}
|
||||
|
||||
//cycle back to the first index if it is above the array's capacity
|
||||
if(index >= hashtable->currentCapacity){
|
||||
index = 0;
|
||||
}
|
||||
|
||||
//check if the loop has circled back to the starting index to stop checking
|
||||
if(index == initialIndex){
|
||||
break;
|
||||
}
|
||||
|
||||
//get the next possible node
|
||||
node = hashtable->nodes[index];
|
||||
//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){
|
||||
nodeFound = ARC_True;
|
||||
}
|
||||
|
||||
//error if the node was not found
|
||||
|
|
@ -235,29 +223,34 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
|
|||
return;
|
||||
}
|
||||
|
||||
//get the found node
|
||||
ARC_HashtableNode node = hashtable->nodes[index];
|
||||
|
||||
//call delete data to clean up item if delete data function exists
|
||||
if(hashtable->destroyKeyValueFn != NULL){
|
||||
(*(hashtable->destroyKeyValueFn))(node.key, node.value);
|
||||
}
|
||||
|
||||
//cycle back to the first index if it is above the array's capacity
|
||||
if(index >= hashtable->currentCapacity){
|
||||
index = 0;
|
||||
}
|
||||
|
||||
//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];
|
||||
//get the currently used next index
|
||||
uint32_t nextIndex = hashtable->nodes[index].nextIndex;
|
||||
|
||||
//move the current node back one
|
||||
hashtable->nodes[index] = hashtable->nodes[node.nextIndex];
|
||||
hashtable->nodes[index] = hashtable->nodes[nextIndex];
|
||||
|
||||
//get the next index to move back
|
||||
index = hashtable->nodes[index].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 = node.nextIndex;
|
||||
hashtable->nodes[index].nextIndex = nextIndex;
|
||||
|
||||
//get the next index to move back
|
||||
index = nextIndex;
|
||||
}
|
||||
|
||||
//set the current value to an empty node
|
||||
|
|
@ -296,6 +289,11 @@ void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key){
|
|||
}
|
||||
|
||||
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(hashtable->destroyKeyValueFn != NULL){
|
||||
ARC_Hashtable_RunIteration(hashtable, *(hashtable->destroyKeyValueFn));
|
||||
}
|
||||
|
||||
//delete the array holding all the nodes
|
||||
free(hashtable->nodes);
|
||||
|
||||
|
|
@ -366,6 +364,6 @@ void ARC_Hashtable_RunIteration(ARC_Hashtable *hashtable, ARC_Hashtable_Iterator
|
|||
}
|
||||
|
||||
//passes current iteration into the callback function
|
||||
iteratorFn(node.key, node.value, index, node.nextIndex);
|
||||
iteratorFn(node.key, node.value);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue