2-d subtotals work
[pspp] / src / language / stats / ctables.c
index edeb97cc28a883e28e44e5b1cda4d2cabaa8bc41..c982f19cf63ad431faaf532c0637133daf43b574 100644 (file)
@@ -2447,6 +2447,34 @@ recurse_totals (struct ctables_table *t, const struct ccase *c,
     }
 }
 
+static void
+recurse_subtotals (struct ctables_table *t, const struct ccase *c,
+                   size_t ix[PIVOT_N_AXES],
+                   const struct ctables_category *cats[PIVOT_N_AXES][10],
+                   double weight,
+                   enum pivot_axis_type start_axis, size_t start_nest)
+{
+  for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
+    {
+      const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
+      for (size_t i = start_nest; i < nest->n; i++)
+        {
+          if (i == nest->scale_idx)
+            continue;
+
+          const struct ctables_category *save = cats[a][i];
+          if (save->subtotal)
+            {
+              cats[a][i] = save->subtotal;
+              ctables_cell_add__ (t, c, ix, cats, weight);
+              recurse_subtotals (t, c, ix, cats, weight, a, i + 1);
+              cats[a][i] = save;
+            }
+        }
+      start_nest = 0;
+    }
+}
+
 static void
 ctables_cell_insert (struct ctables_table *t,
                      const struct ccase *c,
@@ -2484,24 +2512,7 @@ ctables_cell_insert (struct ctables_table *t,
   ctables_cell_add__ (t, c, ix, cats, weight);
 
   recurse_totals (t, c, ix, cats, weight, 0, 0);
-
-  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
-    {
-      const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
-      for (size_t i = 0; i < nest->n; i++)
-        {
-          if (i == nest->scale_idx)
-            continue;
-
-          const struct ctables_category *save = cats[a][i];
-          if (save->subtotal)
-            {
-              cats[a][i] = save->subtotal;
-              ctables_cell_add__ (t, c, ix, cats, weight);
-              cats[a][i] = save;
-            }
-        }
-    }
+  recurse_subtotals (t, c, ix, cats, weight, 0, 0);
 }
 
 struct merge_item
@@ -2722,17 +2733,16 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
                   if (level->type == CTL_CATEGORY)
                     {
                       size_t var_idx = level->var_idx;
-                      if (prev->axes[a].cvs[var_idx].category
-                          != cell->axes[a].cvs[var_idx].category)
-                        {
-                          break;
-                        }
-                      else if (!value_equal (&prev->axes[a].cvs[var_idx].value,
-                                           &cell->axes[a].cvs[var_idx].value,
-                                             var_get_type (nest->vars[var_idx])))
-                        {
-                          break;
-                        }
+                      const struct ctables_category *c = cell->axes[a].cvs[var_idx].category;
+                      if (prev->axes[a].cvs[var_idx].category != c)
+                        break;
+                      else if (c->type != CCT_SUBTOTAL
+                               && c->type != CCT_HSUBTOTAL
+                               && c->type != CCT_TOTAL
+                               && !value_equal (&prev->axes[a].cvs[var_idx].value,
+                                                &cell->axes[a].cvs[var_idx].value,
+                                                var_get_type (nest->vars[var_idx])))
+                        break;
                     }
                 }
             }