1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "data/case.h"
20 #include "interaction.h"
22 #include "data/value.h"
23 #include "data/variable.h"
24 #include "libpspp/str.h"
26 #include "gl/xalloc.h"
32 An interaction is a structure containing a "product" of other
33 variables. The variables can be either string or numeric.
35 Interaction is commutative. That means, that from a mathematical point of
36 view, the order of the variables is irrelevant. However, for display
37 purposes, and for matching with an interaction's value the order is
40 Therefore, when using these functions, make sure the orders of variables
41 and values match when appropriate.
47 interaction_create (const struct variable *v)
49 struct interaction *i = xmalloc (sizeof *i);
50 i->vars = xmalloc (sizeof *i->vars);
61 interaction_destroy (struct interaction *i)
68 interaction_add_variable (struct interaction *i, const struct variable *v)
70 i->vars = xrealloc (i->vars, sizeof (*i->vars) * ++i->n_vars);
71 i->vars[i->n_vars - 1] = v;
76 Do the variables in X->VARS constitute a proper
77 subset of the variables in Y->VARS?
80 interaction_is_proper_subset (const struct interaction *x, const struct interaction *y)
82 if (x->n_vars >= y->n_vars)
85 return interaction_is_subset (x, y);
89 Do the variables in X->VARS constitute a
90 subset (proper or otherwise) of the variables in Y->VARS?
93 interaction_is_subset (const struct interaction *x, const struct interaction *y)
99 /* By definition, a subset cannot have more members than its superset */
100 if (x->n_vars > y->n_vars)
103 /* Count the number of values which are members of both sets */
104 for (i = 0; i < x->n_vars; i++)
106 for (j = 0; j < y->n_vars; j++)
108 if (x->vars [i] == y->vars [j])
115 /* If ALL the members of X were also found in Y, then this must be a subset */
126 interaction_dump (const struct interaction *i)
129 printf ("%s", var_get_name (i->vars[v]));
130 for (v = 1; v < i->n_vars; ++v)
132 printf (" * %s", var_get_name (i->vars[v]));
137 /* Appends STR with a representation of the interaction, suitable for user
140 STR must have been initialised prior to calling this function.
143 interaction_to_string (const struct interaction *iact, struct string *str)
146 ds_put_cstr (str, var_to_string (iact->vars[v]));
147 for (v = 1; v < iact->n_vars; ++v)
149 ds_put_cstr (str, " * ");
150 ds_put_cstr (str, var_to_string (iact->vars[v]));
155 interaction_case_hash (const struct interaction *iact, const struct ccase *c)
159 for (i = 0; i < iact->n_vars; ++i)
161 const struct variable *var = iact->vars[i];
162 const union value *val = case_data (c, var);
163 hash = value_hash (val, var_get_width (var), hash);
169 interaction_case_equal (const struct interaction *iact, const struct ccase *c1, const struct ccase *c2)
174 for (i = 0; i < iact->n_vars; ++i)
176 const struct variable *var = iact->vars[i];
177 if ( ! value_equal (case_data (c1, var), case_data (c2, var), var_get_width (var)))
189 interaction_case_cmp_3way (const struct interaction *iact, const struct ccase *c1, const struct ccase *c2)
194 for (i = 0; i < iact->n_vars; ++i)
196 const struct variable *var = iact->vars[i];
197 result = value_compare_3way (case_data (c1, var), case_data (c2, var), var_get_width (var));
207 interaction_case_is_missing (const struct interaction *iact, const struct ccase *c, enum mv_class exclude)
210 bool missing = false;
212 for (i = 0; i < iact->n_vars; ++i)
214 if ( var_is_value_missing (iact->vars[i], case_data (c, iact->vars[i]), exclude))