X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fcasereader-filter.c;h=f4ad7e625a3e7ea79dfc91c4e93864eac78394d1;hb=e3f149a8d3983254802a6bad9de3fd7869b7dfe2;hp=afb72cca1434e7a40dea2a723ba5b7a68bcd8e69;hpb=5da5a98055ad574120c3e3922af097411a0dcf3a;p=pspp diff --git a/src/data/casereader-filter.c b/src/data/casereader-filter.c index afb72cca14..f4ad7e625a 100644 --- a/src/data/casereader-filter.c +++ b/src/data/casereader-filter.c @@ -1,35 +1,33 @@ -/* PSPP - computes sample statistics. - Copyright (C) 2007 Free Software Foundation, Inc. +/* PSPP - a program for statistical analysis. + Copyright (C) 2007, 2009, 2011 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include -#include +#include "data/casereader.h" #include -#include -#include -#include -#include -#include -#include +#include "data/casereader-provider.h" +#include "data/casewriter.h" +#include "data/variable.h" +#include "data/dictionary.h" +#include "libpspp/taint.h" +#include "libpspp/message.h" -#include "xalloc.h" +#include "gl/xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) @@ -45,7 +43,7 @@ struct casereader_filter struct casewriter *exclude; /* Writer that gets filtered cases, or NULL. */ }; -static struct casereader_class casereader_filter_class; +static const struct casereader_class casereader_filter_class; /* Creates and returns a casereader whose content is a filtered version of the data in SUBREADER. Only the cases for which @@ -80,7 +78,7 @@ casereader_create_filter_func (struct casereader *subreader, filter->aux = aux; filter->exclude = exclude; reader = casereader_create_sequential ( - NULL, casereader_get_value_cnt (filter->subreader), CASENUMBER_MAX, + NULL, casereader_get_proto (filter->subreader), CASENUMBER_MAX, &casereader_filter_class, filter); taint_propagate (casereader_get_taint (filter->subreader), casereader_get_taint (reader)); @@ -88,22 +86,22 @@ casereader_create_filter_func (struct casereader *subreader, } /* Internal read function for filtering casereader. */ -static bool -casereader_filter_read (struct casereader *reader UNUSED, void *filter_, - struct ccase *c) +static struct ccase * +casereader_filter_read (struct casereader *reader UNUSED, void *filter_) { struct casereader_filter *filter = filter_; for (;;) { - if (!casereader_read (filter->subreader, c)) - return false; + struct ccase *c = casereader_read (filter->subreader); + if (c == NULL) + return NULL; else if (filter->include (c, filter->aux)) - return true; + return c; else if (filter->exclude != NULL) casewriter_write (filter->exclude, c); else - case_destroy (c); + case_unref (c); } } @@ -117,12 +115,12 @@ casereader_filter_destroy (struct casereader *reader, void *filter_) casewriter, if there is one. */ if (filter->exclude != NULL) { - struct ccase c; - while (casereader_read (filter->subreader, &c)) - if (filter->include (&c, filter->aux)) - case_destroy (&c); + struct ccase *c; + while ((c = casereader_read (filter->subreader)) != NULL) + if (filter->include (c, filter->aux)) + case_unref (c); else - casewriter_write (filter->exclude, &c); + casewriter_write (filter->exclude, c); } casereader_destroy (filter->subreader); @@ -132,7 +130,7 @@ casereader_filter_destroy (struct casereader *reader, void *filter_) } /* Filtering casereader class. */ -static struct casereader_class casereader_filter_class = +static const struct casereader_class casereader_filter_class = { casereader_filter_read, casereader_filter_destroy, @@ -214,7 +212,7 @@ casereader_filter_weight_include (const struct ccase *c, void *cfw_) { struct casereader_filter_weight *cfw = cfw_; double value = case_num (c, cfw->weight_var); - if (value >= 0.0 && !var_is_num_missing (cfw->weight_var, value, MV_ANY)) + if (value >= 0.0 && !var_is_num_missing (cfw->weight_var, value)) return true; else { @@ -245,8 +243,9 @@ casereader_filter_weight_destroy (void *cfw_) struct casereader_filter_missing { struct variable **vars; /* Variables whose values to filter. */ - size_t var_cnt; /* Number of variables. */ + size_t n_vars; /* Number of variables. */ enum mv_class class; /* Types of missing values to filter. */ + casenumber *n_missing; }; static bool casereader_filter_missing_include (const struct ccase *, void *); @@ -254,7 +253,7 @@ static bool casereader_filter_missing_destroy (void *); /* Creates and returns a casereader that filters out cases from READER that have a missing value in the given CLASS for any of - the VAR_CNT variables in VARS. Only cases that have + the N_VARS variables in VARS. Only cases that have non-missing values for all of these variables are passed through. @@ -266,21 +265,27 @@ static bool casereader_filter_missing_destroy (void *); read or, if that never occurs, until the filtering casereader is destroyed. + If N_MISSING is non-null, then after reading, it will be filled + with the total number of dropped cases. + After this function is called, READER must not ever again be referenced directly. It will be destroyed automatically when the filtering casereader is destroyed. */ struct casereader * casereader_create_filter_missing (struct casereader *reader, - const struct variable **vars, size_t var_cnt, + const struct variable *const *vars, size_t n_vars, enum mv_class class, + casenumber *n_missing, struct casewriter *exclude) { - if (var_cnt > 0 && class != MV_NEVER) + if (n_vars > 0 && class) { struct casereader_filter_missing *cfm = xmalloc (sizeof *cfm); - cfm->vars = xmemdup (vars, sizeof *vars * var_cnt); - cfm->var_cnt = var_cnt; + cfm->vars = xmemdup (vars, sizeof *vars * n_vars); + cfm->n_vars = n_vars; cfm->class = class; + cfm->n_missing = n_missing; + if (n_missing) *n_missing = 0; return casereader_create_filter_func (reader, casereader_filter_missing_include, casereader_filter_missing_destroy, @@ -299,12 +304,16 @@ casereader_filter_missing_include (const struct ccase *c, void *cfm_) const struct casereader_filter_missing *cfm = cfm_; size_t i; - for (i = 0; i < cfm->var_cnt; i++) + for (i = 0; i < cfm->n_vars; i++) { struct variable *var = cfm->vars[i]; const union value *value = case_data (c, var); - if (var_is_value_missing (var, value, cfm->class)) - return false; + if (var_is_value_missing (var, value) & cfm->class) + { + if (cfm->n_missing) + (*cfm->n_missing)++; + return false; + } } return true; }