|
Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
|
A generic priority queue backed by a binary min-heap. More...
#include <stddef.h>Go to the source code of this file.
Data Structures | |
| struct | pqmh_control_t |
| Per type configuration. More... | |
| struct | pqmh_t |
| A priority queue. More... | |
Typedefs | |
| typedef bool(* | pqmh_less_fn) (srn_context_t *ctx, const void *a, const void *b) |
| Strict weak ordering predicate over two elements. | |
| typedef struct pqmh_control_t | pqmh_control_t |
| Per type configuration. | |
| typedef struct pqmh_t | pqmh_t |
| A priority queue. | |
Functions | |
| pqmh_t | pqmh_make (srn_context_t *ctx, const pqmh_control_t *ctl, size_t initial_cap) |
| Create an empty queue ordered by ctl, with room for initial_cap elements (a small default is used when 0). | |
| void | pqmh_push (srn_context_t *ctx, pqmh_t *pq, void *elem) |
| Insert elem, growing the backing array (doubling) if it is full. | |
| bool | pqmh_pop (srn_context_t *ctx, pqmh_t *pq, void **out) |
| Remove the highest priority element into *out and return true, or return false (leaving *out untouched) when the queue is empty. | |
| void * | pqmh_peek (const pqmh_t *pq) |
| The highest priority element without removing it, or NULL when the queue is empty. | |
| size_t | pqmh_len (const pqmh_t *pq) |
| The number of elements currently in the queue. | |
| void | pqmh_free (srn_context_t *ctx, pqmh_t *pq) |
| Release the backing array and reset the queue to empty. | |
A generic priority queue backed by a binary min-heap.
Elements are stored by pointer (void *), and the ordering is supplied through a control vtable (pqmh_control_t), so the same heap serves any element type – the only thing that varies per type is the comparison. The element the control orders first is the one at the root, returned by pqmh_peek and removed by pqmh_pop. A "less" predicate that returns a < b yields a min-heap, its inverse a max-heap.
This is a mutable, growable container. Its backing array is held in the manual lifetime allocator (srn_mm_malloc/srn_mm_reallocate/srn_mm_free) and grows by doubling, so a pqmh_t must be released with pqmh_free. By convention the operations take a srn_context_t * and reach the memory manager through ctx->engine->mm.
It is not synchronized; a pqmh_t is owned by a single thread and IS NOT THREAD SAFE.
Definition in file pqmh.h.
| typedef struct pqmh_control_t pqmh_control_t |
Per type configuration.
Only the comparison varies between element types. The heap operations themselves are type independent, so the control carries just the ordering predicate.
| typedef bool(* pqmh_less_fn) (srn_context_t *ctx, const void *a, const void *b) |
| typedef struct pqmh_t pqmh_t |
A priority queue.
data is the backing heap array of element pointers, len the live count, cap the allocated capacity. The control is bound at pqmh_make and not re-passed per operation.
| void pqmh_free | ( | srn_context_t * | ctx, |
| pqmh_t * | pq ) |
Release the backing array and reset the queue to empty.
The elements themselves are not touched – their lifetime belongs to the caller.
Definition at line 142 of file pqmh.c.
|
nodiscard |
|
nodiscard |
Create an empty queue ordered by ctl, with room for initial_cap elements (a small default is used when 0).
The backing array comes from the manual allocator, so the queue must later be released with pqmh_free.
Definition at line 81 of file pqmh.c.
|
nodiscard |
|
nodiscard |
Remove the highest priority element into *out and return true, or return false (leaving *out untouched) when the queue is empty.
Definition at line 111 of file pqmh.c.
| void pqmh_push | ( | srn_context_t * | ctx, |
| pqmh_t * | pq, | ||
| void * | elem ) |
Insert elem, growing the backing array (doubling) if it is full.
Panics if the growth allocation fails.
Definition at line 92 of file pqmh.c.