diff --git a/.gitignore b/.gitignore index 6f2c669..5ddeb0f 100644 --- a/.gitignore +++ b/.gitignore @@ -529,3 +529,6 @@ doc/doxygen/html .ccls .vscode tests/test_error_out.txt + +vgcore.* +callgrind.* diff --git a/include/arc/std/hashtable.h b/include/arc/std/hashtable.h index e79d15f..5654f14 100644 --- a/include/arc/std/hashtable.h +++ b/include/arc/std/hashtable.h @@ -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); +typedef void (* ARC_Hashtable_IteratorFn)(void *key, void *value, uint32_t temp, uint32_t tempid); /** * @brief a resizable hashtable data type (will find next open slot before resizing) diff --git a/include/arc/std/lexer.h b/include/arc/std/lexer.h index a4d803e..111d04c 100644 --- a/include/arc/std/lexer.h +++ b/include/arc/std/lexer.h @@ -69,8 +69,8 @@ void ARC_Lexer_Destroy(ARC_Lexer *lexer); /** * @brief adds a token rule to a lexer * - * @param [in] lexer the lexer to add a token rule to - * @param [in] tokenRule the token rule to add + * @param[in] lexer the lexer to add a token rule to + * @param[in] tokenRule the token rule to add */ void ARC_Lexer_RegisterTokenRule(ARC_Lexer *lexer, ARC_LexerTokenRule tokenRule); diff --git a/src/std/hashtable.c b/src/std/hashtable.c index 34a92e3..7732d4c 100644 --- a/src/std/hashtable.c +++ b/src/std/hashtable.c @@ -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); } } diff --git a/tests/std/hashtable.c b/tests/std/hashtable.c index 71cbc41..37da6ab 100644 --- a/tests/std/hashtable.c +++ b/tests/std/hashtable.c @@ -2,20 +2,30 @@ #include "arc/std/bool.h" #include "arc/std/errno.h" #include "arc/std/hashtable.h" +#include #include #include #include //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_Print(ARC_Hashtable *hashtable){ + printf("Hashtable: \n"); + ARC_Hashtable_RunIteration(hashtable, TEST_Hashtable_PrintKeyVal); + printf("\n"); +} + ARC_Bool TEST_Hashtable_KeyCompareDataFn(void *dataA, void *dataB){ - return (ARC_Bool)strcmp((const char *)dataA, (const 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){ - free((char *)key); - free((int32_t *)value); + return; } ARC_TEST(Hashtable_Init){ @@ -29,17 +39,79 @@ ARC_TEST(Hashtable_Init){ ARC_Hashtable_Destroy(hashtable); } -ARC_TEST(Hashtable_Add){ +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); - char *key0 = (char *)"tests"; - int32_t val0 = 2; - ARC_Hashtable_Add(hashtable, &key0, &val0); + char *key0 = (char *)"key0"; + char *key1 = (char *)"key1"; + char *key2 = (char *)"key2"; + char *key3 = (char *)"key3"; + char *key4 = (char *)"key4"; + char *key5 = (char *)"key5"; + char *key6 = (char *)"key6"; + char *key7 = (char *)"key7"; + char *key8 = (char *)"key8"; - ARC_CHECK(2 == *(int32_t *)ARC_Hashtable_Get(hashtable, (char *)"tests")); + int32_t val0 = 2; + int32_t val1 = 7; + int32_t val2 = 4; + int32_t val3 = 9; + int32_t val4 = 0; + int32_t val5 = 1; + int32_t val6 = 3; + int32_t val7 = 5; + int32_t val8 = 6; + + ARC_Hashtable_Add(hashtable, key0, &val0); + ARC_Hashtable_Add(hashtable, key1, &val1); + ARC_Hashtable_Add(hashtable, key2, &val2); + ARC_Hashtable_Add(hashtable, key3, &val3); + ARC_Hashtable_Add(hashtable, key4, &val4); + ARC_Hashtable_Add(hashtable, key5, &val5); + ARC_Hashtable_Add(hashtable, key6, &val6); + ARC_Hashtable_Add(hashtable, key7, &val7); + ARC_Hashtable_Add(hashtable, key8, &val8); + + ARC_CHECK(2 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key0")); + ARC_CHECK(7 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key1")); + ARC_CHECK(4 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key2")); + ARC_CHECK(9 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key3")); + ARC_CHECK(0 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key4")); + ARC_CHECK(1 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key5")); + ARC_CHECK(3 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key6")); + ARC_CHECK(5 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key7")); + ARC_CHECK(6 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key8")); + + ARC_Hashtable_Remove(hashtable, (void *)"key2"); + + ARC_CHECK(2 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key0")); + ARC_CHECK(7 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key1")); + ARC_CHECK(9 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key3")); + ARC_CHECK(0 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key4")); + ARC_CHECK(1 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key5")); + ARC_CHECK(3 == *(int32_t *)ARC_Hashtable_Get(hashtable, (void *)"key6")); + 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_Hashtable_Destroy(hashtable); }