aboutsummaryrefslogtreecommitdiff
path: root/src/util/container_heap.c
blob: 1e7077c8003ea19d2a7fe134707971ba7ac2462d (plain) (blame)
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
/*
 This file is part of GNUnet.
 (C) 2008 Christian Grothoff (and other contributing authors)

 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 2, 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., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 */

/**
 * @author Nathan Evans
 * @file util/container_heap.c
 * @brief Implementation of heap operations
 */

#include "platform.h"
#include "gnunet_protocols.h"
#include "gnunet_util.h"
#include "gnunet_util_containers.h"

/*
 * Struct that is stored in hashmap, pointers to
 * locations in min_heap and max_heap.
 */
struct GNUNET_CONTAINER_heap_info
{
  struct GNUNET_CONTAINER_heap_node *min_loc;

  struct GNUNET_CONTAINER_heap_node *max_loc;

};

/*
 * Generic heap node structure, contains pointer to parent
 * left child, right child, and actual neighbor.
 */
struct GNUNET_CONTAINER_heap_node
{
  struct GNUNET_CONTAINER_heap_node *parent;

  struct GNUNET_CONTAINER_heap_node *left_child;

  struct GNUNET_CONTAINER_heap_node *right_child;

  GNUNET_CONTAINER_HeapCost cost;

  void *element;

};

struct GNUNET_CONTAINER_Heap
{
  unsigned int size;

  unsigned int max_size;

  enum type;

  struct GNUNET_CONTAINER_heap_node *root;

  struct GNUNET_CONTAINER_heap_node *traversal_pos;

};

void
internal_print (struct GNUNET_CONTAINER_heap_node *root)
{
  fprintf (stdout, "%d\n", root->cost);
  if (root->left_child != NULL)
    {
      fprintf (stdout, "LEFT of %d\n", root->cost);
      internal_print (root->left_child);
    }
  if (root->right_child != NULL)
    {
      fprintf (stdout, "RIGHT of %d\n", root->cost);
      internal_print (root->right_child);
    }
}

void
printTree (struct GNUNET_CONTAINER_Heap *root)
{
  internal_print (root->root);
}

struct GNUNET_CONTAINER_Heap *
GNUNET_CONTAINER_heap_create (enum type)
{
  struct GNUNET_CONTAINER_Heap *heap;
  heap = malloc (sizeof (struct GNUNET_CONTAINER_Heap));
  heap->max_size = -1;
  heap->type = type;
  heap->root = NULL;
  heap->traversal_pos = NULL;
  heap->size = 0;

  return heap;
}

void
GNUNET_CONTAINER_heap_destroy (struct GNUNET_CONTAINER_Heap *heap)
{
  void *unused;
  while (heap->size > 0)
    {
      unused = GNUNET_CONTAINER_heap_remove_root (heap);
    }

  GNUNET_free (heap);
  return;
}

struct GNUNET_CONTAINER_heap_node *
find_element (struct GNUNET_CONTAINER_heap_node *node, void *element)
{
  struct GNUNET_CONTAINER_heap_node *ret;
  ret = NULL;
  if ((node != NULL) && (node->element == element))
    {
      ret = node;
    }

  if ((ret == NULL) && (node->left_child != NULL))
    {
      ret = find_element (node->left_child, element);
    }

  if ((ret == NULL) && (node->right_child != NULL))
    {
      ret = find_element (node->right_child, element);
    }

  return ret;
}

static struct GNUNET_CONTAINER_heap_node *
getNextPos (struct GNUNET_CONTAINER_Heap *root)
{
  struct GNUNET_CONTAINER_heap_node *ret;
  struct GNUNET_CONTAINER_heap_node *parent;
  int pos;
  int depth;
  int i;

  ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_heap_node));
  pos = root->size + 1;
  depth = (int) log2 (pos);
  ret->left_child = NULL;
  ret->right_child = NULL;

  if (depth == 0)
    {
      ret->parent = NULL;
      root->root = ret;
    }
  else
    {
      parent = root->root;
      for (i = depth; i > 1; i--)
        {
          if (((pos / (1 << (i - 1))) % 2) == 0)
            parent = parent->left_child;
          else
            parent = parent->right_child;
        }

      ret->parent = parent;
      if ((pos % 2) == 0)
        parent->left_child = ret;
      else
        parent->right_child = ret;

    }

  return ret;

}

static struct GNUNET_CONTAINER_heap_node *
getPos (struct GNUNET_CONTAINER_Heap *root, unsigned int pos)
{
  struct GNUNET_CONTAINER_heap_node *ret;

  int depth;
  int i;

  depth = (int) log2 (pos);
  ret = NULL;
  if (pos > root->size)
    {
      return ret;
    }
  else
    {
      ret = root->root;
      for (i = depth; i > 0; i--)
        {
          if (((pos / (1 << (i - 1))) % 2) == 0)
            ret = ret->left_child;
          else
            ret = ret->right_child;
        }
    }

  return ret;

}

void
swapNodes (struct GNUNET_CONTAINER_heap_node *first,
           struct GNUNET_CONTAINER_heap_node *second,
           struct GNUNET_CONTAINER_Heap *root)
{
  void *temp_element;
  GNUNET_CONTAINER_HeapCost temp_cost;

  temp_element = first->element;
  temp_cost = first->cost;
  first->element = second->element;
  first->cost = second->cost;
  second->element = temp_element;
  second->cost = temp_cost;

/*
 * I still worry that there is some good reason for
 * elements being location aware... but it eludes me
 * for the moment...
  if ((root->type == GNUNET_DV_MAX_HEAP))
    {
      first->neighbor->max_loc = first;
      second->neighbor->max_loc = second;
    }
  else if ((root->type == GNUNET_DV_MIN_HEAP))
    {
      first->neighbor->min_loc = first;
      second->neighbor->min_loc = second;
    }
*/
  return;
}

void
percolateHeap (struct GNUNET_CONTAINER_heap_node *pos,
               struct GNUNET_CONTAINER_Heap *root)
{

  while ((pos->parent != NULL) &&
         (((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
           && (pos->parent->cost < pos->cost))
          || ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
              && (pos->parent->cost > pos->cost))))
    {
      swapNodes (pos, pos->parent, root);
      pos = pos->parent;
    }

  return;
}



void
percolateDownHeap (struct GNUNET_CONTAINER_heap_node *pos,
                   struct GNUNET_CONTAINER_Heap *root)
{
  struct GNUNET_CONTAINER_heap_node *switchNeighbor;

  switchNeighbor = pos;

  if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX))
    {
      if ((pos->left_child != NULL)
          && (pos->left_child->cost > switchNeighbor->cost))
        {
          switchNeighbor = pos->left_child;
        }

      if ((pos->right_child != NULL)
          && (pos->right_child->cost > switchNeighbor->cost))
        {
          switchNeighbor = pos->right_child;
        }
    }
  else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN))
    {
      if ((pos->left_child != NULL)
          && (pos->left_child->cost < switchNeighbor->cost))
        {
          switchNeighbor = pos->left_child;
        }

      if ((pos->right_child != NULL)
          && (pos->right_child->cost < switchNeighbor->cost))
        {
          switchNeighbor = pos->right_child;
        }
    }

  if (switchNeighbor != pos)
    {
      swapNodes (switchNeighbor, pos, root);
      percolateDownHeap (switchNeighbor, root);
    }

  return;
}

void *
GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *root,
                                   void *element)
{
  void *ret;
  struct GNUNET_CONTAINER_heap_node *del_node;
  struct GNUNET_CONTAINER_heap_node *last;
  GNUNET_CONTAINER_HeapCost old_cost;

  del_node = NULL;
  del_node = find_element (root->root, element);

  if (del_node == NULL)
    return NULL;

  ret = del_node->element;
  last = getPos (root, root->size);

  old_cost = del_node->cost;
  del_node->element = last->element;
  del_node->cost = last->cost;

  if (last->parent->left_child == last)
    last->parent->left_child = NULL;
  if (last->parent->right_child == last)
    last->parent->right_child = NULL;

  if (root->traversal_pos == last)
    {
      root->traversal_pos = root->root;
    }
  GNUNET_free (last);
  root->size--;

  if (del_node->cost > old_cost)
    {
      if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
        percolateHeap (del_node, root);
      else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
        percolateDownHeap (del_node, root);
    }
  else if (del_node->cost < old_cost)
    {
      if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
        percolateDownHeap (del_node, root);
      else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
        percolateHeap (del_node, root);
    }

  return ret;
}

int
GNUNET_CONTAINER_heap_insert (struct GNUNET_CONTAINER_Heap *root,
                              void *element, GNUNET_CONTAINER_HeapCost cost)
{
  struct GNUNET_CONTAINER_heap_node *new_pos;
  int ret;
  ret = GNUNET_YES;

  if (root->max_size > root->size)
    {
      new_pos = getNextPos (root);
      new_pos->element = element;
      new_pos->cost = cost;
      root->size++;
      /*We no longer can tolerate pointers between heaps :( */
      /*if (root->type == GNUNET_DV_MIN_HEAP)
         new_pos->neighbor->min_loc = new_pos;
         else if (root->type == GNUNET_DV_MAX_HEAP)
         new_pos->neighbor->max_loc = new_pos; */

      percolateHeap (new_pos, root);
    }
  else
    {
      ret = GNUNET_NO;
    }

  return ret;
}

void *
GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *root)
{
  void *ret;
  struct GNUNET_CONTAINER_heap_node *root_node;
  struct GNUNET_CONTAINER_heap_node *last;

  root_node = root->root;
  ret = root_node->element;
  last = getPos (root, root->size);

  if (last->parent->left_child == last)
    last->parent->left_child = NULL;
  else if (last->parent->right_child == last)
    last->parent->right_child = NULL;

  root_node->element = last->element;
  root_node->cost = last->cost;

  if (root->traversal_pos == last)
    {
      root->traversal_pos = root->root;
    }

  GNUNET_free (last);
  root->size--;
  percolateDownHeap (root->root, root);
  return ret;
}

static int
updatedCost (struct GNUNET_CONTAINER_Heap *root,
             struct GNUNET_CONTAINER_heap_node *node)
{
  struct GNUNET_CONTAINER_heap_node *parent;

  if (node == NULL)
    return GNUNET_SYSERR;

  parent = node->parent;

  if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX) && (parent != NULL)
      && (node->cost > parent->cost))
    percolateHeap (node, root);
  else if ((root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN) && (parent != NULL)
           && (node->cost < parent->cost))
    percolateHeap (node, root);
  else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MAX)
    percolateDownHeap (node, root);
  else if (root->type == GNUNET_CONTAINER_HEAP_ORDER_MIN)
    percolateDownHeap (node, root);

  return GNUNET_YES;
}


int
GNUNET_CONTAINER_heap_update_cost (struct GNUNET_CONTAINER_Heap *root,
                                   void *element,
                                   GNUNET_CONTAINER_HeapCost new_cost)
{
  struct GNUNET_CONTAINER_heap_node *node;
  int ret = GNUNET_YES;
  node = find_element (root->root, element);
  if (node == NULL)
    return GNUNET_NO;

  node->cost = new_cost;
  ret = updatedCost (root, node);
  return ret;
}

void
internal_iterator (struct GNUNET_CONTAINER_Heap *root,
                   struct GNUNET_CONTAINER_heap_node *node,
                   GNUNET_CONTAINER_HeapIterator iterator, void *cls)
{
  if (node == NULL)
    return;
  internal_iterator (root, node->left_child, iterator, cls);
  internal_iterator (root, node->right_child, iterator, cls);
  iterator (node->element, node->cost, root, cls);
}

int
GNUNET_CONTAINER_heap_iterate (struct GNUNET_CONTAINER_Heap *heap,
                               GNUNET_CONTAINER_HeapIterator iterator,
                               void *cls)
{
  internal_iterator (heap, heap->root, iterator, cls);
  return GNUNET_OK;
}

void *
GNUNET_CONTAINER_heap_walk_get_next (struct GNUNET_CONTAINER_Heap *root)
{
  unsigned int choice;
  void *element;

  if ((root->traversal_pos == NULL) && (root->root != NULL))
    {
      root->traversal_pos = root->root;
    }

  if (root->traversal_pos == NULL)
    return NULL;

  element = root->traversal_pos->element;

  choice = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 2);

  switch (choice)
    {
    case 1:
      root->traversal_pos = root->traversal_pos->right_child;
      break;
    case 0:
      root->traversal_pos = root->traversal_pos->left_child;
      break;
    }

  return element;

}

unsigned int
GNUNET_CONTAINER_heap_get_size (struct GNUNET_CONTAINER_Heap *heap)
{
  return heap->size;
}

/* end of heap.c */