2c6336d658ca84fc2d850d75e2e21fdd4de9c9ba
[pspp-builds.git] / src / data / casefilter.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include <libpspp/alloc.h>
21 #include <libpspp/compiler.h>
22 #include "casefilter.h"
23 #include <stdlib.h>
24
25 #include <stdio.h>
26 #include <data/case.h>
27 #include <data/variable.h>
28 #include <data/missing-values.h>
29
30 struct casefilter
31  {
32    enum mv_class class;
33
34    const struct variable **vars;
35    int n_vars;
36  };
37
38
39 /* Returns true iff the entire case should be skipped */
40 bool
41 casefilter_skip_case (const struct casefilter *filter, const struct ccase *c)
42 {
43   int i;
44
45   for (i = 0; i < filter->n_vars; ++i)
46     {
47       if ( casefilter_variable_missing (filter, c, filter->vars[i]))
48         return true;
49     }
50
51   return false;
52 }
53
54 /* Returns true iff the variable V in case C is missing */
55 bool
56 casefilter_variable_missing (const struct casefilter *filter,
57                              const struct ccase *c,
58                              const struct variable *var)
59 {
60   const union value *val = case_data (c, var) ;
61   return var_is_value_missing (var, val, filter->class);
62 }
63
64 /* Create a new casefilter that drops cases in which any of the
65    N_VARS variables in VARS are in the given CLASS of missing values.
66    VARS is an array of variables which if *any* of them are missing.
67    N_VARS is the size of VARS.
68  */
69 struct casefilter *
70 casefilter_create (enum mv_class class, const struct variable **vars, int n_vars)
71 {
72   int i;
73   struct casefilter * filter = xmalloc (sizeof (*filter)) ;
74
75   filter->class = class;
76   filter->vars = xnmalloc (n_vars, sizeof (*filter->vars) );
77
78   for ( i = 0 ; i < n_vars ; ++i )
79     filter->vars[i] = vars[i];
80
81   filter->n_vars = n_vars ;
82
83   return filter ;
84 }
85
86
87 /* Add the variables in VARS to the list of variables for which the
88    filter considers. N_VARS is the size of VARS */
89 void
90 casefilter_add_variables (struct casefilter *filter,
91                           const struct variable *const *vars, int n_vars)
92 {
93   int i;
94
95   filter->vars = xnrealloc (filter->vars, filter->n_vars + n_vars,
96                            sizeof (*filter->vars) );
97
98   for ( i = 0 ; i < n_vars ; ++i )
99     filter->vars[i + filter->n_vars] = vars[i];
100
101   filter->n_vars += n_vars ;
102 }
103
104 /* Destroy the filter FILTER */
105 void
106 casefilter_destroy (struct casefilter *filter)
107 {
108   free (filter->vars);
109   free (filter);
110 }