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
|
|
@ -41,7 +41,7 @@ typedef void (* ARC_Hashtable_DestroyKeyValueFn)(void *key, void *value);
|
|||
* @param[in] key a key at the current iteration
|
||||
* @param[in] value a value that matches the key at the current iteration
|
||||
*/
|
||||
typedef void (* ARC_Hashtable_IteratorFn)(void *key, void *value, uint32_t temp, uint32_t tempid);
|
||||
typedef void (* ARC_Hashtable_IteratorFn)(void *key, void *value);
|
||||
|
||||
/**
|
||||
* @brief a resizable hashtable data type (will find next open slot before resizing)
|
||||
|
|
|
|||
|
|
@ -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++;
|
||||
|
||||
//cycle back to the first index if it is above the array's capacity
|
||||
if(index >= hashtable->currentCapacity){
|
||||
index = 0;
|
||||
index = hashtable->nodes[index].nextIndex;
|
||||
}
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,30 +9,28 @@
|
|||
|
||||
//TODO: add hash function for testing
|
||||
|
||||
void TEST_Hashtable_PrintKeyVal(void *key, void *value, uint32_t temp, uint32_t tempid){
|
||||
printf("%2d) %2d: %s\t%d\n", temp, tempid, (char *) key, *(int32_t *)value);
|
||||
void TEST_Hashtable_PrintIter(void *key, void *value){
|
||||
printf("%s, %d\n", (char *)key, *(int32_t *)value);
|
||||
}
|
||||
|
||||
void TEST_Hashtable_Print(ARC_Hashtable *hashtable){
|
||||
printf("Hashtable: \n");
|
||||
ARC_Hashtable_RunIteration(hashtable, TEST_Hashtable_PrintKeyVal);
|
||||
printf("\n");
|
||||
void TEST_Hashtable_Print(void *hashtable){
|
||||
ARC_Hashtable_RunIteration(hashtable, TEST_Hashtable_PrintIter);
|
||||
}
|
||||
|
||||
ARC_Bool TEST_Hashtable_KeyCompareDataFn(void *dataA, void *dataB){
|
||||
printf("%s, %s\n", (char *)dataA, (char *)dataB);
|
||||
return (ARC_Bool)strcmp((const char *)dataA, (const char *)dataB) == 0;
|
||||
}
|
||||
|
||||
//TODO: more tests with destroy data fn added
|
||||
void TEST_Hashtable_DestroyKeyValueFn(void *key, void *value){
|
||||
return;
|
||||
free((char *)key);
|
||||
free((int32_t *)value);
|
||||
}
|
||||
|
||||
ARC_TEST(Hashtable_Init){
|
||||
ARC_Hashtable *hashtable;
|
||||
ARC_Hashtable_KeyCompareFn keyCompareFn = TEST_Hashtable_KeyCompareDataFn;
|
||||
ARC_Hashtable_DestroyKeyValueFn destroyKeyValueFn = TEST_Hashtable_DestroyKeyValueFn;
|
||||
ARC_Hashtable_Create(&hashtable, NULL, &keyCompareFn, &destroyKeyValueFn);
|
||||
ARC_Hashtable_Create(&hashtable, NULL, &keyCompareFn, NULL);
|
||||
|
||||
ARC_CHECK(arc_errno == 0);
|
||||
|
||||
|
|
@ -42,8 +40,7 @@ ARC_TEST(Hashtable_Init){
|
|||
ARC_TEST(Hashtable_Add_Get_Remove){
|
||||
ARC_Hashtable *hashtable;
|
||||
ARC_Hashtable_KeyCompareFn keyCompareFn = TEST_Hashtable_KeyCompareDataFn;
|
||||
ARC_Hashtable_DestroyKeyValueFn destroyKeyValueFn = TEST_Hashtable_DestroyKeyValueFn;
|
||||
ARC_Hashtable_Create(&hashtable, NULL, &keyCompareFn, &destroyKeyValueFn);
|
||||
ARC_Hashtable_Create(&hashtable, NULL, &keyCompareFn, NULL);
|
||||
|
||||
char *key0 = (char *)"key0";
|
||||
char *key1 = (char *)"key1";
|
||||
|
|
@ -96,22 +93,85 @@ ARC_TEST(Hashtable_Add_Get_Remove){
|
|||
ARC_CHECK(5 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key7"));
|
||||
ARC_CHECK(6 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key8"));
|
||||
|
||||
TEST_Hashtable_Print(hashtable);
|
||||
ARC_Hashtable_Remove(hashtable, (void *)"key0");
|
||||
TEST_Hashtable_Print(hashtable);
|
||||
ARC_Hashtable_Remove(hashtable, (void *)"key4");
|
||||
TEST_Hashtable_Print(hashtable);
|
||||
ARC_Hashtable_Remove(hashtable, (void *)"key3");
|
||||
TEST_Hashtable_Print(hashtable);
|
||||
ARC_Hashtable_Remove(hashtable, (void *)"key8");
|
||||
TEST_Hashtable_Print(hashtable);
|
||||
ARC_Hashtable_Remove(hashtable, (void *)"key6");
|
||||
TEST_Hashtable_Print(hashtable);
|
||||
ARC_Hashtable_Remove(hashtable, (void *)"key7");
|
||||
TEST_Hashtable_Print(hashtable);
|
||||
|
||||
ARC_CHECK(7 != *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key1"));
|
||||
ARC_CHECK(1 != *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key5"));
|
||||
ARC_CHECK(7 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key1"));
|
||||
ARC_CHECK(1 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key5"));
|
||||
|
||||
ARC_Hashtable_Destroy(hashtable);
|
||||
}
|
||||
|
||||
ARC_TEST(Hashtable_Add_Get_100){
|
||||
ARC_Hashtable *hashtable;
|
||||
ARC_Hashtable_KeyCompareFn keyCompareFn = TEST_Hashtable_KeyCompareDataFn;
|
||||
ARC_Hashtable_DestroyKeyValueFn destroyKeyValueFn = TEST_Hashtable_DestroyKeyValueFn;
|
||||
ARC_Hashtable_Create(&hashtable, NULL, &keyCompareFn, &destroyKeyValueFn);
|
||||
|
||||
const char *keyCStr = "key%03u";
|
||||
|
||||
for(uint32_t index = 0; index < 100; index++){
|
||||
char *key = (char *)malloc(strlen(keyCStr));
|
||||
sprintf(key, keyCStr, index);
|
||||
|
||||
int32_t *val = (int32_t *)malloc(sizeof(int32_t));
|
||||
*val = index;
|
||||
|
||||
ARC_Hashtable_Add(hashtable, key, val);
|
||||
}
|
||||
|
||||
for(uint32_t index = 0; index < 100; index++){
|
||||
char *key = (char *)malloc(strlen(keyCStr));
|
||||
sprintf(key, keyCStr, index);
|
||||
|
||||
ARC_CHECK(index == *(int32_t *)ARC_Hashtable_Get(hashtable, key));
|
||||
|
||||
free(key);
|
||||
}
|
||||
|
||||
ARC_Hashtable_Destroy(hashtable);
|
||||
}
|
||||
|
||||
//ARC_TEST(Hashtable_Add_Get_Remove_100){
|
||||
// ARC_Hashtable *hashtable;
|
||||
// ARC_Hashtable_KeyCompareFn keyCompareFn = TEST_Hashtable_KeyCompareDataFn;
|
||||
// ARC_Hashtable_DestroyKeyValueFn destroyKeyValueFn = TEST_Hashtable_DestroyKeyValueFn;
|
||||
// ARC_Hashtable_Create(&hashtable, NULL, &keyCompareFn, &destroyKeyValueFn);
|
||||
//
|
||||
// const char *keyCStr = "key%03u";
|
||||
//
|
||||
// for(uint32_t index = 0; index < 100; index++){
|
||||
// char *key = (char *)malloc(strlen(keyCStr));
|
||||
// sprintf(key, keyCStr, index);
|
||||
//
|
||||
// int32_t *val = (int32_t *)malloc(sizeof(int32_t));
|
||||
// *val = index;
|
||||
//
|
||||
// ARC_Hashtable_Add(hashtable, key, val);
|
||||
// }
|
||||
//
|
||||
// for(uint32_t index = 0; index < 100; index++){
|
||||
// char *key = (char *)malloc(strlen(keyCStr));
|
||||
// sprintf(key, keyCStr, index);
|
||||
//
|
||||
// ARC_CHECK(index == *(int32_t *)ARC_Hashtable_Get(hashtable, key));
|
||||
//
|
||||
// free(key);
|
||||
// }
|
||||
//
|
||||
// for(uint32_t index = 0; index < 100; index++){
|
||||
// char *key = (char *)malloc(strlen(keyCStr));
|
||||
// sprintf(key, keyCStr, index);
|
||||
//
|
||||
// TEST_Hashtable_Print(hashtable);
|
||||
// ARC_Hashtable_Remove(hashtable, key);
|
||||
//
|
||||
// free(key);
|
||||
// }
|
||||
//
|
||||
// ARC_Hashtable_Destroy(hashtable);
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -32,10 +32,9 @@ void ARC_Test_UnsetErrnoStream(void) __attribute__ ((destructor));
|
|||
|
||||
#define ARC_CHECK(TEST)\
|
||||
if(temp_arc_test_num_checks_run__ARC_TEST__ == NULL || temp_arc_test_num_checks_passed__ARC_TEST__ == NULL){ return; }\
|
||||
printf(" %4u) ", *temp_arc_test_num_checks_run__ARC_TEST__);\
|
||||
++*temp_arc_test_num_checks_run__ARC_TEST__;\
|
||||
if(TEST){ printf("PASS\t\n"); ++*temp_arc_test_num_checks_passed__ARC_TEST__; }\
|
||||
else { printf("FAIL\t%s\n", #TEST); }
|
||||
if(TEST){ ++*temp_arc_test_num_checks_passed__ARC_TEST__; }\
|
||||
else { printf(" %4u) ", *temp_arc_test_num_checks_run__ARC_TEST__); printf("FAIL\t%s\n", #TEST); }
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue