+ return var_is_value_missing (var, v) ? NULL : othernm;
+}
+
+static void
+ctables_cell_insert (struct ctables_table *t,
+ const struct ccase *c,
+ size_t ir, size_t ic, size_t il,
+ double weight)
+{
+ size_t ix[PIVOT_N_AXES] = {
+ [PIVOT_AXIS_ROW] = ir,
+ [PIVOT_AXIS_COLUMN] = ic,
+ [PIVOT_AXIS_LAYER] = il,
+ };
+ const struct var_array *ss = &t->vaas[t->summary_axis].vas[ix[t->summary_axis]];
+
+ const struct ctables_category *cats[PIVOT_N_AXES][10];
+ 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 variable *var = va->vars[i];
+ const union value *value = case_data (c, var);
+
+ if (var_is_numeric (var) && value->f == SYSMIS)
+ return;
+
+ cats[a][i] = ctables_categories_match (
+ t->categories[var_get_dict_index (var)], value, var);
+ if (!cats[a][i])
+ return;
+ }
+ }
+
+ size_t hash = 0;
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct var_array *va = &t->vaas[a].vas[ix[a]];
+ hash = hash_int (ix[a], hash);
+ for (size_t i = 0; i < va->n; i++)
+ if (i != va->scale_idx)
+ hash = value_hash (case_data (c, va->vars[i]),
+ var_get_width (va->vars[i]), hash);
+ }
+
+ struct ctables_cell *f;
+ HMAP_FOR_EACH_WITH_HASH (f, struct ctables_cell, node, hash, &t->cells)
+ {
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct var_array *va = &t->vaas[a].vas[ix[a]];
+ if (f->axes[a].vaa_idx != ix[a])
+ goto not_equal;
+ for (size_t i = 0; i < va->n; i++)
+ if (i != va->scale_idx
+ && !value_equal (case_data (c, va->vars[i]),
+ &f->axes[a].cvs[i].value,
+ var_get_width (va->vars[i])))
+ goto not_equal;
+ }
+
+ goto summarize;
+
+ not_equal: ;
+ }
+
+ f = xmalloc (sizeof *f);
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct var_array *va = &t->vaas[a].vas[ix[a]];
+ f->axes[a].vaa_idx = ix[a];
+ f->axes[a].cvs = (va->n
+ ? xnmalloc (va->n, sizeof *f->axes[a].cvs)
+ : NULL);
+ for (size_t i = 0; i < va->n; i++)
+ {
+ f->axes[a].cvs[i].category = cats[a][i];
+ value_clone (&f->axes[a].cvs[i].value, case_data (c, va->vars[i]),
+ var_get_width (va->vars[i]));
+ }
+ }
+ f->summaries = xmalloc (ss->n_summaries * sizeof *f->summaries);
+ for (size_t i = 0; i < ss->n_summaries; i++)
+ ctables_summary_init (&f->summaries[i], &ss->summaries[i]);
+ for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+ f->domains[dt] = ctables_domain_insert (t, f, dt);
+ hmap_insert (&t->cells, &f->node, hash);
+
+summarize:
+ for (size_t i = 0; i < ss->n_summaries; i++)
+ ctables_summary_add (&f->summaries[i], &ss->summaries[i], ss->summary_var,
+ case_data (c, ss->summary_var), weight);
+ for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+ f->domains[dt]->valid += weight;
+}
+
+static bool
+ctables_execute (struct dataset *ds, struct ctables *ct)
+{
+ for (size_t i = 0; i < ct->n_tables; i++)
+ {
+ struct ctables_table *t = ct->tables[i];
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ if (t->axes[a])
+ {
+ t->vaas[a] = enumerate_fts (a, t->axes[a]);
+
+ for (size_t j = 0; j < t->vaas[a].n; j++)
+ {
+ struct var_array *va = &t->vaas[a].vas[j];
+ for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+ {
+ va->domains[dt] = xmalloc (va->n * sizeof *va->domains[dt]);
+ va->n_domains[dt] = 0;
+
+ for (size_t k = 0; k < va->n; k++)
+ {
+ if (k == va->scale_idx)
+ continue;
+
+ switch (dt)
+ {
+ case CTDT_TABLE:
+ continue;
+
+ case CTDT_LAYER:
+ if (a != PIVOT_AXIS_LAYER)
+ continue;
+ break;
+
+ case CTDT_SUBTABLE:
+ case CTDT_ROW:
+ case CTDT_COL:
+ if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
+ : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
+ : a == PIVOT_AXIS_ROW)
+ {
+ if (k == va->n - 1
+ || (va->scale_idx == va->n - 1
+ && k == va->n - 2))
+ continue;
+ }
+ break;
+
+ case CTDT_LAYERROW:
+ if (a == PIVOT_AXIS_COLUMN)
+ continue;
+ break;
+
+ case CTDT_LAYERCOL:
+ if (a == PIVOT_AXIS_ROW)
+ continue;
+ break;
+ }
+
+ va->domains[dt][va->n_domains[dt]++] = k;
+ }
+ }
+ }
+ }
+ else
+ {
+ struct var_array *va = xmalloc (sizeof *va);
+ *va = (struct var_array) { .n = 0 };
+ t->vaas[a] = (struct var_array2) { .vas = va, .n = 1 };
+ }
+
+ for (size_t i = 0; i < t->vaas[t->summary_axis].n; i++)
+ {
+ struct var_array *va = &t->vaas[t->summary_axis].vas[i];
+ if (!va->n_summaries)
+ {
+ va->summaries = xmalloc (sizeof *va->summaries);
+ va->n_summaries = 1;
+
+ enum ctables_summary_function function
+ = va->summary_var ? CTSF_MEAN : CTSF_COUNT;
+ struct ctables_var var = { .is_mrset = false, .var = va->summary_var };
+
+ *va->summaries = (struct ctables_summary_spec) {
+ .function = function,
+ .format = ctables_summary_default_format (function, &var),
+ .label = ctables_summary_default_label (function, 0),
+ };
+ if (!va->summary_var)
+ va->summary_var = va->vars[0];
+ }
+ }