60#define NO_WALK UINT_MAX
184 for (
unsigned int i = 0; i < heap->
size; i++)
194#define CHECK(h) check (h)
196#define CHECK(h) do {} while (0)
212 heap->
slots[index] = slot;
233 unsigned int parent = (index - 1) /
ARITY;
240 place (heap, index, slot);
262 uint64_t first = (uint64_t)
ARITY * index + 1;
266 if (first >= heap->
size)
269 (uint64_t) heap->
size);
270 best = (
unsigned int) first;
281 place (heap, index, slot);
320 unsigned int capacity)
326 capacity * sizeof (
struct HeapSlot));
351 if (index != heap->
size)
445 for (
unsigned int i = heap->
size; i > 0; i--)
450 iterator (iterator_cls,
468 if (pos >= heap->
size)
473 if (next < heap->
size)
474 heap->
walk_pos = (
unsigned int) next;
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 ...
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 MIN_CAPACITY
Smallest number of slots the array ever has, from the creation of the heap on and also while it is em...
#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.
#define NO_WALK
Value of walk_pos when no random walk is in progress.
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,...
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,...
static int end
Set if we are to shutdown all services (including ARM).
uint32_t GNUNET_CRYPTO_random_u32(uint32_t i)
Produce a random value.
void * GNUNET_CONTAINER_heap_remove_node(struct GNUNET_CONTAINER_HeapNode *node)
Removes a node from the heap.
void * GNUNET_CONTAINER_heap_peek(const struct GNUNET_CONTAINER_Heap *heap)
Get element stored at the root of heap.
GNUNET_CONTAINER_HeapOrder
Heap type, either max or min.
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_remove_root(struct GNUNET_CONTAINER_Heap *heap)
Remove root of 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.
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.
enum GNUNET_GenericReturnValue(* GNUNET_CONTAINER_HeapIterator)(void *cls, struct GNUNET_CONTAINER_HeapNode *node, void *element, GNUNET_CONTAINER_HeapCostType cost)
Iterator for heap.
unsigned int GNUNET_CONTAINER_heap_get_size(const struct GNUNET_CONTAINER_Heap *heap)
Get the current size of the heap.
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.
uint64_t GNUNET_CONTAINER_HeapCostType
Cost by which elements in a heap can be ordered.
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_walk_get_next(struct GNUNET_CONTAINER_Heap *heap)
Perform a random walk of the tree.
@ GNUNET_CONTAINER_HEAP_ORDER_MAX
Heap with the maximum cost at the root.
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.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
#define GNUNET_free(ptr)
Wrapper around free.
static unsigned int size
Size of the "table".
Handle to an element in a heap.
void * element
Our element.
struct GNUNET_CONTAINER_Heap * heap
Heap this node belongs to.
unsigned int index
Index of the slot in heap that currently holds this node.
unsigned int size
Number of elements in the heap.
unsigned int capacity
Number of slots allocated in slots, at least MIN_CAPACITY.
enum GNUNET_CONTAINER_HeapOrder order
How is the heap sorted?
struct HeapSlot * slots
Array of capacity slots, of which the first size are in use.
unsigned int walk_pos
Index of the slot the random walk visits next, or NO_WALK.
One position of the heap array.
GNUNET_CONTAINER_HeapCostType key
Cost of the node, transformed by key_mask() for the heap's order.
struct GNUNET_CONTAINER_HeapNode * node
Node stored in this slot.