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
28 /* Definition of the max length of a short string value, generally
30 #define MAX_SHORT_STRING ((SIZEOF_DOUBLE)>=8 ? ((SIZEOF_DOUBLE)+1)/2*2 : 8)
31 #define MIN_LONG_STRING (MAX_SHORT_STRING+1)
33 /* FYI: It is a bad situation if sizeof(flt64) < MAX_SHORT_STRING:
34 then short string missing values can be truncated in system files
35 because there's only room for as many characters as can fit in a
37 #if MAX_SHORT_STRING > 8
38 #error MAX_SHORT_STRING must be less than 8.
42 #define SYSMIS (-DBL_MAX)
43 #define LOWEST second_lowest_value
44 #define HIGHEST DBL_MAX
46 /* Describes one value, which is either a floating-point number or a
50 /* A numeric value. */
53 /* A short-string value. */
54 unsigned char s[MAX_SHORT_STRING];
56 /* This member is used by data-in.c to return a string result,
57 since it may need to return a long string. As currently
58 implemented, it's a pointer to a static internal buffer in
61 Also used by evaluate_expression() to return a string result.
62 As currently implemented, it's a pointer to a dynamic buffer in
63 the appropriate expression.
65 Also used by the AGGREGATE procedure in handling string
69 /* Sometimes we insert value's in a hash table. */
70 unsigned long hash[SIZEOF_DOUBLE / SIZEOF_LONG];
73 /* Frequency tables. */
75 /* Frequency table entry. */
78 union value v; /* The value. */
79 double c; /* The number of occurrences of the value. */
82 /* Types of frequency tables. */
89 /* Entire frequency table. */
92 int mode; /* FRQM_GENERAL or FRQM_INTEGER. */
95 struct hsh_table *data; /* Undifferentiated data. */
98 double *vector; /* Frequencies proper. */
99 int min, max; /* The boundaries of the table. */
100 double out_of_range; /* Sum of weights of out-of-range values. */
101 double sysmis; /* Sum of weights of SYSMIS values. */
104 struct freq *valid; /* Valid freqs. */
105 int n_valid; /* Number of total freqs. */
107 struct freq *missing; /* Missing freqs. */
108 int n_missing; /* Number of missing freqs. */
111 double total_cases; /* Sum of weights of all cases. */
112 double valid_cases; /* Sum of weights of valid cases. */
115 /* Procedures' private per-variable data. */
117 /* Structure name suffixes for private data:
118 _proc: for a procedure (i.e., LIST -> list_proc).
119 _trns: for a transformation (i.e., COMPUTE -> compute_trns.
120 _pgm: for an input program (i.e., DATA LIST -> data_list_pgm). */
122 /* CROSSTABS private data. */
125 /* Integer mode only. */
126 int min; /* Minimum value. */
127 int max; /* Maximum value + 1. */
128 int count; /* max - min. */
131 /* FREQUENCIES private data. */
134 frq_mean = 0, frq_semean, frq_median, frq_mode, frq_stddev, frq_variance,
135 frq_kurt, frq_sekurt, frq_skew, frq_seskew, frq_range, frq_min, frq_max,
139 struct frequencies_proc
141 int used; /* 1=This variable already used. */
143 /* Freqency table. */
144 struct freq_tab tab; /* Frequencies table to use. */
147 int n_groups; /* Number of groups. */
148 double *groups; /* Groups. */
151 double stat[frq_n_stats];
154 /* LIST private data. */
157 int newline; /* Whether a new line begins here. */
158 int width; /* Field width. */
159 int vert; /* Whether to print the varname vertically. */
162 /* DESCRIPTIVES private data. Note that the DESCRIPTIVES procedure also
163 has a transformation, descriptives_trns. */
166 /* As these are used as bit indexes, there must be 32 or fewer.
167 Be very careful in adjusting these, see the structure below
168 and the table in descriptives.q. */
169 dsc_mean = 0, dsc_semean, dsc_stddev, dsc_variance, dsc_kurt,
170 dsc_sekurt, dsc_skew, dsc_seskew, dsc_range, dsc_min,
171 dsc_max, dsc_sum, dsc_n_stats
174 struct descriptives_proc
177 int dup; /* Finds duplicates in list of
179 char zname[10]; /* Name for z-score variable. */
182 double valid, miss; /* Valid, missing--general. */
184 /* Mean, moments about the mean. */
185 double X_bar, M2, M3, M4;
189 double stats[dsc_n_stats]; /* Everything glommed together. */
192 /* GET private data. */
195 int fv, nv; /* First, # of values. */
201 SRT_ASCEND, /* A, B, C, ..., X, Y, Z. */
202 SRT_DESCEND /* Z, Y, X, ..., C, B, A. */
205 /* SORT CASES private data. */
206 struct sort_cases_proc
208 int order; /* SRT_ASCEND or SRT_DESCEND. */
211 /* MEANS private data. */
214 double min, max; /* Range for integer mode. */
217 /* Different types of variables for MATRIX DATA procedure. Order is
218 important: these are used for sort keys. */
221 MXD_SPLIT, /* SPLIT FILE variables. */
222 MXD_ROWTYPE, /* ROWTYPE_. */
223 MXD_FACTOR, /* Factor variables. */
224 MXD_VARNAME, /* VARNAME_. */
225 MXD_CONTINUOUS, /* Continuous variables. */
230 /* MATRIX DATA private data. */
231 struct matrix_data_proc
233 int vartype; /* Variable type. */
234 int subtype; /* Subtype. */
237 /* MATCH FILES private data. */
238 struct match_files_proc
240 struct variable *master; /* Corresponding master file variable. */
244 /* Script variables. */
249 NUMERIC, /* A numeric variable. */
250 ALPHA /* A string variable. (STRING is pre-empted by lexer.h) */
253 /* Types of missing values. Order is significant, see
254 mis-val.c:parse_numeric(), sfm-read.c:sfm_read_dictionary()
255 sfm-write.c:sfm_write_dictionary(),
256 sysfile-info.c:cmd_sysfile_info(), mis-val.c:copy_missing_values(),
257 pfm-read.c:read_variables(), pfm-write.c:write_variables(),
258 apply-dict.c:cmd_apply_dictionary(), and more (?). */
261 MISSING_NONE, /* No user-missing values. */
262 MISSING_1, /* One user-missing value. */
263 MISSING_2, /* Two user-missing values. */
264 MISSING_3, /* Three user-missing values. */
265 MISSING_RANGE, /* [a,b]. */
266 MISSING_LOW, /* (-inf,a]. */
267 MISSING_HIGH, /* (a,+inf]. */
268 MISSING_RANGE_1, /* [a,b], c. */
269 MISSING_LOW_1, /* (-inf,a], b. */
270 MISSING_HIGH_1, /* (a,+inf), b. */
274 /* A variable's dictionary entry. Note: don't reorder name[] from the
275 first element; a pointer to `variable' should be a pointer to
279 /* Required by parse_variables() to be in this order. */
280 char name[9]; /* As a string. */
281 int index; /* Index into its dictionary's var[]. */
282 int type; /* NUMERIC or ALPHA. */
284 /* Also important but parse_variables() doesn't need it. Still,
285 check before reordering. */
286 int width; /* Size of string variables in chars. */
287 int fv, nv; /* Index into `value's, number of values. */
288 int left; /* 0=reinitialize each case, 1=don't. */
290 /* Missing values. */
291 int miss_type; /* One of the MISSING_* constants. */
292 union value missing[3]; /* User-missing value. */
294 /* Display formats. */
295 struct fmt_spec print; /* Default format for PRINT. */
296 struct fmt_spec write; /* Default format for WRITE. */
299 struct val_labs *val_labs;
300 char *label; /* Variable label. */
302 /* Per-procedure info. */
307 struct crosstab_proc crs;
308 struct descriptives_proc dsc;
309 struct frequencies_proc frq;
310 struct list_proc lst;
311 struct means_proc mns;
312 struct sort_cases_proc srt;
313 struct matrix_data_proc mxd;
314 struct match_files_proc mtf;
319 int compare_variables (const void *, const void *, void *);
320 unsigned hash_variable (const void *, void *);
322 /* Classes of variables. */
325 DC_ORDINARY, /* Ordinary identifier. */
326 DC_SYSTEM, /* System variable. */
327 DC_SCRATCH /* Scratch variable. */
330 enum dict_class dict_class_from_id (const char *name);
331 const char *dict_class_to_name (enum dict_class dict_class);
333 /* Vector of variables. */
336 int idx; /* Index for dict_get_vector(). */
337 char name[9]; /* Name. */
338 struct variable **var; /* Vector of variables. */
339 int cnt; /* Number of variables. */
344 /* A single case. (This doesn't need to be a struct anymore, but it
345 remains so for hysterical raisins.) */
353 /* Complete dictionary state. */
356 struct dictionary *dict_create (void);
357 struct dictionary *dict_clone (const struct dictionary *);
358 void dict_clear (struct dictionary *);
359 void dict_destroy (struct dictionary *);
361 size_t dict_get_var_cnt (const struct dictionary *);
362 struct variable *dict_get_var (const struct dictionary *, size_t idx);
363 void dict_get_vars (const struct dictionary *,
364 struct variable ***vars, size_t *cnt,
365 unsigned exclude_classes);
367 struct variable *dict_create_var (struct dictionary *, const char *,
369 struct variable *dict_clone_var (struct dictionary *, const struct variable *,
371 void dict_rename_var (struct dictionary *, struct variable *, const char *);
373 struct variable *dict_lookup_var (const struct dictionary *, const char *);
374 int dict_contains_var (const struct dictionary *, const struct variable *);
375 void dict_delete_var (struct dictionary *, struct variable *);
376 void dict_delete_vars (struct dictionary *,
377 struct variable *const *, size_t count);
378 void dict_reorder_vars (struct dictionary *,
379 struct variable *const *, size_t count);
380 int dict_rename_vars (struct dictionary *,
381 struct variable **, char **new_names,
382 size_t count, char **err_name);
384 struct variable *dict_get_weight (const struct dictionary *);
385 double dict_get_case_weight (const struct dictionary *, const struct ccase *);
386 void dict_set_weight (struct dictionary *, struct variable *);
388 struct variable *dict_get_filter (const struct dictionary *);
389 void dict_set_filter (struct dictionary *, struct variable *);
391 int dict_get_case_limit (const struct dictionary *);
392 void dict_set_case_limit (struct dictionary *, int);
394 int dict_get_value_cnt (const struct dictionary *);
395 void dict_compact_values (struct dictionary *);
397 struct variable *const *dict_get_split_vars (const struct dictionary *);
398 size_t dict_get_split_cnt (const struct dictionary *);
399 void dict_set_split_vars (struct dictionary *,
400 struct variable *const *, size_t cnt);
402 const char *dict_get_label (const struct dictionary *);
403 void dict_set_label (struct dictionary *, const char *);
405 const char *dict_get_documents (const struct dictionary *);
406 void dict_set_documents (struct dictionary *, const char *);
408 int dict_create_vector (struct dictionary *,
410 struct variable **, size_t cnt);
411 const struct vector *dict_get_vector (const struct dictionary *,
413 size_t dict_get_vector_cnt (const struct dictionary *);
414 const struct vector *dict_lookup_vector (const struct dictionary *,
416 void dict_clear_vectors (struct dictionary *);
418 void discard_variables (void);
420 /* This is the active file dictionary. */
421 extern struct dictionary *default_dict;
423 /* Transformation state. */
425 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
427 extern struct file_handle *default_handle;
429 /* PROCESS IF expression. */
430 extern struct expression *process_if_expr;
432 /* TEMPORARY support. */
434 /* 1=TEMPORARY has been executed at some point. */
435 extern int temporary;
437 /* If temporary!=0, the saved dictionary. */
438 extern struct dictionary *temp_dict;
440 /* If temporary!=0, index into t_trns[] (declared far below) that
441 gives the point at which data should be written out. -1 means that
442 the data shouldn't be changed since all transformations are
444 extern int temp_trns;
446 /* If FILTER is active, whether it was executed before or after
448 extern int FILTER_before_TEMPORARY;
450 void cancel_temporary (void);
454 void dump_split_vars (const struct ccase *);
456 int is_num_user_missing (double, const struct variable *);
457 int is_str_user_missing (const unsigned char[], const struct variable *);
458 int is_missing (const union value *, const struct variable *);
459 int is_system_missing (const union value *, const struct variable *);
460 int is_user_missing (const union value *, const struct variable *);
461 void copy_missing_values (struct variable *dest, const struct variable *src);
464 struct variable *force_create_variable (struct dictionary *, const char *name,
465 int type, int width);
466 struct variable *force_dup_variable (struct dictionary *,
467 const struct variable *src,
470 #define force_create_variable(A, B, C, D) \
471 create_variable (A, B, C, D)
472 #define force_dup_variable(A, B, C) \
473 dup_variable (A, B, C)
477 /* Transformations. */
479 /* Header for all transformations. */
482 /* Index into t_trns[]. */
485 /* Transformation proc. */
486 int (*proc) (struct trns_header *, struct ccase *);
488 /* Garbage collector proc. */
489 void (*free) (struct trns_header *);
492 /* Array of transformations */
493 extern struct trns_header **t_trns;
495 /* Number of transformations, maximum number in array currently. */
496 extern int n_trns, m_trns;
498 /* Index of first transformation that is really a transformation. Any
499 transformations before this belong to INPUT PROGRAM. */
502 void add_transformation (struct trns_header *trns);
503 void cancel_transformations (void);
507 struct var_set *var_set_create_from_dict (struct dictionary *d);
508 struct var_set *var_set_create_from_array (struct variable **var, size_t);
510 size_t var_set_get_cnt (struct var_set *vs);
511 struct variable *var_set_get_var (struct var_set *vs, size_t idx);
512 struct variable *var_set_lookup_var (struct var_set *vs, const char *name);
513 void var_set_destroy (struct var_set *vs);
516 /* Variable parsers. */
520 PV_NONE = 0, /* No options. */
521 PV_SINGLE = 0001, /* Restrict to a single name or TO use. */
522 PV_DUPLICATE = 0002, /* Don't merge duplicates. */
523 PV_APPEND = 0004, /* Append to existing list. */
524 PV_NO_DUPLICATE = 0010, /* Error on duplicates. */
525 PV_NUMERIC = 0020, /* Vars must be numeric. */
526 PV_STRING = 0040, /* Vars must be string. */
527 PV_SAME_TYPE = 00100, /* All vars must be the same type. */
528 PV_NO_SCRATCH = 00200, /* Disallow scratch variables. */
531 struct variable *parse_variable (void);
532 struct variable *parse_dict_variable (struct dictionary *);
533 int parse_variables (struct dictionary *, struct variable ***, int *,
535 int parse_var_set_vars (struct var_set *, struct variable ***, int *,
537 int parse_DATA_LIST_vars (char ***names, int *cnt, int opts);
538 int parse_mixed_vars (char ***names, int *cnt, int opts);