Move interaction subset predicates out of glm.c into interaction.c
[pspp-builds.git] / src / math / categoricals.c
index 6e39ff2682c45b0bb7c638189d9fe6042711b8a8..9f8215d30eeabd5196252db87605044525529467 100644 (file)
@@ -73,6 +73,7 @@ struct variable_node
   struct hmap valmap;         /* A map of value nodes */
 };
 
+#if 0
 static void
 dump_interaction (const struct interaction *iact)
 {
@@ -81,6 +82,7 @@ dump_interaction (const struct interaction *iact)
   printf ("Interaction: %s\n", ds_cstr (&str));
   ds_destroy (&str);
 }
+#endif
 
 
 static struct variable_node *
@@ -180,7 +182,7 @@ struct categoricals
   void *aux2;
 };
 
-
+#if 0
 static void
 categoricals_dump (const struct categoricals *cat)
 {
@@ -238,11 +240,36 @@ categoricals_dump (const struct categoricals *cat)
       printf (")\n");
     }
 }
-
+#endif
 
 void
 categoricals_destroy (struct categoricals *cat)
 {
+  struct variable_node *vn = NULL;
+  int i;
+  if (NULL == cat)
+    return;
+  for (i = 0; i < cat->n_iap; ++i)
+    {
+      struct interaction_value *iv = NULL;
+      /* Interate over each interaction value, and unref any cases that we reffed */
+      HMAP_FOR_EACH (iv, struct interaction_value, node, &cat->iap[i].ivmap)
+       {
+         case_unref (iv->ccase);
+       }
+      hmap_destroy (&cat->iap[i].ivmap);
+    }
+
+  /* Interate over each variable and delete its value map */
+  HMAP_FOR_EACH (vn, struct variable_node, node, &cat->varmap)
+    {
+      hmap_destroy (&vn->valmap);
+    }
+
+  hmap_destroy (&cat->varmap);
+
+  pool_destroy (cat->pool);
+
   free (cat);
 }
 
@@ -486,6 +513,10 @@ categoricals_done (const struct categoricals *cat_)
       sort (iap->reverse_interaction_value_map, x, sizeof (*iap->reverse_interaction_value_map),
            compare_interaction_value_3way, iap);
 
+      /* Fill the remaining values with null */
+      for (ii = x ; ii < iap->n_cats; ++ii)
+       iap->reverse_interaction_value_map[ii] = NULL;
+
       /* Populate the reverse variable maps. */
       for (ii = 0; ii < iap->df; ++ii)
        cat->reverse_variable_map_short[idx_short++] = i;
@@ -538,6 +569,10 @@ categoricals_get_case_by_subscript (const struct categoricals *cat, int subscrip
   int vindex = reverse_variable_lookup_short (cat, subscript);
   const struct interact_params *vp = &cat->iap[vindex];
   const struct interaction_value *vn = vp->reverse_interaction_value_map [subscript - vp->base_subscript_short];
+
+  if ( vn == NULL)
+    return NULL;
+
   return vn->ccase;
 }
 
@@ -559,6 +594,9 @@ categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript
 
   const struct interaction_value *iv = vp->reverse_interaction_value_map [subscript - vp->base_subscript_short];
 
+  if (iv == NULL)
+    return 0;
+
   return iv->cc;
 }
 
@@ -572,6 +610,9 @@ categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscr
 
   const struct ccase *c2 =  categoricals_get_case_by_subscript (cat, subscript);
 
+  if ( c2 == NULL)
+    return 0;
+
   return interaction_case_equal (iact, c, c2);
 }