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
28 #include <unistd.h> /* Required by SunOS4. */
34 #include "dictionary.h"
37 #include "expressions/public.h"
44 #include "value-labels.h"
47 #define _(msgid) gettext (msgid)
50 Virtual File Manager (vfm):
52 vfm is used to process data files. It uses the model that
53 data is read from one stream (the data source), processed,
54 then written to another (the data sink). The data source is
55 then deleted and the data sink becomes the data source for the
58 /* Procedure execution data. */
59 struct write_case_data
61 /* Function to call for each case. */
62 int (*proc_func) (struct ccase *, void *); /* Function. */
63 void *aux; /* Auxiliary data. */
65 struct ccase trns_case; /* Case used for transformations. */
66 struct ccase sink_case; /* Case written to sink, if
67 compaction is necessary. */
68 size_t cases_written; /* Cases output so far. */
69 size_t cases_analyzed; /* Cases passed to procedure so far. */
72 /* The current active file, from which cases are read. */
73 struct case_source *vfm_source;
75 /* The replacement active file, to which cases are written. */
76 struct case_sink *vfm_sink;
78 /* Nonzero if the case needs to have values deleted before being
79 stored, zero otherwise. */
80 static int compaction_necessary;
82 /* Time at which vfm was last invoked. */
83 time_t last_vfm_invocation;
86 int n_lag; /* Number of cases to lag. */
87 static int lag_count; /* Number of cases in lag_queue so far. */
88 static int lag_head; /* Index where next case will be added. */
89 static struct ccase *lag_queue; /* Array of n_lag ccase * elements. */
91 static void internal_procedure (int (*proc_func) (struct ccase *, void *),
93 static void create_trns_case (struct ccase *, struct dictionary *);
94 static void open_active_file (void);
95 static int write_case (struct write_case_data *wc_data);
96 static int execute_transformations (struct ccase *c,
97 struct trns_header **trns,
98 int first_idx, int last_idx,
100 static int filter_case (const struct ccase *c, int case_num);
101 static void lag_case (const struct ccase *c);
102 static void clear_case (struct ccase *c);
103 static void close_active_file (void);
105 /* Public functions. */
107 /* Reads the data from the input program and writes it to a new
108 active file. For each case we read from the input program, we
111 1. Execute permanent transformations. If these drop the case,
112 start the next case from step 1.
114 2. N OF CASES. If we have already written N cases, start the
115 next case from step 1.
117 3. Write case to replacement active file.
119 4. Execute temporary transformations. If these drop the case,
120 start the next case from step 1.
122 5. FILTER, PROCESS IF. If these drop the case, start the next
125 6. Post-TEMPORARY N OF CASES. If we have already analyzed N
126 cases, start the next case from step 1.
128 7. Pass case to PROC_FUNC, passing AUX as auxiliary data. */
130 procedure (int (*proc_func) (struct ccase *, void *), void *aux)
132 if (proc_func == NULL
133 && case_source_is_class (vfm_source, &storage_source_class)
143 internal_procedure (proc_func, aux);
144 close_active_file ();
147 /* Executes a procedure, as procedure(), except that the caller
148 is responsible for calling open_active_file() and
149 close_active_file(). */
151 internal_procedure (int (*proc_func) (struct ccase *, void *), void *aux)
153 static int recursive_call;
155 struct write_case_data wc_data;
157 assert (++recursive_call == 1);
159 wc_data.proc_func = proc_func;
161 create_trns_case (&wc_data.trns_case, default_dict);
162 case_create (&wc_data.sink_case, dict_get_next_value_idx (default_dict));
163 wc_data.cases_written = 0;
165 last_vfm_invocation = time (NULL);
167 if (vfm_source != NULL)
168 vfm_source->class->read (vfm_source,
170 write_case, &wc_data);
172 case_destroy (&wc_data.sink_case);
173 case_destroy (&wc_data.trns_case);
175 assert (--recursive_call == 0);
178 /* Creates and returns a case, initializing it from the vectors
179 that say which `value's need to be initialized just once, and
180 which ones need to be re-initialized before every case. */
182 create_trns_case (struct ccase *trns_case, struct dictionary *dict)
184 size_t var_cnt = dict_get_var_cnt (dict);
187 case_create (trns_case, dict_get_next_value_idx (dict));
188 for (i = 0; i < var_cnt; i++)
190 struct variable *v = dict_get_var (dict, i);
191 union value *value = case_data_rw (trns_case, v->fv);
193 if (v->type == NUMERIC)
194 value->f = v->reinit ? 0.0 : SYSMIS;
196 memset (value->s, ' ', v->width);
200 /* Makes all preparations for reading from the data source and writing
203 open_active_file (void)
205 /* Make temp_dict refer to the dictionary right before data
210 temp_dict = default_dict;
213 /* Figure out compaction. */
214 compaction_necessary = (dict_get_next_value_idx (temp_dict)
215 != dict_get_compacted_value_cnt (temp_dict));
218 if (vfm_sink == NULL)
219 vfm_sink = create_case_sink (&storage_sink_class, temp_dict, NULL);
220 if (vfm_sink->class->open != NULL)
221 vfm_sink->class->open (vfm_sink);
223 /* Allocate memory for lag queue. */
230 lag_queue = xnmalloc (n_lag, sizeof *lag_queue);
231 for (i = 0; i < n_lag; i++)
232 case_nullify (&lag_queue[i]);
235 /* Close any unclosed DO IF or LOOP constructs. */
236 discard_ctl_stack ();
239 /* Transforms trns_case and writes it to the replacement active
240 file if advisable. Returns nonzero if more cases can be
241 accepted, zero otherwise. Do not call this function again
242 after it has returned zero once. */
244 write_case (struct write_case_data *wc_data)
246 /* Execute permanent transformations. */
247 if (!execute_transformations (&wc_data->trns_case, t_trns, f_trns, temp_trns,
248 wc_data->cases_written + 1))
252 if (dict_get_case_limit (default_dict)
253 && wc_data->cases_written >= dict_get_case_limit (default_dict))
255 wc_data->cases_written++;
257 /* Write case to LAG queue. */
259 lag_case (&wc_data->trns_case);
261 /* Write case to replacement active file. */
262 if (vfm_sink->class->write != NULL)
264 if (compaction_necessary)
266 dict_compact_case (temp_dict, &wc_data->sink_case,
267 &wc_data->trns_case);
268 vfm_sink->class->write (vfm_sink, &wc_data->sink_case);
271 vfm_sink->class->write (vfm_sink, &wc_data->trns_case);
274 /* Execute temporary transformations. */
275 if (!execute_transformations (&wc_data->trns_case, t_trns, temp_trns, n_trns,
276 wc_data->cases_written))
279 /* FILTER, PROCESS IF, post-TEMPORARY N OF CASES. */
280 if (filter_case (&wc_data->trns_case, wc_data->cases_written)
281 || (dict_get_case_limit (temp_dict)
282 && wc_data->cases_analyzed >= dict_get_case_limit (temp_dict)))
284 wc_data->cases_analyzed++;
286 /* Pass case to procedure. */
287 if (wc_data->proc_func != NULL)
288 wc_data->proc_func (&wc_data->trns_case, wc_data->aux);
291 clear_case (&wc_data->trns_case);
295 /* Transforms case C using the transformations in TRNS[] with
296 indexes FIRST_IDX through LAST_IDX, exclusive. Case C will
297 become case CASE_NUM (1-based) in the output file. Returns
298 zero if the case was filtered out by one of the
299 transformations, nonzero otherwise. */
301 execute_transformations (struct ccase *c,
302 struct trns_header **trns,
303 int first_idx, int last_idx,
308 for (idx = first_idx; idx != last_idx; )
310 int retval = trns[idx]->proc (trns[idx], c, case_num);
329 /* Returns nonzero if case C with case number CASE_NUM should be
330 exclude as specified on FILTER or PROCESS IF, otherwise
333 filter_case (const struct ccase *c, int case_idx)
336 struct variable *filter_var = dict_get_filter (default_dict);
337 if (filter_var != NULL)
339 double f = case_num (c, filter_var->fv);
340 if (f == 0.0 || mv_is_num_missing (&filter_var->miss, f))
345 if (process_if_expr != NULL
346 && expr_evaluate_num (process_if_expr, c, case_idx) != 1.0)
352 /* Add C to the lag queue. */
354 lag_case (const struct ccase *c)
356 if (lag_count < n_lag)
358 case_destroy (&lag_queue[lag_head]);
359 case_clone (&lag_queue[lag_head], c);
360 if (++lag_head >= n_lag)
364 /* Clears the variables in C that need to be cleared between
367 clear_case (struct ccase *c)
369 size_t var_cnt = dict_get_var_cnt (default_dict);
372 for (i = 0; i < var_cnt; i++)
374 struct variable *v = dict_get_var (default_dict, i);
375 if (v->init && v->reinit)
377 if (v->type == NUMERIC)
378 case_data_rw (c, v->fv)->f = SYSMIS;
380 memset (case_data_rw (c, v->fv)->s, ' ', v->width);
385 /* Closes the active file. */
387 close_active_file (void)
389 /* Free memory for lag queue, and turn off lagging. */
394 for (i = 0; i < n_lag; i++)
395 case_destroy (&lag_queue[i]);
400 /* Dictionary from before TEMPORARY becomes permanent.. */
403 dict_destroy (default_dict);
404 default_dict = temp_dict;
408 /* Finish compaction. */
409 if (compaction_necessary)
410 dict_compact_values (default_dict);
412 /* Free data source. */
413 free_case_source (vfm_source);
416 /* Old data sink becomes new data source. */
417 if (vfm_sink->class->make_source != NULL)
418 vfm_source = vfm_sink->class->make_source (vfm_sink);
419 free_case_sink (vfm_sink);
422 /* Cancel TEMPORARY, PROCESS IF, FILTER, N OF CASES, vectors,
423 and get rid of all the transformations. */
425 expr_free (process_if_expr);
426 process_if_expr = NULL;
427 if (dict_get_filter (default_dict) != NULL && !FILTER_before_TEMPORARY)
428 dict_set_filter (default_dict, NULL);
429 dict_set_case_limit (default_dict, 0);
430 dict_clear_vectors (default_dict);
431 cancel_transformations ();
434 /* Storage case stream. */
436 /* Information about storage sink or source. */
437 struct storage_stream_info
439 struct casefile *casefile; /* Storage. */
442 /* Initializes a storage sink. */
444 storage_sink_open (struct case_sink *sink)
446 struct storage_stream_info *info;
448 sink->aux = info = xmalloc (sizeof *info);
449 info->casefile = casefile_create (sink->value_cnt);
452 /* Destroys storage stream represented by INFO. */
454 destroy_storage_stream_info (struct storage_stream_info *info)
458 casefile_destroy (info->casefile);
463 /* Writes case C to the storage sink SINK. */
465 storage_sink_write (struct case_sink *sink, const struct ccase *c)
467 struct storage_stream_info *info = sink->aux;
469 casefile_append (info->casefile, c);
472 /* Destroys internal data in SINK. */
474 storage_sink_destroy (struct case_sink *sink)
476 destroy_storage_stream_info (sink->aux);
479 /* Closes the sink and returns a storage source to read back the
481 static struct case_source *
482 storage_sink_make_source (struct case_sink *sink)
484 struct case_source *source
485 = create_case_source (&storage_source_class, sink->aux);
491 const struct case_sink_class storage_sink_class =
496 storage_sink_destroy,
497 storage_sink_make_source,
500 /* Storage source. */
502 /* Returns the number of cases that will be read by
503 storage_source_read(). */
505 storage_source_count (const struct case_source *source)
507 struct storage_stream_info *info = source->aux;
509 return casefile_get_case_cnt (info->casefile);
512 /* Reads all cases from the storage source and passes them one by one to
515 storage_source_read (struct case_source *source,
516 struct ccase *output_case,
517 write_case_func *write_case, write_case_data wc_data)
519 struct storage_stream_info *info = source->aux;
520 struct ccase casefile_case;
521 struct casereader *reader;
523 for (reader = casefile_get_reader (info->casefile);
524 casereader_read (reader, &casefile_case);
525 case_destroy (&casefile_case))
527 case_copy (output_case, 0,
529 casefile_get_value_cnt (info->casefile));
530 write_case (wc_data);
532 casereader_destroy (reader);
535 /* Destroys the source's internal data. */
537 storage_source_destroy (struct case_source *source)
539 destroy_storage_stream_info (source->aux);
542 /* Storage source. */
543 const struct case_source_class storage_source_class =
546 storage_source_count,
548 storage_source_destroy,
552 storage_source_get_casefile (struct case_source *source)
554 struct storage_stream_info *info = source->aux;
556 assert (source->class == &storage_source_class);
557 return info->casefile;
561 storage_source_create (struct casefile *cf)
563 struct storage_stream_info *info;
565 info = xmalloc (sizeof *info);
568 return create_case_source (&storage_source_class, info);
571 /* Null sink. Used by a few procedures that keep track of output
572 themselves and would throw away anything that the sink
575 const struct case_sink_class null_sink_class =
584 /* Returns a pointer to the lagged case from N_BEFORE cases before the
585 current one, or NULL if there haven't been that many cases yet. */
587 lagged_case (int n_before)
589 assert (n_before >= 1 );
590 assert (n_before <= n_lag);
592 if (n_before <= lag_count)
594 int index = lag_head - n_before;
597 return &lag_queue[index];
603 /* Appends TRNS to t_trns[], the list of all transformations to be
604 performed on data as it is read from the active file. */
606 add_transformation (struct trns_header * trns)
608 if (n_trns >= m_trns)
611 t_trns = xnrealloc (t_trns, m_trns, sizeof *t_trns);
613 t_trns[n_trns] = trns;
614 trns->index = n_trns++;
617 /* Cancels all active transformations, including any transformations
618 created by the input program. */
620 cancel_transformations (void)
623 for (i = 0; i < n_trns; i++)
626 t_trns[i]->free (t_trns[i]);
635 /* Creates a case source with class CLASS and auxiliary data AUX
636 and based on dictionary DICT. */
638 create_case_source (const struct case_source_class *class,
641 struct case_source *source = xmalloc (sizeof *source);
642 source->class = class;
647 /* Destroys case source SOURCE. It is the caller's responsible to
648 call the source's destroy function, if any. */
650 free_case_source (struct case_source *source)
654 if (source->class->destroy != NULL)
655 source->class->destroy (source);
660 /* Returns nonzero if a case source is "complex". */
662 case_source_is_complex (const struct case_source *source)
664 return source != NULL && (source->class == &input_program_source_class
665 || source->class == &file_type_source_class);
668 /* Returns nonzero if CLASS is the class of SOURCE. */
670 case_source_is_class (const struct case_source *source,
671 const struct case_source_class *class)
673 return source != NULL && source->class == class;
676 /* Creates a case sink to accept cases from the given DICT with
677 class CLASS and auxiliary data AUX. */
679 create_case_sink (const struct case_sink_class *class,
680 const struct dictionary *dict,
683 struct case_sink *sink = xmalloc (sizeof *sink);
685 sink->value_cnt = dict_get_compacted_value_cnt (dict);
690 /* Destroys case sink SINK. */
692 free_case_sink (struct case_sink *sink)
696 if (sink->class->destroy != NULL)
697 sink->class->destroy (sink);
702 /* Represents auxiliary data for handling SPLIT FILE. */
703 struct split_aux_data
705 size_t case_count; /* Number of cases so far. */
706 struct ccase prev_case; /* Data in previous case. */
708 /* Functions to call... */
709 void (*begin_func) (void *); /* ...before data. */
710 int (*proc_func) (struct ccase *, void *); /* ...with data. */
711 void (*end_func) (void *); /* ...after data. */
712 void *func_aux; /* Auxiliary data. */
715 static int equal_splits (const struct ccase *, const struct ccase *);
716 static int procedure_with_splits_callback (struct ccase *, void *);
717 static void dump_splits (struct ccase *);
719 /* Like procedure(), but it automatically breaks the case stream
720 into SPLIT FILE break groups. Before each group of cases with
721 identical SPLIT FILE variable values, BEGIN_FUNC is called.
722 Then PROC_FUNC is called with each case in the group.
723 END_FUNC is called when the group is finished. FUNC_AUX is
724 passed to each of the functions as auxiliary data.
726 If the active file is empty, none of BEGIN_FUNC, PROC_FUNC,
727 and END_FUNC will be called at all.
729 If SPLIT FILE is not in effect, then there is one break group
730 (if the active file is nonempty), and BEGIN_FUNC and END_FUNC
731 will be called once. */
733 procedure_with_splits (void (*begin_func) (void *aux),
734 int (*proc_func) (struct ccase *, void *aux),
735 void (*end_func) (void *aux),
738 struct split_aux_data split_aux;
740 split_aux.case_count = 0;
741 case_nullify (&split_aux.prev_case);
742 split_aux.begin_func = begin_func;
743 split_aux.proc_func = proc_func;
744 split_aux.end_func = end_func;
745 split_aux.func_aux = func_aux;
748 internal_procedure (procedure_with_splits_callback, &split_aux);
749 if (split_aux.case_count > 0 && end_func != NULL)
751 close_active_file ();
753 case_destroy (&split_aux.prev_case);
756 /* procedure() callback used by procedure_with_splits(). */
758 procedure_with_splits_callback (struct ccase *c, void *split_aux_)
760 struct split_aux_data *split_aux = split_aux_;
762 /* Start a new series if needed. */
763 if (split_aux->case_count == 0
764 || !equal_splits (c, &split_aux->prev_case))
766 if (split_aux->case_count > 0 && split_aux->end_func != NULL)
767 split_aux->end_func (split_aux->func_aux);
770 case_destroy (&split_aux->prev_case);
771 case_clone (&split_aux->prev_case, c);
773 if (split_aux->begin_func != NULL)
774 split_aux->begin_func (split_aux->func_aux);
777 split_aux->case_count++;
778 if (split_aux->proc_func != NULL)
779 return split_aux->proc_func (c, split_aux->func_aux);
784 /* Compares the SPLIT FILE variables in cases A and B and returns
785 nonzero only if they differ. */
787 equal_splits (const struct ccase *a, const struct ccase *b)
789 return case_compare (a, b,
790 dict_get_split_vars (default_dict),
791 dict_get_split_cnt (default_dict)) == 0;
794 /* Dumps out the values of all the split variables for the case C. */
796 dump_splits (struct ccase *c)
798 struct variable *const *split;
803 split_cnt = dict_get_split_cnt (default_dict);
807 t = tab_create (3, split_cnt + 1, 0);
808 tab_dim (t, tab_natural_dimensions);
809 tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, split_cnt);
810 tab_vline (t, TAL_1 | TAL_SPACING, 2, 0, split_cnt);
811 tab_text (t, 0, 0, TAB_NONE, _("Variable"));
812 tab_text (t, 1, 0, TAB_LEFT, _("Value"));
813 tab_text (t, 2, 0, TAB_LEFT, _("Label"));
814 split = dict_get_split_vars (default_dict);
815 for (i = 0; i < split_cnt; i++)
817 struct variable *v = split[i];
821 assert (v->type == NUMERIC || v->type == ALPHA);
822 tab_text (t, 0, i + 1, TAB_LEFT | TAT_PRINTF, "%s", v->name);
824 data_out (temp_buf, &v->print, case_data (c, v->fv));
826 temp_buf[v->print.w] = 0;
827 tab_text (t, 1, i + 1, TAT_PRINTF, "%.*s", v->print.w, temp_buf);
829 val_lab = val_labs_find (v->val_labs, *case_data (c, v->fv));
831 tab_text (t, 2, i + 1, TAB_LEFT, val_lab);
833 tab_flags (t, SOMF_NO_TITLE);
837 /* Represents auxiliary data for handling SPLIT FILE in a
838 multipass procedure. */
839 struct multipass_split_aux_data
841 struct ccase prev_case; /* Data in previous case. */
842 struct casefile *casefile; /* Accumulates data for a split. */
844 /* Function to call with the accumulated data. */
845 void (*split_func) (const struct casefile *, void *);
846 void *func_aux; /* Auxiliary data. */
849 static int multipass_split_callback (struct ccase *c, void *aux_);
850 static void multipass_split_output (struct multipass_split_aux_data *);
853 multipass_procedure_with_splits (void (*split_func) (const struct casefile *,
857 struct multipass_split_aux_data aux;
859 assert (split_func != NULL);
863 case_nullify (&aux.prev_case);
865 aux.split_func = split_func;
866 aux.func_aux = func_aux;
868 internal_procedure (multipass_split_callback, &aux);
869 if (aux.casefile != NULL)
870 multipass_split_output (&aux);
871 case_destroy (&aux.prev_case);
873 close_active_file ();
876 /* procedure() callback used by multipass_procedure_with_splits(). */
878 multipass_split_callback (struct ccase *c, void *aux_)
880 struct multipass_split_aux_data *aux = aux_;
882 /* Start a new series if needed. */
883 if (aux->casefile == NULL || !equal_splits (c, &aux->prev_case))
885 /* Pass any cases to split_func. */
886 if (aux->casefile != NULL)
887 multipass_split_output (aux);
889 /* Start a new casefile. */
890 aux->casefile = casefile_create (dict_get_next_value_idx (default_dict));
892 /* Record split values. */
894 case_destroy (&aux->prev_case);
895 case_clone (&aux->prev_case, c);
898 casefile_append (aux->casefile, c);
904 multipass_split_output (struct multipass_split_aux_data *aux)
906 assert (aux->casefile != NULL);
907 aux->split_func (aux->casefile, aux->func_aux);
908 casefile_destroy (aux->casefile);
909 aux->casefile = NULL;
913 /* Discards all the current state in preparation for a data-input
914 command like DATA LIST or GET. */
916 discard_variables (void)
918 dict_clear (default_dict);
919 default_handle = NULL;
923 if (vfm_source != NULL)
925 free_case_source (vfm_source);
929 cancel_transformations ();
933 expr_free (process_if_expr);
934 process_if_expr = NULL;
938 pgm_state = STATE_INIT;