20 #include <lal/LALHeap.h>
22 #define LEFT(i) (2*(i) + 1)
23 #define RIGHT(i) (2*(i) + 2)
24 #define PARENT(i) (((i) - 1)/2)
27 #define SWAP(x, y) do { void *z = (x); (x) = (y); (y) = z; } while (0)
30 #define UNORDERED(h, x, y) ((h)->cmp((h)->cmp_param, (x), (y)) * (h)->min_or_max_heap > 0)
57 int new_len = ( h->n > 0 ) ? 2*h->n : 1;
58 h->data =
XLALRealloc( h->data, new_len *
sizeof( h->data[0] ) );
60 h->data_len = new_len;
68 while ( i > 0 &&
UNORDERED( h, h->data[i], h->data[
p] ) ) {
69 SWAP( h->data[i], h->data[
p] );
79 int j = -1,
r =
RIGHT( i );
80 if ( r < h->n &&
UNORDERED( h, h->data[
r], h->data[i] ) ) {
82 if (
UNORDERED( h, h->data[l], h->data[
r] ) ) {
89 if ( l < h->n &&
UNORDERED( h, h->data[l], h->data[i] ) ) {
94 SWAP( h->data[i], h->data[j] );
111 SWAP( h->data[0], *
x );
128 if ( h->n + 1 > h->data_len ) {
133 h->data[h->n++] = *
x;
182 h->max_size = max_size;
183 h->min_or_max_heap = min_or_max_heap;
185 h->cmp_param = cmp_param;
197 if ( h->data != NULL ) {
198 if ( h->dtor != NULL ) {
199 for (
int i = 0; i < h->n; ++i ) {
200 h->dtor( h->data[i] );
218 if ( h->data != NULL ) {
219 if ( h->dtor != NULL ) {
220 for (
int i = 0; i < h->n; ++i ) {
221 h->dtor( h->data[i] );
256 return h->max_size > 0 && h->n == h->max_size;
264 return ( h->n > 0 ) ? h->data[0] : NULL;
278 while ( max_size > 0 && h->n > max_size ) {
283 h->max_size = max_size;
301 return ( h->add )( h,
x );
315 void *
x = h->data[0];
318 h->data[0] = h->data[--h->n];
322 if ( 3*h->n < h->data_len ) {
347 if ( h->dtor != NULL ) {
367 SWAP( h->data[0], *
x );
390 for (
int i = 0; i < h->n; ++i ) {
391 void *
x = h->data[i];
396 while ( h2->n > 0 ) {
425 for (
int i = 0; i < h->n; ++i ) {
426 void *
x = h->data[i];
435 while ( h2->n > 0 ) {
459 const void ***
elem = (
const void *** ) param;
474 const void **elems =
XLALMalloc( h->n *
sizeof( *elems ) );
478 const void **
elem = elems;
static int heap_add_full(LALHeap *h, void **x)
static int heap_get_elems_visitor(void *param, const void *x)
static int heap_resize(LALHeap *h)
int(* LALHeapAddFcn)(LALHeap *h, void **x)
static int heap_add_not_full(LALHeap *h, void **x)
static int heap_no_param_cmp(void *param, const void *x, const void *y)
#define UNORDERED(h, x, y)
static void heap_trickle_down(LALHeap *h, int i)
static void heap_bubble_up(LALHeap *h, int i)
static int cmp(REAL4Sequence *a, REAL4Sequence *b)
const void ** XLALHeapElements(const LALHeap *h)
Allocate and return an array containing all elements in the heap.
void(* LALHeapDtorFcn)(void *x)
Function which free memory associated with heap element x
const void * XLALHeapRoot(const LALHeap *h)
Return the root element of a heap.
int XLALHeapResize(LALHeap *h, int max_size)
Change the maximum size of a heap; if the heap is contracted, excess elements are destroyed.
int(* LALHeapCmpFcn)(const void *x, const void *y)
Function which compares heap elements x and y
int XLALHeapVisit(const LALHeap *h, LALHeapVisitFcn visit, void *visit_param)
Visit each element in the heap in the order given by the comparison function.
int XLALHeapAdd(LALHeap *h, void **x)
Add a new element to a heap; if the heap is of fixed size and full, the root element is removed.
int(* LALHeapVisitFcn)(void *param, const void *x)
Function to call when visiting heap element x, with a parameter param.
void * XLALHeapExtractRoot(LALHeap *h)
Remove the root element of a heap.
int(* LALHeapCmpParamFcn)(void *param, const void *x, const void *y)
Function which compares heap elements x and y, with a parameter param.
int XLALHeapModify(LALHeap *h, LALHeapModifyFcn modify, void *modify_param)
Visit (and possibly modify) each element in the heap in the order given by the comparison function.
int XLALHeapSize(const LALHeap *h)
Return the size of a heap.
int XLALHeapExchangeRoot(LALHeap *h, void **x)
Exchange the root element of a non-empty heap with the new element in *x
int XLALHeapRemoveRoot(LALHeap *h)
Remove and destroy the root element of a heap.
int(* LALHeapModifyFcn)(void *param, void *x)
Function to call when visiting (and possibly modify) heap element x, with a parameter param.
int XLALHeapIsFull(const LALHeap *h)
Return >0 if a limited-size heap is full, 0 otherwise (or if heap has unlimited size),...
LALHeap * XLALHeapCreate2(LALHeapDtorFcn dtor, int max_size, int min_or_max_heap, LALHeapCmpParamFcn cmp, void *cmp_param)
Create a heap with a parameterised comparison function.
int XLALHeapClear(LALHeap *h)
Clear a heap.
LALHeap * XLALHeapCreate(LALHeapDtorFcn dtor, int max_size, int min_or_max_heap, LALHeapCmpFcn cmp)
Create a heap.
void XLALHeapDestroy(LALHeap *h)
Destroy a heap and its elements.
int XLALHeapMaxSize(const LALHeap *h)
Return the maximum size of a heap.
#define XLALRealloc(p, n)
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
#define XLAL_CHECK_NULL(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns a pointe...
@ XLAL_ENOMEM
Memory allocation error.
@ XLAL_SUCCESS
Success return value (not an error number)
@ XLAL_EFAULT
Invalid pointer.
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
@ XLAL_EINVAL
Invalid argument.
@ XLAL_EFAILED
Generic failure.
Generic heap with elements of type void *