libvieter/src/heap
Jef Roosens 5b2ce6acaa
ci/woodpecker/pr/lint Pipeline is pending Details
ci/woodpecker/pr/test-mem Pipeline is pending Details
ci/woodpecker/pr/test Pipeline is pending Details
feat(heap): thread-safety features
2023-01-27 21:23:32 +01:00
..
README.md docs(heap): add readme 2023-01-26 11:03:41 +01:00
vieter_heap.c feat(heap): thread-safety features 2023-01-27 21:23:32 +01:00
vieter_heap_internal.h feat(heap): thread-safety features 2023-01-27 21:23:32 +01:00
vieter_heap_tree.c test: also test with -O3 which can produce extra errors 2023-01-26 12:24:34 +01:00
vieter_heap_tree.h feat(heap): thread-safety features 2023-01-27 21:23:32 +01:00

README.md

This min-heap implementation is a pretty standard binomial heap.

Representation in memory

A heap consists of one or more binomial trees, each with a different order k and 2^k total nodes. This heap can contain 2^64 - 1 elements at most, which is far more than your memory can contain, but it's still fun to mention.

A tree does not have its own memory structure; a node that's the root of a binomial tree is simply called the tree.

Each node has the following layout:

typedef struct vieter_heap_node {
    uint64_t key;
    void *data;
    struct vieter_heap_node *largest_order;
    union {
      struct vieter_heap_node *next_tree;
      struct vieter_heap_node *next_largest_order;
    } ptr;
    uint8_t order;
} vieter_heap_node;

Each node has a pointer to its child with the largest order (if the node's order is 0, this pointer will be NULL). Each non-root node has a pointer to its sibling with the next-highest order. These pointers allow the children of a binomial tree to be recombined into a new tree, once their root has been pop'ed.

Roots point to the binomial tree in the heap with the next largest order.