1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008 Free Software Foundation, Inc.
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 3 of the License, or
7 (at your option) any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <data/case.h>
21 #include <data/val-type.h>
22 #include <libpspp/compiler.h>
23 #include <libpspp/ll.h>
32 ll_compare_func *cmp_func;
37 cmp_descending (const struct ll *a_, const struct ll *b_, void *aux UNUSED)
39 const struct extremum *a = ll_data (a_, struct extremum, ll);
40 const struct extremum *b = ll_data (b_, struct extremum, ll);
42 if ( a->value > b->value) return -1;
44 return (a->value < b->value);
48 cmp_ascending (const struct ll *a_, const struct ll *b_, void *aux UNUSED)
50 const struct extremum *a = ll_data (a_, struct extremum, ll);
51 const struct extremum *b = ll_data (b_, struct extremum, ll);
53 if ( a->value < b->value) return -1;
55 return (a->value > b->value);
60 extrema_create (size_t n, enum extreme_end end)
62 struct extrema *extrema = xzalloc (sizeof *extrema);
63 extrema->capacity = n;
65 if ( end == EXTREME_MAXIMA )
66 extrema->cmp_func = cmp_descending;
68 extrema->cmp_func = cmp_ascending;
70 ll_init (&extrema->list);
76 extrema_destroy (struct extrema *extrema)
78 struct ll *ll = ll_head (&extrema->list);
80 while (ll != ll_null (&extrema->list))
82 struct extremum *e = ll_data (ll, struct extremum, ll);
93 extrema_add (struct extrema *extrema, double val,
97 struct extremum *e = xzalloc (sizeof *e) ;
99 e->location = location;
108 ll_insert_ordered (ll_head (&extrema->list), ll_null (&extrema->list),
109 &e->ll, extrema->cmp_func, NULL);
111 if ( extrema->n++ > extrema->capacity)
113 struct ll *tail = ll_tail (&extrema->list);
114 struct extremum *et = ll_data (tail, struct extremum, ll);
123 const struct ll_list *
124 extrema_list (const struct extrema *ex)
131 extrema_top (const struct extrema *ex, double *v)
133 const struct extremum *top;
135 if ( ll_is_empty (&ex->list))
138 top = (const struct extremum *)
139 ll_data (ll_head(&ex->list), struct extremum, ll);