6
6
* Portions Copyright (c) 2012-2023, PostgreSQL Global Development Group
7
7
*
8
8
* IDENTIFICATION
9
- * src/backend/lib /binaryheap.c
9
+ * src/common /binaryheap.c
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
13
13
14
+ #ifdef FRONTEND
15
+ #include "postgres_fe.h"
16
+ #else
14
17
#include "postgres.h"
18
+ #endif
15
19
16
20
#include <math.h>
17
21
22
+ #ifdef FRONTEND
23
+ #include "common/logging.h"
24
+ #endif
18
25
#include "lib/binaryheap.h"
19
26
20
27
static void sift_down (binaryheap * heap , int node_off );
@@ -34,7 +41,7 @@ binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
34
41
int sz ;
35
42
binaryheap * heap ;
36
43
37
- sz = offsetof(binaryheap , bh_nodes ) + sizeof (Datum ) * capacity ;
44
+ sz = offsetof(binaryheap , bh_nodes ) + sizeof (bh_node_type ) * capacity ;
38
45
heap = (binaryheap * ) palloc (sz );
39
46
heap -> bh_space = capacity ;
40
47
heap -> bh_compare = compare ;
@@ -106,10 +113,16 @@ parent_offset(int i)
106
113
* afterwards.
107
114
*/
108
115
void
109
- binaryheap_add_unordered (binaryheap * heap , Datum d )
116
+ binaryheap_add_unordered (binaryheap * heap , bh_node_type d )
110
117
{
111
118
if (heap -> bh_size >= heap -> bh_space )
119
+ {
120
+ #ifdef FRONTEND
121
+ pg_fatal ("out of binary heap slots" );
122
+ #else
112
123
elog (ERROR , "out of binary heap slots" );
124
+ #endif
125
+ }
113
126
heap -> bh_has_heap_property = false;
114
127
heap -> bh_nodes [heap -> bh_size ] = d ;
115
128
heap -> bh_size ++ ;
@@ -138,10 +151,16 @@ binaryheap_build(binaryheap *heap)
138
151
* the heap property.
139
152
*/
140
153
void
141
- binaryheap_add (binaryheap * heap , Datum d )
154
+ binaryheap_add (binaryheap * heap , bh_node_type d )
142
155
{
143
156
if (heap -> bh_size >= heap -> bh_space )
157
+ {
158
+ #ifdef FRONTEND
159
+ pg_fatal ("out of binary heap slots" );
160
+ #else
144
161
elog (ERROR , "out of binary heap slots" );
162
+ #endif
163
+ }
145
164
heap -> bh_nodes [heap -> bh_size ] = d ;
146
165
heap -> bh_size ++ ;
147
166
sift_up (heap , heap -> bh_size - 1 );
@@ -154,7 +173,7 @@ binaryheap_add(binaryheap *heap, Datum d)
154
173
* without modifying the heap. The caller must ensure that this
155
174
* routine is not used on an empty heap. Always O(1).
156
175
*/
157
- Datum
176
+ bh_node_type
158
177
binaryheap_first (binaryheap * heap )
159
178
{
160
179
Assert (!binaryheap_empty (heap ) && heap -> bh_has_heap_property );
@@ -169,10 +188,10 @@ binaryheap_first(binaryheap *heap)
169
188
* that this routine is not used on an empty heap. O(log n) worst
170
189
* case.
171
190
*/
172
- Datum
191
+ bh_node_type
173
192
binaryheap_remove_first (binaryheap * heap )
174
193
{
175
- Datum result ;
194
+ bh_node_type result ;
176
195
177
196
Assert (!binaryheap_empty (heap ) && heap -> bh_has_heap_property );
178
197
@@ -204,7 +223,7 @@ binaryheap_remove_first(binaryheap *heap)
204
223
* sifting the new node down.
205
224
*/
206
225
void
207
- binaryheap_replace_first (binaryheap * heap , Datum d )
226
+ binaryheap_replace_first (binaryheap * heap , bh_node_type d )
208
227
{
209
228
Assert (!binaryheap_empty (heap ) && heap -> bh_has_heap_property );
210
229
@@ -221,7 +240,7 @@ binaryheap_replace_first(binaryheap *heap, Datum d)
221
240
static void
222
241
sift_up (binaryheap * heap , int node_off )
223
242
{
224
- Datum node_val = heap -> bh_nodes [node_off ];
243
+ bh_node_type node_val = heap -> bh_nodes [node_off ];
225
244
226
245
/*
227
246
* Within the loop, the node_off'th array entry is a "hole" that
@@ -232,7 +251,7 @@ sift_up(binaryheap *heap, int node_off)
232
251
{
233
252
int cmp ;
234
253
int parent_off ;
235
- Datum parent_val ;
254
+ bh_node_type parent_val ;
236
255
237
256
/*
238
257
* If this node is smaller than its parent, the heap condition is
@@ -264,7 +283,7 @@ sift_up(binaryheap *heap, int node_off)
264
283
static void
265
284
sift_down (binaryheap * heap , int node_off )
266
285
{
267
- Datum node_val = heap -> bh_nodes [node_off ];
286
+ bh_node_type node_val = heap -> bh_nodes [node_off ];
268
287
269
288
/*
270
289
* Within the loop, the node_off'th array entry is a "hole" that
0 commit comments