Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
cl_vector.h
Go to the documentation of this file.
1#ifndef __CL_VECTOR_H
2#define __CL_VECTOR_H
3
4#include "allocator.h"
5#include "cl_types.h"
6#include "types.h"
7
8#include <stdbool.h>
9
10// FORWARD DECLARATIONS
11typedef struct __Vector CL_Vector;
12
13// MACROS
14
20#define CL_VECTOR_FOREACH(__type, __name, __arr, ...) \
21 for (u32 __i = 0; __i < CL_VectorSize(__arr); __i++) \
22 { \
23 __type __name; \
24 CL_VectorGet(__arr, __i, (void *)&__name); \
25 __VA_ARGS__ \
26 }
27
33#define CL_VECTOR_FOREACH_PTR(__type, __name, __arr, ...) \
34 for (u32 __i = 0; __i < CL_VectorSize(__arr); __i++) \
35 { \
36 __type *__name; \
37 CL_VectorGetPtr(__arr, __i, (void **)&__name); \
38 __VA_ARGS__ \
39 }
40
41// FUNCTIONS
42
49
54CL_Vector *CL_VectorAlloc(MEM_Allocator *const alloc, u32 elem_sz);
55
59void CL_VectorFree(CL_Vector *const arr);
60
66CL_Error CL_VectorPushBack(CL_Vector *const arr, const void *const elem, u32 *out_index);
67
73CL_Error CL_VectorPushFront(CL_Vector *const arr, const void *const elem);
74
78void CL_VectorPopBack(CL_Vector *const arr, void *out_elem);
79
84void CL_VectorPopFront(CL_Vector *const arr, void *out_elem);
85
91CL_Error CL_VectorGet(const CL_Vector *const arr, u32 index, void *out_elem);
92
98CL_Error CL_VectorGetPtr(const CL_Vector *const arr, u32 index, void **out_elem);
99
104CL_Error CL_VectorGetLast(const CL_Vector *const arr, void *out_elem);
105
110CL_Error CL_VectorGetLastPtr(const CL_Vector *const arr, void **out_elem);
111
118CL_Error CL_VectorInsert(CL_Vector *const arr, const void *const elem, u32 index);
119
125CL_Error CL_VectorRemove(CL_Vector *const arr, u32 index);
126
129void CL_VectorClear(CL_Vector *const arr);
130
134bool CL_VectorIsEmpty(const CL_Vector *const arr);
135
139u32 CL_VectorSize(const CL_Vector *const arr);
140
144u32 CL_VectorCapacity(const CL_Vector *const arr);
145
149void CL_VectorSort(CL_Vector *arr, CL_CompareFn compare_fn);
150
157bool CL_VectorSearch(const CL_Vector *const arr, const void *elem, CL_CompareFn compare_fn, u32 *out_index);
158
159#endif
int(* CL_CompareFn)(const void *, const void *)
A function pointer type for comparing two elements.
Definition cl_types.h:7
CL_Error
Specify this in the size parameter of the collections initialization to tell the library that the num...
Definition cl_types.h:24
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
u32 CL_VectorCapacity(const CL_Vector *const arr)
Gets the capacity of the vector.
Definition cl_vector.c:238
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.
Definition cl_vector.c:251
void CL_VectorPopBack(CL_Vector *const arr, void *out_elem)
Removes the last element from the vector, optionally returning it.
Definition cl_vector.c:102
u32 CL_VectorSize(const CL_Vector *const arr)
Gets the size of the vector.
Definition cl_vector.c:233
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_Error CL_VectorGetLast(const CL_Vector *const arr, void *out_elem)
Gets the last element from the vector.
Definition cl_vector.c:158
bool CL_VectorIsEmpty(const CL_Vector *const arr)
Checks if the vector is empty.
Definition cl_vector.c:228
void CL_VectorSort(CL_Vector *arr, CL_CompareFn compare_fn)
Sorts the vector.
Definition cl_vector.c:243
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_Error CL_VectorPushFront(CL_Vector *const arr, const void *const elem)
Adds an element at the front of the vector.
Definition cl_vector.c:96
void CL_VectorPopFront(CL_Vector *const arr, void *out_elem)
Removes the first element from the vector, optionally returning it.
Definition cl_vector.c:115
void CL_VectorFree(CL_Vector *const arr)
Frees the memory previously assigned to the vector.
Definition cl_vector.c:64
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_Error CL_VectorRemove(CL_Vector *const arr, u32 index)
Removes an element at the specified index.
Definition cl_vector.c:199
void CL_VectorClear(CL_Vector *const arr)
Clears the vector.
Definition cl_vector.c:219
CL_Error CL_VectorGetLastPtr(const CL_Vector *const arr, void **out_elem)
Gets a pointer to the last element from the vector.
Definition cl_vector.c:166
CL_Error CL_VectorPushBack(CL_Vector *const arr, const void *const elem, u32 *out_index)
Adds an element at the end of the vector.
Definition cl_vector.c:73
CL_Vector * CL_VectorAlloc(MEM_Allocator *const alloc, u32 elem_sz)
Initializes the vector with a default capacity.
Definition cl_vector.c:59
u32 capacity
Definition cl_vector.c:11
uint32_t u32
Definition types.h:6