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., 59 Temple Place - Suite 330, Boston, MA
29 /* Definition of the max length of a short string value, generally
31 #define MAX_SHORT_STRING ((SIZEOF_DOUBLE)>=8 ? ((SIZEOF_DOUBLE)+1)/2*2 : 8)
32 #define MIN_LONG_STRING (MAX_SHORT_STRING+1)
34 /* FYI: It is a bad situation if sizeof(flt64) < MAX_SHORT_STRING:
35 then short string missing values can be truncated in system files
36 because there's only room for as many characters as can fit in a
38 #if MAX_SHORT_STRING > 8
39 #error MAX_SHORT_STRING must be less than 8.
43 #define SYSMIS (-DBL_MAX)
44 #define LOWEST second_lowest_value
45 #define HIGHEST DBL_MAX
47 /* Describes one value, which is either a floating-point number or a
51 /* A numeric value. */
54 /* A short-string value. */
55 unsigned char s[MAX_SHORT_STRING];
57 /* This member is used by data-in.c to return a string result,
58 since it may need to return a long string. As currently
59 implemented, it's a pointer to a static internal buffer in
62 Also used by evaluate_expression() to return a string result.
63 As currently implemented, it's a pointer to a dynamic buffer in
64 the appropriate expression.
66 Also used by the AGGREGATE procedure in handling string
70 /* Sometimes we insert value's in a hash table. */
71 unsigned long hash[SIZEOF_DOUBLE / SIZEOF_LONG];
74 /* Frequency tables. */
76 /* Frequency table entry. */
79 union value v; /* The value. */
80 double c; /* The number of occurrences of the value. */
83 /* Types of frequency tables. */
90 /* Entire frequency table. */
93 int mode; /* FRQM_GENERAL or FRQM_INTEGER. */
96 struct hsh_table *data; /* Undifferentiated data. */
99 double *vector; /* Frequencies proper. */
100 int min, max; /* The boundaries of the table. */
101 double out_of_range; /* Sum of weights of out-of-range values. */
102 double sysmis; /* Sum of weights of SYSMIS values. */
105 struct freq *valid; /* Valid freqs. */
106 int n_valid; /* Number of total freqs. */
108 struct freq *missing; /* Missing freqs. */
109 int n_missing; /* Number of missing freqs. */
112 double total_cases; /* Sum of weights of all cases. */
113 double valid_cases; /* Sum of weights of valid cases. */
116 /* Procedures' private per-variable data. */
118 /* Structure name suffixes for private data:
119 _proc: for a procedure (i.e., LIST -> list_proc).
120 _trns: for a transformation (i.e., COMPUTE -> compute_trns.
121 _pgm: for an input program (i.e., DATA LIST -> data_list_pgm). */
123 /* CROSSTABS private data. */
126 /* Integer mode only. */
127 int min; /* Minimum value. */
128 int max; /* Maximum value + 1. */
129 int count; /* max - min. */
133 /* FREQUENCIES private data. */
136 frq_mean = 0, frq_semean, frq_median, frq_mode, frq_stddev, frq_variance,
137 frq_kurt, frq_sekurt, frq_skew, frq_seskew, frq_range, frq_min, frq_max,
141 struct frequencies_proc
143 int used; /* 1=This variable already used. */
145 /* Freqency table. */
146 struct freq_tab tab; /* Frequencies table to use. */
149 int n_groups; /* Number of groups. */
150 double *groups; /* Groups. */
153 double stat[frq_n_stats];
156 /* LIST private data. */
159 int newline; /* Whether a new line begins here. */
160 int width; /* Field width. */
161 int vert; /* Whether to print the varname vertically. */
164 /* DESCRIPTIVES private data. Note that the DESCRIPTIVES procedure also
165 has a transformation, descriptives_trns. */
168 /* As these are used as bit indexes, there must be 32 or fewer.
169 Be very careful in adjusting these, see the structure below
170 and the table in descriptives.q. */
171 dsc_mean = 0, dsc_semean, dsc_stddev, dsc_variance, dsc_kurt,
172 dsc_sekurt, dsc_skew, dsc_seskew, dsc_range, dsc_min,
173 dsc_max, dsc_sum, dsc_n_stats
176 struct descriptives_proc
179 int dup; /* Finds duplicates in list of
181 char zname[10]; /* Name for z-score variable. */
184 double valid, miss; /* Valid, missing--general. */
186 /* Mean, moments about the mean. */
187 double X_bar, M2, M3, M4;
191 double stats[dsc_n_stats]; /* Everything glommed together. */
194 /* GET private data. */
197 int fv, nv; /* First, # of values. */
203 SRT_ASCEND, /* A, B, C, ..., X, Y, Z. */
204 SRT_DESCEND /* Z, Y, X, ..., C, B, A. */
207 /* SORT CASES private data. */
208 struct sort_cases_proc
210 int order; /* SRT_ASCEND or SRT_DESCEND. */
213 /* MEANS private data. */
216 double min, max; /* Range for integer mode. */
219 /* Different types of variables for MATRIX DATA procedure. Order is
220 important: these are used for sort keys. */
223 MXD_SPLIT, /* SPLIT FILE variables. */
224 MXD_ROWTYPE, /* ROWTYPE_. */
225 MXD_FACTOR, /* Factor variables. */
226 MXD_VARNAME, /* VARNAME_. */
227 MXD_CONTINUOUS, /* Continuous variables. */
232 /* MATRIX DATA private data. */
233 struct matrix_data_proc
235 int vartype; /* Variable type. */
236 int subtype; /* Subtype. */
239 /* MATCH FILES private data. */
240 struct match_files_proc
242 struct variable *master; /* Corresponding master file variable. */
246 /* Script variables. */
251 NUMERIC, /* A numeric variable. */
252 ALPHA /* A string variable. (STRING is pre-empted by lexer.h) */
255 /* Types of missing values. Order is significant, see
256 mis-val.c:parse_numeric(), sfm-read.c:sfm_read_dictionary()
257 sfm-write.c:sfm_write_dictionary(),
258 sysfile-info.c:cmd_sysfile_info(), mis-val.c:copy_missing_values(),
259 pfm-read.c:read_variables(), pfm-write.c:write_variables(),
260 apply-dict.c:cmd_apply_dictionary(), and more (?). */
263 MISSING_NONE, /* No user-missing values. */
264 MISSING_1, /* One user-missing value. */
265 MISSING_2, /* Two user-missing values. */
266 MISSING_3, /* Three user-missing values. */
267 MISSING_RANGE, /* [a,b]. */
268 MISSING_LOW, /* (-inf,a]. */
269 MISSING_HIGH, /* (a,+inf]. */
270 MISSING_RANGE_1, /* [a,b], c. */
271 MISSING_LOW_1, /* (-inf,a], b. */
272 MISSING_HIGH_1, /* (a,+inf), b. */
276 /* A variable's dictionary entry. Note: don't reorder name[] from the
277 first element; a pointer to `variable' should be a pointer to
278 member `name'. FIXME: is this comment still accurate? */
281 /* Required by parse_variables() to be in this order. */
282 char name[9]; /* As a string. */
283 int index; /* Index into its dictionary's var[]. */
284 int type; /* NUMERIC or ALPHA. */
286 /* Also important but parse_variables() doesn't need it. Still,
287 check before reordering. */
288 int width; /* Size of string variables in chars. */
289 int fv, nv; /* Index into `value's, number of values. */
290 unsigned init : 1; /* 1=VFM must init and possibly reinit. */
291 unsigned reinit : 1; /* Cases are: 1=reinitialized; 0=left. */
293 /* Missing values. */
294 int miss_type; /* One of the MISSING_* constants. */
295 union value missing[3]; /* User-missing value. */
297 /* Display formats. */
298 struct fmt_spec print; /* Default format for PRINT. */
299 struct fmt_spec write; /* Default format for WRITE. */
302 struct val_labs *val_labs;
303 char *label; /* Variable label. */
305 /* Per-procedure info. */
310 struct crosstab_proc crs;
311 struct descriptives_proc dsc;
312 struct frequencies_proc frq;
313 struct list_proc lst;
314 struct means_proc mns;
315 struct sort_cases_proc srt;
316 struct matrix_data_proc mxd;
317 struct match_files_proc mtf;
318 struct t_test_proc t_t;
323 int compare_variables (const void *, const void *, void *);
324 unsigned hash_variable (const void *, void *);
326 /* Classes of variables. */
329 DC_ORDINARY, /* Ordinary identifier. */
330 DC_SYSTEM, /* System variable. */
331 DC_SCRATCH /* Scratch variable. */
334 enum dict_class dict_class_from_id (const char *name);
335 const char *dict_class_to_name (enum dict_class dict_class);
337 /* Vector of variables. */
340 int idx; /* Index for dict_get_vector(). */
341 char name[9]; /* Name. */
342 struct variable **var; /* Vector of variables. */
343 int cnt; /* Number of variables. */
348 /* A single case. (This doesn't need to be a struct anymore, but it
349 remains so for hysterical raisins.) */
357 /* Complete dictionary state. */
360 struct dictionary *dict_create (void);
361 struct dictionary *dict_clone (const struct dictionary *);
362 void dict_clear (struct dictionary *);
363 void dict_destroy (struct dictionary *);
365 size_t dict_get_var_cnt (const struct dictionary *);
366 struct variable *dict_get_var (const struct dictionary *, size_t idx);
367 void dict_get_vars (const struct dictionary *,
368 struct variable ***vars, size_t *cnt,
369 unsigned exclude_classes);
371 struct variable *dict_create_var (struct dictionary *, const char *,
373 struct variable *dict_create_var_assert (struct dictionary *, const char *,
375 struct variable *dict_clone_var (struct dictionary *, const struct variable *,
377 void dict_rename_var (struct dictionary *, struct variable *, const char *);
379 struct variable *dict_lookup_var (const struct dictionary *, const char *);
380 struct variable *dict_lookup_var_assert (const struct dictionary *,
382 int dict_contains_var (const struct dictionary *, const struct variable *);
383 void dict_delete_var (struct dictionary *, struct variable *);
384 void dict_delete_vars (struct dictionary *,
385 struct variable *const *, size_t count);
386 void dict_reorder_vars (struct dictionary *,
387 struct variable *const *, size_t count);
388 int dict_rename_vars (struct dictionary *,
389 struct variable **, char **new_names,
390 size_t count, char **err_name);
392 struct variable *dict_get_weight (const struct dictionary *);
393 double dict_get_case_weight (const struct dictionary *, const struct ccase *);
394 void dict_set_weight (struct dictionary *, struct variable *);
396 struct variable *dict_get_filter (const struct dictionary *);
397 void dict_set_filter (struct dictionary *, struct variable *);
399 int dict_get_case_limit (const struct dictionary *);
400 void dict_set_case_limit (struct dictionary *, int);
402 int dict_get_value_cnt (const struct dictionary *);
403 void dict_compact_values (struct dictionary *);
405 struct variable *const *dict_get_split_vars (const struct dictionary *);
406 size_t dict_get_split_cnt (const struct dictionary *);
407 void dict_set_split_vars (struct dictionary *,
408 struct variable *const *, size_t cnt);
410 const char *dict_get_label (const struct dictionary *);
411 void dict_set_label (struct dictionary *, const char *);
413 const char *dict_get_documents (const struct dictionary *);
414 void dict_set_documents (struct dictionary *, const char *);
416 int dict_create_vector (struct dictionary *,
418 struct variable **, size_t cnt);
419 const struct vector *dict_get_vector (const struct dictionary *,
421 size_t dict_get_vector_cnt (const struct dictionary *);
422 const struct vector *dict_lookup_vector (const struct dictionary *,
424 void dict_clear_vectors (struct dictionary *);
426 void discard_variables (void);
428 /* This is the active file dictionary. */
429 extern struct dictionary *default_dict;
431 /* Transformation state. */
433 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
435 extern struct file_handle *default_handle;
437 /* PROCESS IF expression. */
438 extern struct expression *process_if_expr;
440 /* TEMPORARY support. */
442 /* 1=TEMPORARY has been executed at some point. */
443 extern int temporary;
445 /* If temporary!=0, the saved dictionary. */
446 extern struct dictionary *temp_dict;
448 /* If temporary!=0, index into t_trns[] (declared far below) that
449 gives the point at which data should be written out. -1 means that
450 the data shouldn't be changed since all transformations are
452 extern int temp_trns;
454 /* If FILTER is active, whether it was executed before or after
456 extern int FILTER_before_TEMPORARY;
458 void cancel_temporary (void);
462 void dump_split_vars (const struct ccase *);
464 int is_num_user_missing (double, const struct variable *);
465 int is_str_user_missing (const unsigned char[], const struct variable *);
466 int is_missing (const union value *, const struct variable *);
467 int is_system_missing (const union value *, const struct variable *);
468 int is_user_missing (const union value *, const struct variable *);
469 void copy_missing_values (struct variable *dest, const struct variable *src);
471 /* Transformations. */
473 /* Header for all transformations. */
476 /* Index into t_trns[]. */
479 /* Transformation proc. */
480 int (*proc) (struct trns_header *, struct ccase *);
482 /* Garbage collector proc. */
483 void (*free) (struct trns_header *);
486 /* Array of transformations */
487 extern struct trns_header **t_trns;
489 /* Number of transformations, maximum number in array currently. */
490 extern int n_trns, m_trns;
492 /* Index of first transformation that is really a transformation. Any
493 transformations before this belong to INPUT PROGRAM. */
496 void add_transformation (struct trns_header *trns);
497 void cancel_transformations (void);
501 struct var_set *var_set_create_from_dict (struct dictionary *d);
502 struct var_set *var_set_create_from_array (struct variable **var, size_t);
504 size_t var_set_get_cnt (struct var_set *vs);
505 struct variable *var_set_get_var (struct var_set *vs, size_t idx);
506 struct variable *var_set_lookup_var (struct var_set *vs, const char *name);
507 void var_set_destroy (struct var_set *vs);
510 /* Variable parsers. */
514 PV_NONE = 0, /* No options. */
515 PV_SINGLE = 0001, /* Restrict to a single name or TO use. */
516 PV_DUPLICATE = 0002, /* Don't merge duplicates. */
517 PV_APPEND = 0004, /* Append to existing list. */
518 PV_NO_DUPLICATE = 0010, /* Error on duplicates. */
519 PV_NUMERIC = 0020, /* Vars must be numeric. */
520 PV_STRING = 0040, /* Vars must be string. */
521 PV_SAME_TYPE = 00100, /* All vars must be the same type. */
522 PV_NO_SCRATCH = 00200, /* Disallow scratch variables. */
525 struct variable *parse_variable (void);
526 struct variable *parse_dict_variable (struct dictionary *);
527 int parse_variables (struct dictionary *, struct variable ***, int *,
529 int parse_var_set_vars (struct var_set *, struct variable ***, int *,
531 int parse_DATA_LIST_vars (char ***names, int *cnt, int opts);
532 int parse_mixed_vars (char ***names, int *cnt, int opts);