@@ -30,25 +30,24 @@ static void sift_up(binaryheap *heap, int node_off);
30
30
/*
31
31
* binaryheap_allocate
32
32
*
33
- * Returns a pointer to a newly-allocated heap that has the capacity to
34
- * store the given number of nodes, with the heap property defined by
35
- * the given comparator function, which will be invoked with the additional
36
- * argument specified by 'arg'.
33
+ * Returns a pointer to a newly-allocated heap with the given initial number
34
+ * of nodes, and with the heap property defined by the given comparator
35
+ * function, which will be invoked with the additional argument specified by
36
+ * 'arg'.
37
37
*/
38
38
binaryheap *
39
- binaryheap_allocate (int capacity , binaryheap_comparator compare , void * arg )
39
+ binaryheap_allocate (int num_nodes , binaryheap_comparator compare , void * arg )
40
40
{
41
- int sz ;
42
41
binaryheap * heap ;
43
42
44
- sz = offsetof(binaryheap , bh_nodes ) + sizeof (bh_node_type ) * capacity ;
45
- heap = (binaryheap * ) palloc (sz );
46
- heap -> bh_space = capacity ;
43
+ heap = (binaryheap * ) palloc (sizeof (binaryheap ));
44
+ heap -> bh_space = num_nodes ;
47
45
heap -> bh_compare = compare ;
48
46
heap -> bh_arg = arg ;
49
47
50
48
heap -> bh_size = 0 ;
51
49
heap -> bh_has_heap_property = true;
50
+ heap -> bh_nodes = (bh_node_type * ) palloc (sizeof (bh_node_type ) * num_nodes );
52
51
53
52
return heap ;
54
53
}
@@ -74,6 +73,7 @@ binaryheap_reset(binaryheap *heap)
74
73
void
75
74
binaryheap_free (binaryheap * heap )
76
75
{
76
+ pfree (heap -> bh_nodes );
77
77
pfree (heap );
78
78
}
79
79
@@ -104,6 +104,17 @@ parent_offset(int i)
104
104
return (i - 1 ) / 2 ;
105
105
}
106
106
107
+ /*
108
+ * Double the space allocated for nodes.
109
+ */
110
+ static void
111
+ enlarge_node_array (binaryheap * heap )
112
+ {
113
+ heap -> bh_space *= 2 ;
114
+ heap -> bh_nodes = repalloc (heap -> bh_nodes ,
115
+ sizeof (bh_node_type ) * heap -> bh_space );
116
+ }
117
+
107
118
/*
108
119
* binaryheap_add_unordered
109
120
*
@@ -115,14 +126,10 @@ parent_offset(int i)
115
126
void
116
127
binaryheap_add_unordered (binaryheap * heap , bh_node_type d )
117
128
{
129
+ /* make sure enough space for a new node */
118
130
if (heap -> bh_size >= heap -> bh_space )
119
- {
120
- #ifdef FRONTEND
121
- pg_fatal ("out of binary heap slots" );
122
- #else
123
- elog (ERROR , "out of binary heap slots" );
124
- #endif
125
- }
131
+ enlarge_node_array (heap );
132
+
126
133
heap -> bh_has_heap_property = false;
127
134
heap -> bh_nodes [heap -> bh_size ] = d ;
128
135
heap -> bh_size ++ ;
@@ -153,14 +160,10 @@ binaryheap_build(binaryheap *heap)
153
160
void
154
161
binaryheap_add (binaryheap * heap , bh_node_type d )
155
162
{
163
+ /* make sure enough space for a new node */
156
164
if (heap -> bh_size >= heap -> bh_space )
157
- {
158
- #ifdef FRONTEND
159
- pg_fatal ("out of binary heap slots" );
160
- #else
161
- elog (ERROR , "out of binary heap slots" );
162
- #endif
163
- }
165
+ enlarge_node_array (heap );
166
+
164
167
heap -> bh_nodes [heap -> bh_size ] = d ;
165
168
heap -> bh_size ++ ;
166
169
sift_up (heap , heap -> bh_size - 1 );
0 commit comments