LALBurst 2.0.7.1-eeff03c
SnglBurstUtils.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Jolien Creighton, Kipp Cannon, Patrick Brady, Saikat Ray-Majumder, Xavier Siemens
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
21#include <string.h>
22#include <lal/Date.h>
23#include <lal/LIGOMetadataTables.h>
24#include <lal/SnglBurstUtils.h>
25#include <lal/XLALError.h>
26
27
28/**
29 * Compute the length of a linked list of SnglBurst objects.
30 */
32{
33 int length;
34
35 for(length = 0; head; head = head->next)
36 length++;
37
38 return length;
39}
40
41
42/**
43 * Sort a list of SnglBurst events into increasing order according to the
44 * supplied comparison function.
45 */
47 SnglBurst **head,
48 int (*comparefunc)(const SnglBurst * const *, const SnglBurst * const *)
49)
50{
51 int i;
52 int length;
53 SnglBurst *event;
54 SnglBurst **array;
55 SnglBurst **next;
56
57 /* empty list --> no-op */
58 if(!*head)
59 return head;
60
61 /* construct an array of pointers into the list */
62 length = XLALSnglBurstTableLength(*head);
63 array = XLALCalloc(length, sizeof(*array));
64 if(!array)
66 for(i = 0, event = *head; event; event = event->next)
67 array[i++] = event;
68
69 /* sort the array using the specified function */
70 qsort(array, length, sizeof(*array), (int(*)(const void *, const void *)) comparefunc);
71
72 /* re-link the list according to the sorted array */
73 next = head;
74 for(i = 0; i < length; i++, next = &(*next)->next)
75 *next = array[i];
76 *next = NULL;
77
78 /* free the array */
79 XLALFree(array);
80
81 /* success */
82 return head;
83}
84
85
86/**
87 * Compare the peak times and SNRs of two SnglBurst events.
88 */
89
90
92 const SnglBurst * const *a,
93 const SnglBurst * const *b
94)
95{
96 INT8 ta = XLALGPSToINT8NS(&(*a)->peak_time);
97 INT8 tb = XLALGPSToINT8NS(&(*b)->peak_time);
98 float snra = (*a)->snr;
99 float snrb = (*b)->snr;
100
101 if(ta > tb)
102 return 1;
103 if(ta < tb)
104 return -1;
105 /* ta == tb */
106 if(snra > snrb)
107 return 1;
108 if(snra < snrb)
109 return -1;
110 /* snra == snrb */
111 return 0;
112}
int XLALSnglBurstTableLength(SnglBurst *head)
Compute the length of a linked list of SnglBurst objects.
int XLALCompareSnglBurstByPeakTimeAndSNR(const SnglBurst *const *a, const SnglBurst *const *b)
Compare the peak times and SNRs of two SnglBurst events.
SnglBurst ** XLALSortSnglBurst(SnglBurst **head, int(*comparefunc)(const SnglBurst *const *, const SnglBurst *const *))
Sort a list of SnglBurst events into increasing order according to the supplied comparison function.
double i
int64_t INT8
void * XLALCalloc(size_t m, size_t n)
void XLALFree(void *p)
static const INT4 a
#define XLAL_ERROR_NULL(...)
XLAL_EFUNC
INT8 XLALGPSToINT8NS(const LIGOTimeGPS *epoch)
struct tagSnglBurst * next