27#define PQMH_MIN_CAP 8U
38 size_t parent = (i - 1) / 2;
45 void *tmp = pq->
data[i];
47 pq->
data[parent] = tmp;
58 size_t left = (2 * i) + 1;
59 size_t right = (2 * i) + 2;
62 if (left < pq->len && (
int)pq->
ctl->
less(ctx, pq->
data[left], pq->
data[smallest])) {
66 if (right < pq->len && (
int)pq->
ctl->
less(ctx, pq->
data[right], pq->
data[smallest])) {
74 void *tmp = pq->
data[i];
76 pq->
data[smallest] = tmp;
85 size_t cap = (initial_cap == 0) ?
PQMH_MIN_CAP : initial_cap;
89 return (
pqmh_t){.ctl = ctl, .data = data, .len = 0, .cap = cap};
97 PANIC_IF(pq->
cap > SIZE_MAX / 2 /
sizeof(
void *),
"priority queue capacity overflows");
98 size_t new_cap = pq->
cap * 2;
134 return (pq->
len == 0) ? nullptr : pq->
data[0];
void * srn_mm_reallocate(srn_mm_t *mm, void *ptr, size_t new_size)
void srn_mm_free(srn_mm_t *mm, void *ptr)
Release a pointer previously returned by srn_mm_malloc or srn_mm_reallocate.
void * srn_mm_malloc(srn_mm_t *mm, size_t size)
Generic allocations that do not participate in the block based pools.
void pqmh_free(srn_context_t *ctx, pqmh_t *pq)
Release the backing array and reset the queue to empty.
void pqmh_push(srn_context_t *ctx, pqmh_t *pq, void *elem)
Insert elem, growing the backing array (doubling) if it is full.
#define PQMH_MIN_CAP
Capacity a queue created with initial_cap == 0 starts at.
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 whe...
void * pqmh_peek(const pqmh_t *pq)
The highest priority element without removing it, or NULL when the queue is empty.
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 untouche...
static void sift_down(srn_context_t *ctx, pqmh_t *pq, size_t i)
Move the element at i down toward the leaves until the heap order holds (neither child is ordered bef...
static void sift_up(srn_context_t *ctx, pqmh_t *pq, size_t i)
Move the element at i up toward the root until the heap order holds (its parent is no longer ordered ...
size_t pqmh_len(const pqmh_t *pq)
The number of elements currently in the queue.
A generic priority queue backed by a binary min-heap.
const pqmh_control_t * ctl
srn_engine_t * engine
Long term state of the compiler.
srn_mm_t * mm
Memory manager.
#define PANIC_IF_NULL(ptr)
#define PANIC_IF(cond, msg)