Re-implement MEANS.
[pspp] / src / language / stats / means.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011, 2012, 2013, 2019 Free Software Foundation, Inc.
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 3 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 this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef MEANS_H
18 #define MEANS_H
19
20 #include "libpspp/compiler.h"
21
22 struct cell_container
23 {
24   /* A hash table containing the cells.  The table is indexed by a hash
25      based on the cell's categorical value.  */
26   struct hmap map;
27
28   /* A binary tree containing the cells.  This  is
29    used to sort the elements in order of their categorical
30    values.  */
31   struct bt bt;
32 };
33
34
35
36 struct layer
37 {
38   size_t n_factor_vars;
39   const struct variable **factor_vars;
40 };
41
42
43 struct statistic;
44
45 typedef struct statistic *stat_create (struct pool *pool);
46 typedef void stat_update  (struct statistic *stat, double w, double x);
47 typedef double stat_get   (const struct statistic *);
48 typedef void stat_destroy (struct statistic *);
49
50
51 struct cell_spec
52 {
53   /* Printable title for output */
54   const char *title;
55
56   /* Keyword for syntax */
57   const char *keyword;
58
59   /* The result class for the datum.  */
60   const char *rc;
61
62   stat_create *sc;
63   stat_update *su;
64   stat_get *sd;
65   stat_destroy *sf;
66 };
67
68 struct summary
69 {
70   double n_total;
71   double n_missing;
72 };
73
74 /* Intermediate data per table.  */
75 struct workspace
76 {
77   /* An array of n_layers integers which are used
78      to permute access into the factor_vars of each layer.  */
79   int *control_idx;
80
81   /* An array of n_layers cell_containers which hold the union
82      of instances used respectively by each layer.  */
83   struct cell_container *instances;
84
85   struct cell *root_cell;
86 };
87
88 /* The thing parsed after TABLES= */
89 struct mtable
90 {
91   size_t n_dep_vars;
92   const struct variable **dep_vars;
93
94   struct layer **layers;
95   int n_layers;
96
97   int n_combinations;
98
99   /* An array of n_combinations workspaces.  */
100   struct workspace *ws;
101
102   /* An array of n_combinations * n_dep_vars summaries.
103      These are displayed in the Case Processing
104      Summary box.  */
105   struct summary *summ;
106 };
107
108 /* A structure created by the parser.  Contains the definition of the
109    what the procedure should calculate.  */
110 struct means
111 {
112   const struct dictionary *dict;
113
114   /* The "tables" (ie, a definition of how the data should
115      be broken down).  */
116   struct mtable *table;
117   size_t n_tables;
118
119   /* Missing value class for categorical variables.  */
120   enum mv_class ctrl_exclude;
121
122   /* Missing value class for dependent variables */
123   enum mv_class dep_exclude;
124
125   /* The statistics to be calculated for each cell.  */
126   int *statistics;
127   int n_statistics;
128
129   /* Pool on which cell functions may allocate data.  */
130   struct pool *pool;
131 };
132
133
134
135 #define n_MEANS_STATISTICS 17
136 extern const struct cell_spec cell_spec[n_MEANS_STATISTICS];
137
138 /* This enum must be consistent with the array cell_spec (in means-calc.c).
139    A bitfield instead of enums would in my opinion be
140    more elegent.  However we want the order of the specified
141    statistics to be retained in the output.  */
142 enum
143   {
144     MEANS_MEAN = 0,
145     MEANS_N,
146     MEANS_STDDEV
147   };
148
149
150
151 struct dataset;
152 struct casereader;
153 void run_means (struct means *cmd, struct casereader *input, const struct dataset *ds UNUSED);
154
155 void means_shipout (const struct mtable *mt, const struct means *means);
156
157 void means_case_processing_summary (const struct mtable *mt);
158
159
160 void destroy_means (struct means *means);
161
162
163 #endif