category filtering works
[pspp] / src / language / stats / ctables.c
index 88c326c8d0c4576f216159b5f0671a43054f80c9..07de68d919361ecb73cd7c8b269b1698b21295f7 100644 (file)
@@ -146,13 +146,18 @@ enum {
 
 enum ctables_domain_type
   {
-    CTDT_TABLE,                  /* Entire table. */
-    CTDT_SUBTABLE,               /* Innermost variable in a single layer. */
+    /* Within a section, where stacked variables divide one section from
+       another. */
+    CTDT_TABLE,                  /* All layers of a whole section. */
+    CTDT_LAYER,                  /* One layer within a section. */
+    CTDT_LAYERROW,               /* Row in one layer within a section. */
+    CTDT_LAYERCOL,               /* Column in one layer within a section. */
+
+    /* Within a subtable, where a subtable pairs an innermost row variable with
+       an innermost column variable within a single layer.  */
+    CTDT_SUBTABLE,               /* Whole subtable. */
     CTDT_ROW,                    /* Row within a subtable. */
     CTDT_COL,                    /* Column within a subtable. */
-    CTDT_LAYER,                  /* Entire layer. */
-    CTDT_LAYERROW,               /* Row within a layer. */
-    CTDT_LAYERCOL,               /* Column within a layer. */
 #define N_CTDTS 7
   };
 
@@ -309,7 +314,6 @@ struct ctables_table
 
     struct ctables_chisq *chisq;
     struct ctables_pairwise *pairwise;
-
   };
 
 struct ctables_var
@@ -1929,8 +1933,6 @@ static struct ctables_domain *
 ctables_domain_insert (struct ctables_table *t, struct ctables_freq *f,
                        enum ctables_domain_type domain)
 {
-  /* XXX how can we handle CTDT_LAYERROW and CTDT_LAYERCOL?  Crossing subtables
-     is hard.  */
   size_t hash = 0;
   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
     {
@@ -1976,6 +1978,49 @@ ctables_domain_insert (struct ctables_table *t, struct ctables_freq *f,
   return d;
 }
 
+static const struct ctables_cat_value *
+ctables_categories_match (const struct ctables_categories *cats,
+                          const union value *v, const struct variable *var)
+{
+  const struct ctables_cat_value *othernm = NULL;
+  for (size_t i = cats->n_values; i-- > 0; )
+    {
+      const struct ctables_cat_value *cv = &cats->values[i];
+      switch (cv->type)
+        {
+        case CCVT_NUMBER:
+          if (cv->number == v->f)
+            return cv;
+          break;
+
+        case CCVT_STRING:
+          NOT_REACHED ();
+
+        case CCVT_RANGE:
+          if ((cv->range[0] == -DBL_MAX || v->f >= cv->range[0])
+              && (cv->range[1] == DBL_MAX || v->f <= cv->range[1]))
+            return cv;
+          break;
+
+        case CCVT_MISSING:
+          if (var_is_value_missing (var, v))
+            return cv;
+          break;
+
+        case CCVT_OTHERNM:
+          if (!othernm)
+            othernm = cv;
+          break;
+
+        case CCVT_SUBTOTAL:
+        case CCVT_HSUBTOTAL:
+          break;
+        }
+    }
+
+  return var_is_value_missing (var, v) ? NULL : othernm;
+}
+
 static void
 ctables_freqtab_insert (struct ctables_table *t,
                         const struct ccase *c,
@@ -1989,6 +2034,23 @@ ctables_freqtab_insert (struct ctables_table *t,
   };
   const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
 
+  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+    {
+      const struct var_array *va = &t->vaas[a].vas[ix[a]];
+      for (size_t i = 0; i < va->n; i++)
+        {
+          if (i == va->scale_idx)
+            continue;
+
+          const struct ctables_categories *cats = t->categories[var_get_dict_index (va->vars[i])];
+          if (!cats || !cats->n_values)
+            continue;
+
+          if (!ctables_categories_match (cats, case_data (c, va->vars[i]), va->vars[i]))
+            return;
+        }
+    }
+
   size_t hash = 0;
   for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
     {