Archeus 0.0.0
A C library and game engine that focuses on documentation
Loading...
Searching...
No Matches
vector.c
Go to the documentation of this file.
1
2#include "arc/std/vector.h"
3
4#include "arc/std/bool.h"
5#include "arc/std/errno.h"
6#include <stdint.h>
7#include <stdlib.h>
8
18
19//this is a private function used as the default check for removing data from a given pointer
20ARC_Bool ARC_Vector_CompareDataDefaultFn(void *dataA, void *dataB){
21 if(dataA == dataB){
22 return ARC_True;
23 }
24
25 return ARC_False;
26}
27
29 //create the vector
30 *vector = (ARC_Vector *)malloc(sizeof(ARC_Vector));
31
32 //initialize all the values stored in the vector
33 (*vector)->data = (void **)malloc(sizeof(void *));
34 (*vector)->currentCapacity = 1;
35 (*vector)->currentSize = 0;
36
37 //set a default for compareDataFn, then override it if it is passed in through parameters
38 (*vector)->compareDataFn = ARC_Vector_CompareDataDefaultFn;
39 if(compareDataFn != NULL){
40 (*vector)->compareDataFn = *compareDataFn;
41 }
42
43 //set NULL as a default for deleteDataFn, then copy the delete data function callback if it exists
44 (*vector)->destroyDataFn = NULL;
45 if(destroyDataFn != NULL){
46 (*vector)->destroyDataFn = (ARC_Vector_DestroyDataFn *)malloc(sizeof(ARC_Vector_DestroyDataFn));
47 *((*vector)->destroyDataFn) = *destroyDataFn;
48 }
49}
50
52 //remove all the contents before destroying the vector
53 ARC_Vector_Clear(vector);
54
55 //free the delete data function if it exists
56 if(vector->destroyDataFn){
57 free(vector->destroyDataFn);
58 }
59
60 //free everything stored in the vector
61 free(vector->data);
62
63 //free the vector
64 free(vector);
65}
66
67void ARC_Vector_Add(ARC_Vector *vector, void *data){
68 //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
69 if(vector->currentSize == ~((uint32_t)0)){
71 ARC_DEBUG_LOG_ERROR("ARC_Vector_Add(vector, data), vector at max capacity tried adding another value");
72 return;
73 }
74
75 //check if we are at the max of the current capacity
76 if(vector->currentSize == vector->currentCapacity){
77 //increase the current capacity by double
78 vector->currentCapacity <<= 1;
79
80 //if for some reason the capacity is 0, we should set it to one so we do not error on realloc
81 if(vector->currentCapacity != 0){
82 vector->currentCapacity++;
83 }
84
85 //resize the vectors array and copy the contents at the same time
86 vector->data = (void **)realloc(vector->data, sizeof(void *) * vector->currentCapacity);
87 }
88
89 //add to the vectors array and increase its current size
90 vector->data[vector->currentSize] = data;
91 vector->currentSize++;
92}
93
94void ARC_Vector_Remove(ARC_Vector *vector, void *data){
95 //iterate through every item to check to see if it exists
96 for(uint32_t index = 0; index < vector->currentSize; index++){
97 //keep the code cleaner by pulling the current index data into a temp variable
98 void *dataB = vector->data[index];
99
100 //check if the data matches, and if so remove by index
101 if(vector->compareDataFn(data, dataB) == ARC_True){
102 ARC_Vector_RemoveIndex(vector, index);
103 }
104 }
105}
106
107void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index){
108 //check to make sure the given index is in bounds of the vector
109 if(index >= vector->currentSize){
111 ARC_DEBUG_LOG_ERROR("ARC_Vector_Add(vector, data), vector at max capacity tried adding another value");
112 return;
113 }
114
115 //call delete data to clean up item if delete data function exists
116 if(vector->destroyDataFn != NULL){
117 (*(vector->destroyDataFn))(vector->data[index]);
118 }
119
120 //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
121 for(; index + 1 < vector->currentSize; index++){
122 //override the data from index to the end by shifting it back one
123 vector->data[index] = vector->data[index + 1];
124 }
125
126 //we have removed the item so we can decrease the current size
127 vector->currentSize--;
128
129 //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
130 if(vector->currentSize != vector->currentCapacity >> 1 || vector->currentCapacity == 1){
131 return;
132 }
133
134 //half the capacity and copy it into a smaller array
135 vector->currentCapacity >>= 1;
136 vector->data = (void **)realloc(vector->data, sizeof(void *) * vector->currentCapacity);
137}
138
140 //remove each item in the vector untill the vector is empty
141 while(ARC_Vector_GetSize(vector) != 0){
142 ARC_Vector_RemoveIndex(vector, 0);
143 }
144}
145
147 return vector->currentSize;
148}
149
150void *ARC_Vector_Get(ARC_Vector *vector, uint32_t index){
151 //check to make sure the given index is in bounds of the vector
152 if(index >= vector->currentSize){
154 ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_Vector_Get(vector, %u), null value as the index was out of bounds", index);
155 return NULL;
156 }
157
158 return vector->data[index];
159}
#define ARC_False
Definition bool.h:12
#define ARC_True
Definition bool.h:11
#define ARC_Bool
Definition bool.h:10
int32_t arc_errno
Definition errno.c:5
#define ARC_DEBUG_LOG_ERROR_WITH_VARIABLES(STR,...)
Definition errno.h:40
#define ARC_ERRNO_OVERFLOW
Definition errno.h:10
#define ARC_DEBUG_LOG_ERROR(STR)
Definition errno.h:39
#define ARC_ERRNO_DATA
Definition errno.h:7
void ** data
Definition vector.c:13
ARC_Vector_DestroyDataFn * destroyDataFn
Definition vector.c:16
uint32_t currentCapacity
Definition vector.c:10
ARC_Vector_CompareDataFn compareDataFn
Definition vector.c:15
uint32_t currentSize
Definition vector.c:11
uint32_t ARC_Vector_GetSize(ARC_Vector *vector)
gets the current size of an ARC_Vector as an unsigned 32 bit integer
Definition vector.c:146
void * ARC_Vector_Get(ARC_Vector *vector, uint32_t index)
gets an item from an ARC_Vector at a position index
Definition vector.c:150
void ARC_Vector_Add(ARC_Vector *vector, void *data)
adds an item to an ARC_Vector
Definition vector.c:67
void ARC_Vector_Remove(ARC_Vector *vector, void *data)
removes an item from a matching item in an ARC_Vector
Definition vector.c:94
void ARC_Vector_Destroy(ARC_Vector *vector)
destroys an ARC_Vector
Definition vector.c:51
void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index)
removes an item from an ARC_Vector at an index
Definition vector.c:107
void ARC_Vector_Clear(ARC_Vector *vector)
clears all items from a vector
Definition vector.c:139
void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DestroyDataFn *destroyDataFn)
creates an ARC_Vector which is an "expandable" array
Definition vector.c:28
ARC_Bool ARC_Vector_CompareDataDefaultFn(void *dataA, void *dataB)
Definition vector.c:20
void(* ARC_Vector_DestroyDataFn)(void *data)
a callback that cleans up memory when it is removed from the vector
Definition vector.h:31
ARC_Bool(* ARC_Vector_CompareDataFn)(void *dataA, void *dataB)
a callback that allows the user to define a way to check the data stored in a vector for a match
Definition vector.h:24