X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fvfm.c;h=1ea4211e32c521065c6ab866ec2d92eb14e4bdd3;hb=205ac3afa4c2b19c85819d8695abf3975bb11807;hp=17bd9f266e58e18fa4f67bf745bface9ec0556ec;hpb=3a7fba81ceae5b049d0f7d671e9e3c3c43bbf703;p=pspp-builds.git diff --git a/src/vfm.c b/src/vfm.c index 17bd9f26..1ea4211e 100644 --- a/src/vfm.c +++ b/src/vfm.c @@ -17,27 +17,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* AIX requires this to be the first thing in the file. */ #include -#if __GNUC__ -#define alloca __builtin_alloca -#else -#if HAVE_ALLOCA_H -#include -#else -#ifdef _AIX -#pragma alloca -#else -#ifndef alloca /* predefined by HP cc +Olibcalls */ -char *alloca (); -#endif -#endif -#endif -#endif - #include "vfm.h" #include "vfmP.h" -#include +#include "error.h" #include #include #include @@ -45,12 +28,12 @@ char *alloca (); #include /* Required by SunOS4. */ #endif #include "alloc.h" -#include "approx.h" #include "do-ifP.h" #include "error.h" #include "expr.h" #include "misc.h" #include "random.h" +#include "settings.h" #include "som.h" #include "str.h" #include "tab.h" @@ -60,486 +43,366 @@ char *alloca (); /* Virtual File Manager (vfm): - vfm is used to process data files. It uses the model that data is - read from one stream (the data source), then written to another - (the data sink). The data source is then deleted and the data sink - becomes the data source for the next procedure. */ - -#include "debug-print.h" - -/* This is used to read from the active file. */ -struct case_stream *vfm_source; + vfm is used to process data files. It uses the model that + data is read from one stream (the data source), processed, + then written to another (the data sink). The data source is + then deleted and the data sink becomes the data source for the + next procedure. */ -/* `value' indexes to initialize to particular values for certain cases. */ -struct long_vec reinit_sysmis; /* SYSMIS for every case. */ -struct long_vec reinit_blanks; /* Blanks for every case. */ -struct long_vec init_zero; /* Zero for first case only. */ -struct long_vec init_blanks; /* Blanks for first case only. */ - -/* This is used to write to the replacement active file. */ -struct case_stream *vfm_sink; - -/* Information about the data source. */ -struct stream_info vfm_source_info; - -/* Information about the data sink. */ -struct stream_info vfm_sink_info; +/* Procedure execution data. */ +struct write_case_data + { + /* Function to call for each case. */ + int (*proc_func) (struct ccase *, void *); /* Function. */ + void *aux; /* Auxiliary data. */ + + struct ccase *trns_case; /* Case used for transformations. */ + struct ccase *sink_case; /* Case written to sink, if + compaction is necessary. */ + size_t cases_written; /* Cases output so far. */ + size_t cases_analyzed; /* Cases passed to procedure so far. */ + }; -/* Filter variable and `value' index. */ -static struct variable *filter_var; -static int filter_index; +/* The current active file, from which cases are read. */ +struct case_source *vfm_source; -#define FILTERED \ - (filter_index != -1 \ - && (temp_case->data[filter_index].f == 0.0 \ - || temp_case->data[filter_index].f == SYSMIS \ - || is_num_user_missing (temp_case->data[filter_index].f, \ - filter_var))) +/* The replacement active file, to which cases are written. */ +struct case_sink *vfm_sink; /* Nonzero if the case needs to have values deleted before being stored, zero otherwise. */ -int compaction_necessary; - -/* Number of values after compaction, or the same as - vfm_sink_info.nval, if compaction is not necessary. */ -int compaction_nval; - -/* Temporary case buffer with enough room for `compaction_nval' - `value's. */ -struct ccase *compaction_case; +static int compaction_necessary; -/* Within a session, when paging is turned on, it is never turned back - off. This policy might be too aggressive. */ -static int paging = 0; +/* Nonzero means that we've overflowed our allotted workspace. + After that happens once during a session, we always store the + active file on disk instead of in memory. (This policy may be + too aggressive.) */ +static int workspace_overflow = 0; /* Time at which vfm was last invoked. */ time_t last_vfm_invocation; -/* Functions called during procedure processing. */ -static int (*proc_func) (struct ccase *); /* Called for each case. */ -static int (*virt_proc_func) (struct ccase *); /* From SPLIT_FILE_procfunc. */ -static void (*begin_func) (void); /* Called at beginning of a series. */ -static void (*virt_begin_func) (void); /* Called by SPLIT_FILE_procfunc. */ -static void (*end_func) (void); /* Called after end of a series. */ -int (*write_case) (void); - -/* Number of cases passed to proc_func(). */ -static int case_count; - /* Lag queue. */ int n_lag; /* Number of cases to lag. */ static int lag_count; /* Number of cases in lag_queue so far. */ static int lag_head; /* Index where next case will be added. */ static struct ccase **lag_queue; /* Array of n_lag ccase * elements. */ +static struct ccase *create_trns_case (struct dictionary *); static void open_active_file (void); +static int write_case (struct write_case_data *wc_data); +static int execute_transformations (struct ccase *c, + struct trns_header **trns, + int first_idx, int last_idx, + int case_num); +static int filter_case (const struct ccase *c, int case_num); +static void lag_case (const struct ccase *c); +static void compact_case (struct ccase *dest, const struct ccase *src); +static void clear_case (struct ccase *c); static void close_active_file (void); -static int SPLIT_FILE_procfunc (struct ccase *); -static void finish_compaction (void); -static void lag_case (void); -static int procedure_write_case (void); /* Public functions. */ -/* Reads all the cases from the active file, transforms them by the - active set of transformations, calls PROCFUNC with CURCASE set to - the case and CASENUM set to the case number, and writes them to a - new active file. +/* Reads the data from the input program and writes it to a new + active file. For each case we read from the input program, we + do the following - Divides the active file into zero or more series of one or more - cases each. BEGINFUNC is called before each series. ENDFUNC is - called after each series. */ + 1. Execute permanent transformations. If these drop the case, + start the next case from step 1. + + 2. N OF CASES. If we have already written N cases, start the + next case from step 1. + + 3. Write case to replacement active file. + + 4. Execute temporary transformations. If these drop the case, + start the next case from step 1. + + 5. FILTER, PROCESS IF. If these drop the case, start the next + case from step 1. + + 6. Post-TEMPORARY N OF CASES. If we have already analyzed N + cases, start the next case from step 1. + + 7. Pass case to PROC_FUNC, passing AUX as auxiliary data. */ void -procedure (void (*beginfunc) (void), - int (*procfunc) (struct ccase *curcase), - void (*endfunc) (void)) +procedure (int (*proc_func) (struct ccase *, void *), void *aux) { - end_func = endfunc; - write_case = procedure_write_case; + static int recursive_call; - if (dict_get_split_cnt (default_dict) != 0 && procfunc != NULL) - { - virt_proc_func = procfunc; - proc_func = SPLIT_FILE_procfunc; - - virt_begin_func = beginfunc; - begin_func = NULL; - } else { - begin_func = beginfunc; - proc_func = procfunc; - } + struct write_case_data wc_data; + + assert (++recursive_call == 1); + + wc_data.proc_func = proc_func; + wc_data.aux = aux; + wc_data.trns_case = create_trns_case (default_dict); + wc_data.sink_case = xmalloc (dict_get_case_size (default_dict)); + wc_data.cases_written = 0; last_vfm_invocation = time (NULL); open_active_file (); - vfm_source->read (); + if (vfm_source != NULL) + vfm_source->class->read (vfm_source, + wc_data.trns_case, + write_case, &wc_data); close_active_file (); -} - -/* Active file processing support. Subtly different semantics from - procedure(). */ - -static int process_active_file_write_case (void); - -/* The casefunc might want us to stop calling it. */ -static int not_canceled; - -/* Reads all the cases from the active file and passes them one-by-one - to CASEFUNC in temp_case. Before any cases are passed, calls - BEGINFUNC. After all the cases have been passed, calls ENDFUNC. - BEGINFUNC, CASEFUNC, and ENDFUNC can write temp_case to the output - file by calling process_active_file_output_case(). - process_active_file() ignores TEMPORARY, SPLIT FILE, and N. */ -void -process_active_file (void (*beginfunc) (void), - int (*casefunc) (struct ccase *curcase), - void (*endfunc) (void)) -{ - proc_func = casefunc; - write_case = process_active_file_write_case; - not_canceled = 1; + free (wc_data.sink_case); + free (wc_data.trns_case); - open_active_file (); - beginfunc (); - - /* There doesn't necessarily need to be an active file. */ - if (vfm_source) - vfm_source->read (); - - endfunc (); - close_active_file (); + assert (--recursive_call == 0); } -/* Pass the current case to casefunc. */ -static int -process_active_file_write_case (void) +/* Creates and returns a case, initializing it from the vectors + that say which `value's need to be initialized just once, and + which ones need to be re-initialized before every case. */ +static struct ccase * +create_trns_case (struct dictionary *dict) { - /* Index of current transformation. */ - int cur_trns; + struct ccase *c = xmalloc (dict_get_case_size (dict)); + size_t var_cnt = dict_get_var_cnt (dict); + size_t i; - for (cur_trns = f_trns ; cur_trns != temp_trns; ) + for (i = 0; i < var_cnt; i++) { - int code; - - code = t_trns[cur_trns]->proc (t_trns[cur_trns], temp_case); - switch (code) - { - case -1: - /* Next transformation. */ - cur_trns++; - break; - case -2: - /* Delete this case. */ - goto done; - default: - /* Go to that transformation. */ - cur_trns = code; - break; - } + struct variable *v = dict_get_var (dict, i); + + if (v->type == NUMERIC) + { + if (v->reinit) + c->data[v->fv].f = 0.0; + else + c->data[v->fv].f = SYSMIS; + } + else + memset (c->data[v->fv].s, ' ', v->width); } - - if (n_lag) - lag_case (); - - /* Call the procedure if FILTER and PROCESS IF don't prohibit it. */ - if (not_canceled - && !FILTERED - && (process_if_expr == NULL || - expr_evaluate (process_if_expr, temp_case, NULL) == 1.0)) - not_canceled = proc_func (temp_case); - - case_count++; - - done: - { - long *lp; - - /* This case is finished. Initialize the variables for the next case. */ - for (lp = reinit_sysmis.vec; *lp != -1;) - temp_case->data[*lp++].f = SYSMIS; - for (lp = reinit_blanks.vec; *lp != -1;) - memset (temp_case->data[*lp++].s, ' ', MAX_SHORT_STRING); - } - - return 1; + return c; } -/* Write temp_case to the active file. */ -void -process_active_file_output_case (void) -{ - vfm_sink_info.ncases++; - vfm_sink->write (); -} - -/* Opening the active file. */ - -/* It might be usefully noted that the following several functions are - given in the order that they are called by open_active_file(). */ - -/* Prepare to write to the replacement active file. */ +/* Makes all preparations for reading from the data source and writing + to the data sink. */ static void -prepare_for_writing (void) +open_active_file (void) { - /* FIXME: If ALL the conditions listed below hold true, then the - replacement active file is guaranteed to be identical to the - original active file: - - 1. TEMPORARY was the first transformation, OR, there were no - transformations at all. - - 2. Input is not coming from an input program. - - 3. Compaction is not necessary. + /* Make temp_dict refer to the dictionary right before data + reaches the sink */ + if (!temporary) + { + temp_trns = n_trns; + temp_dict = default_dict; + } - So, in this case, we shouldn't have to replace the active - file--it's just a waste of time and space. */ + /* Figure out compaction. */ + compaction_necessary = (dict_get_next_value_idx (temp_dict) + != dict_get_compacted_value_cnt (temp_dict)); - vfm_sink_info.ncases = 0; - vfm_sink_info.nval = dict_get_value_cnt (default_dict); - vfm_sink_info.case_size = (sizeof (struct ccase) - + ((dict_get_value_cnt (default_dict) - 1) - * sizeof (union value))); - + /* Prepare sink. */ if (vfm_sink == NULL) + vfm_sink = create_case_sink (&storage_sink_class, temp_dict, NULL); + if (vfm_sink->class->open != NULL) + vfm_sink->class->open (vfm_sink); + + /* Allocate memory for lag queue. */ + if (n_lag > 0) { - if (vfm_sink_info.case_size * vfm_source_info.ncases > MAX_WORKSPACE - && !paging) - { - msg (MW, _("Workspace overflow predicted. Max workspace is " - "currently set to %d KB (%d cases at %d bytes each). " - "Paging active file to disk."), - MAX_WORKSPACE / 1024, MAX_WORKSPACE / vfm_sink_info.case_size, - vfm_sink_info.case_size); - - paging = 1; - } - - vfm_sink = paging ? &vfm_disk_stream : &vfm_memory_stream; + int i; + + lag_count = 0; + lag_head = 0; + lag_queue = xmalloc (n_lag * sizeof *lag_queue); + for (i = 0; i < n_lag; i++) + lag_queue[i] = xmalloc (dict_get_case_size (temp_dict)); } + + /* Close any unclosed DO IF or LOOP constructs. */ + discard_ctl_stack (); } -/* Arrange for compacting the output cases for storage. */ -static void -arrange_compaction (void) +/* Transforms trns_case and writes it to the replacement active + file if advisable. Returns nonzero if more cases can be + accepted, zero otherwise. Do not call this function again + after it has returned zero once. */ +static int +write_case (struct write_case_data *wc_data) { - int count_values = 0; + /* Execute permanent transformations. */ + if (!execute_transformations (wc_data->trns_case, t_trns, f_trns, temp_trns, + wc_data->cases_written + 1)) + goto done; + + /* N OF CASES. */ + if (dict_get_case_limit (default_dict) + && wc_data->cases_written >= dict_get_case_limit (default_dict)) + goto done; + wc_data->cases_written++; + + /* Write case to LAG queue. */ + if (n_lag) + lag_case (wc_data->trns_case); - { - int i; - - /* Count up the number of `value's that will be output. */ - for (i = 0; i < dict_get_var_cnt (temp_dict); i++) - { - struct variable *v = dict_get_var (temp_dict, i); - - if (v->name[0] != '#') - { - assert (v->nv > 0); - count_values += v->nv; - } - } - assert (temporary == 2 || count_values <= dict_get_value_cnt (temp_dict)); - } + /* Write case to replacement active file. */ + if (vfm_sink->class->write != NULL) + { + if (compaction_necessary) + { + compact_case (wc_data->sink_case, wc_data->trns_case); + vfm_sink->class->write (vfm_sink, wc_data->sink_case); + } + else + vfm_sink->class->write (vfm_sink, wc_data->trns_case); + } - /* Compaction is only necessary if the number of `value's to output - differs from the number already present. */ - compaction_nval = count_values; - compaction_necessary = (temporary == 2 - || count_values != dict_get_value_cnt (temp_dict)); + /* Execute temporary transformations. */ + if (!execute_transformations (wc_data->trns_case, t_trns, temp_trns, n_trns, + wc_data->cases_written)) + goto done; - if (vfm_sink->init) - vfm_sink->init (); -} + /* FILTER, PROCESS IF, post-TEMPORARY N OF CASES. */ + if (filter_case (wc_data->trns_case, wc_data->cases_written) + || (dict_get_case_limit (temp_dict) + && wc_data->cases_analyzed >= dict_get_case_limit (temp_dict))) + goto done; + wc_data->cases_analyzed++; -/* Prepares the temporary case and compaction case. */ -static void -make_temp_case (void) -{ - temp_case = xmalloc (vfm_sink_info.case_size); + /* Pass case to procedure. */ + if (wc_data->proc_func != NULL) + wc_data->proc_func (wc_data->trns_case, wc_data->aux); - if (compaction_necessary) - compaction_case = xmalloc (sizeof (struct ccase) - + sizeof (union value) * (compaction_nval - 1)); + done: + clear_case (wc_data->trns_case); + return 1; } -#if DEBUGGING -/* Returns the name of the variable that owns the index CCASE_INDEX - into ccase. */ -static const char * -index_to_varname (int ccase_index) +/* Transforms case C using the transformations in TRNS[] with + indexes FIRST_IDX through LAST_IDX, exclusive. Case C will + become case CASE_NUM (1-based) in the output file. Returns + zero if the case was filtered out by one of the + transformations, nonzero otherwise. */ +static int +execute_transformations (struct ccase *c, + struct trns_header **trns, + int first_idx, int last_idx, + int case_num) { - int i; + int idx; - for (i = 0; i < default_dict.nvar; i++) + for (idx = first_idx; idx != last_idx; ) { - struct variable *v = default_dict.var[i]; - - if (ccase_index >= v->fv && ccase_index < v->fv + v->nv) - return default_dict.var[i]->name; + int retval = trns[idx]->proc (trns[idx], c, case_num); + switch (retval) + { + case -1: + idx++; + break; + + case -2: + return 0; + + default: + idx = retval; + break; + } } - return _(""); + + return 1; } -#endif -/* Initializes temp_case from the vectors that say which `value's need - to be initialized just once, and which ones need to be - re-initialized before every case. */ -static void -vector_initialization (void) +/* Returns nonzero if case C with case number CASE_NUM should be + exclude as specified on FILTER or PROCESS IF, otherwise + zero. */ +static int +filter_case (const struct ccase *c, int case_num) { - int i; - long *lp; - - /* Just once. */ - for (i = 0; i < init_zero.n; i++) - temp_case->data[init_zero.vec[i]].f = 0.0; - for (i = 0; i < init_blanks.n; i++) - memset (temp_case->data[init_blanks.vec[i]].s, ' ', MAX_SHORT_STRING); - - /* These vectors need to be repeatedly accessed, so we add a - sentinel to (hopefully) improve speed. */ - vec_insert (&reinit_sysmis, -1); - vec_insert (&reinit_blanks, -1); - - for (lp = reinit_sysmis.vec; *lp != -1;) - temp_case->data[*lp++].f = SYSMIS; - for (lp = reinit_blanks.vec; *lp != -1;) - memset (temp_case->data[*lp++].s, ' ', MAX_SHORT_STRING); - -#if DEBUGGING - printf ("vfm: init_zero="); - for (i = 0; i < init_zero.n; i++) - printf ("%s%s", i ? "," : "", index_to_varname (init_zero.vec[i])); - printf (" init_blanks="); - for (i = 0; i < init_blanks.n; i++) - printf ("%s%s", i ? "," : "", index_to_varname (init_blanks.vec[i])); - printf (" reinit_sysmis="); - for (lp = reinit_sysmis.vec; *lp != -1; lp++) - printf ("%s%s", lp != reinit_sysmis.vec ? "," : "", - index_to_varname (*lp)); - printf (" reinit_blanks="); - for (lp = reinit_blanks.vec; *lp != -1; lp++) - printf ("%s%s", lp != reinit_blanks.vec ? "," : "", - index_to_varname (*lp)); - printf ("\n"); -#endif + /* FILTER. */ + struct variable *filter_var = dict_get_filter (default_dict); + if (filter_var != NULL) + { + double f = c->data[filter_var->fv].f; + if (f == 0.0 || f == SYSMIS || is_num_user_missing (f, filter_var)) + return 1; + } + + /* PROCESS IF. */ + if (process_if_expr != NULL + && expr_evaluate (process_if_expr, c, case_num, NULL) != 1.0) + return 1; + + return 0; } -/* Sets filter_index to an appropriate value. */ +/* Add C to the lag queue. */ static void -setup_filter (void) +lag_case (const struct ccase *c) { - filter_var = dict_get_filter (default_dict); - - if (filter_var != NULL) - { - assert (filter_var->type == NUMERIC); - filter_index = filter_var->index; - } else { - filter_index = -1; - } + if (lag_count < n_lag) + lag_count++; + memcpy (lag_queue[lag_head], c, dict_get_case_size (temp_dict)); + if (++lag_head >= n_lag) + lag_head = 0; } -/* Sets all the lag-related variables based on value of n_lag. */ +/* Copies case SRC to case DEST, compacting it in the process. */ static void -setup_lag (void) +compact_case (struct ccase *dest, const struct ccase *src) { int i; + int nval = 0; + size_t var_cnt; - if (n_lag == 0) - return; + assert (compaction_necessary); - lag_count = 0; - lag_head = 0; - lag_queue = xmalloc (n_lag * sizeof *lag_queue); - for (i = 0; i < n_lag; i++) - lag_queue[i] = xmalloc (dict_get_value_cnt (temp_dict) - * sizeof **lag_queue); + /* Copy all the variables except scratch variables from SRC to + DEST. */ + var_cnt = dict_get_var_cnt (default_dict); + for (i = 0; i < var_cnt; i++) + { + struct variable *v = dict_get_var (default_dict, i); + + if (dict_class_from_id (v->name) == DC_SCRATCH) + continue; + + if (v->type == NUMERIC) + dest->data[nval++] = src->data[v->fv]; + else + { + int w = DIV_RND_UP (v->width, sizeof (union value)); + + memcpy (&dest->data[nval], &src->data[v->fv], w * sizeof (union value)); + nval += w; + } + } } -/* There is a lot of potential confusion in the vfm and related - routines over the number of `value's at each stage of the process. - Here is each nval count, with explanation, as set up by - open_active_file(): - - vfm_source_info.nval: Number of `value's in the cases returned by - the source stream. This value turns out not to be very useful, but - we maintain it anyway. - - vfm_sink_info.nval: Number of `value's in the cases after all - transformations have been performed. Never less than - vfm_source_info.nval. - - temp_dict->nval: Number of `value's in the cases after the - transformations leading up to TEMPORARY have been performed. If - TEMPORARY was not specified, this is equal to vfm_sink_info.nval. - Never less than vfm_sink_info.nval. - - compaction_nval: Number of `value's in the cases after the - transformations leading up to TEMPORARY have been performed and the - case has been compacted by compact_case(), if compaction is - necessary. This the number of `value's in the cases saved by the - sink stream. (However, note that the cases passed to the sink - stream have not yet been compacted. It is the responsibility of - the data sink to call compact_case().) This may be less than, - greater than, or equal to vfm_source_info.nval. `compaction' - becomes the new value of default_dict.nval after the procedure is - completed. - - default_dict.nval: This is often an alias for temp_dict->nval. As - such it can really have no separate existence until the procedure - is complete. For this reason it should *not* be referenced inside - the execution of a procedure. */ -/* Makes all preparations for reading from the data source and writing - to the data sink. */ +/* Clears the variables in C that need to be cleared between + processing cases. */ static void -open_active_file (void) +clear_case (struct ccase *c) { - /* Sometimes we want to refer to the dictionary that applies to the - data actually written to the sink. This is either temp_dict or - default_dict. However, if TEMPORARY is not on, then temp_dict - does not apply. So, we can set temp_dict to default_dict in this - case. */ - if (!temporary) + size_t var_cnt = dict_get_var_cnt (default_dict); + size_t i; + + for (i = 0; i < var_cnt; i++) { - temp_trns = n_trns; - temp_dict = default_dict; + struct variable *v = dict_get_var (default_dict, i); + if (v->init && v->reinit) + { + if (v->type == NUMERIC) + c->data[v->fv].f = SYSMIS; + else + memset (c->data[v->fv].s, ' ', v->width); + } } - - /* No cases passed to the procedure yet. */ - case_count = 0; - - /* The rest. */ - prepare_for_writing (); - arrange_compaction (); - make_temp_case (); - vector_initialization (); - discard_ctl_stack (); - setup_filter (); - setup_lag (); - - /* Debug output. */ - debug_printf (("vfm: reading from %s source, writing to %s sink.\n", - vfm_source->name, vfm_sink->name)); - debug_printf (("vfm: vfm_source_info.nval=%d, vfm_sink_info.nval=%d, " - "temp_dict->nval=%d, compaction_nval=%d, " - "default_dict.nval=%d\n", - vfm_source_info.nval, vfm_sink_info.nval, temp_dict->nval, - compaction_nval, default_dict.nval)); } - + /* Closes the active file. */ static void close_active_file (void) { - /* Close the current case group. */ - if (case_count && end_func != NULL) - end_func (); - - /* Stop lagging (catch up?). */ - if (n_lag) + /* Free memory for lag queue, and turn off lagging. */ + if (n_lag > 0) { int i; @@ -549,8 +412,7 @@ close_active_file (void) n_lag = 0; } - /* Assume the dictionary from right before TEMPORARY, if any. Turn - off TEMPORARY. */ + /* Dictionary from before TEMPORARY becomes permanent.. */ if (temporary) { dict_destroy (default_dict); @@ -560,395 +422,361 @@ close_active_file (void) /* Finish compaction. */ if (compaction_necessary) - finish_compaction (); + dict_compact_values (default_dict); - /* Old data sink --> New data source. */ - if (vfm_source && vfm_source->destroy_source) - vfm_source->destroy_source (); - - vfm_source = vfm_sink; - vfm_source_info.ncases = vfm_sink_info.ncases; - vfm_source_info.nval = compaction_nval; - vfm_source_info.case_size = (sizeof (struct ccase) - + (compaction_nval - 1) * sizeof (union value)); - if (vfm_source->mode) - vfm_source->mode (); - - /* Old data sink is gone now. */ + /* Free data source. */ + if (vfm_source != NULL) + { + if (vfm_source->class->destroy != NULL) + vfm_source->class->destroy (vfm_source); + free (vfm_source); + } + + /* Old data sink becomes new data source. */ + if (vfm_sink->class->make_source != NULL) + vfm_source = vfm_sink->class->make_source (vfm_sink); + else + { + if (vfm_sink->class->destroy != NULL) + vfm_sink->class->destroy (vfm_sink); + vfm_source = NULL; + } + free_case_sink (vfm_sink); vfm_sink = NULL; - /* Cancel TEMPORARY. */ + /* Cancel TEMPORARY, PROCESS IF, FILTER, N OF CASES, vectors, + and get rid of all the transformations. */ cancel_temporary (); - - /* Free temporary cases. */ - free (temp_case); - temp_case = NULL; - - free (compaction_case); - compaction_case = NULL; - - /* Cancel PROCESS IF. */ expr_free (process_if_expr); process_if_expr = NULL; - - /* Cancel FILTER if temporary. */ - if (filter_var != NULL && !FILTER_before_TEMPORARY) + if (dict_get_filter (default_dict) != NULL && !FILTER_before_TEMPORARY) dict_set_filter (default_dict, NULL); - - /* Cancel transformations. */ - cancel_transformations (); - - /* Clear value-initialization vectors. */ - vec_clear (&init_zero); - vec_clear (&init_blanks); - vec_clear (&reinit_sysmis); - vec_clear (&reinit_blanks); - - /* Turn off case limiter. */ dict_set_case_limit (default_dict, 0); - - /* Clear VECTOR vectors. */ dict_clear_vectors (default_dict); - - debug_printf (("vfm: procedure complete\n\n")); + cancel_transformations (); } -/* Disk case stream. */ +/* Storage case stream. */ -/* Associated files. */ -FILE *disk_source_file; -FILE *disk_sink_file; +/* Information about storage sink or source. */ +struct storage_stream_info + { + size_t case_cnt; /* Number of cases. */ + size_t case_size; /* Number of bytes in case. */ + enum { DISK, MEMORY } mode; /* Where is data stored? */ -/* Initializes the disk sink. */ + /* Disk storage. */ + FILE *file; /* Data file. */ + + /* Memory storage. */ + int max_cases; /* Maximum cases before switching to disk. */ + struct case_list *head; /* First case in list. */ + struct case_list *tail; /* Last case in list. */ + }; + +static void open_storage_file (struct storage_stream_info *info); + +/* Initializes a storage sink. */ static void -disk_stream_init (void) +storage_sink_open (struct case_sink *sink) { - disk_sink_file = tmpfile (); - if (!disk_sink_file) + struct storage_stream_info *info; + + sink->aux = info = xmalloc (sizeof *info); + info->case_cnt = 0; + info->case_size = sink->value_cnt * sizeof (union value); + info->file = NULL; + info->max_cases = 0; + info->head = info->tail = NULL; + if (workspace_overflow) { - msg (ME, _("An error occurred attempting to create a temporary " - "file for use as the active file: %s."), - strerror (errno)); - err_failure (); + info->mode = DISK; + open_storage_file (info); } -} - -/* Reads all cases from the disk source and passes them one by one to - write_case(). */ -static void -disk_stream_read (void) -{ - int i; - - for (i = 0; i < vfm_source_info.ncases; i++) + else { - if (!fread (temp_case, vfm_source_info.case_size, 1, disk_source_file)) - { - msg (ME, _("An error occurred while attempting to read from " - "a temporary file created for the active file: %s."), - strerror (errno)); - err_failure (); - return; - } - - if (!write_case ()) - return; + info->mode = MEMORY; + info->max_cases = (get_max_workspace() + / (sizeof (struct case_list) + info->case_size)); } } -/* Writes temp_case to the disk sink. */ +/* Creates a new temporary file and puts it into INFO. */ static void -disk_stream_write (void) +open_storage_file (struct storage_stream_info *info) { - union value *src_case; - - if (compaction_necessary) - { - compact_case (compaction_case, temp_case); - src_case = (union value *) compaction_case; - } - else src_case = (union value *) temp_case; - - if (fwrite (src_case, sizeof *src_case * compaction_nval, 1, - disk_sink_file) != 1) + info->file = tmpfile (); + if (info->file == NULL) { - msg (ME, _("An error occurred while attempting to write to a " - "temporary file used as the active file: %s."), - strerror (errno)); + msg (ME, _("An error occurred creating a temporary " + "file for use as the active file: %s."), + strerror (errno)); err_failure (); } } -/* Switches the stream from a sink to a source. */ +/* Writes the VALUE_CNT values in VALUES to FILE. */ static void -disk_stream_mode (void) +write_storage_file (FILE *file, const union value *values, size_t value_cnt) { - /* Rewind the sink. */ - if (fseek (disk_sink_file, 0, SEEK_SET) != 0) + if (fwrite (values, sizeof *values * value_cnt, 1, file) != 1) { - msg (ME, _("An error occurred while attempting to rewind a " + msg (ME, _("An error occurred writing to a " "temporary file used as the active file: %s."), strerror (errno)); err_failure (); } - - /* Sink --> source variables. */ - disk_source_file = disk_sink_file; } -/* Destroys the source's internal data. */ +/* If INFO represents records in memory, moves them to disk. + Each comprises VALUE_CNT `union value's. */ static void -disk_stream_destroy_source (void) +storage_to_disk (struct storage_stream_info *info, size_t value_cnt) { - if (disk_source_file) + struct case_list *cur, *next; + + if (info->mode == MEMORY) { - fclose (disk_source_file); - disk_source_file = NULL; + info->mode = DISK; + open_storage_file (info); + for (cur = info->head; cur; cur = next) + { + next = cur->next; + write_storage_file (info->file, cur->c.data, value_cnt); + free (cur); + } + info->head = info->tail = NULL; } } -/* Destroys the sink's internal data. */ +/* Destroys storage stream represented by INFO. */ static void -disk_stream_destroy_sink (void) +destroy_storage_stream_info (struct storage_stream_info *info) { - if (disk_sink_file) + if (info->mode == DISK) + { + if (info->file != NULL) + fclose (info->file); + } + else { - fclose (disk_sink_file); - disk_sink_file = NULL; + struct case_list *cur, *next; + + for (cur = info->head; cur; cur = next) + { + next = cur->next; + free (cur); + } } + free (info); } -/* Disk stream. */ -struct case_stream vfm_disk_stream = - { - disk_stream_init, - disk_stream_read, - disk_stream_write, - disk_stream_mode, - disk_stream_destroy_source, - disk_stream_destroy_sink, - "disk", - }; - -/* Memory case stream. */ +/* Writes case C to the storage sink SINK. */ +static void +storage_sink_write (struct case_sink *sink, const struct ccase *c) +{ + struct storage_stream_info *info = sink->aux; -/* List of cases stored in the stream. */ -struct case_list *memory_source_cases; -struct case_list *memory_sink_cases; + info->case_cnt++; + if (info->mode == MEMORY) + { + struct case_list *new_case; -/* Current case. */ -struct case_list *memory_sink_iter; + /* Copy case. */ + new_case = xmalloc (sizeof (struct case_list) + + ((sink->value_cnt - 1) * sizeof (union value))); + memcpy (&new_case->c, c, sizeof (union value) * sink->value_cnt); -/* Maximum number of cases. */ -int memory_sink_max_cases; + /* Append case to linked list. */ + new_case->next = NULL; + if (info->head != NULL) + info->tail->next = new_case; + else + info->head = new_case; + info->tail = new_case; + + /* Dump all the cases to disk if we've run out of + workspace. */ + if (info->case_cnt > info->max_cases) + { + workspace_overflow = 1; + msg (MW, _("Workspace limit of %d KB (%d cases at %d bytes each) " + "overflowed. Writing active file to disk."), + get_max_workspace() / 1024, info->max_cases, + sizeof (struct case_list) + info->case_size); + + storage_to_disk (info, sink->value_cnt); + } + } + else + write_storage_file (info->file, c->data, sink->value_cnt); +} -/* Initializes the memory stream variables for writing. */ +/* Destroys internal data in SINK. */ static void -memory_stream_init (void) +storage_sink_destroy (struct case_sink *sink) { - memory_sink_cases = NULL; - memory_sink_iter = NULL; - - assert (compaction_nval); - memory_sink_max_cases = MAX_WORKSPACE / (sizeof (union value) * compaction_nval); + destroy_storage_stream_info (sink->aux); } -/* Reads the case stream from memory and passes it to write_case(). */ -static void -memory_stream_read (void) +/* Closes and destroys the sink and returns a storage source to + read back the written data. */ +static struct case_source * +storage_sink_make_source (struct case_sink *sink) { - while (memory_source_cases != NULL) + struct storage_stream_info *info = sink->aux; + + if (info->mode == DISK) { - memcpy (temp_case, &memory_source_cases->c, vfm_source_info.case_size); - - { - struct case_list *current = memory_source_cases; - memory_source_cases = memory_source_cases->next; - free (current); - } - - if (!write_case ()) - return; + /* Rewind the file. */ + assert (info->file != NULL); + if (fseek (info->file, 0, SEEK_SET) != 0) + { + msg (ME, _("An error occurred while attempting to rewind a " + "temporary file used as the active file: %s."), + strerror (errno)); + err_failure (); + } } + + return create_case_source (&storage_source_class, sink->dict, info); } -/* Writes temp_case to the memory stream. */ +/* Storage sink. */ +const struct case_sink_class storage_sink_class = + { + "storage", + storage_sink_open, + storage_sink_write, + storage_sink_destroy, + storage_sink_make_source, + }; + +/* Storage source. */ + +/* Returns the number of cases that will be read by + storage_source_read(). */ +static int +storage_source_count (const struct case_source *source) +{ + struct storage_stream_info *info = source->aux; + + return info->case_cnt; +} + +/* Reads all cases from the storage source and passes them one by one to + write_case(). */ static void -memory_stream_write (void) +storage_source_read (struct case_source *source, + struct ccase *c, + write_case_func *write_case, write_case_data wc_data) { - struct case_list *new_case = malloc (sizeof (struct case_list) - + ((compaction_nval - 1) - * sizeof (union value))); + struct storage_stream_info *info = source->aux; - /* If we've got memory to spare then add it to the linked list. */ - if (vfm_sink_info.ncases <= memory_sink_max_cases && new_case != NULL) + if (info->mode == DISK) { - if (compaction_necessary) - compact_case (&new_case->c, temp_case); - else - memcpy (&new_case->c, temp_case, sizeof (union value) * compaction_nval); + int i; - /* Append case to linked list. */ - if (memory_sink_cases) - memory_sink_iter = memory_sink_iter->next = new_case; - else - memory_sink_iter = memory_sink_cases = new_case; + for (i = 0; i < info->case_cnt; i++) + { + if (!fread (c, info->case_size, 1, info->file)) + { + msg (ME, _("An error occurred while attempting to read from " + "a temporary file created for the active file: %s."), + strerror (errno)); + err_failure (); + break; + } + + if (!write_case (wc_data)) + break; + } } - else + else { - /* Out of memory. Write the active file to disk. */ - struct case_list *cur, *next; - - /* Notify the user. */ - if (!new_case) - msg (MW, _("Virtual memory exhausted. Paging active file " - "to disk.")); - else - msg (MW, _("Workspace limit of %d KB (%d cases at %d bytes each) " - "overflowed. Paging active file to disk."), - MAX_WORKSPACE / 1024, memory_sink_max_cases, - compaction_nval * sizeof (union value)); - - free (new_case); + while (info->head != NULL) + { + struct case_list *iter = info->head; + memcpy (c, &iter->c, info->case_size); + if (!write_case (wc_data)) + break; + + info->head = iter->next; + free (iter); + } + info->tail = NULL; + } +} - /* Switch to a disk sink. */ - vfm_sink = &vfm_disk_stream; - vfm_sink->init (); - paging = 1; +/* Destroys the source's internal data. */ +static void +storage_source_destroy (struct case_source *source) +{ + destroy_storage_stream_info (source->aux); +} - /* Terminate the list. */ - if (memory_sink_iter) - memory_sink_iter->next = NULL; +/* Storage source. */ +const struct case_source_class storage_source_class = + { + "storage", + storage_source_count, + storage_source_read, + storage_source_destroy, + }; - /* Write the cases to disk and destroy them. We can't call - vfm->sink->write() because of compaction. */ - for (cur = memory_sink_cases; cur; cur = next) - { - next = cur->next; - if (fwrite (cur->c.data, sizeof (union value) * compaction_nval, 1, - disk_sink_file) != 1) - { - msg (ME, _("An error occurred while attempting to " - "write to a temporary file created as the " - "active file, while paging to disk: %s."), - strerror (errno)); - err_failure (); - } - free (cur); - } +/* Returns nonzero only if SOURCE is stored on disk (instead of + in memory). */ +int +storage_source_on_disk (const struct case_source *source) +{ + struct storage_stream_info *info = source->aux; - /* Write the current case to disk. */ - vfm_sink->write (); - } + return info->mode == DISK; } -/* If the data is stored in memory, causes it to be written to disk. - To be called only *between* procedure()s, not within them. */ -void -page_to_disk (void) +/* Returns the list of cases in storage source SOURCE. */ +struct case_list * +storage_source_get_cases (const struct case_source *source) { - if (vfm_source == &vfm_memory_stream) - { - /* Switch to a disk sink. */ - vfm_sink = &vfm_disk_stream; - vfm_sink->init (); - paging = 1; - - /* Write the cases to disk and destroy them. We can't call - vfm->sink->write() because of compaction. */ - { - struct case_list *cur, *next; - - for (cur = memory_source_cases; cur; cur = next) - { - next = cur->next; - if (fwrite (cur->c.data, sizeof *cur->c.data * compaction_nval, 1, - disk_sink_file) != 1) - { - msg (ME, _("An error occurred while attempting to " - "write to a temporary file created as the " - "active file, while paging to disk: %s."), - strerror (errno)); - err_failure (); - } - free (cur); - } - } - - vfm_source = &vfm_disk_stream; - vfm_source->mode (); + struct storage_stream_info *info = source->aux; - vfm_sink = NULL; - } + assert (info->mode == MEMORY); + return info->head; } -/* Switch the memory stream from sink to source mode. */ -static void -memory_stream_mode (void) +/* Sets the list of cases in memory source SOURCE to CASES. */ +void +storage_source_set_cases (const struct case_source *source, + struct case_list *cases) { - /* Terminate the list. */ - if (memory_sink_iter) - memory_sink_iter->next = NULL; + struct storage_stream_info *info = source->aux; - /* Sink --> source variables. */ - memory_source_cases = memory_sink_cases; - memory_sink_cases = NULL; + assert (info->mode == MEMORY); + info->head = cases; } -/* Destroy all memory source data. */ -static void -memory_stream_destroy_source (void) +/* If SOURCE has its cases in memory, writes them to disk. */ +void +storage_source_to_disk (struct case_source *source) { - struct case_list *cur, *next; - - for (cur = memory_source_cases; cur; cur = next) - { - next = cur->next; - free (cur); - } - memory_source_cases = NULL; -} + struct storage_stream_info *info = source->aux; -/* Destroy all memory sink data. */ -static void -memory_stream_destroy_sink (void) -{ - struct case_list *cur, *next; - - for (cur = memory_sink_cases; cur; cur = next) - { - next = cur->next; - free (cur); - } - memory_sink_cases = NULL; + storage_to_disk (info, source->value_cnt); } - -/* Memory stream. */ -struct case_stream vfm_memory_stream = + +/* Null sink. Used by a few procedures that keep track of output + themselves and would throw away anything that the sink + contained anyway. */ + +const struct case_sink_class null_sink_class = { - memory_stream_init, - memory_stream_read, - memory_stream_write, - memory_stream_mode, - memory_stream_destroy_source, - memory_stream_destroy_sink, - "memory", + "null", + NULL, + NULL, + NULL, + NULL, }; -#include "debug-print.h" - -/* Add temp_case to the lag queue. */ -static void -lag_case (void) -{ - if (lag_count < n_lag) - lag_count++; - memcpy (lag_queue[lag_head], temp_case, - sizeof (union value) * dict_get_value_cnt (temp_dict)); - if (++lag_head >= n_lag) - lag_head = 0; -} - /* Returns a pointer to the lagged case from N_BEFORE cases before the current one, or NULL if there haven't been that many cases yet. */ struct ccase * @@ -966,99 +794,6 @@ lagged_case (int n_before) } } -/* Transforms temp_case and writes it to the replacement active file - if advisable. Returns nonzero if more cases can be accepted, zero - otherwise. Do not call this function again after it has returned - zero once. */ -int -procedure_write_case (void) -{ - /* Index of current transformation. */ - int cur_trns; - - /* Return value: whether it's reasonable to write any more cases. */ - int more_cases = 1; - - debug_printf ((_("transform: "))); - - cur_trns = f_trns; - for (;;) - { - /* Output the case if this is temp_trns. */ - if (cur_trns == temp_trns) - { - debug_printf (("REC")); - - if (n_lag) - lag_case (); - - vfm_sink_info.ncases++; - vfm_sink->write (); - - if (dict_get_case_limit (default_dict)) - more_cases = (vfm_sink_info.ncases - < dict_get_case_limit (default_dict)); - } - - /* Are we done? */ - if (cur_trns >= n_trns) - break; - - debug_printf (("$%d", cur_trns)); - - /* Decide which transformation should come next. */ - { - int code; - - code = t_trns[cur_trns]->proc (t_trns[cur_trns], temp_case); - switch (code) - { - case -1: - /* Next transformation. */ - cur_trns++; - break; - case -2: - /* Delete this case. */ - goto done; - default: - /* Go to that transformation. */ - cur_trns = code; - break; - } - } - } - - /* Call the beginning of group function. */ - if (!case_count && begin_func != NULL) - begin_func (); - - /* Call the procedure if there is one and FILTER and PROCESS IF - don't prohibit it. */ - if (proc_func != NULL - && !FILTERED - && (process_if_expr == NULL || - expr_evaluate (process_if_expr, temp_case, NULL) == 1.0)) - proc_func (temp_case); - - case_count++; - -done: - debug_putc ('\n', stdout); - - { - long *lp; - - /* This case is finished. Initialize the variables for the next case. */ - for (lp = reinit_sysmis.vec; *lp != -1;) - temp_case->data[*lp++].f = SYSMIS; - for (lp = reinit_blanks.vec; *lp != -1;) - memset (temp_case->data[*lp++].s, ' ', MAX_SHORT_STRING); - } - - /* Return previously determined value. */ - return more_cases; -} - /* Appends TRNS to t_trns[], the list of all transformations to be performed on data as it is read from the active file. */ void @@ -1092,81 +827,150 @@ cancel_transformations (void) m_trns = 0; } } + +/* Creates a case source with class CLASS and auxiliary data AUX + and based on dictionary DICT. */ +struct case_source * +create_case_source (const struct case_source_class *class, + const struct dictionary *dict, + void *aux) +{ + struct case_source *source = xmalloc (sizeof *source); + source->class = class; + source->value_cnt = dict_get_next_value_idx (dict); + source->aux = aux; + return source; +} -/* Dumps out the values of all the split variables for the case C. */ -static void -dump_splits (struct ccase *c) +/* Returns nonzero if a case source is "complex". */ +int +case_source_is_complex (const struct case_source *source) { - struct variable *const *split; - struct tab_table *t; - size_t split_cnt; - int i; + return source != NULL && (source->class == &input_program_source_class + || source->class == &file_type_source_class); +} - split_cnt = dict_get_split_cnt (default_dict); - t = tab_create (3, split_cnt + 1, 0); - tab_dim (t, tab_natural_dimensions); - tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, split_cnt); - tab_vline (t, TAL_1 | TAL_SPACING, 2, 0, split_cnt); - tab_text (t, 0, 0, TAB_NONE, _("Variable")); - tab_text (t, 1, 0, TAB_LEFT, _("Value")); - tab_text (t, 2, 0, TAB_LEFT, _("Label")); - split = dict_get_split_vars (default_dict); - for (i = 0; i < split_cnt; i++) - { - struct variable *v = split[i]; - char temp_buf[80]; - const char *val_lab; +/* Returns nonzero if CLASS is the class of SOURCE. */ +int +case_source_is_class (const struct case_source *source, + const struct case_source_class *class) +{ + return source != NULL && source->class == class; +} - assert (v->type == NUMERIC || v->type == ALPHA); - tab_text (t, 0, i + 1, TAB_LEFT | TAT_PRINTF, "%s", v->name); - - { - union value val = c->data[v->fv]; - if (v->type == ALPHA) - val.c = c->data[v->fv].s; - data_out (temp_buf, &v->print, &val); - } - - temp_buf[v->print.w] = 0; - tab_text (t, 1, i + 1, TAT_PRINTF, "%.*s", v->print.w, temp_buf); +/* Creates a case sink with class CLASS and auxiliary data + AUX. */ +struct case_sink * +create_case_sink (const struct case_sink_class *class, + const struct dictionary *dict, + void *aux) +{ + struct case_sink *sink = xmalloc (sizeof *sink); + sink->class = class; + sink->dict = dict; + sink->idx_to_fv = dict_get_compacted_idx_to_fv (dict); + sink->value_cnt = dict_get_compacted_value_cnt (dict); + sink->aux = aux; + return sink; +} - val_lab = val_labs_find (v->val_labs, c->data[v->fv]); - if (val_lab) - tab_text (t, 2, i + 1, TAB_LEFT, val_lab); - } - tab_flags (t, SOMF_NO_TITLE); - tab_submit (t); +/* Destroys case sink SINK. It is the caller's responsible to + call the sink's destroy function, if any. */ +void +free_case_sink (struct case_sink *sink) +{ + free (sink->idx_to_fv); + free (sink); +} + +/* Represents auxiliary data for handling SPLIT FILE. */ +struct split_aux_data + { + size_t case_count; /* Number of cases so far. */ + struct ccase *prev_case; /* Data in previous case. */ + + /* Functions to call... */ + void (*begin_func) (void *); /* ...before data. */ + int (*proc_func) (struct ccase *, void *); /* ...with data. */ + void (*end_func) (void *); /* ...after data. */ + void *func_aux; /* Auxiliary data. */ + }; + +static int equal_splits (const struct ccase *, const struct ccase *); +static int procedure_with_splits_callback (struct ccase *, void *); +static void dump_splits (struct ccase *); + +/* Like procedure(), but it automatically breaks the case stream + into SPLIT FILE break groups. Before each group of cases with + identical SPLIT FILE variable values, BEGIN_FUNC is called. + Then PROC_FUNC is called with each case in the group. + END_FUNC is called when the group is finished. FUNC_AUX is + passed to each of the functions as auxiliary data. + + If the active file is empty, none of BEGIN_FUNC, PROC_FUNC, + and END_FUNC will be called at all. + + If SPLIT FILE is not in effect, then there is one break group + (if the active file is nonempty), and BEGIN_FUNC and END_FUNC + will be called once. */ +void +procedure_with_splits (void (*begin_func) (void *aux), + int (*proc_func) (struct ccase *, void *aux), + void (*end_func) (void *aux), + void *func_aux) +{ + struct split_aux_data split_aux; + + split_aux.case_count = 0; + split_aux.prev_case = xmalloc (dict_get_case_size (default_dict)); + split_aux.begin_func = begin_func; + split_aux.proc_func = proc_func; + split_aux.end_func = end_func; + split_aux.func_aux = func_aux; + + procedure (procedure_with_splits_callback, &split_aux); + + if (split_aux.case_count > 0 && end_func != NULL) + end_func (func_aux); + free (split_aux.prev_case); } -/* This procfunc is substituted for the user-supplied procfunc when - SPLIT FILE is active. This function forms a wrapper around that - procfunc by dividing the input into series. */ +/* procedure() callback used by procedure_with_splits(). */ static int -SPLIT_FILE_procfunc (struct ccase *c) +procedure_with_splits_callback (struct ccase *c, void *split_aux_) { - static struct ccase *prev_case; - struct variable *const *split; - size_t split_cnt; - size_t i; + struct split_aux_data *split_aux = split_aux_; - /* The first case always begins a new series. We also need to - preserve the values of the case for later comparison. */ - if (case_count == 0) + /* Start a new series if needed. */ + if (split_aux->case_count == 0 + || !equal_splits (c, split_aux->prev_case)) { - if (prev_case) - free (prev_case); - prev_case = xmalloc (vfm_sink_info.case_size); - memcpy (prev_case, c, vfm_sink_info.case_size); + if (split_aux->case_count > 0 && split_aux->end_func != NULL) + split_aux->end_func (split_aux->func_aux); dump_splits (c); - if (virt_begin_func != NULL) - virt_begin_func (); - - return virt_proc_func (c); + memcpy (split_aux->prev_case, c, dict_get_case_size (default_dict)); + + if (split_aux->begin_func != NULL) + split_aux->begin_func (split_aux->func_aux); } - /* Compare the value of each SPLIT FILE variable to the values on - the previous case. */ + split_aux->case_count++; + if (split_aux->proc_func != NULL) + return split_aux->proc_func (c, split_aux->func_aux); + else + return 1; +} + +/* Compares the SPLIT FILE variables in cases A and B and returns + nonzero only if they differ. */ +static int +equal_splits (const struct ccase *a, const struct ccase *b) +{ + struct variable *const *split; + size_t split_cnt; + size_t i; + split = dict_get_split_vars (default_dict); split_cnt = dict_get_split_cnt (default_dict); for (i = 0; i < split_cnt; i++) @@ -1176,89 +980,60 @@ SPLIT_FILE_procfunc (struct ccase *c) switch (v->type) { case NUMERIC: - if (approx_ne (c->data[v->fv].f, prev_case->data[v->fv].f)) - goto not_equal; + if (a->data[v->fv].f != b->data[v->fv].f) + return 0; break; case ALPHA: - if (memcmp (c->data[v->fv].s, prev_case->data[v->fv].s, v->width)) - goto not_equal; + if (memcmp (a->data[v->fv].s, b->data[v->fv].s, v->width)) + return 0; break; default: assert (0); } } - return virt_proc_func (c); - -not_equal: - /* The values of the SPLIT FILE variable are different from the - values on the previous case. That means that it's time to begin - a new series. */ - if (end_func != NULL) - end_func (); - dump_splits (c); - if (virt_begin_func != NULL) - virt_begin_func (); - memcpy (prev_case, c, vfm_sink_info.case_size); - return virt_proc_func (c); -} - -/* Case compaction. */ - -/* Copies case SRC to case DEST, compacting it in the process. */ -void -compact_case (struct ccase *dest, const struct ccase *src) -{ - int i; - int nval = 0; - size_t var_cnt; - - assert (compaction_necessary); - if (temporary == 2) - { - if (dest != compaction_case) - memcpy (dest, compaction_case, sizeof (union value) * compaction_nval); - return; - } - - /* Copy all the variables except the scratch variables from SRC to - DEST. */ - var_cnt = dict_get_var_cnt (default_dict); - for (i = 0; i < var_cnt; i++) - { - struct variable *v = dict_get_var (default_dict, i); - - if (v->name[0] == '#') - continue; - - if (v->type == NUMERIC) - dest->data[nval++] = src->data[v->fv]; - else - { - int w = DIV_RND_UP (v->width, sizeof (union value)); - - memcpy (&dest->data[nval], &src->data[v->fv], w * sizeof (union value)); - nval += w; - } - } + return 1; } -/* Reassigns `fv' for each variable. Deletes scratch variables. */ +/* Dumps out the values of all the split variables for the case C. */ static void -finish_compaction (void) +dump_splits (struct ccase *c) { + struct variable *const *split; + struct tab_table *t; + size_t split_cnt; int i; - for (i = 0; i < dict_get_var_cnt (default_dict); ) + split_cnt = dict_get_split_cnt (default_dict); + if (split_cnt == 0) + return; + + t = tab_create (3, split_cnt + 1, 0); + tab_dim (t, tab_natural_dimensions); + tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, split_cnt); + tab_vline (t, TAL_1 | TAL_SPACING, 2, 0, split_cnt); + tab_text (t, 0, 0, TAB_NONE, _("Variable")); + tab_text (t, 1, 0, TAB_LEFT, _("Value")); + tab_text (t, 2, 0, TAB_LEFT, _("Label")); + split = dict_get_split_vars (default_dict); + for (i = 0; i < split_cnt; i++) { - struct variable *v = dict_get_var (default_dict, i); + struct variable *v = split[i]; + char temp_buf[80]; + const char *val_lab; - if (v->name[0] == '#') - dict_delete_var (default_dict, v); - else - i++; + assert (v->type == NUMERIC || v->type == ALPHA); + tab_text (t, 0, i + 1, TAB_LEFT | TAT_PRINTF, "%s", v->name); + + data_out (temp_buf, &v->print, &c->data[v->fv]); + + temp_buf[v->print.w] = 0; + tab_text (t, 1, i + 1, TAT_PRINTF, "%.*s", v->print.w, temp_buf); + + val_lab = val_labs_find (v->val_labs, c->data[v->fv]); + if (val_lab) + tab_text (t, 2, i + 1, TAB_LEFT, val_lab); } - dict_compact_values (default_dict); + tab_flags (t, SOMF_NO_TITLE); + tab_submit (t); } - -