|
|
|
|
/*
|
|
|
|
|
* binaryheap.h
|
|
|
|
|
*
|
|
|
|
|
* A simple binary heap implementation
|
|
|
|
|
*
|
|
|
|
|
* Portions Copyright (c) 2012-2024, PostgreSQL Global Development Group
|
|
|
|
|
*
|
|
|
|
|
* src/include/lib/binaryheap.h
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef BINARYHEAP_H
|
|
|
|
|
#define BINARYHEAP_H
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We provide a Datum-based API for backend code and a void *-based API for
|
|
|
|
|
* frontend code (since the Datum definitions are not available to frontend
|
|
|
|
|
* code). You should typically avoid using bh_node_type directly and instead
|
|
|
|
|
* use Datum or void * as appropriate.
|
|
|
|
|
*/
|
|
|
|
|
#ifdef FRONTEND
|
|
|
|
|
typedef void *bh_node_type;
|
|
|
|
|
#else
|
|
|
|
|
typedef Datum bh_node_type;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* For a max-heap, the comparator must return <0 iff a < b, 0 iff a == b,
|
|
|
|
|
* and >0 iff a > b. For a min-heap, the conditions are reversed.
|
|
|
|
|
*/
|
|
|
|
|
typedef int (*binaryheap_comparator) (bh_node_type a, bh_node_type b, void *arg);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* binaryheap
|
|
|
|
|
*
|
|
|
|
|
* bh_size how many nodes are currently in "nodes"
|
|
|
|
|
* bh_space how many nodes can be stored in "nodes"
|
|
|
|
|
* bh_has_heap_property no unordered operations since last heap build
|
|
|
|
|
* bh_compare comparison function to define the heap property
|
|
|
|
|
* bh_arg user data for comparison function
|
|
|
|
|
* bh_nodes variable-length array of "space" nodes
|
|
|
|
|
*/
|
|
|
|
|
typedef struct binaryheap
|
|
|
|
|
{
|
|
|
|
|
int bh_size;
|
|
|
|
|
int bh_space;
|
|
|
|
|
bool bh_has_heap_property; /* debugging cross-check */
|
|
|
|
|
binaryheap_comparator bh_compare;
|
|
|
|
|
void *bh_arg;
|
|
|
|
|
bh_node_type bh_nodes[FLEXIBLE_ARRAY_MEMBER];
|
|
|
|
|
} binaryheap;
|
|
|
|
|
|
|
|
|
|
extern binaryheap *binaryheap_allocate(int capacity,
|
|
|
|
|
binaryheap_comparator compare,
|
|
|
|
|
void *arg);
|
|
|
|
|
extern void binaryheap_reset(binaryheap *heap);
|
|
|
|
|
extern void binaryheap_free(binaryheap *heap);
|
|
|
|
|
extern void binaryheap_add_unordered(binaryheap *heap, bh_node_type d);
|
|
|
|
|
extern void binaryheap_build(binaryheap *heap);
|
|
|
|
|
extern void binaryheap_add(binaryheap *heap, bh_node_type d);
|
|
|
|
|
extern bh_node_type binaryheap_first(binaryheap *heap);
|
|
|
|
|
extern bh_node_type binaryheap_remove_first(binaryheap *heap);
|
|
|
|
|
extern void binaryheap_remove_node(binaryheap *heap, int n);
|
|
|
|
|
extern void binaryheap_replace_first(binaryheap *heap, bh_node_type d);
|
|
|
|
|
|
|
|
|
|
#define binaryheap_empty(h) ((h)->bh_size == 0)
|
|
|
|
|
#define binaryheap_size(h) ((h)->bh_size)
|
|
|
|
|
#define binaryheap_get_node(h, n) ((h)->bh_nodes[n])
|
|
|
|
|
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
9 years ago
|
|
|
#endif /* BINARYHEAP_H */
|