+ assert (t->axes[a]);
+
+ struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
+ size_t n_sorted = 0;
+
+ struct ctables_cell *cell;
+ HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
+ if (!cell->hide)
+ sorted[n_sorted++] = cell;
+ assert (n_sorted <= t->cells.count);
+
+ struct ctables_cell_sort_aux aux = { .t = t, .a = a };
+ sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
+
+ size_t max_depth = 0;
+ for (size_t j = 0; j < t->stacks[a].n; j++)
+ if (t->stacks[a].nests[j].n > max_depth)
+ max_depth = t->stacks[a].nests[j].n;
+
+ /* Pivot categories:
+
+ - variable label for nest->vars[0], if vlabel != CTVL_NONE
+ - category for nest->vars[0], if nest->scale_idx != 0
+ - variable label for nest->vars[1], if vlabel != CTVL_NONE
+ - category for nest->vars[1], if nest->scale_idx != 1
+ ...
+ - variable label for nest->vars[n - 1], if vlabel != CTVL_NONE
+ - category for nest->vars[n - 1], if t->label_axis[a] == a && nest->scale_idx != n - 1.
+ - summary function, if 'a == t->slabels_axis && a ==
+ t->summary_axis'.
+
+ Additional dimensions:
+
+ - If 'a == t->slabels_axis && a != t->summary_axis', add a summary
+ dimension.
+ - If 't->label_axis[b] == a' for some 'b != a', add a category
+ dimension to 'a'.
+ */
+
+ struct ctables_level
+ {
+ enum ctables_level_type
+ {
+ CTL_VAR, /* Variable label for nest->vars[var_idx]. */
+ CTL_CATEGORY, /* Category for nest->vars[var_idx]. */
+ CTL_SUMMARY, /* Summary functions. */
+ }
+ type;
+
+ size_t var_idx;
+ };
+ struct ctables_level *levels = xnmalloc (1 + 2 * max_depth, sizeof *levels);
+ size_t n_levels = 0;
+
+ struct pivot_category **groups = xnmalloc (1 + 2 * max_depth, sizeof *groups);
+ int prev_leaf = 0;
+ for (size_t j = 0; j < n_sorted; j++)
+ {
+ struct ctables_cell *cell = sorted[j];
+ struct ctables_cell *prev = j > 0 ? sorted[j - 1] : NULL;
+ const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
+
+ bool new_subtable = !prev || prev->axes[a].stack_idx != cell->axes[a].stack_idx;
+ if (new_subtable)
+ {
+ n_levels = 0;
+ printf ("%s levels:", pivot_axis_type_to_string (a));
+ for (size_t k = 0; k < nest->n; k++)
+ {
+ enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k])];
+ if (vlabel != CTVL_NONE)
+ {
+ printf (" var(%s)", var_get_name (nest->vars[k]));
+ levels[n_levels++] = (struct ctables_level) {
+ .type = CTL_VAR,
+ .var_idx = k,
+ };
+ }
+
+ if (nest->scale_idx != k
+ && (k != nest->n - 1 || t->label_axis[a] == a))
+ {
+ printf (" category(%s)", var_get_name (nest->vars[k]));
+ levels[n_levels++] = (struct ctables_level) {
+ .type = CTL_CATEGORY,
+ .var_idx = k,
+ };
+ }
+ }
+
+ if (a == t->slabels_axis && a == t->summary_axis)
+ {
+ printf (" summary");
+ levels[n_levels++] = (struct ctables_level) {
+ .type = CTL_SUMMARY,
+ .var_idx = SIZE_MAX,
+ };
+ }
+ printf ("\n");
+ }
+
+ size_t n_common = 0;
+ if (!new_subtable)
+ {
+ for (; n_common < n_levels; n_common++)
+ {
+ const struct ctables_level *level = &levels[n_common];
+ 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;
+ }
+ }
+ }
+ }
+
+ for (size_t k = n_common; k < n_levels; k++)
+ {
+ const struct ctables_level *level = &levels[k];
+ struct pivot_category *parent = k ? groups[k - 1] : d[a]->root;
+ if (level->type == CTL_SUMMARY)
+ {
+ const struct ctables_summary_spec_set *specs = &t->summary_specs;
+ for (size_t m = 0; m < specs->n; m++)
+ pivot_category_create_leaf (
+ parent, pivot_value_new_text (specs->specs[m].label));
+ }
+ else
+ {
+ const struct variable *var = nest->vars[level->var_idx];
+ struct pivot_value *label;
+ if (level->type == CTL_VAR)
+ label = pivot_value_new_variable (var);
+ else if (level->type == CTL_CATEGORY)
+ {
+ const struct ctables_cell_value *cv = &cell->axes[a].cvs[level->var_idx];
+ label = ctables_category_create_label (cv->category,
+ var, &cv->value);
+ }
+ else
+ NOT_REACHED ();
+
+ if (k == n_levels - 1)
+ pivot_category_create_leaf (parent, label);
+ else
+ groups[k] = pivot_category_create_group__ (parent, label);
+ }
+ }
+
+ cell->axes[a].leaf = prev_leaf;
+ }
+ free (sorted);
+ free (groups);
+ }
+ pivot_table_submit (pt);
+}
+
+
+static void
+ctables_prepare_table (struct ctables_table *t)
+{
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ if (t->axes[a])
+ {
+ t->stacks[a] = enumerate_fts (a, t->axes[a]);
+
+ for (size_t j = 0; j < t->stacks[a].n; j++)
+ {
+ struct ctables_nest *nest = &t->stacks[a].nests[j];
+ for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)