1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
/*
This file is part of GNUnet.
Copyright (C) 2008, 2009 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/**
* @file util/container_heap.c
* @brief Implementation of a heap
* @author Nathan Evans
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_container_lib.h"
#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
#define EXTRA_CHECKS 0
/**
* Node in the heap.
*/
struct GNUNET_CONTAINER_HeapNode
{
/**
* Heap this node belongs to.
*/
struct GNUNET_CONTAINER_Heap *heap;
/**
* Parent node.
*/
struct GNUNET_CONTAINER_HeapNode *parent;
/**
* Left child.
*/
struct GNUNET_CONTAINER_HeapNode *left_child;
/**
* Right child.
*/
struct GNUNET_CONTAINER_HeapNode *right_child;
/**
* Our element.
*/
void *element;
/**
* Cost for this element.
*/
GNUNET_CONTAINER_HeapCostType cost;
/**
* Number of elements below this node in the heap
* (excluding this node itself).
*/
unsigned int tree_size;
};
/**
* Handle to a node in a heap.
*/
struct GNUNET_CONTAINER_Heap
{
/**
* Root of the heap.
*/
struct GNUNET_CONTAINER_HeapNode *root;
/**
* Current position of our random walk.
*/
struct GNUNET_CONTAINER_HeapNode *walk_pos;
/**
* Number of elements in the heap.
*/
unsigned int size;
/**
* How is the heap sorted?
*/
enum GNUNET_CONTAINER_HeapOrder order;
};
#if EXTRA_CHECKS
/**
* Check if internal invariants hold for the given node.
*
* @param node subtree to check
*/
static void
check (const struct GNUNET_CONTAINER_HeapNode *node)
{
if (NULL == node)
return;
GNUNET_assert (node->tree_size ==
((node->left_child ==
NULL) ? 0 : 1 + node->left_child->tree_size) +
((node->right_child ==
NULL) ? 0 : 1 + node->right_child->tree_size));
check (node->left_child);
check (node->right_child);
}
#define CHECK(n) check(n)
#else
#define CHECK(n) do {} while (0)
#endif
/**
* Create a new heap.
*
* @param order how should the heap be sorted?
* @return handle to the heap
*/
struct GNUNET_CONTAINER_Heap *
GNUNET_CONTAINER_heap_create (enum GNUNET_CONTAINER_HeapOrder order)
{
struct GNUNET_CONTAINER_Heap *heap;
heap = GNUNET_new (struct GNUNET_CONTAINER_Heap);
heap->order = order;
return heap;
}
/**
* Destroys the heap. Only call on a heap that
* is already empty.
*
* @param heap heap to destroy
*/
void
GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
{
GNUNET_break (heap->size == 0);
GNUNET_free (heap);
}
/**
* Get element stored at the root of @a heap.
*
* @param heap Heap to inspect.
* @return Element at the root, or NULL if heap is empty.
*/
void *
GNUNET_CONTAINER_heap_peek (const struct GNUNET_CONTAINER_Heap *heap)
{
if (heap->root == NULL)
return NULL;
return heap->root->element;
}
/**
* Get @a element and @a cost stored at the root of @a heap.
*
* @param[in] heap Heap to inspect.
* @param[out] element Root element is returned here.
* @param[out] cost Cost of @a element is returned here.
* @return #GNUNET_YES if an element is returned,
* #GNUNET_NO if the heap is empty.
*/
int
GNUNET_CONTAINER_heap_peek2 (const struct GNUNET_CONTAINER_Heap *heap,
void **element,
GNUNET_CONTAINER_HeapCostType *cost)
{
if (NULL == heap->root)
return GNUNET_NO;
if (NULL != element)
*element = heap->root->element;
if (NULL != cost)
*cost = heap->root->cost;
return GNUNET_YES;
}
/**
* Get the current size of the heap
*
* @param heap the heap to get the size of
* @return number of elements stored
*/
unsigned int
GNUNET_CONTAINER_heap_get_size (const struct GNUNET_CONTAINER_Heap *heap)
{
return heap->size;
}
/**
* Get the current cost of the node
*
* @param node the node to get the cost of
* @return cost of the node
*/
GNUNET_CONTAINER_HeapCostType
GNUNET_CONTAINER_heap_node_get_cost (const struct GNUNET_CONTAINER_HeapNode
*node)
{
return node->cost;
}
/**
* Iterate over the children of the given node.
*
* @param heap argument to give to iterator
* @param node node to iterate over
* @param iterator function to call on each node
* @param iterator_cls closure for iterator
* @return GNUNET_YES to continue to iterate
*/
static int
node_iterator (const struct GNUNET_CONTAINER_Heap *heap,
struct GNUNET_CONTAINER_HeapNode *node,
GNUNET_CONTAINER_HeapIterator iterator, void *iterator_cls)
{
if (node == NULL)
return GNUNET_YES;
if (GNUNET_YES !=
node_iterator (heap, node->left_child, iterator, iterator_cls))
return GNUNET_NO;
if (GNUNET_YES !=
node_iterator (heap, node->right_child, iterator, iterator_cls))
return GNUNET_NO;
return iterator (iterator_cls, node, node->element, node->cost);
}
/**
* Iterate over all entries in the heap.
*
* @param heap the heap
* @param iterator function to call on each entry
* @param iterator_cls closure for iterator
*/
void
GNUNET_CONTAINER_heap_iterate (const struct GNUNET_CONTAINER_Heap *heap,
GNUNET_CONTAINER_HeapIterator iterator,
void *iterator_cls)
{
(void) node_iterator (heap, heap->root, iterator, iterator_cls);
}
/**
* 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.
*
* @param heap heap to walk
* @return data stored at the next random node in the walk;
* NULL if the tree is empty.
*/
void *
GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *heap)
{
struct GNUNET_CONTAINER_HeapNode *pos;
void *element;
if (heap->root == NULL)
return NULL;
pos = heap->walk_pos;
if (pos == NULL)
pos = heap->root;
element = pos->element;
heap->walk_pos =
(0 ==
GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2)) ? pos->right_child : pos->left_child;
return element;
}
/**
* Insert the given node 'node' into the subtree starting
* at 'pos' (while keeping the tree somewhat balanced).
*
* @param heap heap to modify
* @param pos existing tree
* @param node node to insert (which may be a subtree itself)
*/
static void
insert_node (struct GNUNET_CONTAINER_Heap *heap,
struct GNUNET_CONTAINER_HeapNode *pos,
struct GNUNET_CONTAINER_HeapNode *node)
{
struct GNUNET_CONTAINER_HeapNode *parent;
GNUNET_assert (node->parent == NULL);
while ((heap->order == GNUNET_CONTAINER_HEAP_ORDER_MAX) ? (pos->cost >=
node->cost)
: (pos->cost <= node->cost))
{
/* node is descendent of pos */
pos->tree_size += (1 + node->tree_size);
if (pos->left_child == NULL)
{
pos->left_child = node;
node->parent = pos;
return;
}
if (pos->right_child == NULL)
{
pos->right_child = node;
node->parent = pos;
return;
}
/* keep it balanced by descending into smaller subtree */
if (pos->left_child->tree_size < pos->right_child->tree_size)
pos = pos->left_child;
else
pos = pos->right_child;
}
/* make 'node' parent of 'pos' */
parent = pos->parent;
pos->parent = NULL;
node->parent = parent;
if (NULL == parent)
{
heap->root = node;
}
else
{
if (parent->left_child == pos)
parent->left_child = node;
else
parent->right_child = node;
}
/* insert 'pos' below 'node' */
insert_node (heap, node, pos);
CHECK (pos);
}
/**
* Inserts a new element into the heap.
*
* @param heap heap to modify
* @param element element to insert
* @param cost cost for the element
* @return node for the new element
*/
struct GNUNET_CONTAINER_HeapNode *
GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *heap, void *element,
GNUNET_CONTAINER_HeapCostType cost)
{
struct GNUNET_CONTAINER_HeapNode *node;
node = GNUNET_new (struct GNUNET_CONTAINER_HeapNode);
node->heap = heap;
node->element = element;
node->cost = cost;
heap->size++;
if (NULL == heap->root)
heap->root = node;
else
insert_node (heap, heap->root, node);
GNUNET_assert (heap->size == heap->root->tree_size + 1);
CHECK (heap->root);
return node;
}
/**
* Remove root of the heap.
*
* @param heap heap to modify
* @return element data stored at the root node, NULL if heap is empty
*/
void *
GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *heap)
{
void *ret;
struct GNUNET_CONTAINER_HeapNode *root;
if (NULL == (root = heap->root))
return NULL;
heap->size--;
ret = root->element;
if (root->left_child == NULL)
{
heap->root = root->right_child;
if (root->right_child != NULL)
root->right_child->parent = NULL;
}
else if (root->right_child == NULL)
{
heap->root = root->left_child;
root->left_child->parent = NULL;
}
else
{
root->left_child->parent = NULL;
root->right_child->parent = NULL;
heap->root = root->left_child;
insert_node (heap, heap->root, root->right_child);
}
if (heap->walk_pos == root)
heap->walk_pos = heap->root;
GNUNET_free (root);
#if EXTRA_CHECKS
GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
(heap->size == heap->root->tree_size + 1));
CHECK (heap->root);
#endif
return ret;
}
/**
* Remove the given node 'node' from the tree and update
* the 'tree_size' fields accordingly. Preserves the
* children of 'node' and does NOT change the overall
* 'size' field of the tree.
*/
static void
remove_node (struct GNUNET_CONTAINER_HeapNode *node)
{
struct GNUNET_CONTAINER_HeapNode *ancestor;
struct GNUNET_CONTAINER_Heap *heap = node->heap;
/* update 'size' of the ancestors */
ancestor = node;
while (NULL != (ancestor = ancestor->parent))
ancestor->tree_size--;
/* update 'size' of node itself */
if (node->left_child != NULL)
node->tree_size -= (1 + node->left_child->tree_size);
if (node->right_child != NULL)
node->tree_size -= (1 + node->right_child->tree_size);
/* unlink 'node' itself and insert children in its place */
if (node->parent == NULL)
{
if (node->left_child != NULL)
{
heap->root = node->left_child;
node->left_child->parent = NULL;
if (node->right_child != NULL)
{
node->right_child->parent = NULL;
insert_node (heap, heap->root, node->right_child);
}
}
else
{
heap->root = node->right_child;
if (node->right_child != NULL)
node->right_child->parent = NULL;
}
}
else
{
if (node->parent->left_child == node)
node->parent->left_child = NULL;
else
node->parent->right_child = NULL;
if (node->left_child != NULL)
{
node->left_child->parent = NULL;
node->parent->tree_size -= (1 + node->left_child->tree_size);
insert_node (heap, node->parent, node->left_child);
}
if (node->right_child != NULL)
{
node->right_child->parent = NULL;
node->parent->tree_size -= (1 + node->right_child->tree_size);
insert_node (heap, node->parent, node->right_child);
}
}
node->parent = NULL;
node->left_child = NULL;
node->right_child = NULL;
GNUNET_assert (node->tree_size == 0);
CHECK (heap->root);
}
/**
* Removes a node from the heap.
*
* @param node node to remove
* @return element data stored at the node
*/
void *
GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_HeapNode *node)
{
void *ret;
struct GNUNET_CONTAINER_Heap *heap;
heap = node->heap;
CHECK (heap->root);
if (heap->walk_pos == node)
(void) GNUNET_CONTAINER_heap_walk_get_next (heap);
remove_node (node);
heap->size--;
ret = node->element;
if (heap->walk_pos == node)
heap->walk_pos = NULL;
GNUNET_free (node);
#if EXTRA_CHECKS
CHECK (heap->root);
GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
(heap->size == heap->root->tree_size + 1));
#endif
return ret;
}
/**
* Updates the cost of any node in the tree
*
* @param heap heap to modify
* @param node node for which the cost is to be changed
* @param new_cost new cost for the node
*/
void
GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *heap,
struct GNUNET_CONTAINER_HeapNode *node,
GNUNET_CONTAINER_HeapCostType new_cost)
{
#if EXTRA_CHECKS
GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
(heap->size == heap->root->tree_size + 1));
CHECK (heap->root);
#endif
remove_node (node);
#if EXTRA_CHECKS
CHECK (heap->root);
GNUNET_assert (((heap->size == 1) && (heap->root == NULL)) ||
(heap->size == heap->root->tree_size + 2));
#endif
node->cost = new_cost;
if (heap->root == NULL)
heap->root = node;
else
insert_node (heap, heap->root, node);
#if EXTRA_CHECKS
CHECK (heap->root);
GNUNET_assert (((heap->size == 0) && (heap->root == NULL)) ||
(heap->size == heap->root->tree_size + 1));
#endif
}
/* end of heap.c */
|