File: snacc.c

package info (click to toggle)
gsl 2.5%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,628 kB
  • sloc: ansic: 245,573; sh: 11,435; makefile: 911; python: 68
file content (114 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* movstat/snacc.c
 *
 * Moving window S_n accumulator
 * 
 * Copyright (C) 2018 Patrick Alken
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_movstat.h>
#include <gsl/gsl_sort.h>
#include <gsl/gsl_statistics.h>

typedef double snacc_type_t;
typedef snacc_type_t ringbuf_type_t;

#include "ringbuf.c"

typedef struct
{
  snacc_type_t *window; /* linear array for current window */
  snacc_type_t *work;   /* workspace */
  ringbuf *rbuf;        /* ring buffer storing current window */
} snacc_state_t;

static size_t
snacc_size(const size_t n)
{
  size_t size = 0;

  size += sizeof(snacc_state_t);
  size += 2 * n * sizeof(snacc_type_t);
  size += ringbuf_size(n);

  return size;
}

static int
snacc_init(const size_t n, void * vstate)
{
  snacc_state_t * state = (snacc_state_t *) vstate;

  state->window = (snacc_type_t *) ((unsigned char *) vstate + sizeof(snacc_state_t));
  state->work = (snacc_type_t *) ((unsigned char *) state->window + n * sizeof(snacc_type_t));
  state->rbuf = (ringbuf *) ((unsigned char *) state->work + n * sizeof(snacc_type_t));

  ringbuf_init(n, state->rbuf);

  return GSL_SUCCESS;
}

static int
snacc_insert(const snacc_type_t x, void * vstate)
{
  snacc_state_t * state = (snacc_state_t *) vstate;

  /* add new element to ring buffer */
  ringbuf_insert(x, state->rbuf);

  return GSL_SUCCESS;
}

static int
snacc_delete(void * vstate)
{
  snacc_state_t * state = (snacc_state_t *) vstate;

  if (!ringbuf_is_empty(state->rbuf))
    ringbuf_pop_back(state->rbuf);

  return GSL_SUCCESS;
}

/* FIXME XXX: this is inefficient - could be improved by maintaining a sorted ring buffer */
static int
snacc_get(void * params, snacc_type_t * result, const void * vstate)
{
  const snacc_state_t * state = (const snacc_state_t *) vstate;
  size_t n = ringbuf_copy(state->window, state->rbuf);

  (void) params;

  gsl_sort(state->window, 1, n);
  *result = gsl_stats_Sn_from_sorted_data(state->window, 1, n, state->work);

  return GSL_SUCCESS;
}

static const gsl_movstat_accum sn_accum_type =
{
  snacc_size,
  snacc_init,
  snacc_insert,
  snacc_delete,
  snacc_get
};

const gsl_movstat_accum *gsl_movstat_accum_Sn = &sn_accum_type;