GNUnet 0.29.1-21-g8c89d4373
 
Loading...
Searching...
No Matches
container_heap.c File Reference

Implementation of a heap. More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for container_heap.c:

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.
 

Detailed Description

Implementation of a heap.

Author
Nathan Evans
Christian Grothoff

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.

Macro Definition Documentation

◆ ARITY

#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.

◆ NO_WALK

#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.

◆ MIN_CAPACITY

#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.

◆ EXTRA_CHECKS

#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.

◆ CHECK

#define CHECK (   h)    do {} while (0)

Definition at line 196 of file container_heap.c.

Function Documentation

◆ key_mask()

static GNUNET_CONTAINER_HeapCostType key_mask ( const struct GNUNET_CONTAINER_Heap *  heap)
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.

Parameters
heapheap to get the mask for
Returns
mask to XOR costs and keys with

Definition at line 164 of file container_heap.c.

165{
167 return ~((GNUNET_CONTAINER_HeapCostType) 0);
168 return 0;
169}
uint64_t GNUNET_CONTAINER_HeapCostType
Cost by which elements in a heap can be ordered.
@ GNUNET_CONTAINER_HEAP_ORDER_MAX
Heap with the maximum cost at the root.
enum GNUNET_CONTAINER_HeapOrder order
How is the heap sorted?

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().

Here is the caller graph for this function:

◆ place()

static void place ( struct GNUNET_CONTAINER_Heap *  heap,
unsigned int  index,
struct HeapSlot  slot 
)
static

Store slot at index of heap and tell its node where it is.

Parameters
heapheap to modify
indexindex of the slot to write
slotslot to store

Definition at line 208 of file container_heap.c.

211{
212 heap->slots[index] = slot;
213 slot.node->index = index;
214}
unsigned int index
Index of the slot in heap that currently holds this node.
struct HeapSlot * slots
Array of capacity slots, of which the first size are in use.
struct GNUNET_CONTAINER_HeapNode * node
Node stored in this slot.

References GNUNET_CONTAINER_HeapNode::index, HeapSlot::node, and GNUNET_CONTAINER_Heap::slots.

Referenced by sift_down(), and sift_up().

Here is the caller graph for this function:

◆ sift_up()

static void sift_up ( struct GNUNET_CONTAINER_Heap *  heap,
unsigned int  index,
struct HeapSlot  slot 
)
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.

Parameters
heapheap to modify
indexindex of the free slot to start from
slotslot to insert

Definition at line 227 of file container_heap.c.

230{
231 while (index > 0)
232 {
233 unsigned int parent = (index - 1) / ARITY;
234
235 if (heap->slots[parent].key <= slot.key)
236 break;
237 place (heap, index, heap->slots[parent]);
238 index = parent;
239 }
240 place (heap, index, slot);
241}
#define ARITY
Number of children per node.
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.
GNUNET_CONTAINER_HeapCostType key
Cost of the node, transformed by key_mask() for the heap's order.

References ARITY, HeapSlot::key, place(), and GNUNET_CONTAINER_Heap::slots.

Referenced by GNUNET_CONTAINER_heap_insert(), and reposition().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sift_down()

static void sift_down ( struct GNUNET_CONTAINER_Heap *  heap,
unsigned int  index,
struct HeapSlot  slot 
)
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.

Parameters
heapheap to modify
indexindex of the free slot to start from
slotslot to insert

Definition at line 255 of file container_heap.c.

258{
259 while (1)
260 {
261 /* 64-bit arithmetic: ARITY * index can exceed UINT_MAX */
262 uint64_t first = (uint64_t) ARITY * index + 1;
263 uint64_t end;
264 unsigned int best;
265
266 if (first >= heap->size)
267 break;
268 end = GNUNET_MIN (first + ARITY,
269 (uint64_t) heap->size);
270 best = (unsigned int) first;
271 for (unsigned int child = best + 1; child < end; child++)
272 {
273 if (heap->slots[child].key < heap->slots[best].key)
274 best = child;
275 }
276 if (slot.key <= heap->slots[best].key)
277 break;
278 place (heap, index, heap->slots[best]);
279 index = best;
280 }
281 place (heap, index, slot);
282}
static int end
Set if we are to shutdown all services (including ARM).
Definition gnunet-arm.c:33
static pid_t child
#define GNUNET_MIN(a, b)
unsigned int size
Number of elements in the heap.

References ARITY, child, end, GNUNET_MIN, HeapSlot::key, place(), GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.

Referenced by reposition().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reposition()

static void reposition ( struct GNUNET_CONTAINER_Heap *  heap,
unsigned int  index,
struct HeapSlot  slot 
)
static

Fill the unused slot at index with slot, moving it up or down as its key requires.

Parameters
heapheap to modify
indexindex of the free slot
slotslot to insert

Definition at line 294 of file container_heap.c.

297{
298 if ( (index > 0) &&
299 (slot.key < heap->slots[(index - 1) / ARITY].key) )
300 sift_up (heap, index, slot);
301 else
302 sift_down (heap, index, slot);
303}
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 ...
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 ...

References ARITY, HeapSlot::key, sift_down(), sift_up(), and GNUNET_CONTAINER_Heap::slots.

Referenced by GNUNET_CONTAINER_heap_update_cost(), and remove_slot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resize()

static void resize ( struct GNUNET_CONTAINER_Heap *  heap,
unsigned int  capacity 
)
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.

Parameters
heapheap to modify
capacitynew number of slots, at least the size of heap and at least MIN_CAPACITY

Definition at line 319 of file container_heap.c.

321{
322 GNUNET_assert (capacity >= heap->size);
323 GNUNET_assert (capacity >= MIN_CAPACITY);
324 GNUNET_assert (capacity <= SIZE_MAX / sizeof (struct HeapSlot));
325 heap->slots = GNUNET_realloc (heap->slots,
326 capacity * sizeof (struct HeapSlot));
327 heap->capacity = capacity;
328}
#define MIN_CAPACITY
Smallest number of slots the array ever has, from the creation of the heap on and also while it is em...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
#define SIZE_MAX
Definition platform.h:213
unsigned int capacity
Number of slots allocated in slots, at least MIN_CAPACITY.
One position of the heap array.

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().

Here is the caller graph for this function:

◆ remove_slot()

static void remove_slot ( struct GNUNET_CONTAINER_Heap *  heap,
unsigned int  index 
)
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.

Parameters
heapheap to modify
indexindex of the slot to remove

Definition at line 344 of file container_heap.c.

346{
347 struct HeapSlot last;
348
349 heap->size--;
350 last = heap->slots[heap->size];
351 if (index != heap->size)
352 reposition (heap, index, last);
353 /* test against twice the minimum, not the minimum itself: after
354 growing to UINT_MAX the capacity is odd, and halving it again and
355 again would go from 15 slots to 7 */
356 if ( (heap->capacity >= 2 * MIN_CAPACITY) &&
357 (heap->size <= heap->capacity / 4) )
358 resize (heap, heap->capacity / 2);
359 CHECK (heap);
360}
static void resize(struct GNUNET_CONTAINER_Heap *heap, unsigned int capacity)
Change the number of slots allocated for heap.
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.
#define CHECK(h)

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().

Here is the call graph for this function:
Here is the caller graph for this function: