18#define BASE_CAPACITY 10
25#define PARENT(i) ((i - 1) / 2)
26#define LEFT(i) (2 * i + 1)
27#define RIGHT(i) (2 * i + 2)
31 void *temp = alloca(size);
32 memcpy(temp, a, size);
34 memcpy(b, temp, size);
46 u8 *parent_elem, *new_elem;
66 u32 idx = 0, smallest_idx = 0;
67 u8 *root_elem, *left_elem, *smallest_elem;
84 smallest_idx =
LEFT(idx);
89 smallest_idx =
RIGHT(idx);
92 if (smallest_idx == idx)
118 if (!alloc || !capacity || !elem_sz || !compare_fn)
128 .capacity = capacity,
129 .elem_size = elem_sz,
130 .compare_fn = compare_fn,
181 if (!pq || !out_elem)
200 if (!pq || !out_elem)
213 return pq ? pq->
size : 0;
218 return pq ? pq->
size == 0 :
true;
void MEM_Free(MEM_Allocator *ma, void *ptr)
Free a previously allocated block.
void * MEM_Alloc(MEM_Allocator *ma, u32 size)
Allocate a block of memory from the pool with at least 'size' bytes.
void * MEM_Realloc(MEM_Allocator *ma, void *ptr, u32 new_size)
Reallocate a previously allocated block to a new size.
_PRIVATE void bubble_up_leaf(CL_PQueue *const pq)
bool CL_PQueueIsEmpty(const CL_PQueue *const pq)
Checks if the priority queue is empty.
void CL_PQueueFree(CL_PQueue *const pq)
Frees the memory previously assigned to the priority queue.
_PRIVATE void * realloc_data(CL_PQueue *const pq, u32 new_capacity)
CL_Error CL_PQueuePeek(const CL_PQueue *const pq, void *out_elem)
Gets the element with the highest priority from the priority queue without removing it.
CL_Error CL_PQueueDequeue(CL_PQueue *const pq, void *out_elem)
Removes the element with the highest priority from the priority queue.
u32 CL_PQueueSize(const CL_PQueue *const pq)
Gets the number of elements in the priority queue.
_PRIVATE void bubble_down_root(CL_PQueue *const pq)
_PRIVATE void pSwap(void *const a, void *const b, u32 size)
CL_PQueue * CL_PQueueAlloc(MEM_Allocator *const alloc, u32 elem_sz, CL_PQueueCompareFn compare_fn)
Initializes a priority queue with a default capacity.
CL_PQueue * CL_PQueueAllocWithCapacity(MEM_Allocator *const alloc, u32 capacity, u32 elem_sz, CL_PQueueCompareFn compare_fn)
Initializes a priority queue with the allocated memory provided by the user.
CL_Error CL_PQueueEnqueue(CL_PQueue *const pq, const void *const elem)
Adds an element to the priority queue.
int(* CL_PQueueCompareFn)(const void *, const void *)
struct __PQueue CL_PQueue
CL_Error
Specify this in the size parameter of the collections initialization to tell the library that the num...
@ CL_ERR_EMPTY
Empty collection.
@ CL_ERR_NO_MEMORY
No memory available.
@ CL_ERR_INVALID_PARAMS
Invalid arguments.
MEM_Allocator * allocator
The memory allocator.
CL_PQueueCompareFn compare_fn
The comparison function.