+static struct ctables_domain *
+ctables_domain_insert (struct ctables_section *s, struct ctables_cell *cell,
+ enum ctables_domain_type domain)
+{
+ size_t hash = 0;
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct ctables_nest *nest = s->nests[a];
+ for (size_t i = 0; i < nest->n_domains[domain]; i++)
+ {
+ size_t v_idx = nest->domains[domain][i];
+ hash = value_hash (&cell->axes[a].cvs[v_idx].value,
+ var_get_width (nest->vars[v_idx]), hash);
+ }
+ }
+
+ struct ctables_domain *d;
+ HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &s->domains[domain])
+ {
+ const struct ctables_cell *df = d->example;
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct ctables_nest *nest = s->nests[a];
+ for (size_t i = 0; i < nest->n_domains[domain]; i++)
+ {
+ size_t v_idx = nest->domains[domain][i];
+ if (!value_equal (&df->axes[a].cvs[v_idx].value,
+ &cell->axes[a].cvs[v_idx].value,
+ var_get_width (nest->vars[v_idx])))
+ goto not_equal;
+ }
+ }
+ return d;
+
+ not_equal: ;
+ }
+
+ d = xmalloc (sizeof *d);
+ *d = (struct ctables_domain) { .example = cell };
+ hmap_insert (&s->domains[domain], &d->node, hash);
+ return d;
+}
+
+static const struct ctables_category *
+ctables_categories_match (const struct ctables_categories *c,
+ const union value *v, const struct variable *var)
+{
+ const struct ctables_category *othernm = NULL;
+ for (size_t i = c->n_cats; i-- > 0; )
+ {
+ const struct ctables_category *cat = &c->cats[i];
+ switch (cat->type)
+ {
+ case CCT_NUMBER:
+ if (cat->number == v->f)
+ return cat;
+ break;
+
+ case CCT_STRING:
+ NOT_REACHED ();
+
+ case CCT_RANGE:
+ if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
+ && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
+ return cat;
+ break;
+
+ case CCT_MISSING:
+ if (var_is_value_missing (var, v))
+ return cat;
+ break;
+
+ case CCT_OTHERNM:
+ if (!othernm)
+ othernm = cat;
+ break;
+
+ case CCT_SUBTOTAL:
+ case CCT_HSUBTOTAL:
+ case CCT_TOTAL:
+ break;
+
+ case CCT_VALUE:
+ case CCT_LABEL:
+ case CCT_FUNCTION:
+ return (cat->include_missing || !var_is_value_missing (var, v) ? cat
+ : NULL);
+ }
+ }
+
+ return var_is_value_missing (var, v) ? NULL : othernm;
+}
+
+static const struct ctables_category *
+ctables_categories_total (const struct ctables_categories *c)
+{
+ const struct ctables_category *first = &c->cats[0];
+ const struct ctables_category *last = &c->cats[c->n_cats - 1];
+ return (first->type == CCT_TOTAL ? first
+ : last->type == CCT_TOTAL ? last
+ : NULL);
+}
+
+static struct ctables_cell *
+ctables_cell_insert__ (struct ctables_section *s, const struct ccase *c,
+ const struct ctables_category *cats[PIVOT_N_AXES][10])
+{
+ const struct ctables_nest *ss = s->nests[s->table->summary_axis];
+
+ size_t hash = 0;
+ enum ctables_summary_variant sv = CSV_CELL;
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct ctables_nest *nest = s->nests[a];
+ for (size_t i = 0; i < nest->n; i++)
+ if (i != nest->scale_idx)
+ {
+ hash = hash_pointer (cats[a][i], hash);
+ if (cats[a][i]->type != CCT_TOTAL
+ && cats[a][i]->type != CCT_SUBTOTAL
+ && cats[a][i]->type != CCT_HSUBTOTAL)
+ hash = value_hash (case_data (c, nest->vars[i]),
+ var_get_width (nest->vars[i]), hash);
+ else
+ sv = CSV_TOTAL;
+ }
+ }
+
+ struct ctables_cell *cell;
+ HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &s->cells)
+ {
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct ctables_nest *nest = s->nests[a];
+ for (size_t i = 0; i < nest->n; i++)
+ if (i != nest->scale_idx
+ && (cats[a][i] != cell->axes[a].cvs[i].category
+ || (cats[a][i]->type != CCT_TOTAL
+ && cats[a][i]->type != CCT_SUBTOTAL
+ && cats[a][i]->type != CCT_HSUBTOTAL
+ && !value_equal (case_data (c, nest->vars[i]),
+ &cell->axes[a].cvs[i].value,
+ var_get_width (nest->vars[i])))))
+ goto not_equal;
+ }
+
+ return cell;
+
+ not_equal: ;
+ }
+
+ cell = xmalloc (sizeof *cell);
+ cell->hide = false;
+ cell->sv = sv;
+ cell->contributes_to_domains = true;
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct ctables_nest *nest = s->nests[a];
+ cell->axes[a].cvs = (nest->n
+ ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
+ : NULL);
+ for (size_t i = 0; i < nest->n; i++)
+ {
+ const struct ctables_category *cat = cats[a][i];
+
+ if (i != nest->scale_idx)
+ {
+ const struct ctables_category *subtotal = cat->subtotal;
+ if (subtotal && subtotal->type == CCT_HSUBTOTAL)
+ cell->hide = true;
+
+ if (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
+ cell->contributes_to_domains = false;
+ }
+
+ cell->axes[a].cvs[i].category = cat;
+ value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
+ var_get_width (nest->vars[i]));
+ }
+ }
+
+ const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
+ cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
+ for (size_t i = 0; i < specs->n; i++)
+ ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
+ for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+ cell->domains[dt] = ctables_domain_insert (s, cell, dt);
+ hmap_insert (&s->cells, &cell->node, hash);
+ return cell;
+}
+
+static void
+ctables_cell_add__ (struct ctables_section *s, const struct ccase *c,
+ const struct ctables_category *cats[PIVOT_N_AXES][10],
+ double d_weight, double e_weight)
+{
+ struct ctables_cell *cell = ctables_cell_insert__ (s, c, cats);
+ const struct ctables_nest *ss = s->nests[s->table->summary_axis];
+
+ const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
+ for (size_t i = 0; i < specs->n; i++)
+ ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
+ case_data (c, specs->var), d_weight, e_weight);
+ if (cell->contributes_to_domains)
+ {
+ for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+ {
+ cell->domains[dt]->d_valid += d_weight;
+ cell->domains[dt]->e_valid += e_weight;
+ }
+ }
+}
+
+static void
+recurse_totals (struct ctables_section *s, const struct ccase *c,
+ const struct ctables_category *cats[PIVOT_N_AXES][10],
+ double d_weight, double e_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 = s->nests[a];
+ for (size_t i = start_nest; i < nest->n; i++)
+ {
+ if (i == nest->scale_idx)
+ continue;
+
+ const struct variable *var = nest->vars[i];
+
+ const struct ctables_category *total = ctables_categories_total (
+ s->table->categories[var_get_dict_index (var)]);
+ if (total)
+ {
+ const struct ctables_category *save = cats[a][i];
+ cats[a][i] = total;
+ ctables_cell_add__ (s, c, cats, d_weight, e_weight);
+ recurse_totals (s, c, cats, d_weight, e_weight, a, i + 1);
+ cats[a][i] = save;
+ }
+ }
+ start_nest = 0;
+ }
+}
+
+static void
+recurse_subtotals (struct ctables_section *s, const struct ccase *c,
+ const struct ctables_category *cats[PIVOT_N_AXES][10],
+ double d_weight, double e_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 = s->nests[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__ (s, c, cats, d_weight, e_weight);
+ recurse_subtotals (s, c, cats, d_weight, e_weight, a, i + 1);
+ cats[a][i] = save;
+ }
+ }
+ start_nest = 0;
+ }
+}
+
+static void
+ctables_cell_insert (struct ctables_section *s,
+ const struct ccase *c,
+ double d_weight, double e_weight)
+{
+ const struct ctables_category *cats[PIVOT_N_AXES][10];
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct ctables_nest *nest = s->nests[a];
+ for (size_t i = 0; i < nest->n; i++)
+ {
+ if (i == nest->scale_idx)
+ continue;
+
+ const struct variable *var = nest->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 (
+ s->table->categories[var_get_dict_index (var)], value, var);
+ if (!cats[a][i])
+ return;
+ }
+ }
+
+ ctables_cell_add__ (s, c, cats, d_weight, e_weight);
+
+ recurse_totals (s, c, cats, d_weight, e_weight, 0, 0);
+ recurse_subtotals (s, c, cats, d_weight, e_weight, 0, 0);
+}
+
+struct merge_item
+ {
+ const struct ctables_summary_spec_set *set;
+ size_t ofs;
+ };
+
+static int
+merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
+{
+ const struct ctables_summary_spec *as = &a->set->specs[a->ofs];
+ const struct ctables_summary_spec *bs = &b->set->specs[b->ofs];
+ if (as->function != bs->function)
+ return as->function > bs->function ? 1 : -1;
+ else if (as->percentile != bs->percentile)
+ return as->percentile < bs->percentile ? 1 : -1;
+ return strcmp (as->label, bs->label);
+}
+
+static struct pivot_value *
+ctables_category_create_label (const struct ctables_category *cat,
+ const struct variable *var,
+ const union value *value)
+{
+ return (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL
+ ? pivot_value_new_user_text (cat->total_label, SIZE_MAX)
+ : pivot_value_new_var_value (var, value));
+}
+
+static struct ctables_value *
+ctables_value_find__ (struct ctables_table *t, const union value *value,
+ int width, unsigned int hash)
+{
+ struct ctables_value *clv;
+ HMAP_FOR_EACH_WITH_HASH (clv, struct ctables_value, node,
+ hash, &t->clabels_values_map)
+ if (value_equal (value, &clv->value, width))
+ return clv;
+ return NULL;
+}
+
+static struct ctables_value *
+ctables_value_find (struct ctables_table *t,
+ const union value *value, int width)
+{
+ return ctables_value_find__ (t, value, width,
+ value_hash (value, width, 0));
+}
+