vector class cleaned, slight work on handler
This commit is contained in:
parent
cd5471c9c8
commit
55e8c44221
5 changed files with 56 additions and 57 deletions
|
|
@ -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){
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue