Debian Codegraph @

packages/seacas/libraries/chaco/connect/heap.c

raw
1 /*
2 * Copyright(C) 1999-2020 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8 #include "structs.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #define left(i) (2 * (i))
13 #define right(i) (2 * (i) + 1)
14 #define parent(i) ((int)((i) / 2))
15
16 /* NOTE: heap assumes indices are 1-based. */
17
18 /* Note further: All the map arguments and manipulations allow me to */
19 /* quickly find a particular tag value. This assumes that tags */
20 /* are integers between 0 and nvals. */
21
22 void heapify(struct heap *heap, /* array of vals/tag to make into heap */
23 int index, /* root of subtree to heapify */
24 int nvals, /* number of values in array */
25 int *map /* maps from tag values to heap indices */
26 )
27 {
28 double swap_val; /* temporary storage for swapping values */
29 double swap_tag; /* temporary storage for swapping values */
30 int l, r; /* indices of left & right children */
31 int largest; /* index of largest value */
32
33 l = left(index);
34 r = right(index);
35
36 if (l <= nvals && heap[l].val > heap[index].val) {
37 largest = l;
38 }
39 else {
40 largest = index;
41 }
42
43 if (r <= nvals && heap[r].val > heap[largest].val) {
44 largest = r;
45 }
46
47 if (largest != index) { /* swap index with largest and recurse */
48 swap_val = heap[index].val;
49 swap_tag = heap[index].tag;
50
51 heap[index].val = heap[largest].val;
52 heap[index].tag = heap[largest].tag;
53
54 heap[largest].val = swap_val;
55 heap[largest].tag = swap_tag;
56
57 if (map != NULL) { /* update pointers to heap tags */
58 map[heap[index].tag] = index;
59 map[heap[largest].tag] = largest;
60 }
61
62 heapify(heap, largest, nvals, map);
63 }
64 }
65
66 /* Construct a heap from an unordered set of values. */
67 void heap_build(struct heap *heap, /* array of vals/tag to make into heap */
68 int nvals, /* number of values in array */
69 int *map /* maps from tag values to heap indices */
70 )
71 {
72 int i; /* loop counter */
73
74 for (i = nvals / 2; i; i--) {
75 heapify(heap, i, nvals, (int *)NULL);
76 }
77
78 if (map != NULL) {
79 for (i = 1; i <= nvals; i++) {
80 map[heap[i].tag] = i;
81 }
82 }
83 }
84
85 double heap_extract_max(struct heap *heap, /* array of vals/tag in a heap */
86 int nvals, /* number of values in array */
87 int *ptag, /* tag associated with return value */
88 int *map /* maps from tag values to heap indices */
89 )
90 {
91 double maxval; /* return value */
92
93 if (nvals < 1) {
94 printf("Heap underflow\n");
95 exit(0);
96 }
97
98 if (map != NULL) { /* turn off map value for extracted tag */
99 map[heap[1].tag] = 0;
100 }
101
102 maxval = heap[1].val;
103 *ptag = heap[1].tag;
104
105 heap[1].val = heap[nvals].val;
106 heap[1].tag = heap[nvals].tag;
107
108 if (map != NULL) { /* update map value for root */
109 map[heap[1].tag] = 1;
110 }
111
112 heapify(heap, 1, nvals - 1, map);
113
114 return (maxval);
115 }
116
117 void heap_update_val(struct heap *heap, /* array of vals/tag in a heap */
118 int index, /* index of value to update */
119 double newval, /* new value to insert */
120 int *map /* maps from tag values to heap indices */
121 )
122 {
123 int tag; /* tag value associated with updated val */
124 int dad; /* parent of a tree node */
125
126 tag = heap[index].tag;
127
128 dad = parent(index);
129 while (index > 1 && heap[dad].val < newval) {
130 heap[index].val = heap[dad].val;
131 heap[index].tag = heap[dad].tag;
132 if (map != NULL) {
133 map[heap[index].tag] = index;
134 }
135 index = dad;
136 dad = parent(index);
137 }
138
139 heap[index].val = newval;
140 heap[index].tag = tag;
141 if (map != NULL) {
142 map[tag] = index;
143 }
144 }