X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Finteraction.c;h=a36755b541265fbc3da3e1ca8fcc465ec0ea120e;hb=5d085e3326530983f7c2f3843dd2a1eebc0a6e73;hp=aa24250007da27b09723f5ddc4f115419fb10db5;hpb=3a82e941418b74d8f270cf164291a3938fff61d5;p=pspp diff --git a/src/math/interaction.c b/src/math/interaction.c index aa24250007..a36755b541 100644 --- a/src/math/interaction.c +++ b/src/math/interaction.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2011 Free Software Foundation, Inc. + Copyright (C) 2011, 2012 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 @@ -42,20 +42,44 @@ */ - struct interaction * interaction_create (const struct variable *v) { struct interaction *i = xmalloc (sizeof *i); i->vars = xmalloc (sizeof *i->vars); - i->vars[0] = v; - i->n_vars = 1; + i->n_vars = 0; + if ( v ) + { + i->vars[0] = v; + i->n_vars = 1; + } + return i; +} + +/* Deep copy an interaction */ +struct interaction * +interaction_clone (const struct interaction *iact) +{ + int v; + struct interaction *i = xmalloc (sizeof *i); + i->vars = xcalloc (iact->n_vars, sizeof *i->vars); + i->n_vars = iact->n_vars; + + for (v = 0; v < iact->n_vars; ++v) + { + i->vars[v] = iact->vars[v]; + } + return i; } + void interaction_destroy (struct interaction *i) { + if (NULL == i) + return; + free (i->vars); free (i); } @@ -68,15 +92,68 @@ interaction_add_variable (struct interaction *i, const struct variable *v) } +/* + Do the variables in X->VARS constitute a proper + subset of the variables in Y->VARS? + */ +bool +interaction_is_proper_subset (const struct interaction *x, const struct interaction *y) +{ + if (x->n_vars >= y->n_vars) + return false; + + return interaction_is_subset (x, y); +} + +/* + Do the variables in X->VARS constitute a + subset (proper or otherwise) of the variables in Y->VARS? + */ +bool +interaction_is_subset (const struct interaction *x, const struct interaction *y) +{ + size_t i; + size_t j; + size_t n = 0; + + /* By definition, a subset cannot have more members than its superset */ + if (x->n_vars > y->n_vars) + return false; + + /* Count the number of values which are members of both sets */ + for (i = 0; i < x->n_vars; i++) + { + for (j = 0; j < y->n_vars; j++) + { + if (x->vars [i] == y->vars [j]) + { + n++; + } + } + } + + /* If ALL the members of X were also found in Y, then this must be a subset */ + if (n >= x->n_vars) + return true; + + return false; +} + + + + void interaction_dump (const struct interaction *i) { int v = 0; - printf ("%s", var_get_name (i->vars[v])); - for (v = 1; v < i->n_vars; ++v) + if ( i->n_vars == 0) { - printf (" * %s", var_get_name (i->vars[v])); + printf ("(empty)\n"); + return; } + printf ("%s", var_get_name (i->vars[v])); + for (v = 1; v < i->n_vars; ++v) + printf (" * %s", var_get_name (i->vars[v])); printf ("\n"); } @@ -89,6 +166,8 @@ void interaction_to_string (const struct interaction *iact, struct string *str) { int v = 0; + if ( iact->n_vars == 0) + return; ds_put_cstr (str, var_to_string (iact->vars[v])); for (v = 1; v < iact->n_vars; ++v) { @@ -98,10 +177,10 @@ interaction_to_string (const struct interaction *iact, struct string *str) } unsigned int -interaction_case_hash (const struct interaction *iact, const struct ccase *c) +interaction_case_hash (const struct interaction *iact, const struct ccase *c, unsigned int base) { int i; - size_t hash = 0; + size_t hash = base; for (i = 0; i < iact->n_vars; ++i) { const struct variable *var = iact->vars[i]; @@ -130,6 +209,25 @@ interaction_case_equal (const struct interaction *iact, const struct ccase *c1, return same; } + +int +interaction_case_cmp_3way (const struct interaction *iact, const struct ccase *c1, const struct ccase *c2) +{ + int i; + int result = 0; + + for (i = 0; i < iact->n_vars; ++i) + { + const struct variable *var = iact->vars[i]; + result = value_compare_3way (case_data (c1, var), case_data (c2, var), var_get_width (var)); + if (result != 0) + break; + } + + return result; +} + + bool interaction_case_is_missing (const struct interaction *iact, const struct ccase *c, enum mv_class exclude) {