Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
HeapToplist.h
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Bernd Machenschalk, Reinhard Prix
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/* This module keeps a "toplist", i.e. a list of the top n elements (accoding to an externally
21 supplied comparison function) in a standard heap structure.
22
23 Author (of this implementation): Bernd Machenschalk
24 */
25
26#ifndef HEAPTOPLIST_H
27#define HEAPTOPLIST_H
28
29/* exclude from SWIG interface; functions/structs are not XLAL namespaced */
30#ifndef SWIG
31
32#include <stddef.h>
33
34/* toplist structure */
35typedef struct {
36 size_t length; /* the length (maximal number of entries) of the toplist */
37 size_t elems; /* number of elements currently in the toplist */
38 size_t size; /* size of an element */
39 char *data; /* the actual data array of 'length'*'size' chars */
40 char **heap; /* array of 'length' pointers into data */
41 int ( *smaller )( const void *, const void * ); /* comparison function */
42} toplist_t;
43
44
45/* creates a toplist with 'length' elements of size 'size', with
46 odering based on comparison function 'smaller'.
47 returns -1 on error (out of memory), else 0 */
48extern int create_toplist( toplist_t **list, size_t length, size_t size,
49 int ( *smaller )( const void *, const void * ) );
50
51
52/* frees the space occupied by the toplist */
53extern void free_toplist( toplist_t **list );
54
55
56/* Inserts an element in to the toplist either if there is space left
57 or the element is larger than the smallest element in the toplist.
58 In the latter case, remove the smallest element from the toplist.
59 Returns 1 if the element was actually inserted, 0 if not. */
60extern int insert_into_toplist( toplist_t *list, void *element );
61
62/* return non-zero value iff the passed element would be inserted into
63 the toplist by calling insert_into_toplist, but no actual insertion is done.
64 Can be called with a partially filled element to decide whether
65 further processing of a candidate is even necessary. The field(s) used
66 for sorting the toplist must be filled */
67
68#define TEST_FSTAT_TOPLIST_INCLUSION(list, element) \
69 ( ( (list)->elems < (list)->length ||( ((list)->smaller) ((const void *)(element),((list)->heap)[0]) < 0) ) )
70
71/* clears an existing toplist of all elements inserted so far */
72extern void clear_toplist( toplist_t *list );
73
74
75/* apply a function to all elements of the list in the current order
76 (possibly after calling qsort_toplist(), e.g. for writing out */
77extern void go_through_toplist( toplist_t *list, void ( *handle )( void * ) );
78
79
80/* sort the toplist with an arbitrary sorting function
81 (potentially) destroying the heap property.
82
83 note that a (q-)sorted list is a heap, but due to the interface of qsort
84 the same comparison function will give the reverse order than the heap.
85 in order to restore a heap with qsort_toplist() (e.g. to add more elements) you must
86 qsort_toplist() with the inverse function of the "smaller" function of the heap. */
87extern void qsort_toplist( toplist_t *list, int ( *compare )( const void *, const void * ) );
88
89
90/* therefore we provide a qsort_toplist_r() function that gives the reverse ordering of
91 qsort_toplist(), which restores the heap property with the same comparison function */
92extern void qsort_toplist_r( toplist_t *list, int ( *compare )( const void *, const void * ) );
93
94
95/* access a certain element of the toplist (shouldn't be done by fiddling in toplist_t)
96 returns a NULL pointer on error (i.e. index out of bounds) */
97extern void *toplist_elem( toplist_t *list, size_t idx );
98
99
100/* compare two toplists
101 returns -1 if list1 is "smaller", 1 if list2 is "smaller", 0 if they are equal,
102 2 if they are uncomparable (different data types or "smaller" functions */
103extern int compare_toplists( toplist_t *list1, toplist_t *list2 );
104
105#endif /* SWIG */
106
107#endif /* HEAPTOPLIST_H - double inclusion protection */
int create_toplist(toplist_t **list, size_t length, size_t size, int(*smaller)(const void *, const void *))
Definition: HeapToplist.c:101
void free_toplist(toplist_t **list)
Definition: HeapToplist.c:139
void qsort_toplist(toplist_t *list, int(*compare)(const void *, const void *))
Definition: HeapToplist.c:241
void qsort_toplist_r(toplist_t *list, int(*compare)(const void *, const void *))
Definition: HeapToplist.c:249
int insert_into_toplist(toplist_t *list, void *element)
Definition: HeapToplist.c:151
int compare_toplists(toplist_t *list1, toplist_t *list2)
Definition: HeapToplist.c:196
void go_through_toplist(toplist_t *list, void(*handle)(void *))
Definition: HeapToplist.c:177
void clear_toplist(toplist_t *list)
Definition: HeapToplist.c:132
void * toplist_elem(toplist_t *list, size_t idx)
Definition: HeapToplist.c:185
static int smaller(const void *a, const void *b)
char ** heap
Definition: HeapToplist.h:40
size_t length
Definition: HeapToplist.h:36
size_t elems
Definition: HeapToplist.h:37
size_t size
Definition: HeapToplist.h:38
char * data
Definition: HeapToplist.h:39