Implementation of a heap. More...
Go to the source code of this file.
Data Structures | |
| struct | GNUNET_CONTAINER_HeapNode |
| Handle to an element in a heap. More... | |
| struct | HeapSlot |
| One position of the heap array. More... | |
| struct | GNUNET_CONTAINER_Heap |
| Handle to a heap. More... | |
Macros | |
| #define | ARITY 4 |
| Number of children per node. | |
| #define | NO_WALK UINT_MAX |
Value of walk_pos when no random walk is in progress. | |
| #define | MIN_CAPACITY 8 |
| Smallest number of slots the array ever has, from the creation of the heap on and also while it is empty. | |
| #define | EXTRA_CHECKS 0 |
| Set to 1 to check all heap invariants after every modification. | |
| #define | CHECK(h) do {} while (0) |
Functions | |
| static GNUNET_CONTAINER_HeapCostType | key_mask (const struct GNUNET_CONTAINER_Heap *heap) |
| Return the mask that converts between a cost and its key in heap, in either direction, by XOR. | |
| static void | place (struct GNUNET_CONTAINER_Heap *heap, unsigned int index, struct HeapSlot slot) |
| Store slot at index of heap and tell its node where it is. | |
| static void | sift_up (struct GNUNET_CONTAINER_Heap *heap, unsigned int index, struct HeapSlot slot) |
| Fill the unused slot at index with slot, moving it up towards the root for as long as its parent has a larger key. | |
| static void | sift_down (struct GNUNET_CONTAINER_Heap *heap, unsigned int index, struct HeapSlot slot) |
| Fill the unused slot at index with slot, moving it down away from the root for as long as one of its children has a smaller key. | |
| static void | reposition (struct GNUNET_CONTAINER_Heap *heap, unsigned int index, struct HeapSlot slot) |
| Fill the unused slot at index with slot, moving it up or down as its key requires. | |
| static void | resize (struct GNUNET_CONTAINER_Heap *heap, unsigned int capacity) |
| Change the number of slots allocated for heap. | |
| static void | remove_slot (struct GNUNET_CONTAINER_Heap *heap, unsigned int index) |
| Remove the slot at index from heap: move the last slot into it, restore the heap order, and shrink the array if it became sparse. | |
| struct GNUNET_CONTAINER_Heap * | GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order) |
| Create a new heap. | |
| void | GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap) |
| Destroys the heap. | |
| void * | GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap) |
| Get element stored at the root of heap. | |
| enum GNUNET_GenericReturnValue | GNUNET_CONTAINER_heap_peek2 (const struct GNUNET_CONTAINER_Heap *heap, void **element, GNUNET_CONTAINER_HeapCostType *cost) |
| Get element and cost stored at the root of heap. | |
| unsigned int | GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap) |
| Get the current size of the heap. | |
| GNUNET_CONTAINER_HeapCostType | GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode *node) |
| Get the current cost of the node. | |
| void | GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap, GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls) |
| Iterate over all entries in the heap, in no particular order. | |
| void * | GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap) |
| Perform a random walk of the tree. | |
| struct GNUNET_CONTAINER_HeapNode * | GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element, GNUNET_CONTAINER_HeapCostType cost) |
| Inserts a new element into the heap. | |
| void * | GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap) |
| Remove root of the heap. | |
| void * | GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node) |
| Removes a node from the heap. | |
| void | GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_HeapNode *node, GNUNET_CONTAINER_HeapCostType new_cost) |
| Updates the cost of any node in the tree. | |
Implementation of a heap.
The heap is an implicit ARITY-ary tree stored in an array of slots. Slot 0 holds the root, and the children of slot i are the slots ARITY * i + 1 to ARITY * i + ARITY. No slot has a smaller key than its parent, so the root has the smallest key. In a MIN heap the key is the cost; in a MAX heap it is the complemented cost (see key_mask()), so the largest cost ends up at the root.
Callers refer to elements through struct GNUNET_CONTAINER_HeapNode handles, which stay valid while their element moves through the array: each node records the index of its current slot, and each slot points back to its node. This lets remove_node() and update_cost() find the element without a search.
Insert, remove and update are O(log n) in the worst case; peek, size and cost queries are O(1).
Definition in file container_heap.c.
| #define ARITY 4 |
Number of children per node.
With four children the tree is half as deep as a binary one, and the keys of all children of a node, which a sift-down compares, are adjacent in memory.
Definition at line 54 of file container_heap.c.
| #define NO_WALK UINT_MAX |
Value of walk_pos when no random walk is in progress.
Any value that is not a valid slot index works; this one never is.
Definition at line 60 of file container_heap.c.
| #define MIN_CAPACITY 8 |
Smallest number of slots the array ever has, from the creation of the heap on and also while it is empty.
Eight slots take 128 bytes, and keeping them means that a heap that holds only a handful of elements never reallocates its array.
Definition at line 68 of file container_heap.c.
| #define EXTRA_CHECKS 0 |
Set to 1 to check all heap invariants after every modification.
That makes every operation O(n), so only use it for debugging.
Definition at line 75 of file container_heap.c.
| #define CHECK | ( | h | ) | do {} while (0) |
Definition at line 196 of file container_heap.c.
|
static |
Return the mask that converts between a cost and its key in heap, in either direction, by XOR.
For a MAX heap the key is the bitwise complement of the cost, which reverses the order of unsigned values. The array can then always keep the smallest key at the root, and a single unsigned comparison serves both heap orders.
| heap | heap to get the mask for |
Definition at line 164 of file container_heap.c.
References GNUNET_CONTAINER_HEAP_ORDER_MAX, and GNUNET_CONTAINER_Heap::order.
Referenced by GNUNET_CONTAINER_heap_insert(), GNUNET_CONTAINER_heap_iterate(), GNUNET_CONTAINER_heap_node_get_cost(), GNUNET_CONTAINER_heap_peek2(), and GNUNET_CONTAINER_heap_update_cost().
|
static |
Store slot at index of heap and tell its node where it is.
| heap | heap to modify |
| index | index of the slot to write |
| slot | slot to store |
Definition at line 208 of file container_heap.c.
References GNUNET_CONTAINER_HeapNode::index, HeapSlot::node, and GNUNET_CONTAINER_Heap::slots.
Referenced by sift_down(), and sift_up().
|
static |
Fill the unused slot at index with slot, moving it up towards the root for as long as its parent has a larger key.
Each parent it passes moves down one level into the free slot.
| heap | heap to modify |
| index | index of the free slot to start from |
| slot | slot to insert |
Definition at line 227 of file container_heap.c.
References ARITY, HeapSlot::key, place(), and GNUNET_CONTAINER_Heap::slots.
Referenced by GNUNET_CONTAINER_heap_insert(), and reposition().
|
static |
Fill the unused slot at index with slot, moving it down away from the root for as long as one of its children has a smaller key.
At each level the child with the smallest key moves up into the free slot.
| heap | heap to modify |
| index | index of the free slot to start from |
| slot | slot to insert |
Definition at line 255 of file container_heap.c.
References ARITY, child, end, GNUNET_MIN, HeapSlot::key, place(), GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.
Referenced by reposition().
|
static |
Fill the unused slot at index with slot, moving it up or down as its key requires.
| heap | heap to modify |
| index | index of the free slot |
| slot | slot to insert |
Definition at line 294 of file container_heap.c.
References ARITY, HeapSlot::key, sift_down(), sift_up(), and GNUNET_CONTAINER_Heap::slots.
Referenced by GNUNET_CONTAINER_heap_update_cost(), and remove_slot().
|
static |
Change the number of slots allocated for heap.
Uses GNUNET_realloc() rather than GNUNET_array_grow(): the latter allocates through GNUNET_malloc(), which refuses blocks larger than GNUNET_MAX_MALLOC_CHECKED (40 MiB, about 2.6 million slots), and always copies, while realloc() can often resize in place.
| heap | heap to modify |
| capacity | new number of slots, at least the size of heap and at least MIN_CAPACITY |
Definition at line 319 of file container_heap.c.
References GNUNET_CONTAINER_Heap::capacity, GNUNET_assert, GNUNET_realloc, MIN_CAPACITY, GNUNET_CONTAINER_Heap::size, SIZE_MAX, and GNUNET_CONTAINER_Heap::slots.
Referenced by GNUNET_CONTAINER_heap_insert(), and remove_slot().
|
static |
Remove the slot at index from heap: move the last slot into it, restore the heap order, and shrink the array if it became sparse.
The node that was stored at index is not freed.
The array is halved once at most a quarter of it is in use. It is then half full, so a few inserts and removals around that size do not resize it every time. Halving stops at MIN_CAPACITY slots.
| heap | heap to modify |
| index | index of the slot to remove |
Definition at line 344 of file container_heap.c.
References GNUNET_CONTAINER_Heap::capacity, CHECK, MIN_CAPACITY, reposition(), resize(), GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.
Referenced by GNUNET_CONTAINER_heap_remove_node(), and GNUNET_CONTAINER_heap_remove_root().