Avoid declaring variables in the middle of a block, to avoid requiring C99.
[pspp] / src / math / interaction.c
index c06acde8b027850c82d1b05174bece68daee7210..269aa7e9ad9af816a56b5c95d2fc4ce9ea90dbf7 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <config.h>
 
+#include "data/case.h"
 #include "interaction.h"
 
 #include "data/value.h"
@@ -47,14 +48,21 @@ 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;
 }
 
 void
 interaction_destroy (struct interaction *i)
 {
+  if (NULL == i)
+    return;
+
   free (i->vars);
   free (i);
 }
@@ -67,6 +75,56 @@ 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)
 {
@@ -97,27 +155,29 @@ interaction_to_string (const struct interaction *iact, struct string *str)
 }
 
 unsigned int
-interaction_value_hash (const struct interaction *iact, const union value *val)
+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)
     {
-      hash = value_hash (&val[i], var_get_width (iact->vars[i]), hash);
+      const struct variable *var = iact->vars[i];
+      const union value *val = case_data (c, var);
+      hash = value_hash (val, var_get_width (var), hash);
     }
-
   return hash;
 }
 
 bool
-interaction_value_equal (const struct interaction *iact, const union value *val1, const union value *val2)
+interaction_case_equal (const struct interaction *iact, const struct ccase *c1, const struct ccase *c2)
 {
   int i;
   bool same = true;
 
   for (i = 0; i < iact->n_vars; ++i)
     {
-      if ( ! value_equal (&val1[i], &val2[i], var_get_width (iact->vars[i])))
+      const struct variable *var = iact->vars[i];
+      if ( ! value_equal (case_data (c1, var), case_data (c2, var), var_get_width (var)))
        {
          same = false;
          break;
@@ -128,15 +188,33 @@ interaction_value_equal (const struct interaction *iact, const union value *val1
 }
 
 
+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_value_is_missing (const struct interaction *iact, const union value *val, enum mv_class exclude)
+interaction_case_is_missing (const struct interaction *iact, const struct ccase *c, enum mv_class exclude)
 {
   int i;
   bool missing = false;
 
   for (i = 0; i < iact->n_vars; ++i)
     {
-      if ( var_is_value_missing (iact->vars[i], &val[i], exclude))
+      if ( var_is_value_missing (iact->vars[i], case_data (c, iact->vars[i]), exclude))
        {
          missing = true;
          break;
@@ -145,3 +223,4 @@ interaction_value_is_missing (const struct interaction *iact, const union value
 
   return missing;
 }
+