Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
- Reput hook for raw sockets, needed for BW experiments
[simgrid.git] / src / nws_portability / Forecast / run_mean.c
1 /* $Id$ */
2
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <math.h>
7 #include <string.h>
8 #include <strings.h>
9
10 #include "fbuff.h"
11 #include "run_mean.h"
12
13 struct run_mean_state
14 {
15         fbuff series;                   /* the series so far */
16         fbuff time_stamps;              /* the time stamps */
17         double total;
18         double count;
19 };
20
21 /*
22  * init local state.  can save a copy of the pointer to the
23  * series and time stamps, if desired
24  */
25 char *
26 InitRunMean(fbuff series, fbuff time_stamps, char *params)
27 {
28         struct run_mean_state *state;
29         
30         state = (struct run_mean_state *)
31                 malloc(sizeof(struct run_mean_state));
32         
33         if(state == NULL)
34         {
35                 return(NULL);
36         }
37         
38         /*
39          * all functions take a forcb
40          */
41         state->series = series;
42         state->time_stamps = time_stamps;
43         state->total = 0.0;
44         state->count = 0.0;
45         
46         return((char *)state);
47 }
48
49 void
50 FreeRunMean(char *state)
51 {
52         free(state);
53         return;
54 }
55
56 void
57 UpdateRunMean(char *state,
58                 double ts,
59                 double value)
60 {
61         struct run_mean_state *s;
62         
63         s = (struct run_mean_state *)state;
64         
65         s->total += value;
66         s->count += 1.0;
67         return;
68 }
69
70 int
71 ForcRunMean(char *state, double *v)
72 {
73         double val;
74         struct run_mean_state *s = (struct run_mean_state *)state;
75         
76         if(s->count != 0.0)
77         {
78                 val = s->total / s->count;
79                 *v = val;
80                 return(1);
81         }
82         else
83         {
84                 *v = 0.0;
85                 return(0);
86         }
87 }
88
89