GNUnet 0.29.1-21-g8c89d4373
 
Loading...
Searching...
No Matches
container_heap.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2008, 2009, 2026 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
45#include "platform.h"
46#include "gnunet_util_lib.h"
47
48
54#define ARITY 4
55
60#define NO_WALK UINT_MAX
61
68#define MIN_CAPACITY 8
69
74#ifndef EXTRA_CHECKS
75#define EXTRA_CHECKS 0
76#endif
77
78
83{
88
92 void *element;
93
97 unsigned int index;
98};
99
100
118
119
124{
129
133 unsigned int size;
134
138 unsigned int capacity;
139
144 unsigned int walk_pos;
145
150};
151
152
164key_mask (const struct GNUNET_CONTAINER_Heap *heap)
165{
167 return ~((GNUNET_CONTAINER_HeapCostType) 0);
168 return 0;
169}
170
171
172#if EXTRA_CHECKS
179static void
180check (const struct GNUNET_CONTAINER_Heap *heap)
181{
182 GNUNET_assert (heap->size <= heap->capacity);
184 for (unsigned int i = 0; i < heap->size; i++)
185 {
186 GNUNET_assert (heap == heap->slots[i].node->heap);
187 GNUNET_assert (i == heap->slots[i].node->index);
188 if (i > 0)
189 GNUNET_assert (heap->slots[(i - 1) / ARITY].key <= heap->slots[i].key);
190 }
191}
192
193
194#define CHECK(h) check (h)
195#else
196#define CHECK(h) do {} while (0)
197#endif
198
199
207static void
209 unsigned int index,
210 struct HeapSlot slot)
211{
212 heap->slots[index] = slot;
213 slot.node->index = index;
214}
215
216
226static void
228 unsigned int index,
229 struct HeapSlot slot)
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}
242
243
254static void
256 unsigned int index,
257 struct HeapSlot slot)
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}
283
284
293static void
295 unsigned int index,
296 struct HeapSlot slot)
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}
304
305
318static void
320 unsigned int capacity)
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}
329
330
343static void
345 unsigned int index)
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}
361
362
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}
376
377
378void
380{
381 GNUNET_break (0 == heap->size);
382 GNUNET_free (heap->slots);
383 GNUNET_free (heap);
384}
385
386
387void *
389{
390 if (0 == heap->size)
391 return NULL;
392 return heap->slots[0].node->element;
393}
394
395
407 void **element,
409{
410 if (0 == heap->size)
411 return GNUNET_NO;
412 if (NULL != element)
413 *element = heap->slots[0].node->element;
414 if (NULL != cost)
415 *cost = heap->slots[0].key ^ key_mask (heap);
416 return GNUNET_YES;
417}
418
419
420unsigned int
422{
423 return heap->size;
424}
425
426
429 *node)
430{
431 const struct GNUNET_CONTAINER_Heap *heap = node->heap;
432
433 return heap->slots[node->index].key ^ key_mask (heap);
434}
435
436
437void
440 void *iterator_cls)
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}
457
458
459void *
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}
479
480
483 void *element,
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}
512
513
514void *
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}
528
529
530void *
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}
542
543
544void
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}
561
562
563/* end of container_heap.c */
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.
#define CHECK(h)
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).
Definition gnunet-arm.c:33
static pid_t child
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_MIN(a, b)
@ GNUNET_YES
@ GNUNET_NO
#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".
Definition peer.c:68
#define SIZE_MAX
Definition platform.h:213
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.
Handle to a heap.
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.