1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "dictionary.h"
30 #include "range-parser.h"
35 #define _(msgid) gettext (msgid)
40 CNT_SINGLE, /* Single value. */
41 CNT_RANGE /* a <= x <= b. */
44 /* Numeric count criteria. */
47 enum value_type type; /* How to interpret a, b. */
48 double a, b; /* Values to count. */
53 struct criteria *next;
55 /* Variables to count. */
56 struct variable **vars;
59 /* Count special values?. */
60 bool count_system_missing; /* Count system missing? */
61 bool count_user_missing; /* Count user missing? */
63 /* Criterion values. */
67 struct num_value *num;
76 struct variable *var; /* Destination variable. */
77 char *name; /* Name of dest var. */
78 struct criteria *crit; /* The criteria specifications. */
83 struct dst_var *dst_vars;
87 static trns_proc_func count_trns_proc;
88 static trns_free_func count_trns_free;
90 static bool parse_numeric_criteria (struct pool *, struct criteria *);
91 static bool parse_string_criteria (struct pool *, struct criteria *);
96 struct dst_var *dv; /* Destination var being parsed. */
97 struct count_trns *trns; /* Transformation. */
99 /* Parses each slash-delimited specification. */
100 trns = pool_create_container (struct count_trns, pool);
101 trns->dst_vars = dv = pool_alloc (trns->pool, sizeof *dv);
104 struct criteria *crit;
106 /* Initialize this struct dst_var to ensure proper cleanup. */
111 /* Get destination variable, or at least its name. */
112 if (!lex_force_id ())
114 dv->var = dict_lookup_var (default_dict, tokid);
117 if (dv->var->type == ALPHA)
119 msg (SE, _("Destination cannot be a string variable."));
124 dv->name = pool_strdup (trns->pool, tokid);
127 if (!lex_force_match ('='))
130 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
137 if (!parse_variables (default_dict, &crit->vars, &crit->var_cnt,
138 PV_DUPLICATE | PV_SAME_TYPE))
140 pool_register (trns->pool, free, crit->vars);
142 if (!lex_force_match ('('))
146 if (crit->vars[0]->type == NUMERIC)
147 ok = parse_numeric_criteria (trns->pool, crit);
149 ok = parse_string_criteria (trns->pool, crit);
153 if (token == '/' || token == '.')
156 crit = crit->next = pool_alloc (trns->pool, sizeof *crit);
162 if (!lex_force_match ('/'))
164 dv = dv->next = pool_alloc (trns->pool, sizeof *dv);
167 /* Create all the nonexistent destination variables. */
168 for (dv = trns->dst_vars; dv; dv = dv->next)
171 /* It's valid, though motivationally questionable, to count to
172 the same dest var more than once. */
173 dv->var = dict_lookup_var (default_dict, dv->name);
176 dv->var = dict_create_var_assert (default_dict, dv->name, 0);
179 add_transformation (count_trns_proc, count_trns_free, trns);
183 count_trns_free (trns);
187 /* Parses a set of numeric criterion values. Returns success. */
189 parse_numeric_criteria (struct pool *pool, struct criteria *crit)
191 size_t allocated = 0;
193 crit->values.num = NULL;
194 crit->count_system_missing = false;
195 crit->count_user_missing = false;
200 if (lex_match_id ("SYSMIS"))
201 crit->count_system_missing = true;
202 else if (lex_match_id ("MISSING"))
203 crit->count_user_missing = true;
204 else if (parse_num_range (&low, &high, NULL))
206 struct num_value *cur;
208 if (crit->value_cnt >= allocated)
209 crit->values.num = pool_2nrealloc (pool, crit->values.num,
211 sizeof *crit->values.num);
212 cur = &crit->values.num[crit->value_cnt++];
213 cur->type = low == high ? CNT_SINGLE : CNT_RANGE;
227 /* Parses a set of string criteria values. Returns success. */
229 parse_string_criteria (struct pool *pool, struct criteria *crit)
232 size_t allocated = 0;
235 for (i = 0; i < crit->var_cnt; i++)
236 if (crit->vars[i]->width > len)
237 len = crit->vars[i]->width;
239 crit->values.str = NULL;
243 if (crit->value_cnt >= allocated)
244 crit->values.str = pool_2nrealloc (pool, crit->values.str,
246 sizeof *crit->values.str);
248 if (!lex_force_string ())
250 cur = &crit->values.str[crit->value_cnt++];
251 *cur = pool_alloc (pool, len + 1);
252 str_copy_rpad (*cur, len + 1, ds_c_str (&tokstr));
263 /* Transformation. */
265 /* Counts the number of values in case C matching CRIT. */
267 count_numeric (struct criteria *crit, struct ccase *c)
272 for (i = 0; i < crit->var_cnt; i++)
274 double x = case_num (c, crit->vars[i]->fv);
276 counter += crit->count_system_missing;
277 else if (crit->count_user_missing
278 && mv_is_num_user_missing (&crit->vars[i]->miss, x))
284 for (v = crit->values.num; v < crit->values.num + crit->value_cnt;
286 if (v->type == CNT_SINGLE ? x == v->a : x >= v->a && x <= v->b)
297 /* Counts the number of values in case C matching CRIT. */
299 count_string (struct criteria *crit, struct ccase *c)
304 for (i = 0; i < crit->var_cnt; i++)
307 for (v = crit->values.str; v < crit->values.str + crit->value_cnt; v++)
308 if (!memcmp (case_str (c, crit->vars[i]->fv), *v,
309 crit->vars[i]->width))
319 /* Performs the COUNT transformation T on case C. */
321 count_trns_proc (void *trns_, struct ccase *c,
324 struct count_trns *trns = trns_;
327 for (dv = trns->dst_vars; dv; dv = dv->next)
329 struct criteria *crit;
333 for (crit = dv->crit; crit; crit = crit->next)
334 if (crit->vars[0]->type == NUMERIC)
335 counter += count_numeric (crit, c);
337 counter += count_string (crit, c);
338 case_data_rw (c, dv->var->fv)->f = counter;
340 return TRNS_CONTINUE;
343 /* Destroys all dynamic data structures associated with TRNS. */
345 count_trns_free (void *trns_)
347 struct count_trns *trns = (struct count_trns *) trns_;
348 pool_destroy (trns->pool);