added vector inline to hopefully make caching work for the entity component system

This commit is contained in:
herbglitch 2025-03-13 19:22:27 -06:00
parent ccf13ba470
commit ba09aff914
7 changed files with 482 additions and 2 deletions

View file

@ -1,3 +1,31 @@
#include "arc/std/entity.h"
#include "arc/std/bool.h"
#include "arc/std/vector.h"
#include <stdlib.h>
//TODO: might want to change how vector holds data for speed
struct ARC_EntitySystem {
ARC_Vector *flagVector;
ARC_Vector *maskVector;
void *data;
};
ARC_Bool ARC_EntitySystem_VectorCompareDataFn(void *dataA, void *dataB){
return (ARC_Bool)(*(uint32_t *)dataA == *(uint32_t *)dataB);
}
void ARC_EntitySystem_VectorDestroyDataFn(void *data){
free((uint32_t *)data);
}
void ARC_EntitySystem_Create(ARC_EntitySystem **entitySystem){
*entitySystem = (ARC_EntitySystem *)malloc(sizeof(ARC_EntitySystem));
ARC_Vector_CompareDataFn compareDataFn = ARC_EntitySystem_VectorCompareDataFn;
ARC_Vector_DestroyDataFn destroyDataFn = ARC_EntitySystem_VectorDestroyDataFn;
ARC_Vector_Create(&((*entitySystem)->flagVector), &compareDataFn, &destroyDataFn);
ARC_Vector_Create(&((*entitySystem)->maskVector), &compareDataFn, &destroyDataFn);
}

View file

@ -1,4 +1,3 @@
#include "arc/std/vector.h"
#include "arc/std/bool.h"

167
src/std/vector/inline.c Normal file
View file

@ -0,0 +1,167 @@
#include "arc/std/vector/inline.h"
#include "arc/std/bool.h"
#include "arc/std/errno.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
struct ARC_VectorInline {
uint32_t currentCapacity;
uint32_t currentSize;
uint32_t typeSize;
void *data;
ARC_Vector_CompareDataFn compareDataFn;
ARC_Vector_DestroyDataFn *destroyDataFn;
};
void ARC_VectorInline_Create(ARC_VectorInline **vectorInline, uint32_t typeSize, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DestroyDataFn *destroyDataFn){
//error if no type size was provided
if(typeSize == 0){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR("ARC_VectorInline_Create(vectorInline, typeSize, compareDataFn, destroyDataFn), vector inline could not be created with typeSize 0");
*vectorInline = NULL;
return;
}
//create the vector
*vectorInline = (ARC_VectorInline *)malloc(sizeof(ARC_VectorInline));
//initialize all the values stored in the vector
(*vectorInline)->data = (void *)malloc(typeSize);
(*vectorInline)->currentCapacity = 1;
(*vectorInline)->currentSize = 0;
(*vectorInline)->typeSize = typeSize;
//set a default for compareDataFn, then override it if it is passed in through parameters
(*vectorInline)->compareDataFn = NULL;
if(compareDataFn != NULL){
(*vectorInline)->compareDataFn = *compareDataFn;
}
//set NULL as a default for deleteDataFn, then copy the delete data function callback if it exists
(*vectorInline)->destroyDataFn = NULL;
if(destroyDataFn != NULL){
(*vectorInline)->destroyDataFn = (ARC_Vector_DestroyDataFn *)malloc(sizeof(ARC_Vector_DestroyDataFn));
*((*vectorInline)->destroyDataFn) = *destroyDataFn;
}
}
void ARC_VectorInline_Destroy(ARC_VectorInline *vectorInline){
//remove all the contents before destroying the vector
ARC_VectorInline_Clear(vectorInline);
//free the delete data function if it exists
if(vectorInline->destroyDataFn != NULL){
free(vectorInline->destroyDataFn);
}
//free everything stored in the vector
free(vectorInline->data);
//free the vector
free(vectorInline);
}
void ARC_VectorInline_Add(ARC_VectorInline *vectorInline, void *data){
//check to see if the current size is the same as a max uint32_t and if so it will overflow so throw an error
if(vectorInline->currentSize == ~((uint32_t)0)){
arc_errno = ARC_ERRNO_OVERFLOW;
ARC_DEBUG_LOG_ERROR("ARC_VectorInline_Add(vectorInline, data), vector inline at max capacity tried adding another value");
return;
}
//check if we are at the max of the current capacity
if(vectorInline->currentSize == vectorInline->currentCapacity){
//increase the current capacity by double
vectorInline->currentCapacity <<= 1;
//if for some reason the capacity is 0, we should set it to one so we do not error on realloc
if(vectorInline->currentCapacity != 0){
vectorInline->currentCapacity++;
}
//resize the vectors array and copy the contents at the same time
vectorInline->data = (void *)realloc(vectorInline->data, vectorInline->typeSize * vectorInline->currentCapacity);
}
//add to the vectors array and increase its current size
memcpy(vectorInline->data + (vectorInline->currentSize * vectorInline->typeSize), data, vectorInline->typeSize);
vectorInline->currentSize++;
}
void ARC_VectorInline_Remove(ARC_VectorInline *vectorInline, void *data){
//error if return function was never set
if(vectorInline->compareDataFn == NULL){
arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG_ERROR("ARC_VectorInline_Remove(vectorInline, data), vector inline did not have a compare data function set on creation");
return;
}
//iterate through every item to check to see if it exists
for(uint32_t index = 0; index < vectorInline->currentSize; index++){
//keep the code cleaner by pulling the current index data into a temp variable
void *dataB = vectorInline->data + (vectorInline->typeSize * index);
//check if the data matches, and if so remove by index
if(vectorInline->compareDataFn(data, dataB) == ARC_True){
ARC_VectorInline_RemoveIndex(vectorInline, index);
}
}
}
void ARC_VectorInline_RemoveIndex(ARC_VectorInline *vectorInline, uint32_t index){
//check to make sure the given index is in bounds of the vector
if(index >= vectorInline->currentSize){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_VectorInline_Add(vectorInline, data), vector at max capacity tried adding another value");
return;
}
//call delete data to clean up item if delete data function exists
if(vectorInline->destroyDataFn != NULL){
(*(vectorInline->destroyDataFn))(vectorInline->data + index);
}
//we will be using index to iterate as we will not use it again, so we can skip the first part of the for loop
for(; index + 1 < vectorInline->currentSize; index++){
//override the data from index to the end by shifting it back one
memcpy(vectorInline->data + (index * vectorInline->typeSize), vectorInline->data + ((index + 1) * vectorInline->typeSize), vectorInline->typeSize);
}
//we have removed the item so we can decrease the current size
vectorInline->currentSize--;
//if the current size is half the current capacity or the current capacity is at the smallest limit, we do not need to do anything else
if(vectorInline->currentSize != vectorInline->currentCapacity >> 1 || vectorInline->currentCapacity == 1){
return;
}
//half the capacity and copy it into a smaller array
vectorInline->currentCapacity >>= 1;
vectorInline->data = (void *)realloc(vectorInline->data, vectorInline->typeSize * vectorInline->currentCapacity);
}
void ARC_VectorInline_Clear(ARC_VectorInline *vectorInline){
//remove each item in the vector untill the vector is empty
while(ARC_VectorInline_GetSize(vectorInline) != 0){
ARC_VectorInline_RemoveIndex(vectorInline, 0);
}
}
uint32_t ARC_VectorInline_GetSize(ARC_VectorInline *vectorInline){
return vectorInline->currentSize;
}
void *ARC_VectorInline_Get(ARC_VectorInline *vectorInline, uint32_t index){
//check to make sure the given index is in bounds of the vector
if(index >= vectorInline->currentSize){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_VectorInline_Get(vectorInline, %u), index %u bounds", index, index);
return NULL;
}
return (vectorInline->data + (vectorInline->typeSize * index));
}