Skip to content
Gustav Louw edited this page Jan 13, 2021 · 6 revisions
#include <pqu.h>

The CTL pqu, analogous to the STL std::priority_queue, is a container specializing in prioritized queue operations. A pqu can be seen as a simplified but extended vec, and therefor maintains all pointer validity and memory contiguousness properties of a vec.

Example: Highest Priority Queuing

#include <stdio.h>

typedef struct
{
    int value;
    int priority;
}
elem;

#define P
#define T elem
#include <pqu.h>

int compare(elem* a, elem* b)
{
    return a->priority < b->priority;
}

int main(void)
{
    pqu_elem a = pqu_elem_init(compare);
    for(int i = 0; i < 32; i++)
        pqu_elem_push(&a, (elem) { rand() % 1024, rand() % 1024 });
    int sum = 0;
    int index = 0;
    while(!pqu_elem_empty(&a))
    {
        elem* e = pqu_elem_top(&a);
        printf("%2d: %4d %4d\n", index, e->value, e->priority);
        sum += e->value;
        index += 1;
        pqu_elem_pop(&a);
    }
    printf("%d\n", sum);
    pqu_elem_free(&a);
}
gcc main.c -I ctl

Lower priority elements can be queued first by reversing the comparison operator.

Clone this wiki locally