data filled in
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 2 Jan 2022 01:23:38 +0000 (17:23 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 13 Mar 2022 23:56:02 +0000 (16:56 -0700)
src/language/stats/ctables.c

index 9f8223cbb6936a1afe3da25c8c265d965acc756e..4f23b3607d46aab491e832c407bc1a00ad8a40cd 100644 (file)
@@ -1821,6 +1821,7 @@ struct ctables_freq
       {
         size_t vaa_idx;
         union value *values;
+        int leaf;
       }
     axes[PIVOT_N_AXES];
 
@@ -2051,7 +2052,7 @@ ctables_execute (struct dataset *ds, struct ctables *ct)
           assert (n == t->ft.count);
 
           struct ctables_freq_sort_aux aux = { .t = t, .a = a };
-          n = sort_unique (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
+          sort (sorted, n, sizeof *sorted, ctables_freq_compare_3way, &aux);
 
           size_t max_depth = 0;
           for (size_t j = 0; j < t->vaas[a].n; j++)
@@ -2060,6 +2061,7 @@ ctables_execute (struct dataset *ds, struct ctables *ct)
 
           struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
           struct pivot_category *top = NULL;
+          int prev_leaf = 0;
           for (size_t j = 0; j < n; j++)
             {
               struct ctables_freq *f = sorted[j];
@@ -2083,10 +2085,20 @@ ctables_execute (struct dataset *ds, struct ctables *ct)
                 }
               else
                 new_subtable = true;
+
               if (new_subtable)
-                top = pivot_category_create_group__ (
-                  d[a]->root, pivot_value_new_variable (va->vars[0]));
-              printf ("n_common=%zu\n", n_common);
+                {
+                  enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[0])];
+                  top = d[a]->root;
+                  if (vlabel != CTVL_NONE)
+                    top = pivot_category_create_group__ (
+                      top, pivot_value_new_variable (va->vars[0]));
+                }
+              if (n_common == va->n)
+                {
+                  f->axes[a].leaf = prev_leaf;
+                  continue;
+                }
 
               for (size_t k = n_common; k < va->n; k++)
                 {
@@ -2101,23 +2113,45 @@ ctables_execute (struct dataset *ds, struct ctables *ct)
                         {
                           parent = pivot_category_create_group__ (parent, label);
                           for (size_t m = 0; m < va->n_summaries; m++)
-                            pivot_category_create_leaf (
-                              parent, pivot_value_new_text (va->summaries[m].label));
+                            {
+                              int leaf = pivot_category_create_leaf (
+                                parent, pivot_value_new_text (va->summaries[m].label));
+                              if (m == 0)
+                                prev_leaf = leaf;
+                            }
                         }
                       else
-                        pivot_category_create_leaf (parent, label);
+                        prev_leaf = pivot_category_create_leaf (parent, label);
                       break;
                     }
 
                   parent = pivot_category_create_group__ (parent, label);
 
-                  parent = pivot_category_create_group__ (
-                    parent, pivot_value_new_variable (va->vars[k]));
+                  enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (va->vars[k + 1])];
+                  if (vlabel != CTVL_NONE)
+                    parent = pivot_category_create_group__ (
+                      parent, pivot_value_new_variable (va->vars[k + 1]));
                   groups[k] = parent;
-                    }
                 }
+
+              f->axes[a].leaf = prev_leaf;
+            }
+          free (sorted);
           free (groups);
         }
+      struct ctables_freq *f;
+      HMAP_FOR_EACH (f, struct ctables_freq, node, &t->ft)
+        {
+          size_t dindexes[3];
+          size_t n_dindexes = 0;
+
+          for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+            if (d[a])
+              dindexes[n_dindexes++] = f->axes[a].leaf;
+          pivot_table_put (pt, dindexes, n_dindexes,
+                           pivot_value_new_number (f->count));
+        }
+
       pivot_table_submit (pt);
     }