Remove unnecessary include directives of hash.h
[pspp] / src / language / stats / means.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 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 #include <config.h>
18
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include <data/dictionary.h>
23 #include <data/procedure.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/lexer/lexer.h>
27 #include <libpspp/message.h>
28
29 #include "xalloc.h"
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 /* (headers) */
35
36 /* (specification)
37    means (mns_):
38      *tables=custom;
39      +format=lab:!labels/nolabels/nocatlabs,
40             name:!names/nonames,
41             val:!values/novalues,
42             fmt:!table/tree;
43      missing=miss:!table/include/dependent;
44      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
45      +statistics[st_]=anova,linearity,all,none.
46 */
47 /* (declarations) */
48 /* (functions) */
49
50 /* TABLES: Variable lists for each dimension. */
51 static int n_dim;               /* Number of dimensions. */
52 static size_t *nv_dim;          /* Number of variables in each dimension. */
53 static const struct variable ***v_dim;  /* Variables in each dimension.  */
54
55 /* VARIABLES: List of variables. */
56 static struct variable **v_var;
57
58 /* Parses and executes the T-TEST procedure. */
59 int
60 cmd_means (struct lexer *lexer, struct dataset *ds)
61 {
62   struct cmd_means cmd;
63   int success = CMD_FAILURE;
64
65   n_dim = 0;
66   nv_dim = NULL;
67   v_dim = NULL;
68   v_var = NULL;
69
70   if (!parse_means (lexer, ds, &cmd, NULL))
71     goto free;
72
73   if (cmd.sbc_cells)
74     {
75       int i;
76       for (i = 0; i < MNS_CL_count; i++)
77         if (cmd.a_cells[i])
78           break;
79       if (i >= MNS_CL_count)
80         cmd.a_cells[MNS_CL_ALL] = 1;
81     }
82   else
83     cmd.a_cells[MNS_CL_DEFAULT] = 1;
84   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
85     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
86   if (cmd.a_cells[MNS_CL_ALL])
87     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
88
89   if (cmd.sbc_statistics)
90     {
91       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
92         cmd.a_statistics[MNS_ST_ANOVA] = 1;
93       if (cmd.a_statistics[MNS_ST_ALL])
94         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
95     }
96
97   if (!cmd.sbc_tables)
98     {
99       msg (SE, _("Missing required subcommand TABLES."));
100       goto free;
101     }
102
103   success = CMD_SUCCESS;
104
105 free:
106   {
107     int i;
108
109     for (i = 0; i < n_dim; i++)
110       free (v_dim[i]);
111     free (nv_dim);
112     free (v_dim);
113     free (v_var);
114   }
115
116   return success;
117 }
118
119 /* Parses the TABLES subcommand. */
120 static int
121 mns_custom_tables (struct lexer *lexer, struct dataset *ds, struct cmd_means *cmd, void *aux UNUSED)
122 {
123   struct const_var_set *var_set;
124
125   if (!lex_match_id (lexer, "TABLES")
126       && (lex_token (lexer) != T_ID || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) == NULL)
127       && lex_token (lexer) != T_ALL)
128     return 2;
129   lex_match (lexer, '=');
130
131   if (cmd->sbc_tables)
132     {
133       msg (SE, _("TABLES subcommand may not appear more "
134                  "than once."));
135       return 0;
136     }
137
138   var_set = const_var_set_create_from_dict (dataset_dict (ds));
139   assert (var_set != NULL);
140
141   do
142     {
143       size_t nvl;
144       const struct variable **vl;
145
146       if (!parse_const_var_set_vars (lexer, var_set, &vl, &nvl,
147                                PV_NO_DUPLICATE | PV_NO_SCRATCH))
148         goto lossage;
149
150       n_dim++;
151       nv_dim = xnrealloc (nv_dim, n_dim, sizeof *nv_dim);
152       v_dim = xnrealloc (v_dim, n_dim, sizeof *v_dim);
153
154       nv_dim[n_dim - 1] = nvl;
155       v_dim[n_dim - 1] = vl;
156     }
157   while (lex_match (lexer, T_BY));
158
159   const_var_set_destroy (var_set);
160   return 1;
161
162  lossage:
163   const_var_set_destroy (var_set);
164   return 0;
165 }
166
167 /*
168    Local Variables:
169    mode: c
170    End:
171 */