working on unload and added getsize to hashtable

This commit is contained in:
herbglitch 2025-03-09 06:20:07 -06:00
parent 3fbccd9752
commit ba467ac6b6
3 changed files with 53 additions and 1 deletions

View file

@ -104,6 +104,15 @@ void ARC_Hashtable_Clear(ARC_Hashtable *hashtable);
*/
void *ARC_Hashtable_Get(ARC_Hashtable *hashtable, void *key);
/**
* @brief gets the number of elements stored in the hashtable
*
* @param[in] hashtable the hashtable to get number of elements from
*
* @return the size of the vector
*/
uint32_t ARC_Hashtable_GetSize(ARC_Hashtable *hashtable);
/**
* @brief iterates through a hashtable passing available key value pairs to a callback
*

View file

@ -16,6 +16,8 @@ struct ARC_Chemical {
ARC_Hashtable *groups;
ARC_Hashtable *currentGroup;
ARC_Bool load;
};
typedef struct ARC_ChemicalTypeData {
@ -313,6 +315,13 @@ void ARC_ChemicalData_RunVariableLineTag(ARC_ParserTagToken *tagToken, ARC_Chemi
ARC_String_Create(&variableString, NULL, 0);
ARC_ParserData_HelperRecurseStringAdd(&variableString, childTagToken);
//check if removing
if(chemical->load == ARC_False){
ARC_Hashtable_Remove(chemical->currentGroup, (void *)variableString->data);
ARC_String_Destroy(variableString);
return;
}
//check to see if the current variable is already in the current group hashtable
ARC_ChemicalTypeData *typeData = (ARC_ChemicalTypeData *)ARC_Hashtable_Get(chemical->currentGroup, variableString->data);
if(typeData != NULL){
@ -420,6 +429,13 @@ void ARC_ChemicalData_RunGroupTag(ARC_ParserTagToken *tagToken, ARC_Chemical *ch
ARC_String_Create(&groupVariable, NULL, 0);
ARC_ParserData_HelperRecurseStringAdd(&groupVariable, childTagToken);
//check if removing
if(chemical->load == ARC_False){
//TODO: check if the group is empty and remove it if it is
ARC_String_Destroy(groupVariable);
return;
}
//get the needed hashtable or create it from the groups hashtable
ARC_Hashtable *groupHashtable = ARC_Hashtable_Get(chemical->groups, (void *)groupVariable->data);
if(groupHashtable == NULL){
@ -577,6 +593,8 @@ void ARC_Chemical_Create(ARC_Chemical **chemical){
ARC_Hashtable_Create(&((*chemical)->currentGroup), NULL, &keyCompareFn, &groupDataDestroyKeyValueFn);
ARC_Hashtable_Add((*chemical)->groups, (void *)emptyCStr, (*chemical)->currentGroup);
(*chemical)->load = ARC_True;
//cleanup
ARC_String_Destroy(languageString);
}
@ -691,16 +709,22 @@ void *ARC_Chemical_GetWithCStr(ARC_Chemical *chemical, const char *element){
}
void ARC_Chemical_LoadFromString(ARC_Chemical *chemical, ARC_String **string){
chemical->load = ARC_True;
ARC_Parser_Parse(chemical->parser, string);
}
void ARC_Chemical_LoadFromFile(ARC_Chemical *chemical, ARC_String *path){
chemical->load = ARC_True;
ARC_Parser_ParseFile(chemical->parser, path);
}
void ARC_Chemical_UnloadFromString(ARC_Chemical *chemical, ARC_String **string){
chemical->load = ARC_False;
ARC_Parser_Parse(chemical->parser, string);
}
void ARC_Chemical_UnloadFromFile(ARC_Chemical *chemical, ARC_String *data){
void ARC_Chemical_UnloadFromFile(ARC_Chemical *chemical, ARC_String *path){
chemical->load = ARC_False;
ARC_Parser_ParseFile(chemical->parser, path);
}

View file

@ -406,6 +406,25 @@ void *ARC_Hashtable_Get(ARC_Hashtable *hashtable, void *key){
return NULL;
}
uint32_t ARC_Hashtable_GetSize(ARC_Hashtable *hashtable){
uint32_t size = 0;
//loop through the vector and add iterate the size when the value is not null
for(uint32_t index = 0; index < hashtable->currentCapacity; index++){
//get the current node
ARC_HashtableNode node = hashtable->nodes[index];
//skip past NULL keys
if(node.key == NULL){
continue;
}
size++;
}
return size;
}
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++){