vector class cleaned, slight work on handler

This commit is contained in:
herbglitch 2022-12-15 16:45:45 -07:00
parent cd5471c9c8
commit 55e8c44221
5 changed files with 56 additions and 57 deletions

View file

@ -12,6 +12,18 @@ extern "C" {
*/
typedef struct ARC_Handler ARC_Handler;
/**
* @brief data comparison function ptr
*
* @note this is used for comparison within vectors
*
* @param a first data struct
* @param b second data struct
*
* @return 0 when a == b
*/
typedef uint32_t (* ARC_Handler_CompareDataFn)(void *a, void *b);
/**
* @brief a function that will take iterated data
*
@ -30,9 +42,8 @@ typedef void (* ARC_Handler_CleanDataFn)(void *data);
* @brief creates ARC_Handler type
*
* @param config ARC_Handler to initialize
* @param dataSize size of type the handler will use
*/
void ARC_Handler_Create(ARC_Handler **handler, uint32_t dataSize);
void ARC_Handler_Create(ARC_Handler **handler);
/**
* @brief destroyes ARC_Handler type
@ -57,7 +68,7 @@ void ARC_Handler_Add(ARC_Handler *handler, void *data);
* @param handler ARC_Handler to remove from
* @param data data that is being removed
*/
void ARC_Handler_Remove(ARC_Handler *handler, void *data);
void ARC_Handler_Remove(ARC_Handler *handler, void *data, ARC_Handler_CompareDataFn compare);
/**
* @brief remove from handler

View file

@ -20,15 +20,14 @@ typedef struct ARC_Vector ARC_Vector;
*
* @return 0 when a == b
*/
typedef int8_t (* ARC_Vector_CompareDataFn)(void *a, void *b);
typedef int32_t (* ARC_Vector_CompareDataFn)(void *a, void *b);
/**
* @brief creates ARC_Vector type
*
* @param vector ARC_Vector to initialize
* @param dataSize size of type the vector will store
* @param vector ARC_Vector to initialize
*/
void ARC_Vector_Create(ARC_Vector **vector, uint32_t dataSize);
void ARC_Vector_Create(ARC_Vector **vector);
/**
* @brief destroyes ARC_Vector type
@ -80,4 +79,4 @@ void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index);
}
#endif
#endif //ARC_STD_VECTOR_H_
#endif //ARC_STD_VECTOR_H_

View file

@ -24,7 +24,7 @@ void ARC_EngineData_Create(ARC_EngineData **data){
(*data)->renderer = NULL;
(*data)->mouse = NULL;
ARC_Handler_Create(&((*data)->state), sizeof(ARC_State));
ARC_Handler_Create(&((*data)->state));
ARC_WindowInfo windowInfo;
ARC_RenderInfo renderInfo;

View file

@ -9,10 +9,10 @@ struct ARC_Handler {
ARC_Vector *trash;
};
void ARC_Handler_Create(ARC_Handler **handler, uint32_t dataSize){
void ARC_Handler_Create(ARC_Handler **handler){
*handler = (ARC_Handler *) malloc(sizeof(ARC_Handler));
ARC_Vector_Create(&((*handler)->data), dataSize);
ARC_Vector_Create(&((*handler)->trash), dataSize);
ARC_Vector_Create(&((*handler)->data));
ARC_Vector_Create(&((*handler)->trash));
}
void ARC_Handler_Destroy(ARC_Handler *handler, ARC_Handler_CleanDataFn cleanfn){
@ -29,11 +29,9 @@ void ARC_Handler_Add(ARC_Handler *handler, void *data){
ARC_Vector_Add(handler->data, data);
}
int8_t ARC_Handler_RemoveCompareFn(void *a, void *b){ return a == b; }
void ARC_Handler_Remove(ARC_Handler *handler, void *data){
void ARC_Handler_Remove(ARC_Handler *handler, void *data, ARC_Handler_CompareDataFn compare){
ARC_Vector_Add(handler->trash, data);
ARC_Vector_Remove(handler->data, data, ARC_Handler_RemoveCompareFn);
ARC_Vector_Remove(handler->data, data, (ARC_Vector_CompareDataFn) compare);
}
void ARC_Handler_RemoveIndex(ARC_Handler *handler, uint32_t *index){

View file

@ -7,27 +7,23 @@
struct ARC_Vector {
uint32_t *currentSize, *capacity;
uint32_t *dataSize;
void *data;
void **data;
};
void ARC_Vector_Create(ARC_Vector **vector, uint32_t dataSize){
void ARC_Vector_Create(ARC_Vector **vector){
*vector = (ARC_Vector *) malloc(sizeof(ARC_Vector));
(*vector)->currentSize = (uint32_t *)malloc(sizeof(uint32_t));
(*vector)->capacity = (uint32_t *)malloc(sizeof(uint32_t));
(*vector)->dataSize = (uint32_t *)malloc(sizeof(uint32_t));
(*vector)->data = (void *)malloc(dataSize);
(*vector)->data = (void *)malloc(sizeof(void *));
*(*vector)->currentSize = 0;
*(*vector)->capacity = 1;
*(*vector)->dataSize = dataSize;
}
void ARC_Vector_Destroy(ARC_Vector *vector){
free(vector->currentSize);
free(vector->capacity);
free(vector->data);
free(vector->dataSize);
free(vector);
}
@ -43,46 +39,30 @@ void ARC_Vector_Add(ARC_Vector *vector, void *data){
}
*vector->capacity <<= 1;
vector->data = (void *)realloc(vector->data, *vector->dataSize * *vector->capacity);
vector->data = (void *)realloc(vector->data, sizeof(void *) * *vector->capacity);
}
memcpy(vector->data + (*vector->currentSize * *vector->dataSize), data, *vector->dataSize);
vector->data[*vector->currentSize] = data;
++*(vector->currentSize);
}
//this function removes the redundant checking currentSize and index that would happen if ARC_Vector_Remove called ARC_Vector_RemoveIndex
void ARC_Vector_RemoveIndexNoCheck(ARC_Vector *vector, uint32_t *index);
void ARC_Vector_Remove(ARC_Vector *vector, void *data, ARC_Vector_CompareDataFn compare){
if(!*vector->currentSize){
arc_errno = ARC_ERRNO_DATA;
return;
}
--*(vector->currentSize);
if(*vector->currentSize != *vector->capacity >> 1){
for(uint32_t i = 0; i <= *vector->currentSize; i++){
if(!compare(data, vector->data + (i * *vector->dataSize))){
memcpy(vector->data + (i * *vector->dataSize), vector->data + ((i + 1) * *vector->dataSize), *(vector->currentSize - i) * *vector->dataSize);
return;
}
}
arc_errno = ARC_ERRNO_DATA;
return;
}
*vector->capacity >>= 1;
void *temp = (void *) malloc(*vector->dataSize * *vector->capacity);
for(uint32_t i = 0; i <= *vector->currentSize; i++){
if(compare(data, vector->data + (i * *vector->dataSize))){
memcpy(temp, vector->data, i * *vector->dataSize);
memcpy(temp + (i * *vector->dataSize), vector->data + ((i + 1) * *vector->dataSize), *(vector->currentSize - i) * *vector->dataSize);
free(vector->data);
vector->data = temp;
for(uint32_t i = 0; i < *vector->currentSize; i++){
if(!compare(data, vector->data[i])){
ARC_Vector_RemoveIndexNoCheck(vector, &i);
return;
}
}
//no matching data found in compare function
arc_errno = ARC_ERRNO_DATA;
}
@ -92,21 +72,32 @@ void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t *index){
return;
}
ARC_Vector_RemoveIndexNoCheck(vector, index);
}
void ARC_Vector_RemoveIndexNoCheck(ARC_Vector *vector, uint32_t *index){
for(uint32_t i = *index; i <= *vector->currentSize; i++){
vector->data[i] = vector->data[i + 1];
}
--*vector->currentSize;
if(*vector->currentSize != *vector->capacity >> 1){
memcpy(vector->data + (*index * *vector->dataSize), vector->data + ((*index + 1) * *vector->dataSize), *(vector->currentSize - *index) * *vector->dataSize);
return;
}
*vector->capacity >>= 1;
void **temp = (void **)malloc(sizeof(void *) * (*(vector->capacity)));
memcpy(temp, vector->data, *index * *vector->dataSize);
memcpy(temp + (*index * *vector->dataSize), vector->data + ((*index + 1) * *vector->dataSize), *(vector->currentSize - *index) * *vector->dataSize);
free(vector->data);
vector->data = temp;
vector->data = (void *)realloc(vector->data, sizeof(void *) * *vector->capacity);
}
uint32_t *ARC_Vector_Size(ARC_Vector *vector){ return vector->currentSize; }
uint32_t *ARC_Vector_Size(ARC_Vector *vector){
return vector->currentSize;
}
void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index){ return (void *)(uint8_t (*)[*vector->dataSize]) vector->data + (*index * *vector->dataSize); }
void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index){
if(*index >= *vector->currentSize){
return NULL;
}
return vector->data[*index];
}