Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
cl_prioqueue.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_PQUEUE_FOREACH(__type, __name, __queue, ...)
 

Typedefs

typedef struct __PQueue CL_PQueue
 
typedef int(* CL_PQueueCompareFn) (const void *, const void *)
 

Functions

CL_PQueueCL_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_PQueueCL_PQueueAlloc (MEM_Allocator *const alloc, u32 elem_sz, CL_PQueueCompareFn compare_fn)
 Initializes a priority queue with a default capacity.
 
void CL_PQueueFree (CL_PQueue *const pq)
 Frees the memory previously assigned to the priority queue.
 
CL_Error CL_PQueueEnqueue (CL_PQueue *const pq, const void *const elem)
 Adds an element to the priority queue.
 
CL_Error CL_PQueueDequeue (CL_PQueue *const pq, void *out_elem)
 Removes the element with the highest priority from the priority queue.
 
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.
 
u32 CL_PQueueSize (const CL_PQueue *const pq)
 Gets the number of elements in the priority queue.
 
bool CL_PQueueIsEmpty (const CL_PQueue *const pq)
 Checks if the priority queue is empty.
 

Macro Definition Documentation

◆ CL_PQUEUE_FOREACH

#define CL_PQUEUE_FOREACH (   __type,
  __name,
  __queue,
  ... 
)
Value:
for (u32 __i = 0; __i < CL_PQueueSize(__queue); __i++) \
{ \
__type *__pq_arr = (__type *)CL_PQueueAsArray(__queue); \
__type __name = __pq_arr[__i]; \
__VA_ARGS__ \
}
u32 CL_PQueueSize(const CL_PQueue *const pq)
Gets the number of elements in the priority queue.
uint32_t u32
Definition types.h:6

Definition at line 15 of file cl_prioqueue.h.

17 { \
18 __type *__pq_arr = (__type *)CL_PQueueAsArray(__queue); \
19 __type __name = __pq_arr[__i]; \
20 __VA_ARGS__ \
21 }

Typedef Documentation

◆ CL_PQueue

typedef struct __PQueue CL_PQueue

Definition at line 11 of file cl_prioqueue.h.

◆ CL_PQueueCompareFn

typedef int(* CL_PQueueCompareFn) (const void *, const void *)

Definition at line 29 of file cl_prioqueue.h.

Function Documentation

◆ CL_PQueueAlloc()

CL_PQueue * CL_PQueueAlloc ( MEM_Allocator *const  alloc,
u32  elem_sz,
CL_PQueueCompareFn  compare_fn 
)

Initializes a priority queue with a default capacity.

Parameters
allocThe memory allocator to use.
elem_szThe size of each element.
compare_fnThe comparison function to use for handling priorities.
Returns
Pointer to the new priority queue or NULL on error.

Definition at line 143 of file cl_prioqueue.c.

144{
145 return CL_PQueueAllocWithCapacity(alloc, BASE_CAPACITY, elem_sz, compare_fn);
146}
#define BASE_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_PQueueAllocWithCapacity()

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.

Parameters
allocatorThe memory allocator to use.
capacityThe initial capacity of the priority queue.
elem_szThe size of each element.
compare_fnThe comparison function to use for handling priorities.
Returns
Pointer to the new priority queue or NULL on error.

Definition at line 115 of file cl_prioqueue.c.

117{
118 if (!alloc || !capacity || !elem_sz || !compare_fn)
119 return NULL;
120
121 CL_PQueue *const pq = MEM_Alloc(alloc, sizeof(CL_PQueue));
122 if (!pq)
123 return NULL;
124
125 *pq = (CL_PQueue){
126 .data = MEM_Alloc(alloc, capacity * elem_sz),
127 .size = 0,
128 .capacity = capacity,
129 .elem_size = elem_sz,
130 .compare_fn = compare_fn,
131 .allocator = alloc,
132 };
133
134 if (!pq->data)
135 {
136 MEM_Free(alloc, pq);
137 return NULL;
138 }
139
140 return pq;
141}
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 __PQueue CL_PQueue
void * data

◆ CL_PQueueDequeue()

CL_Error CL_PQueueDequeue ( CL_PQueue *const  pq,
void *  out_elem 
)

Removes the element with the highest priority from the priority queue.

Parameters
pqThe priority queue to remove the element from.
out_elem[OUTPUT] The element with the highest priority.
Returns
CL_Error The error code.

Definition at line 179 of file cl_prioqueue.c.

180{
181 if (!pq || !out_elem)
183
184 if (!pq->size)
185 return CL_ERR_EMPTY;
186
187 // Getting the element with the highest priority (i.e. the root of the tree).
188 memcpy(out_elem, pq->data, pq->elem_size);
189 pq->size--;
190
191 // Moving the last element to the root and bubbling it down.
192 const u32 last_elem_offset = pq->size * pq->elem_size;
193 memcpy(pq->data, (u8 *)(pq->data) + last_elem_offset, pq->elem_size);
195 return CL_ERR_OK;
196}
_PRIVATE void bubble_down_root(CL_PQueue *const pq)
@ CL_ERR_EMPTY
Empty collection.
Definition cl_types.h:41
@ CL_ERR_INVALID_PARAMS
Invalid arguments.
Definition cl_types.h:29
@ CL_ERR_OK
No error.
Definition cl_types.h:26
u32 elem_size
uint8_t u8
Definition types.h:8

◆ CL_PQueueEnqueue()

CL_Error CL_PQueueEnqueue ( CL_PQueue *const  pq,
const void *const  elem 
)

Adds an element to the priority queue.

Parameters
pqThe priority queue to add the element to.
elemThe element to add.
Returns
CL_Error The error code.

Definition at line 157 of file cl_prioqueue.c.

158{
159 if (!pq || !elem)
161
162 if (pq->size >= pq->capacity)
163 {
164 const u32 new_capacity = pq->capacity * RESZ_FACTOR;
165 if (!realloc_data(pq, new_capacity))
166 return CL_ERR_NO_MEMORY;
167 }
168
169 // Adding the element at the end (queues are FIFO)
170 const u32 offset = pq->size * pq->elem_size;
171 memcpy((u8 *)(pq->data) + offset, elem, pq->elem_size);
172 pq->size++;
173
174 // Bubbling up the new element to its correct position.
175 bubble_up_leaf(pq);
176 return CL_ERR_OK;
177}
_PRIVATE void bubble_up_leaf(CL_PQueue *const pq)
_PRIVATE void * realloc_data(CL_PQueue *const pq, u32 new_capacity)
#define RESZ_FACTOR
@ CL_ERR_NO_MEMORY
No memory available.
Definition cl_types.h:35

◆ CL_PQueueFree()

void CL_PQueueFree ( CL_PQueue *const  pq)

Frees the memory previously assigned to the priority queue.

Parameters
pqThe priority queue to release the memory of.
Note
CL_PQueue does not take ownership of the elements, so it's the user's responsibility to free them.

Definition at line 148 of file cl_prioqueue.c.

149{
150 if (!pq)
151 return;
152
153 MEM_Free(pq->allocator, pq->data);
154 MEM_Free(pq->allocator, pq);
155}
MEM_Allocator * allocator
The memory allocator.

◆ CL_PQueueIsEmpty()

bool CL_PQueueIsEmpty ( const CL_PQueue *const  pq)

Checks if the priority queue is empty.

Parameters
pqThe priority queue to check.
Returns
Whether the priority queue is empty.

Definition at line 216 of file cl_prioqueue.c.

217{
218 return pq ? pq->size == 0 : true;
219}

◆ CL_PQueuePeek()

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.

Parameters
pqThe priority queue to get the element from.
out_elem[OUTPUT] The element with the highest priority.
Returns
CL_Error The error code.

Definition at line 198 of file cl_prioqueue.c.

199{
200 if (!pq || !out_elem)
202
203 if (!pq->size)
204 return CL_ERR_EMPTY;
205
206 // Getting the element with the highest priority (i.e. the root of the tree).
207 memcpy(out_elem, pq->data, pq->elem_size);
208 return CL_ERR_OK;
209}

◆ CL_PQueueSize()

u32 CL_PQueueSize ( const CL_PQueue *const  pq)

Gets the number of elements in the priority queue.

Parameters
pqThe priority queue to get the number of elements from.
Returns
The number of elements in the priority queue.

Definition at line 211 of file cl_prioqueue.c.

212{
213 return pq ? pq->size : 0;
214}