1 /* movstat/snacc.c
2 *
3 * Moving window S_n accumulator
4 *
5 * Copyright (C) 2018 Patrick Alken
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include <config.h>
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_vector.h>
25 #include <gsl/gsl_errno.h>
26 #include <gsl/gsl_movstat.h>
27 #include <gsl/gsl_sort.h>
28 #include <gsl/gsl_statistics.h>
29
30 typedef double snacc_type_t;
31 typedef snacc_type_t ringbuf_type_t;
32
33 #include "ringbuf.c"
34
35 typedef struct
36 {
37 snacc_type_t *window; /* linear array for current window */
38 snacc_type_t *work; /* workspace */
39 ringbuf *rbuf; /* ring buffer storing current window */
40 } snacc_state_t;
41
42 static size_t
snacc_size(const size_t n)43 snacc_size(const size_t n)
44 {
45 size_t size = 0;
46
47 size += sizeof(snacc_state_t);
48 size += 2 * n * sizeof(snacc_type_t);
49 size += ringbuf_size(n);
50
51 return size;
52 }
53
54 static int
snacc_init(const size_t n,void * vstate)55 snacc_init(const size_t n, void * vstate)
56 {
57 snacc_state_t * state = (snacc_state_t *) vstate;
58
59 state->window = (snacc_type_t *) ((unsigned char *) vstate + sizeof(snacc_state_t));
60 state->work = (snacc_type_t *) ((unsigned char *) state->window + n * sizeof(snacc_type_t));
61 state->rbuf = (ringbuf *) ((unsigned char *) state->work + n * sizeof(snacc_type_t));
62
63 ringbuf_init(n, state->rbuf);
64
65 return GSL_SUCCESS;
66 }
67
68 static int
snacc_insert(const snacc_type_t x,void * vstate)69 snacc_insert(const snacc_type_t x, void * vstate)
70 {
71 snacc_state_t * state = (snacc_state_t *) vstate;
72
73 /* add new element to ring buffer */
74 ringbuf_insert(x, state->rbuf);
75
76 return GSL_SUCCESS;
77 }
78
79 static int
snacc_delete(void * vstate)80 snacc_delete(void * vstate)
81 {
82 snacc_state_t * state = (snacc_state_t *) vstate;
83
84 if (!ringbuf_is_empty(state->rbuf))
85 ringbuf_pop_back(state->rbuf);
86
87 return GSL_SUCCESS;
88 }
89
90 /* FIXME XXX: this is inefficient - could be improved by maintaining a sorted ring buffer */
91 static int
snacc_get(void * params,snacc_type_t * result,const void * vstate)92 snacc_get(void * params, snacc_type_t * result, const void * vstate)
93 {
94 const snacc_state_t * state = (const snacc_state_t *) vstate;
95 size_t n = ringbuf_copy(state->window, state->rbuf);
96
97 (void) params;
98
99 gsl_sort(state->window, 1, n);
100 *result = gsl_stats_Sn_from_sorted_data(state->window, 1, n, state->work);
101
102 return GSL_SUCCESS;
103 }
104
105 static const gsl_movstat_accum sn_accum_type =
106 {
107 snacc_size,
108 snacc_init,
109 snacc_insert,
110 snacc_delete,
111 snacc_get
112 };
113
114 const gsl_movstat_accum *gsl_movstat_accum_Sn = &sn_accum_type;
115