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 /* Frequency tables. */
30 /* Frequency table entry. */
33 union value v; /* The value. */
34 double c; /* The number of occurrences of the value. */
37 /* Types of frequency tables. */
44 /* Entire frequency table. */
47 int mode; /* FRQM_GENERAL or FRQM_INTEGER. */
50 struct hsh_table *data; /* Undifferentiated data. */
53 double *vector; /* Frequencies proper. */
54 int min, max; /* The boundaries of the table. */
55 double out_of_range; /* Sum of weights of out-of-range values. */
56 double sysmis; /* Sum of weights of SYSMIS values. */
59 struct freq *valid; /* Valid freqs. */
60 int n_valid; /* Number of total freqs. */
62 struct freq *missing; /* Missing freqs. */
63 int n_missing; /* Number of missing freqs. */
66 double total_cases; /* Sum of weights of all cases. */
67 double valid_cases; /* Sum of weights of valid cases. */
70 /* Procedures' private per-variable data. */
72 /* Structure name suffixes for private data:
73 _proc: for a procedure (i.e., LIST -> list_proc).
74 _trns: for a transformation (i.e., COMPUTE -> compute_trns.
75 _pgm: for an input program (i.e., DATA LIST -> data_list_pgm). */
77 /* CROSSTABS private data. */
80 /* Integer mode only. */
81 int min; /* Minimum value. */
82 int max; /* Maximum value + 1. */
83 int count; /* max - min. */
87 /* FREQUENCIES private data. */
90 frq_mean = 0, frq_semean, frq_median, frq_mode, frq_stddev, frq_variance,
91 frq_kurt, frq_sekurt, frq_skew, frq_seskew, frq_range, frq_min, frq_max,
95 struct frequencies_proc
97 int used; /* 1=This variable already used. */
100 struct freq_tab tab; /* Frequencies table to use. */
103 int n_groups; /* Number of groups. */
104 double *groups; /* Groups. */
107 double stat[frq_n_stats];
110 /* LIST private data. */
113 int newline; /* Whether a new line begins here. */
114 int width; /* Field width. */
115 int vert; /* Whether to print the varname vertically. */
118 /* DESCRIPTIVES private data. Note that the DESCRIPTIVES procedure also
119 has a transformation, descriptives_trns. */
122 /* As these are used as bit indexes, there must be 32 or fewer.
123 Be very careful in adjusting these, see the structure below
124 and the table in descriptives.q. */
125 dsc_mean = 0, dsc_semean, dsc_stddev, dsc_variance, dsc_kurt,
126 dsc_sekurt, dsc_skew, dsc_seskew, dsc_range, dsc_min,
127 dsc_max, dsc_sum, dsc_n_stats
130 struct descriptives_proc
133 int dup; /* Finds duplicates in list of
135 char zname[10]; /* Name for z-score variable. */
138 double valid, miss; /* Valid, missing--general. */
140 /* Mean, moments about the mean. */
141 double X_bar, M2, M3, M4;
145 double stats[dsc_n_stats]; /* Everything glommed together. */
148 /* GET private data. */
151 int fv, nv; /* First, # of values. */
154 /* MEANS private data. */
157 double min, max; /* Range for integer mode. */
160 /* Different types of variables for MATRIX DATA procedure. Order is
161 important: these are used for sort keys. */
164 MXD_SPLIT, /* SPLIT FILE variables. */
165 MXD_ROWTYPE, /* ROWTYPE_. */
166 MXD_FACTOR, /* Factor variables. */
167 MXD_VARNAME, /* VARNAME_. */
168 MXD_CONTINUOUS, /* Continuous variables. */
173 /* MATRIX DATA private data. */
174 struct matrix_data_proc
176 int vartype; /* Variable type. */
177 int subtype; /* Subtype. */
180 /* MATCH FILES private data. */
181 struct match_files_proc
183 struct variable *master; /* Corresponding master file variable. */
187 /* Script variables. */
192 NUMERIC, /* A numeric variable. */
193 ALPHA /* A string variable. (STRING is pre-empted by lexer.h) */
196 /* Types of missing values. Order is significant, see
197 mis-val.c:parse_numeric(), sfm-read.c:sfm_read_dictionary()
198 sfm-write.c:sfm_write_dictionary(),
199 sysfile-info.c:cmd_sysfile_info(), mis-val.c:copy_missing_values(),
200 pfm-read.c:read_variables(), pfm-write.c:write_variables(),
201 apply-dict.c:cmd_apply_dictionary(), and more (?). */
204 MISSING_NONE, /* No user-missing values. */
205 MISSING_1, /* One user-missing value. */
206 MISSING_2, /* Two user-missing values. */
207 MISSING_3, /* Three user-missing values. */
208 MISSING_RANGE, /* [a,b]. */
209 MISSING_LOW, /* (-inf,a]. */
210 MISSING_HIGH, /* (a,+inf]. */
211 MISSING_RANGE_1, /* [a,b], c. */
212 MISSING_LOW_1, /* (-inf,a], b. */
213 MISSING_HIGH_1, /* (a,+inf), b. */
217 /* A variable's dictionary entry. */
220 char name[9]; /* As a string. */
221 int index; /* Index into its dictionary's var[]. */
222 int type; /* NUMERIC or ALPHA. */
224 int width; /* Size of string variables in chars. */
225 int fv, nv; /* Index into `value's, number of values. */
226 unsigned init : 1; /* 1=VFM must init and possibly reinit. */
227 unsigned reinit : 1; /* Cases are: 1=reinitialized; 0=left. */
229 /* Missing values. */
230 int miss_type; /* One of the MISSING_* constants. */
231 union value missing[3]; /* User-missing value. */
233 /* Display formats. */
234 struct fmt_spec print; /* Default format for PRINT. */
235 struct fmt_spec write; /* Default format for WRITE. */
238 struct val_labs *val_labs;
239 char *label; /* Variable label. */
241 /* Per-procedure info. */
246 struct crosstab_proc crs;
247 struct descriptives_proc dsc;
248 struct frequencies_proc frq;
249 struct list_proc lst;
250 struct means_proc mns;
251 struct matrix_data_proc mxd;
252 struct match_files_proc mtf;
253 struct t_test_proc t_t;
258 int compare_variables (const void *, const void *, void *);
259 unsigned hash_variable (const void *, void *);
261 /* Classes of variables. */
264 DC_ORDINARY, /* Ordinary identifier. */
265 DC_SYSTEM, /* System variable. */
266 DC_SCRATCH /* Scratch variable. */
269 enum dict_class dict_class_from_id (const char *name);
270 const char *dict_class_to_name (enum dict_class dict_class);
272 /* Vector of variables. */
275 int idx; /* Index for dict_get_vector(). */
276 char name[9]; /* Name. */
277 struct variable **var; /* Vector of variables. */
278 int cnt; /* Number of variables. */
283 /* A single case. (This doesn't need to be a struct anymore, but it
284 remains so for hysterical raisins.) */
290 /* Linked list of cases. */
293 struct case_list *next;
299 /* Complete dictionary state. */
302 struct dictionary *dict_create (void);
303 struct dictionary *dict_clone (const struct dictionary *);
304 void dict_clear (struct dictionary *);
305 void dict_destroy (struct dictionary *);
307 size_t dict_get_var_cnt (const struct dictionary *);
308 struct variable *dict_get_var (const struct dictionary *, size_t idx);
309 void dict_get_vars (const struct dictionary *,
310 struct variable ***vars, size_t *cnt,
311 unsigned exclude_classes);
313 struct variable *dict_create_var (struct dictionary *, const char *,
315 struct variable *dict_create_var_assert (struct dictionary *, const char *,
317 struct variable *dict_clone_var (struct dictionary *, const struct variable *,
319 void dict_rename_var (struct dictionary *, struct variable *, const char *);
321 struct variable *dict_lookup_var (const struct dictionary *, const char *);
322 struct variable *dict_lookup_var_assert (const struct dictionary *,
324 int dict_contains_var (const struct dictionary *, const struct variable *);
325 void dict_delete_var (struct dictionary *, struct variable *);
326 void dict_delete_vars (struct dictionary *,
327 struct variable *const *, size_t count);
328 void dict_reorder_vars (struct dictionary *,
329 struct variable *const *, size_t count);
330 int dict_rename_vars (struct dictionary *,
331 struct variable **, char **new_names,
332 size_t count, char **err_name);
334 struct variable *dict_get_weight (const struct dictionary *);
335 double dict_get_case_weight (const struct dictionary *, const struct ccase *);
336 void dict_set_weight (struct dictionary *, struct variable *);
338 struct variable *dict_get_filter (const struct dictionary *);
339 void dict_set_filter (struct dictionary *, struct variable *);
341 int dict_get_case_limit (const struct dictionary *);
342 void dict_set_case_limit (struct dictionary *, int);
344 int dict_get_next_value_idx (const struct dictionary *);
345 size_t dict_get_case_size (const struct dictionary *);
347 void dict_compact_values (struct dictionary *);
348 size_t dict_get_compacted_value_cnt (const struct dictionary *);
349 int *dict_get_compacted_idx_to_fv (const struct dictionary *);
351 struct variable *const *dict_get_split_vars (const struct dictionary *);
352 size_t dict_get_split_cnt (const struct dictionary *);
353 void dict_set_split_vars (struct dictionary *,
354 struct variable *const *, size_t cnt);
356 const char *dict_get_label (const struct dictionary *);
357 void dict_set_label (struct dictionary *, const char *);
359 const char *dict_get_documents (const struct dictionary *);
360 void dict_set_documents (struct dictionary *, const char *);
362 int dict_create_vector (struct dictionary *,
364 struct variable **, size_t cnt);
365 const struct vector *dict_get_vector (const struct dictionary *,
367 size_t dict_get_vector_cnt (const struct dictionary *);
368 const struct vector *dict_lookup_vector (const struct dictionary *,
370 void dict_clear_vectors (struct dictionary *);
372 void discard_variables (void);
374 /* This is the active file dictionary. */
375 extern struct dictionary *default_dict;
377 /* Transformation state. */
379 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
381 extern struct file_handle *default_handle;
383 /* PROCESS IF expression. */
384 extern struct expression *process_if_expr;
386 /* TEMPORARY support. */
388 /* 1=TEMPORARY has been executed at some point. */
389 extern int temporary;
391 /* If temporary!=0, the saved dictionary. */
392 extern struct dictionary *temp_dict;
394 /* If temporary!=0, index into t_trns[] (declared far below) that
395 gives the point at which data should be written out. -1 means that
396 the data shouldn't be changed since all transformations are
398 extern int temp_trns;
400 /* If FILTER is active, whether it was executed before or after
402 extern int FILTER_before_TEMPORARY;
404 void cancel_temporary (void);
408 void dump_split_vars (const struct ccase *);
409 typedef int (* is_missing_func )(const union value *, const struct variable *);
411 int is_num_user_missing (double, const struct variable *);
412 int is_str_user_missing (const unsigned char[], const struct variable *);
413 int is_missing (const union value *, const struct variable *);
414 int is_system_missing (const union value *, const struct variable *);
415 int is_user_missing (const union value *, const struct variable *);
416 void copy_missing_values (struct variable *dest, const struct variable *src);
418 /* Transformations. */
421 typedef int trns_proc_func (struct trns_header *, struct ccase *, int);
422 typedef void trns_free_func (struct trns_header *);
424 /* Header for all transformations. */
427 int index; /* Index into t_trns[]. */
428 trns_proc_func *proc; /* Transformation proc. */
429 trns_free_func *free; /* Garbage collector proc. */
432 /* Array of transformations */
433 extern struct trns_header **t_trns;
435 /* Number of transformations, maximum number in array currently. */
436 extern int n_trns, m_trns;
438 /* Index of first transformation that is really a transformation. Any
439 transformations before this belong to INPUT PROGRAM. */
442 void add_transformation (struct trns_header *trns);
443 void cancel_transformations (void);
447 struct var_set *var_set_create_from_dict (struct dictionary *d);
448 struct var_set *var_set_create_from_array (struct variable **var, size_t);
450 size_t var_set_get_cnt (struct var_set *vs);
451 struct variable *var_set_get_var (struct var_set *vs, size_t idx);
452 struct variable *var_set_lookup_var (struct var_set *vs, const char *name);
453 void var_set_destroy (struct var_set *vs);
455 /* Variable parsers. */
459 PV_NONE = 0, /* No options. */
460 PV_SINGLE = 0001, /* Restrict to a single name or TO use. */
461 PV_DUPLICATE = 0002, /* Don't merge duplicates. */
462 PV_APPEND = 0004, /* Append to existing list. */
463 PV_NO_DUPLICATE = 0010, /* Error on duplicates. */
464 PV_NUMERIC = 0020, /* Vars must be numeric. */
465 PV_STRING = 0040, /* Vars must be string. */
466 PV_SAME_TYPE = 00100, /* All vars must be the same type. */
467 PV_NO_SCRATCH = 00200, /* Disallow scratch variables. */
470 struct variable *parse_variable (void);
471 struct variable *parse_dict_variable (struct dictionary *);
472 int parse_variables (struct dictionary *, struct variable ***, int *,
474 int parse_var_set_vars (struct var_set *, struct variable ***, int *,
476 int parse_DATA_LIST_vars (char ***names, int *cnt, int opts);
477 int parse_mixed_vars (char ***names, int *cnt, int opts);