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

@ -11,8 +11,16 @@ struct ARC_Chemical {
ARC_Parser *parser;
ARC_Hashtable *groups;
ARC_Hashtable *types;
ARC_Hashtable *currentGroup;
};
typedef struct ARC_ChemicalTypeData {
void *data;
ARC_ChemicalType_DestroyFn *destroyFn;
} ARC_ChemicalTypeData;
//the grouping is based on the ascii table, but the ids are sequential to make finding tokens quicker (look at the lexer continious for more explanation)
static const uint32_t ARC_CHEMICAL_TAB = 0x01;
static const uint32_t ARC_CHEMICAL_NEWLINE = 0x02;
@ -307,11 +315,13 @@ uint32_t ARC_Chemical_GetStringIdFn(ARC_String *string){
void ARC_ChemicalData_CreateFn(void **data, ARC_ParserTagToken *parsedData, void *userData){
*data = NULL;
if(data == NULL || userData == NULL){
if(parsedData == NULL || userData == NULL){
//TODO: error here?
*data = NULL;
return;
}
}
void ARC_ChemicalData_DestroyFn(void *data, ARC_Bool clear, void *userData){
@ -368,10 +378,34 @@ void ARC_Chemical_Destroy(ARC_Chemical *chemical){
free(chemical);
}
void ARC_Chemical_RegisterType(ARC_Chemical *chemical, ARC_String *typeName, ARC_ChemicalData_CopyToTypeFn *copyToTypeFn, ARC_ParserCSV_DestroyTypeFn destroyTypeFn){
void ARC_Chemical_HashtableCheckKeyIteratorFn(void *key, void *value){
}
void ARC_Chemical_RegisterType(ARC_Chemical *chemical, ARC_String *typeName, ARC_ChemicalType type){
//TODO: check if name currently exists
ARC_String *typeNameCopy;
ARC_String_Copy(&typeNameCopy, typeName);
ARC_ChemicalType *typeCopy = (ARC_ChemicalType *)malloc(sizeof(ARC_ChemicalType));
*typeCopy = type;
ARC_Hashtable_Add(chemical->types, typeName, typeCopy);
}
void ARC_Chemical_RegisterTypeWithCStr(ARC_Chemical *chemical, const char *typeNameCStr, ARC_ChemicalType type){
//TODO: check if name currently exists
ARC_String *typeName;
ARC_String_CreateWithStrlen(&typeName, (char *)typeNameCStr);
ARC_ChemicalType *typeCopy = (ARC_ChemicalType *)malloc(sizeof(ARC_ChemicalType));
*typeCopy = type;
ARC_Hashtable_Add(chemical->types, typeName, typeCopy);
}
void ARC_Chemical_SetGroup(ARC_Chemical *chemical, ARC_String *groupName){
}
void *ARC_Chemical_Get(ARC_Chemical *chemical, ARC_String *element){

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);
}
}