GNUnet 0.29.1-21-g8c89d4373
 
Loading...
Searching...
No Matches

Min- or max-heap with arbitrary element removal. More...

Collaboration diagram for Heap:

Typedefs

typedef uint64_t GNUNET_CONTAINER_HeapCostType
 Cost by which elements in a heap can be ordered.
 
typedef enum GNUNET_GenericReturnValue(* GNUNET_CONTAINER_HeapIterator) (void *cls, struct GNUNET_CONTAINER_HeapNode *node, void *element, GNUNET_CONTAINER_HeapCostType cost)
 Iterator for heap.
 

Enumerations

enum  GNUNET_CONTAINER_HeapOrder { GNUNET_CONTAINER_HEAP_ORDER_MAX , GNUNET_CONTAINER_HEAP_ORDER_MIN }
 Heap type, either max or min. More...
 

Functions

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

Min- or max-heap with arbitrary element removal.

Insertion, removal and cost updates take O(log n) time, peeking takes O(1).

Typedef Documentation

◆ GNUNET_CONTAINER_HeapCostType

Cost by which elements in a heap can be ordered.

Definition at line 770 of file gnunet_container_lib.h.

◆ GNUNET_CONTAINER_HeapIterator

typedef enum GNUNET_GenericReturnValue(* GNUNET_CONTAINER_HeapIterator) (void *cls, struct GNUNET_CONTAINER_HeapNode *node, void *element, GNUNET_CONTAINER_HeapCostType cost)

Iterator for heap.

Parameters
clsclosure
nodeinternal node of the heap
elementvalue stored at the node
costcost associated with the node
Returns
GNUNET_YES if we should continue to iterate, GNUNET_NO if not.

Definition at line 874 of file gnunet_container_lib.h.

Enumeration Type Documentation

◆ GNUNET_CONTAINER_HeapOrder

Heap type, either max or min.

Enumerator
GNUNET_CONTAINER_HEAP_ORDER_MAX 

Heap with the maximum cost at the root.

GNUNET_CONTAINER_HEAP_ORDER_MIN 

Heap with the minimum cost at the root.

Definition at line 777 of file gnunet_container_lib.h.

778{
784
790};
@ GNUNET_CONTAINER_HEAP_ORDER_MIN
Heap with the minimum cost at the root.
@ GNUNET_CONTAINER_HEAP_ORDER_MAX
Heap with the maximum cost at the root.

Function Documentation

◆ GNUNET_CONTAINER_heap_create()

struct GNUNET_CONTAINER_Heap * GNUNET_CONTAINER_heap_create ( enum GNUNET_CONTAINER_HeapOrder  order)

Create a new heap.

Parameters
orderhow should the heap be sorted?
Returns
handle to the heap

Definition at line 364 of file container_heap.c.

365{
366 struct GNUNET_CONTAINER_Heap *heap;
367
368 heap = GNUNET_new (struct GNUNET_CONTAINER_Heap);
370 struct HeapSlot);
371 heap->capacity = MIN_CAPACITY;
372 heap->order = order;
373 heap->walk_pos = NO_WALK;
374 return heap;
375}
#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 NO_WALK
Value of walk_pos when no random walk is in progress.
#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.
Handle to a 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.

References GNUNET_CONTAINER_Heap::capacity, GNUNET_new, GNUNET_new_array, MIN_CAPACITY, NO_WALK, GNUNET_CONTAINER_Heap::order, GNUNET_CONTAINER_Heap::slots, and GNUNET_CONTAINER_Heap::walk_pos.

Referenced by GCO_init(), GCP_get(), GDS_CLIENTS_init(), GDS_ROUTING_init(), GNS_resolver_init(), GSF_pending_request_init_(), GSF_plan_add_(), handle_fragment_box(), libgnunet_plugin_datacache_heap_init(), libgnunet_plugin_datastore_heap_init(), run(), run(), run(), run(), run(), and run().

Here is the caller graph for this function:

◆ GNUNET_CONTAINER_heap_destroy()

void GNUNET_CONTAINER_heap_destroy ( struct GNUNET_CONTAINER_Heap *  heap)

Destroys the heap.

Only call on a heap that is already empty.

Parameters
heapheap to destroy

Definition at line 379 of file container_heap.c.

380{
381 GNUNET_break (0 == heap->size);
382 GNUNET_free (heap->slots);
383 GNUNET_free (heap);
384}
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_free(ptr)
Wrapper around free.
unsigned int size
Number of elements in the heap.

References GNUNET_break, GNUNET_free, GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.

Referenced by __attribute__(), cleanup(), cleanup(), destroy_peer(), do_shutdown(), do_shutdown(), do_shutdown(), free_virtual_link(), GCO_shutdown(), GCP_drop_owned_paths(), GDS_ROUTING_done(), GNS_resolver_done(), GSF_pending_request_done_(), GSF_plan_notify_peer_disconnect_(), libgnunet_plugin_datacache_heap_done(), and libgnunet_plugin_datastore_heap_done().

Here is the caller graph for this function:

◆ GNUNET_CONTAINER_heap_peek()

void * GNUNET_CONTAINER_heap_peek ( const struct GNUNET_CONTAINER_Heap *  heap)

Get element stored at the root of heap.

Parameters
heapHeap to inspect.
Returns
Element at the root, or NULL if heap is empty.

Definition at line 388 of file container_heap.c.

389{
390 if (0 == heap->size)
391 return NULL;
392 return heap->slots[0].node->element;
393}
void * element
Our element.
struct GNUNET_CONTAINER_HeapNode * node
Node stored in this slot.

References GNUNET_CONTAINER_HeapNode::element, HeapSlot::node, GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.

Referenced by check_timeouts(), expire_channel(), expire_destination(), expire_oldest_entry(), GSF_pending_request_create_(), handle_fragment_box(), heap_plugin_get_expiration(), insert_sorted(), path_heap_cleanup(), process_queue(), process_queue(), reassembly_cleanup_task(), schedule_peer_transmission(), timeout_cb(), update_next_challenge_time(), and validation_start_cb().

Here is the caller graph for this function:

◆ GNUNET_CONTAINER_heap_get_size()

unsigned int GNUNET_CONTAINER_heap_get_size ( const struct GNUNET_CONTAINER_Heap *  heap)

Get the current size of the heap.

Parameters
heapthe heap to get the size of
Returns
number of elements stored

Definition at line 421 of file container_heap.c.

422{
423 return heap->size;
424}

References GNUNET_CONTAINER_Heap::size.

Referenced by __attribute__(), consider_peer_destroy(), GCP_attach_path(), GDS_ROUTING_add(), GDS_ROUTING_done(), GSF_pending_request_create_(), path_heap_cleanup(), schedule_peer_transmission(), setup_state_record(), and start_dht_request().

Here is the caller graph for this function:

◆ GNUNET_CONTAINER_heap_node_get_cost()

GNUNET_CONTAINER_HeapCostType GNUNET_CONTAINER_heap_node_get_cost ( const struct GNUNET_CONTAINER_HeapNode *  node)

Get the current cost of the node.

Parameters
nodethe node to get the cost of
Returns
cost of the node

Definition at line 428 of file container_heap.c.

430{
431 const struct GNUNET_CONTAINER_Heap *heap = node->heap;
432
433 return heap->slots[node->index].key ^ key_mask (heap);
434}
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,...
struct GNUNET_CONTAINER_Heap * heap
Heap this node belongs to.
unsigned int index
Index of the slot in heap that currently holds this node.
GNUNET_CONTAINER_HeapCostType key
Cost of the node, transformed by key_mask() for the heap's order.

References GNUNET_CONTAINER_HeapNode::heap, GNUNET_CONTAINER_HeapNode::index, HeapSlot::key, key_mask(), and GNUNET_CONTAINER_Heap::slots.

Here is the call graph for this function:

◆ GNUNET_CONTAINER_heap_iterate()

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.

The iterator must not modify the heap.

Parameters
heapthe heap
iteratorfunction to call on each entry
iterator_clsclosure for iterator

Definition at line 438 of file container_heap.c.

441{
442 /* Visit the slots from last to first, so that children come before
443 their parents and the root comes last, as with the post-order
444 traversal of the earlier tree-based implementation. */
445 for (unsigned int i = heap->size; i > 0; i--)
446 {
447 const struct HeapSlot *slot = &heap->slots[i - 1];
448
449 if (GNUNET_YES !=
450 iterator (iterator_cls,
451 slot->node,
452 slot->node->element,
453 slot->key ^ key_mask (heap)))
454 return;
455 }
456}
@ GNUNET_YES

References GNUNET_CONTAINER_HeapNode::element, GNUNET_YES, HeapSlot::key, key_mask(), HeapSlot::node, GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.

Here is the call graph for this function:

◆ GNUNET_CONTAINER_heap_walk_get_next()

void * GNUNET_CONTAINER_heap_walk_get_next ( struct GNUNET_CONTAINER_Heap *  heap)

Perform a random walk of the tree.

The walk is biased towards elements closer to the root of the tree (since each walk starts at the root and ends at a random leaf). The heap internally tracks the current position of the walk.

Parameters
heapheap to walk
Returns
data stored at the next random node in the walk; NULL if the tree is empty.

Definition at line 460 of file container_heap.c.

461{
462 unsigned int pos;
463 uint64_t next;
464
465 if (0 == heap->size)
466 return NULL;
467 pos = heap->walk_pos;
468 if (pos >= heap->size)
469 pos = 0;
470 /* Continue at a random child. If that child does not exist, the walk
471 has reached a leaf and the next call starts again at the root. */
472 next = (uint64_t) ARITY * pos + 1 + GNUNET_CRYPTO_random_u32 (ARITY);
473 if (next < heap->size)
474 heap->walk_pos = (unsigned int) next;
475 else
476 heap->walk_pos = NO_WALK;
477 return heap->slots[pos].node->element;
478}
#define ARITY
Number of children per node.
uint32_t GNUNET_CRYPTO_random_u32(uint32_t i)
Produce a random value.
static unsigned int size
Size of the "table".
Definition peer.c:68

References ARITY, GNUNET_CONTAINER_HeapNode::element, GNUNET_CRYPTO_random_u32(), NO_WALK, HeapSlot::node, GNUNET_CONTAINER_Heap::size, size, GNUNET_CONTAINER_Heap::slots, and GNUNET_CONTAINER_Heap::walk_pos.

Referenced by heap_plugin_get_replication().

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

◆ GNUNET_CONTAINER_heap_insert()

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.

Parameters
heapheap to modify
elementelement to insert
costcost for the element
Returns
node for the new element

Definition at line 482 of file container_heap.c.

485{
486 struct GNUNET_CONTAINER_HeapNode *node;
487 struct HeapSlot slot;
488
489 if (heap->size == heap->capacity)
490 {
491 /* Double the array. With the halving in remove_slot(), the array
492 stays more than a quarter full unless it has #MIN_CAPACITY
493 slots. */
494 GNUNET_assert (heap->size < UINT_MAX);
495 if (heap->capacity > UINT_MAX / 2)
496 resize (heap, UINT_MAX);
497 else
498 resize (heap, 2 * heap->capacity);
499 }
501 node->heap = heap;
502 node->element = element;
503 slot.key = cost ^ key_mask (heap);
504 slot.node = node;
505 heap->size++;
506 sift_up (heap,
507 heap->size - 1,
508 slot);
509 CHECK (heap);
510 return node;
511}
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.
#define CHECK(h)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Handle to an element in a heap.

References GNUNET_CONTAINER_Heap::capacity, CHECK, GNUNET_CONTAINER_HeapNode::element, GNUNET_assert, GNUNET_new, GNUNET_CONTAINER_HeapNode::heap, HeapSlot::key, key_mask(), HeapSlot::node, resize(), sift_up(), and GNUNET_CONTAINER_Heap::size.

Referenced by create_receiver(), GCP_attach_path(), GDS_ROUTING_add(), GSF_pending_request_create_(), handle_client_redirect_to_ip(), handle_client_redirect_to_service(), handle_connection_create(), handle_dht_local_get(), handle_fragment_box(), heap_plugin_get_replication(), heap_plugin_put(), heap_plugin_put(), insert_sorted(), insert_sorted(), plan(), route_packet(), schedule_peer_transmission(), setup_sender(), setup_state_record(), start_dht_request(), transmit_next_request_task(), and update_next_challenge_time().

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

◆ GNUNET_CONTAINER_heap_remove_root()

void * GNUNET_CONTAINER_heap_remove_root ( struct GNUNET_CONTAINER_Heap *  heap)

Remove root of the heap.

Parameters
heapheap to modify
Returns
element data stored at the root node, NULL if heap is empty

Definition at line 515 of file container_heap.c.

516{
517 struct GNUNET_CONTAINER_HeapNode *root;
518 void *element;
519
520 if (0 == heap->size)
521 return NULL;
522 root = heap->slots[0].node;
523 element = root->element;
524 remove_slot (heap, 0);
525 GNUNET_free (root);
526 return element;
527}
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,...

References GNUNET_CONTAINER_HeapNode::element, GNUNET_free, GNUNET_CONTAINER_HeapNode::heap, HeapSlot::node, remove_slot(), GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.

Referenced by do_shutdown(), drop_paths(), GCP_drop_owned_paths(), GSF_plan_notify_peer_disconnect_(), heap_plugin_del(), heap_plugin_get_replication(), libgnunet_plugin_datacache_heap_done(), path_heap_cleanup(), process_queue(), process_queue(), schedule_peer_transmission(), setup_state_record(), start_dht_request(), and transmit_next_request_task().

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

◆ GNUNET_CONTAINER_heap_remove_node()

void * GNUNET_CONTAINER_heap_remove_node ( struct GNUNET_CONTAINER_HeapNode *  node)

Removes a node from the heap.

Parameters
nodenode to remove
Returns
element data stored at the node

Definition at line 531 of file container_heap.c.

532{
533 struct GNUNET_CONTAINER_Heap *heap = node->heap;
534 void *element = node->element;
535
536 GNUNET_assert ( (node->index < heap->size) &&
537 (node == heap->slots[node->index].node) );
538 remove_slot (heap, node->index);
539 GNUNET_free (node);
540 return element;
541}

References GNUNET_CONTAINER_HeapNode::element, GNUNET_assert, GNUNET_free, GNUNET_CONTAINER_HeapNode::heap, GNUNET_CONTAINER_HeapNode::index, HeapSlot::node, remove_slot(), GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.

Referenced by clean_channel(), clean_request(), delete_value(), destroy_route(), dht_lookup_timeout(), expire_oldest_entry(), free_channel_state(), free_destination_entry(), free_reassembly_context(), free_validation_state(), GCP_detach_path(), GNS_resolver_lookup_cancel(), GSF_plan_notify_request_done_(), handle_dht_response(), receiver_destroy(), remove_client_query_record(), and sender_destroy().

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

◆ GNUNET_CONTAINER_heap_update_cost()

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.

Parameters
nodenode for which the cost is to be changed
new_costnew cost for the node

Definition at line 545 of file container_heap.c.

547{
548 struct GNUNET_CONTAINER_Heap *heap = node->heap;
549 struct HeapSlot slot;
550
551 GNUNET_assert ( (node->index < heap->size) &&
552 (node == heap->slots[node->index].node) );
553 /* the node's own slot is the free slot to refill */
554 slot.key = new_cost ^ key_mask (heap);
555 slot.node = node;
556 reposition (heap,
557 node->index,
558 slot);
559 CHECK (heap);
560}
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.

References CHECK, GNUNET_assert, GNUNET_CONTAINER_HeapNode::heap, GNUNET_CONTAINER_HeapNode::index, HeapSlot::key, key_mask(), HeapSlot::node, reposition(), GNUNET_CONTAINER_Heap::size, and GNUNET_CONTAINER_Heap::slots.

Referenced by get_redirect_state(), handle_icmp_back(), handle_tcp_back(), handle_udp_back(), put_cb(), reschedule_receiver_timeout(), reschedule_sender_timeout(), reset_retry_backoff(), route_message(), route_packet(), update_iterator(), and update_next_challenge_time().

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