Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
cl_vector.h File Reference
#include "allocator.h"
#include "cl_types.h"
#include "types.h"
#include <stdbool.h>

Go to the source code of this file.

Macros

#define CL_VECTOR_FOREACH(__type, __name, __arr, ...)
 Macro to iterate over the elements of the vector.
 
#define CL_VECTOR_FOREACH_PTR(__type, __name, __arr, ...)
 Macro to iterate over the elements of the vector with a pointer to the current element.
 

Typedefs

typedef struct __Vector CL_Vector
 

Functions

CL_VectorCL_VectorAllocWithCapacity (MEM_Allocator *const alloc, u32 capacity, u32 elem_sz)
 Initializes the vector with allocated memory provided by the user.
 
CL_VectorCL_VectorAlloc (MEM_Allocator *const alloc, u32 elem_sz)
 Initializes the vector with a default capacity.
 
void CL_VectorFree (CL_Vector *const arr)
 Frees the memory previously assigned to the vector.
 
CL_Error CL_VectorPushBack (CL_Vector *const arr, const void *const elem, u32 *out_index)
 Adds an element at the end of the vector.
 
CL_Error CL_VectorPushFront (CL_Vector *const arr, const void *const elem)
 Adds an element at the front of the vector.
 
void CL_VectorPopBack (CL_Vector *const arr, void *out_elem)
 Removes the last element from the vector, optionally returning it.
 
void CL_VectorPopFront (CL_Vector *const arr, void *out_elem)
 Removes the first element from the vector, optionally returning it.
 
CL_Error CL_VectorGet (const CL_Vector *const arr, u32 index, void *out_elem)
 Gets an element at the specified index.
 
CL_Error CL_VectorGetPtr (const CL_Vector *const arr, u32 index, void **out_elem)
 Gets a pointer to an element at the specified index.
 
CL_Error CL_VectorGetLast (const CL_Vector *const arr, void *out_elem)
 Gets the last element from the vector.
 
CL_Error CL_VectorGetLastPtr (const CL_Vector *const arr, void **out_elem)
 Gets a pointer to the last element from the vector.
 
CL_Error CL_VectorInsert (CL_Vector *const arr, const void *const elem, u32 index)
 Inserts an element at the specified index.
 
CL_Error CL_VectorRemove (CL_Vector *const arr, u32 index)
 Removes an element at the specified index.
 
void CL_VectorClear (CL_Vector *const arr)
 Clears the vector.
 
bool CL_VectorIsEmpty (const CL_Vector *const arr)
 Checks if the vector is empty.
 
u32 CL_VectorSize (const CL_Vector *const arr)
 Gets the size of the vector.
 
u32 CL_VectorCapacity (const CL_Vector *const arr)
 Gets the capacity of the vector.
 
void CL_VectorSort (CL_Vector *arr, CL_CompareFn compare_fn)
 Sorts the vector.
 
bool CL_VectorSearch (const CL_Vector *const arr, const void *elem, CL_CompareFn compare_fn, u32 *out_index)
 Searches for an element in the vector.
 

Macro Definition Documentation

◆ CL_VECTOR_FOREACH

#define CL_VECTOR_FOREACH (   __type,
  __name,
  __arr,
  ... 
)
Value:
for (u32 __i = 0; __i < CL_VectorSize(__arr); __i++) \
{ \
__type __name; \
CL_VectorGet(__arr, __i, (void *)&__name); \
__VA_ARGS__ \
}
u32 CL_VectorSize(const CL_Vector *const arr)
Gets the size of the vector.
Definition cl_vector.c:233
uint32_t u32
Definition types.h:6

Macro to iterate over the elements of the vector.

Parameters
__typeThe type of the elements in the vector.
__nameThe name of the variable to hold the current element.
__arrThe vector to iterate over.
...The code to execute for each element.

Definition at line 20 of file cl_vector.h.

22 { \
23 __type __name; \
24 CL_VectorGet(__arr, __i, (void *)&__name); \
25 __VA_ARGS__ \
26 }

◆ CL_VECTOR_FOREACH_PTR

#define CL_VECTOR_FOREACH_PTR (   __type,
  __name,
  __arr,
  ... 
)
Value:
for (u32 __i = 0; __i < CL_VectorSize(__arr); __i++) \
{ \
__type *__name; \
CL_VectorGetPtr(__arr, __i, (void **)&__name); \
__VA_ARGS__ \
}

Macro to iterate over the elements of the vector with a pointer to the current element.

Parameters
__typeThe type of the elements in the vector.
__nameThe name of the variable to hold the current element.
__arrThe vector to iterate over.
...The code to execute for each element.

Definition at line 33 of file cl_vector.h.

35 { \
36 __type *__name; \
37 CL_VectorGetPtr(__arr, __i, (void **)&__name); \
38 __VA_ARGS__ \
39 }

Typedef Documentation

◆ CL_Vector

typedef struct __Vector CL_Vector

Definition at line 11 of file cl_vector.h.

Function Documentation

◆ CL_VectorAlloc()

CL_Vector * CL_VectorAlloc ( MEM_Allocator *const  alloc,
u32  elem_sz 
)

Initializes the vector with a default capacity.

Parameters
allocThe allocator to use for memory allocation.
elem_szThe size of each element in the vector.
Returns
Pointer to the new vector or NULL on error.

Definition at line 59 of file cl_vector.c.

60{
61 return CL_VectorAllocWithCapacity(alloc, BASE_CAPACITY, elem_sz);
62}
#define BASE_CAPACITY
Definition cl_vector.c:16
CL_Vector * CL_VectorAllocWithCapacity(MEM_Allocator *const alloc, u32 capacity, u32 elem_sz)
Initializes the vector with allocated memory provided by the user.
Definition cl_vector.c:33

◆ CL_VectorAllocWithCapacity()

CL_Vector * CL_VectorAllocWithCapacity ( MEM_Allocator *const  alloc,
u32  capacity,
u32  elem_sz 
)

Initializes the vector with allocated memory provided by the user.

Parameters
allocThe allocator to use for memory allocation.
capacityThe initial capacity of the vector.
elem_szThe size of each element in the vector.
Returns
Pointer to the new vector or NULL on error.

Definition at line 33 of file cl_vector.c.

34{
35 if (!alloc || !capacity || !elem_sz)
36 return NULL;
37
38 CL_Vector *const arr = MEM_Alloc(alloc, sizeof(CL_Vector));
39 if (!arr)
40 return NULL;
41
42 *arr = (CL_Vector){
43 .data = MEM_Alloc(alloc, capacity * elem_sz),
44 .size = 0,
45 .capacity = capacity,
46 .elem_size = elem_sz,
47 .allocator = alloc,
48 };
49
50 if (!arr->data)
51 {
52 MEM_Free(alloc, arr);
53 return NULL;
54 }
55
56 return arr;
57}
void MEM_Free(MEM_Allocator *ma, void *ptr)
Free a previously allocated block.
Definition allocator.c:98
void * MEM_Alloc(MEM_Allocator *ma, u32 size)
Allocate a block of memory from the pool with at least 'size' bytes.
Definition allocator.c:52
struct __Vector CL_Vector
Definition cl_vector.h:11
void * data
Definition cl_vector.c:10

◆ CL_VectorCapacity()

u32 CL_VectorCapacity ( const CL_Vector *const  arr)

Gets the capacity of the vector.

Parameters
arrThe vector to get the capacity of.
Returns
The capacity of the vector.

Definition at line 238 of file cl_vector.c.

239{
240 return arr ? arr->capacity : 0;
241}
u32 capacity
Definition cl_vector.c:11

◆ CL_VectorClear()

void CL_VectorClear ( CL_Vector *const  arr)

Clears the vector.

Parameters
arrThe vector to clear.

Definition at line 219 of file cl_vector.c.

220{
221 if (!arr || !arr->size)
222 return;
223
224 // memset(arr->data, 0, arr->size * arr->elem_size); // Not necessary
225 arr->size = 0;
226}
u32 size
Definition cl_vector.c:11

◆ CL_VectorFree()

void CL_VectorFree ( CL_Vector *const  arr)

Frees the memory previously assigned to the vector.

Parameters
arrThe vector to release the memory of.
Note
CL_Vector does not take ownership of the elements, so it's the user's responsibility to free them.

Definition at line 64 of file cl_vector.c.

65{
66 if (!arr)
67 return;
68
69 MEM_Free(arr->allocator, arr->data);
70 MEM_Free(arr->allocator, arr);
71}
MEM_Allocator * allocator
Definition cl_vector.c:12

◆ CL_VectorGet()

CL_Error CL_VectorGet ( const CL_Vector *const  arr,
u32  index,
void *  out_elem 
)

Gets an element at the specified index.

Parameters
arrThe vector to get the element from.
indexThe index of the element to get.
out_elem[OUTPUT] The element at the specified index.
Returns
CL_Error The error code.

Definition at line 126 of file cl_vector.c.

127{
128 if (!arr || !out_elem)
130
131 if (index >= arr->size || index < 0)
133
134 if (arr->size == 0)
135 return CL_ERR_EMPTY;
136
137 const u32 offset = index * arr->elem_size;
138 memcpy(out_elem, (u8 *)(arr->data) + offset, arr->elem_size);
139 return CL_ERR_OK;
140}
@ CL_ERR_EMPTY
Empty collection.
Definition cl_types.h:41
@ CL_ERR_OUT_OF_BOUNDS
Out of bounds.
Definition cl_types.h:32
@ CL_ERR_INVALID_PARAMS
Invalid arguments.
Definition cl_types.h:29
@ CL_ERR_OK
No error.
Definition cl_types.h:26
u32 elem_size
Definition cl_vector.c:11
uint8_t u8
Definition types.h:8

◆ CL_VectorGetLast()

CL_Error CL_VectorGetLast ( const CL_Vector *const  arr,
void *  out_elem 
)

Gets the last element from the vector.

Parameters
arrThe vector to get the last element from.
out_elem[OUTPUT] The last element.
Returns
CL_Error The error code.

Definition at line 158 of file cl_vector.c.

159{
160 if (arr->size == 0)
161 return CL_ERR_EMPTY;
162
163 return CL_VectorGet(arr, arr->size - 1, out_elem);
164}
CL_Error CL_VectorGet(const CL_Vector *const arr, u32 index, void *out_elem)
Gets an element at the specified index.
Definition cl_vector.c:126

◆ CL_VectorGetLastPtr()

CL_Error CL_VectorGetLastPtr ( const CL_Vector *const  arr,
void **  out_elem 
)

Gets a pointer to the last element from the vector.

Parameters
arrThe vector to get the last element from.
out_elem[OUTPUT] The pointer to the last element.
Returns
CL_Error The error code.

Definition at line 166 of file cl_vector.c.

167{
168 if (arr->size == 0)
169 return CL_ERR_EMPTY;
170
171 return CL_VectorGetPtr(arr, arr->size - 1, out_elem);
172}
CL_Error CL_VectorGetPtr(const CL_Vector *const arr, u32 index, void **out_elem)
Gets a pointer to an element at the specified index.
Definition cl_vector.c:142

◆ CL_VectorGetPtr()

CL_Error CL_VectorGetPtr ( const CL_Vector *const  arr,
u32  index,
void **  out_elem 
)

Gets a pointer to an element at the specified index.

Parameters
arrThe vector to get the element from.
indexThe index of the element to get.
out_elem[OUTPUT] The pointer to the element at the specified index.
Returns
CL_Error The error code.

Definition at line 142 of file cl_vector.c.

143{
144 if (!arr || !out_elem)
146
147 if (index >= arr->size || index < 0)
149
150 if (arr->size == 0)
151 return CL_ERR_EMPTY;
152
153 const u32 offset = index * arr->elem_size;
154 *out_elem = (u8 *)(arr->data) + offset;
155 return CL_ERR_OK;
156}

◆ CL_VectorInsert()

CL_Error CL_VectorInsert ( CL_Vector *const  arr,
const void *const  elem,
u32  index 
)

Inserts an element at the specified index.

Parameters
arrThe vector to insert the element to.
elemThe element to insert.
indexThe index to insert the element at.
Returns
CL_Error The error code.
Note
This operation is not O(1) for vectors. Consider using a list if you need frequent insertions.

Definition at line 174 of file cl_vector.c.

175{
176 if (!arr || !elem)
178
179 if (index > arr->size || index < 0)
181
182 if (arr->size >= arr->capacity)
183 {
184 const u32 new_cap = arr->capacity * RESZ_FACTOR;
185 if (!realloc_data(arr, new_cap))
186 return CL_ERR_NO_MEMORY;
187 }
188
189 const u32 where_offset = index * arr->elem_size;
190 const u32 next_offset = where_offset + arr->elem_size;
191
192 // Shift the elements to the right, in order to make space for the new element
193 memmove((u8 *)arr->data + next_offset, (u8 *)arr->data + where_offset, (arr->size - index) * arr->elem_size);
194 memcpy((u8 *)arr->data + where_offset, elem, arr->elem_size);
195 arr->size++;
196 return CL_ERR_OK;
197}
@ CL_ERR_NO_MEMORY
No memory available.
Definition cl_types.h:35
#define RESZ_FACTOR
Definition cl_vector.c:15
_PRIVATE void * realloc_data(CL_Vector *const arr, u32 new_capacity)
Definition cl_vector.c:20

◆ CL_VectorIsEmpty()

bool CL_VectorIsEmpty ( const CL_Vector *const  arr)

Checks if the vector is empty.

Parameters
arrThe vector to check.
Returns
true if the vector is empty, false otherwise.

Definition at line 228 of file cl_vector.c.

229{
230 return arr ? arr->size == 0 : true;
231}

◆ CL_VectorPopBack()

void CL_VectorPopBack ( CL_Vector *const  arr,
void *  out_elem 
)

Removes the last element from the vector, optionally returning it.

Parameters
arrThe vector to remove the last element from.
out_elem[OUTPUT] The removed element, if not NULL.

Definition at line 102 of file cl_vector.c.

103{
104 if (!arr || !arr->size)
105 return;
106
107 const u32 offset = (arr->size - 1) * arr->elem_size;
108 if (out_elem)
109 memcpy(out_elem, (u8 *)(arr->data) + offset, arr->elem_size);
110
111 // memset((u8 *)(arr->data) + offset, 0, arr->elem_size); // Not necessary
112 arr->size--;
113}

◆ CL_VectorPopFront()

void CL_VectorPopFront ( CL_Vector *const  arr,
void *  out_elem 
)

Removes the first element from the vector, optionally returning it.

Parameters
arrThe vector to remove the first element from.
out_elem[OUTPUT] The removed element, if not NULL.
Note
This operation is not O(1) for vectors. Consider using a list if you need frequent removals at the front.

Definition at line 115 of file cl_vector.c.

116{
117 if (!arr || !arr->size)
118 return;
119
120 if (out_elem)
121 CL_VectorGet(arr, 0, out_elem);
122
123 CL_VectorRemove(arr, 0);
124}
CL_Error CL_VectorRemove(CL_Vector *const arr, u32 index)
Removes an element at the specified index.
Definition cl_vector.c:199

◆ CL_VectorPushBack()

CL_Error CL_VectorPushBack ( CL_Vector *const  arr,
const void *const  elem,
u32 out_index 
)

Adds an element at the end of the vector.

Parameters
arrThe vector to add the element to.
elemThe element to add.
out_index[OUTPUT] The index of the added element, if not NULL.
Returns
CL_Error The error code.

Definition at line 73 of file cl_vector.c.

74{
75 if (!arr || !elem)
77
78 // Checking if the array can hold the element
79 if (arr->size >= arr->capacity)
80 {
81 const u32 new_cap = arr->capacity * RESZ_FACTOR;
82 if (!realloc_data(arr, new_cap))
83 return CL_ERR_NO_MEMORY;
84 }
85
86 const u32 offset = arr->size * arr->elem_size;
87 memcpy((u8 *)(arr->data) + offset, elem, arr->elem_size);
88 arr->size++;
89
90 if (out_index)
91 *out_index = arr->size - 1;
92
93 return CL_ERR_OK;
94}

◆ CL_VectorPushFront()

CL_Error CL_VectorPushFront ( CL_Vector *const  arr,
const void *const  elem 
)

Adds an element at the front of the vector.

Parameters
arrThe vector to add the element to.
elemThe element to add.
Returns
CL_Error The error code.
Note
This operation is not O(1) for vectors. Consider using a list if you need frequent insertions at the front.

Definition at line 96 of file cl_vector.c.

97{
98 // Inserting at the front is equivalent to inserting at index 0
99 return CL_VectorInsert(arr, elem, 0);
100}
CL_Error CL_VectorInsert(CL_Vector *const arr, const void *const elem, u32 index)
Inserts an element at the specified index.
Definition cl_vector.c:174

◆ CL_VectorRemove()

CL_Error CL_VectorRemove ( CL_Vector *const  arr,
u32  index 
)

Removes an element at the specified index.

Parameters
arrThe vector to remove the element from.
indexThe index to remove the element from.
Returns
CL_Error The error code.
Note
This operation is not O(1) for vectors. Consider using a list if you need frequent removald in the middle.

Definition at line 199 of file cl_vector.c.

200{
201 if (!arr)
203
204 if (index >= arr->size || index < 0)
206
207 if (arr->size == 0)
208 return CL_ERR_EMPTY;
209
210 const u32 where_offset = index * arr->elem_size;
211 const u32 next_offset = where_offset + arr->elem_size;
212
213 // Shift the elements to the left, in order to remove the element
214 memmove((u8 *)arr->data + where_offset, (u8 *)arr->data + next_offset, (arr->size - index - 1) * arr->elem_size);
215 arr->size--;
216 return CL_ERR_OK;
217}

◆ CL_VectorSearch()

bool CL_VectorSearch ( const CL_Vector *const  arr,
const void *  elem,
CL_CompareFn  compare_fn,
u32 out_index 
)

Searches for an element in the vector.

Parameters
arrThe vector to search in.
elemThe element to search for.
compare_fnThe comparison function.
out_index[OUTPUT] The index of the element, if found and not NULL.
Returns
true if the element was found, false otherwise.

Definition at line 251 of file cl_vector.c.

252{
253 if (!arr || !elem || !compare_fn)
254 return false;
255
256 u32 offset;
257 for (u32 i = 0; i < arr->size; i++)
258 {
259 offset = i * arr->elem_size;
260 if (!compare_fn(elem, (u8 *)(arr->data) + offset))
261 {
262 if (out_index)
263 *out_index = i;
264
265 return true;
266 }
267 }
268
269 return false;
270}

◆ CL_VectorSize()

u32 CL_VectorSize ( const CL_Vector *const  arr)

Gets the size of the vector.

Parameters
arrThe vector to get the size of.
Returns
The size of the vector.

Definition at line 233 of file cl_vector.c.

234{
235 return arr ? arr->size : 0;
236}

◆ CL_VectorSort()

void CL_VectorSort ( CL_Vector arr,
CL_CompareFn  compare_fn 
)

Sorts the vector.

Parameters
arrThe vector to sort.
compare_fnThe comparison function.

Definition at line 243 of file cl_vector.c.

244{
245 if (!arr || !cmp || arr->size < 2)
246 return;
247
248 qsort(arr->data, arr->size, arr->elem_size, cmp);
249}