- struct ctables_cell *cell = sorted[j];
- const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
-
- size_t n_common = 0;
- bool new_subtable = false;
- if (j > 0)
- {
- struct ctables_cell *prev = sorted[j - 1];
- if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
- {
- for (; n_common < nest->n; n_common++)
- if (n_common != nest->scale_idx
- && (prev->axes[a].cvs[n_common].category
- != cell->axes[a].cvs[n_common].category
- || !value_equal (&prev->axes[a].cvs[n_common].value,
- &cell->axes[a].cvs[n_common].value,
- var_get_type (nest->vars[n_common]))))
- break;
- }
- else
- new_subtable = true;
- }
- else
- new_subtable = true;
-
- if (new_subtable)
- {
- enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
- top = d[a]->root;
- if (vlabel != CTVL_NONE)
- top = pivot_category_create_group__ (
- top, pivot_value_new_variable (nest->vars[0]));
- }
- if (n_common == nest->n)
- {
- cell->axes[a].leaf = prev_leaf;
- continue;
- }
-
- for (size_t k = n_common; k < nest->n; k++)
- {
- struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
-
- struct pivot_value *label
- = (k == nest->scale_idx ? NULL
- : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
- || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
- || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
- ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
- SIZE_MAX)
- : pivot_value_new_var_value (nest->vars[k],
- &cell->axes[a].cvs[k].value));
- if (k == nest->n - 1)
- {
- if (a == t->summary_axis)
- {
- if (label)
- parent = pivot_category_create_group__ (parent, label);
- const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
- for (size_t m = 0; m < specs->n; m++)
- {
- int leaf = pivot_category_create_leaf (
- parent, pivot_value_new_text (specs->specs[m].label));
- if (m == 0)
- prev_leaf = leaf;
- }
- }
- else
- {
- /* This assertion is true as long as the summary axis
- is the axis where the summaries are displayed. */
- assert (label);
-
- prev_leaf = pivot_category_create_leaf (parent, label);
- }
- break;
- }
-
- if (label)
- parent = pivot_category_create_group__ (parent, label);
-
- enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
- if (vlabel != CTVL_NONE)
- parent = pivot_category_create_group__ (
- parent, pivot_value_new_variable (nest->vars[k + 1]));
- groups[k] = parent;
- }
-
- cell->axes[a].leaf = prev_leaf;