LAL  7.5.0.1-ec27e42
LALHeap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Karl Wette
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with with program; see the file COPYING. If not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  */
19 
20 #ifndef _LALHEAP_H
21 #define _LALHEAP_H
22 
23 #include <lal/LALStdlib.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30  * \defgroup LALHeap_h Header LALHeap.h
31  * \ingroup lal_utilities
32  * \author Karl Wette
33  * \brief Implementation of a generic heap, following Chapter 10.1 of \cite open-data-structs .
34  */
35 /** @{ */
36 
37 /**
38  * Generic heap with elements of type <tt>void *</tt>
39  */
40 typedef struct tagLALHeap LALHeap;
41 
42 /**
43  * Function which free memory associated with heap element <tt>x</tt>
44  */
45 typedef void ( *LALHeapDtorFcn )( void *x );
46 
47 /**
48  * Function which compares heap elements <tt>x</tt> and <tt>y</tt>
49  */
50 typedef int ( *LALHeapCmpFcn )( const void *x, const void *y );
51 
52 /**
53  * Function which compares heap elements <tt>x</tt> and <tt>y</tt>, with a parameter \c param
54  */
55 typedef int ( *LALHeapCmpParamFcn )( void *param, const void *x, const void *y );
56 
57 /**
58  * Function to call when visiting heap element <tt>x</tt>, with a parameter \c param.
59  * Return XLAL_SUCCESS if successful, or XLAL_FAILURE otherwise.
60  */
61 typedef int ( *LALHeapVisitFcn )( void *param, const void *x );
62 
63 /**
64  * Function to call when visiting (and possibly modify) heap element <tt>x</tt>, with a parameter \c param.
65  * Return XLAL_SUCCESS if successful, or XLAL_FAILURE otherwise.
66  */
67 typedef int ( *LALHeapModifyFcn )( void *param, void *x );
68 
69 /**
70  * Create a heap
71  */
72 LALHeap *XLALHeapCreate(
73  LALHeapDtorFcn dtor, /**< [in] Heap element destructor function, if required */
74  int max_size, /**< [in] Maximum size of the heap; if zero, heap has unlimited size */
75  int min_or_max_heap, /**< [in] -1|+1 if root of heap is minimum|maximum element */
76  LALHeapCmpFcn cmp /**< [in[ Heap element comparison function */
77  );
78 
79 /**
80  * Create a heap with a parameterised comparison function
81  */
82 LALHeap *XLALHeapCreate2(
83  LALHeapDtorFcn dtor, /**< [in] Heap element destructor function, if required */
84  int max_size, /**< [in] Maximum size of the heap; if zero, heap has unlimited size */
85  int min_or_max_heap, /**< [in] -1|+1 if root of heap is minimum|maximum element */
86  LALHeapCmpParamFcn cmp, /**< [in] Parameterised heap element comparison function */
87  void *cmp_param /**< [in] Parameter to pass to comparison function */
88  );
89 
90 /**
91  * Destroy a heap and its elements
92  */
93 void XLALHeapDestroy(
94  LALHeap *h /**< [in] Pointer to heap */
95  );
96 
97 /**
98  * Clear a heap
99  */
100 int XLALHeapClear(
101  LALHeap *h /**< [in] Pointer to heap */
102  );
103 
104 /**
105  * Return the size of a heap
106  */
107 int XLALHeapSize(
108  const LALHeap *h /**< [in] Pointer to heap */
109  );
110 
111 /**
112  * Return the maximum size of a heap
113  */
114 int XLALHeapMaxSize(
115  const LALHeap *h /**< [in] Pointer to heap */
116  );
117 
118 /**
119  * Return >0 if a limited-size heap is full, 0 otherwise (or if heap has unlimited size), or <0 on error
120  */
121 int XLALHeapIsFull(
122  const LALHeap *h /**< [in] Pointer to heap */
123  );
124 
125 /**
126  * Return the root element of a heap
127  */
128 const void *XLALHeapRoot(
129  const LALHeap *h /**< [in] Pointer to heap */
130  );
131 
132 /**
133  * Change the maximum size of a heap; if the heap is contracted, excess elements are destroyed
134  */
135 int XLALHeapResize(
136  LALHeap *h, /**< [in] Pointer to heap */
137  int max_size /**< [in] New maximum size of the heap; if zero, heap has unlimited size */
138  );
139 
140 /**
141  * Add a new element to a heap; if the heap is of fixed size and full, the root element is removed
142  */
143 int XLALHeapAdd(
144  LALHeap *h, /**< [in] Pointer to heap */
145  void **x /**< [in/out] Pointer to new element. If the root element is removed, it
146  is returned in <tt>*x</tt>; otherwise <tt>*x</tt> is set to \c NULL */
147  );
148 
149 /**
150  * Remove the root element of a heap
151  */
152 void *XLALHeapExtractRoot(
153  LALHeap *h /**< [in] Pointer to heap */
154  );
155 
156 /**
157  * Remove and destroy the root element of a heap
158  */
160  LALHeap *h /**< [in] Pointer to heap */
161  );
162 
163 /**
164  * Exchange the root element of a non-empty heap with the new element in <tt>*x</tt>
165  */
167  LALHeap *h, /**< [in] Pointer to heap */
168  void **x /**< [in/out] Pointer to new element. On return, contains root element */
169  );
170 
171 /**
172  * Visit each element in the heap in the order given by the comparison function
173  */
174 int XLALHeapVisit(
175  const LALHeap *h, /**< [in] Pointer to heap */
176  LALHeapVisitFcn visit, /**< [in] Visitor function to call for each heap element */
177  void *visit_param /**< [in] Parameter to pass to visitor function */
178  );
179 
180 /**
181  * Visit (and possibly modify) each element in the heap in the order given by the comparison function
182  */
183 int XLALHeapModify(
184  LALHeap *h, /**< [in] Pointer to heap */
185  LALHeapModifyFcn modify, /**< [in] Modifier function to call for each heap element */
186  void *modify_param /**< [in] Parameter to pass to modifier function */
187  );
188 
189 /**
190  * Allocate and return an array containing all elements in the heap
191  */
192 const void **XLALHeapElements(
193  const LALHeap *h /**< [in] Pointer to heap */
194  );
195 
196 /** @} */
197 
198 #ifdef __cplusplus
199 }
200 #endif
201 
202 #endif // _LALHEAP_H
static int cmp(REAL4Sequence *a, REAL4Sequence *b)
Definition: SequenceTest.c:62
const void ** XLALHeapElements(const LALHeap *h)
Allocate and return an array containing all elements in the heap.
Definition: LALHeap.c:465
void(* LALHeapDtorFcn)(void *x)
Function which free memory associated with heap element x
Definition: LALHeap.h:45
const void * XLALHeapRoot(const LALHeap *h)
Return the root element of a heap.
Definition: LALHeap.c:259
int XLALHeapResize(LALHeap *h, int max_size)
Change the maximum size of a heap; if the heap is contracted, excess elements are destroyed.
Definition: LALHeap.c:267
int(* LALHeapCmpFcn)(const void *x, const void *y)
Function which compares heap elements x and y
Definition: LALHeap.h:50
int XLALHeapVisit(const LALHeap *h, LALHeapVisitFcn visit, void *visit_param)
Visit each element in the heap in the order given by the comparison function.
Definition: LALHeap.c:374
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.
Definition: LALHeap.c:289
int(* LALHeapVisitFcn)(void *param, const void *x)
Function to call when visiting heap element x, with a parameter param.
Definition: LALHeap.h:61
void * XLALHeapExtractRoot(LALHeap *h)
Remove the root element of a heap.
Definition: LALHeap.c:305
int(* LALHeapCmpParamFcn)(void *param, const void *x, const void *y)
Function which compares heap elements x and y, with a parameter param.
Definition: LALHeap.h:55
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.
Definition: LALHeap.c:409
int XLALHeapSize(const LALHeap *h)
Return the size of a heap.
Definition: LALHeap.c:235
int XLALHeapExchangeRoot(LALHeap *h, void **x)
Exchange the root element of a non-empty heap with the new element in *x
Definition: LALHeap.c:355
int XLALHeapRemoveRoot(LALHeap *h)
Remove and destroy the root element of a heap.
Definition: LALHeap.c:333
int(* LALHeapModifyFcn)(void *param, void *x)
Function to call when visiting (and possibly modify) heap element x, with a parameter param.
Definition: LALHeap.h:67
int XLALHeapIsFull(const LALHeap *h)
Return >0 if a limited-size heap is full, 0 otherwise (or if heap has unlimited size),...
Definition: LALHeap.c:251
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.
Definition: LALHeap.c:162
int XLALHeapClear(LALHeap *h)
Clear a heap.
Definition: LALHeap.c:209
LALHeap * XLALHeapCreate(LALHeapDtorFcn dtor, int max_size, int min_or_max_heap, LALHeapCmpFcn cmp)
Create a heap.
Definition: LALHeap.c:146
void XLALHeapDestroy(LALHeap *h)
Destroy a heap and its elements.
Definition: LALHeap.c:192
int XLALHeapMaxSize(const LALHeap *h)
Return the maximum size of a heap.
Definition: LALHeap.c:243
Generic heap with elements of type void *
Definition: LALHeap.c:35
LALHeapDtorFcn dtor
Definition: LALHeap.c:39
int min_or_max_heap
Definition: LALHeap.c:41
void * cmp_param
Definition: LALHeap.c:43
int max_size
Definition: LALHeap.c:40