2022-10-27 15:16:54 -06:00
|
|
|
#include "arc/std/vector.h"
|
|
|
|
|
|
|
|
|
|
#include "arc/std/errno.h"
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
struct ARC_Vector {
|
2024-05-20 03:46:04 -06:00
|
|
|
uint32_t currentSize, capacity;
|
2022-12-15 16:45:45 -07:00
|
|
|
void **data;
|
2022-10-27 15:16:54 -06:00
|
|
|
};
|
|
|
|
|
|
2022-12-15 16:45:45 -07:00
|
|
|
void ARC_Vector_Create(ARC_Vector **vector){
|
2022-10-27 15:16:54 -06:00
|
|
|
*vector = (ARC_Vector *) malloc(sizeof(ARC_Vector));
|
2024-05-20 03:46:04 -06:00
|
|
|
(*vector)->currentSize = 0;
|
|
|
|
|
(*vector)->capacity = 1;
|
|
|
|
|
(*vector)->data = (void **)malloc(sizeof(void *));
|
2022-10-27 15:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Vector_Destroy(ARC_Vector *vector){
|
|
|
|
|
free(vector->data);
|
|
|
|
|
free(vector);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Vector_Add(ARC_Vector *vector, void *data){
|
2024-05-20 03:46:04 -06:00
|
|
|
if(vector->currentSize == ~((uint32_t)0)){
|
2022-10-27 15:16:54 -06:00
|
|
|
arc_errno = ARC_ERRNO_OVERFLOW;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
if(vector->currentSize == vector->capacity){
|
|
|
|
|
if(!vector->capacity){
|
|
|
|
|
++vector->capacity;
|
2022-11-29 14:50:20 -07:00
|
|
|
}
|
2024-05-20 03:46:04 -06:00
|
|
|
vector->capacity <<= 1;
|
2022-10-27 15:16:54 -06:00
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
vector->data = (void *)realloc(vector->data, sizeof(void *) * vector->capacity);
|
2022-10-27 15:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
vector->data[vector->currentSize] = data;
|
|
|
|
|
++(vector->currentSize);
|
2022-10-27 15:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:45:45 -07:00
|
|
|
//this function removes the redundant checking currentSize and index that would happen if ARC_Vector_Remove called ARC_Vector_RemoveIndex
|
2024-05-20 03:46:04 -06:00
|
|
|
void ARC_Vector_RemoveIndexNoCheck(ARC_Vector *vector, uint32_t index);
|
2022-12-15 16:45:45 -07:00
|
|
|
|
2022-10-27 15:16:54 -06:00
|
|
|
void ARC_Vector_Remove(ARC_Vector *vector, void *data, ARC_Vector_CompareDataFn compare){
|
2024-05-20 03:46:04 -06:00
|
|
|
if(!vector->currentSize){
|
2022-10-27 15:16:54 -06:00
|
|
|
arc_errno = ARC_ERRNO_DATA;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
for(uint32_t i = 0; i < vector->currentSize; i++){
|
2022-12-15 16:45:45 -07:00
|
|
|
if(!compare(data, vector->data[i])){
|
2024-05-20 03:46:04 -06:00
|
|
|
ARC_Vector_RemoveIndexNoCheck(vector, i);
|
2022-10-27 15:16:54 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:45:45 -07:00
|
|
|
//no matching data found in compare function
|
2022-10-27 15:16:54 -06:00
|
|
|
arc_errno = ARC_ERRNO_DATA;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index){
|
|
|
|
|
if(!vector->currentSize || index >= vector->currentSize){
|
2022-10-27 15:16:54 -06:00
|
|
|
arc_errno = ARC_ERRNO_DATA;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 16:45:45 -07:00
|
|
|
ARC_Vector_RemoveIndexNoCheck(vector, index);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
void ARC_Vector_RemoveIndexNoCheck(ARC_Vector *vector, uint32_t index){
|
|
|
|
|
for(uint32_t i = index; i <= vector->currentSize; i++){
|
|
|
|
|
if(i + 1 >= vector->currentSize - 1){
|
2023-08-26 01:36:22 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2022-12-15 16:45:45 -07:00
|
|
|
vector->data[i] = vector->data[i + 1];
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
--vector->currentSize;
|
2022-12-15 16:45:45 -07:00
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
if(vector->currentSize != vector->capacity >> 1){
|
2022-10-27 15:16:54 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
vector->capacity >>= 1;
|
|
|
|
|
if(vector->capacity <= 0){
|
|
|
|
|
vector->capacity = 1;
|
2023-08-26 01:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
vector->data = (void *)realloc(vector->data, sizeof(void *) * vector->capacity);
|
2022-12-15 16:45:45 -07:00
|
|
|
}
|
2022-10-27 15:16:54 -06:00
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
uint32_t ARC_Vector_Size(ARC_Vector *vector){
|
2022-12-15 16:45:45 -07:00
|
|
|
return vector->currentSize;
|
2022-10-27 15:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
void *ARC_Vector_Get(ARC_Vector *vector, uint32_t index){
|
|
|
|
|
if(index >= vector->currentSize){
|
2022-12-15 16:45:45 -07:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2022-10-27 15:16:54 -06:00
|
|
|
|
2024-05-20 03:46:04 -06:00
|
|
|
return vector->data[index];
|
2024-02-08 02:48:08 -07:00
|
|
|
}
|