Removed my authorship lines.
[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    bool exclude_user_missing;
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
62   if ( var_is_numeric (var) && val->f == SYSMIS )
63     return true;
64
65   if ( filter->exclude_user_missing &&
66        var_is_value_user_missing (var, val) )
67     return true;
68
69   return false;
70 }
71
72 /* Create a new casefilter.
73    If EXCL is true, then the filter  user missing values to be missing,
74    otherwise they are considered at their face value.
75    VARS is an array of variables which if *any* of them are missing.
76    N_VARS is the size of VARS.
77  */
78 struct casefilter *
79 casefilter_create (bool excl, struct variable **vars, int n_vars)
80 {
81   int i;
82   struct casefilter * filter = xmalloc (sizeof (*filter)) ;
83
84   filter->exclude_user_missing = excl ;
85   filter->vars = xnmalloc (n_vars, sizeof (*filter->vars) );
86
87   for ( i = 0 ; i < n_vars ; ++i )
88     filter->vars[i] = vars[i];
89
90   filter->n_vars = n_vars ;
91
92   return filter ;
93 }
94
95
96 /* Add the variables in VARS to the list of variables for which the
97    filter considers. N_VARS is the size of VARS */
98 void
99 casefilter_add_variables (struct casefilter *filter,
100                            struct variable **vars, int n_vars)
101 {
102   int i;
103
104   filter->vars = xnrealloc (filter->vars, filter->n_vars + n_vars,
105                            sizeof (*filter->vars) );
106
107   for ( i = 0 ; i < n_vars ; ++i )
108     filter->vars[i + filter->n_vars] = vars[i];
109
110   filter->n_vars += n_vars ;
111 }
112
113 /* Destroy the filter FILTER */
114 void
115 casefilter_destroy (struct casefilter *filter)
116 {
117   free (filter->vars);
118   free (filter);
119 }
120
121