+ return 0;
+
+found: ;
+ const struct ctables_table *t = s->table;
+ const struct ctables_nest *specs_nest = s->nests[t->summary_axis];
+ const struct ctables_summary_spec_set *specs = &specs_nest->specs[tc->sv];
+ return ctables_summary_value (tc->areas, &tc->summaries[ctx->summary_idx],
+ &specs->specs[ctx->summary_idx]);
+}
+
+static double
+ctables_pcexpr_evaluate (const struct ctables_pcexpr_evaluate_ctx *ctx,
+ const struct ctables_pcexpr *e)
+{
+ switch (e->op)
+ {
+ case CTPO_CONSTANT:
+ return e->number;
+
+ case CTPO_CAT_NRANGE:
+ case CTPO_CAT_SRANGE:
+ case CTPO_CAT_MISSING:
+ case CTPO_CAT_OTHERNM:
+ {
+ struct ctables_cell_value cv = {
+ .category = ctables_find_category_for_postcompute (ctx->section->table->ctables->dict, ctx->cats, ctx->parse_format, e)
+ };
+ assert (cv.category != NULL);
+
+ struct hmap *occurrences = &ctx->section->occurrences[ctx->pc_a][ctx->pc_a_idx];
+ const struct ctables_occurrence *o;
+
+ double sum = 0.0;
+ const struct variable *var = ctx->section->nests[ctx->pc_a]->vars[ctx->pc_a_idx];
+ HMAP_FOR_EACH (o, struct ctables_occurrence, node, occurrences)
+ if (ctables_categories_match (ctx->cats, &o->value, var) == cv.category)
+ {
+ cv.value = o->value;
+ sum += ctables_pcexpr_evaluate_category (ctx, &cv);
+ }
+ return sum;
+ }
+
+ case CTPO_CAT_NUMBER:
+ case CTPO_CAT_SUBTOTAL:
+ case CTPO_CAT_TOTAL:
+ {
+ struct ctables_cell_value cv = {
+ .category = ctables_find_category_for_postcompute (ctx->section->table->ctables->dict, ctx->cats, ctx->parse_format, e),
+ .value = { .f = e->number },
+ };
+ assert (cv.category != NULL);
+ return ctables_pcexpr_evaluate_category (ctx, &cv);
+ }
+
+ case CTPO_CAT_STRING:
+ {
+ int width = var_get_width (ctx->section->nests[ctx->pc_a]->vars[ctx->pc_a_idx]);
+ char *s = NULL;
+ if (width > e->string.length)
+ {
+ s = xmalloc (width);
+ buf_copy_rpad (s, width, e->string.string, e->string.length, ' ');
+ }
+
+ const struct ctables_category *category
+ = ctables_find_category_for_postcompute (
+ ctx->section->table->ctables->dict,
+ ctx->cats, ctx->parse_format, e);
+ assert (category != NULL);
+
+ struct ctables_cell_value cv = { .category = category };
+ if (category->type == CCT_NUMBER)
+ cv.value.f = category->number;
+ else if (category->type == CCT_STRING)
+ cv.value.s = CHAR_CAST (uint8_t *, s ? s : e->string.string);
+ else
+ NOT_REACHED ();
+
+ double retval = ctables_pcexpr_evaluate_category (ctx, &cv);
+ free (s);
+ return retval;
+ }
+
+ case CTPO_ADD:
+ return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_add);
+
+ case CTPO_SUB:
+ return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_sub);
+
+ case CTPO_MUL:
+ return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_mul);
+
+ case CTPO_DIV:
+ return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_div);
+
+ case CTPO_POW:
+ return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_pow);
+
+ case CTPO_NEG:
+ return ctables_pcexpr_evaluate_nonterminal (ctx, e, 1, ctpo_neg);
+ }
+
+ NOT_REACHED ();
+}
+
+static const struct ctables_category *
+ctables_cell_postcompute (const struct ctables_section *s,
+ const struct ctables_cell *cell,
+ enum pivot_axis_type *pc_a_p,
+ size_t *pc_a_idx_p)
+{
+ assert (cell->postcompute);
+ const struct ctables_category *pc_cat = NULL;
+ for (enum pivot_axis_type pc_a = 0; pc_a < PIVOT_N_AXES; pc_a++)
+ for (size_t pc_a_idx = 0; pc_a_idx < s->nests[pc_a]->n; pc_a_idx++)
+ {
+ const struct ctables_cell_value *cv = &cell->axes[pc_a].cvs[pc_a_idx];
+ if (cv->category->type == CCT_POSTCOMPUTE)
+ {
+ if (pc_cat)
+ {
+ /* Multiple postcomputes cross each other. The value is
+ undefined. */
+ return NULL;
+ }
+
+ pc_cat = cv->category;
+ if (pc_a_p)
+ *pc_a_p = pc_a;
+ if (pc_a_idx_p)
+ *pc_a_idx_p = pc_a_idx;
+ }
+ }
+
+ assert (pc_cat != NULL);
+ return pc_cat;
+}
+
+static double
+ctables_cell_calculate_postcompute (const struct ctables_section *s,
+ const struct ctables_cell *cell,
+ const struct ctables_summary_spec *ss,
+ struct fmt_spec *format,
+ bool *is_ctables_format,
+ size_t summary_idx)
+{
+ enum pivot_axis_type pc_a = 0;
+ size_t pc_a_idx = 0;
+ const struct ctables_category *pc_cat = ctables_cell_postcompute (
+ s, cell, &pc_a, &pc_a_idx);
+ if (!pc_cat)
+ return SYSMIS;
+
+ const struct ctables_postcompute *pc = pc_cat->pc;
+ if (pc->specs)
+ {
+ for (size_t i = 0; i < pc->specs->n; i++)
+ {
+ const struct ctables_summary_spec *ss2 = &pc->specs->specs[i];
+ if (ss->function == ss2->function
+ && ss->weighting == ss2->weighting
+ && ss->calc_area == ss2->calc_area
+ && ss->percentile == ss2->percentile)
+ {
+ *format = ss2->format;
+ *is_ctables_format = ss2->is_ctables_format;
+ break;
+ }
+ }
+ }
+
+ const struct variable *var = s->nests[pc_a]->vars[pc_a_idx];
+ const struct ctables_categories *cats = s->table->categories[
+ var_get_dict_index (var)];
+ struct ctables_pcexpr_evaluate_ctx ctx = {
+ .cell = cell,
+ .section = s,
+ .cats = cats,
+ .pc_a = pc_a,
+ .pc_a_idx = pc_a_idx,
+ .summary_idx = summary_idx,
+ .parse_format = pc_cat->parse_format,
+ };
+ return ctables_pcexpr_evaluate (&ctx, pc->expr);
+}
+\f
+/* Chi-square test (SIGTEST). */
+struct ctables_chisq
+ {
+ double alpha;
+ bool include_mrsets;
+ bool all_visible;
+ };
+
+/* Pairwise comparison test (COMPARETEST). */
+struct ctables_pairwise
+ {
+ enum { PROP, MEAN } type;
+ double alpha[2];
+ bool include_mrsets;
+ bool meansvariance_allcats;
+ bool all_visible;
+ enum { BONFERRONI = 1, BH } adjust;
+ bool merge;
+ bool apa_style;
+ bool show_sig;
+ };
+
+
+
+static bool
+parse_col_width (struct lexer *lexer, const char *name, double *width)
+{
+ lex_match (lexer, T_EQUALS);
+ if (lex_match_id (lexer, "DEFAULT"))
+ *width = SYSMIS;
+ else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
+ {
+ *width = lex_number (lexer);
+ lex_get (lexer);
+ }
+ else
+ return false;
+
+ return true;
+}
+
+static bool
+parse_bool (struct lexer *lexer, bool *b)
+{
+ if (lex_match_id (lexer, "NO"))
+ *b = false;
+ else if (lex_match_id (lexer, "YES"))
+ *b = true;
+ else
+ {
+ lex_error_expecting (lexer, "YES", "NO");
+ return false;
+ }
+ return true;
+}
+
+static void
+ctables_chisq_destroy (struct ctables_chisq *chisq)
+{
+ free (chisq);
+}
+
+static void
+ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
+{
+ free (pairwise);
+}
+
+static void
+ctables_table_destroy (struct ctables_table *t)
+{
+ if (!t)
+ return;
+
+ for (size_t i = 0; i < t->n_sections; i++)
+ ctables_section_uninit (&t->sections[i]);
+ free (t->sections);
+
+ for (size_t i = 0; i < t->n_categories; i++)
+ ctables_categories_unref (t->categories[i]);
+ free (t->categories);
+
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ ctables_axis_destroy (t->axes[a]);
+ ctables_stack_uninit (&t->stacks[a]);
+ }
+ free (t->summary_specs.specs);
+
+ struct ctables_value *ctv, *next_ctv;
+ HMAP_FOR_EACH_SAFE (ctv, next_ctv, struct ctables_value, node,
+ &t->clabels_values_map)
+ {
+ value_destroy (&ctv->value, var_get_width (t->clabels_example));
+ hmap_delete (&t->clabels_values_map, &ctv->node);
+ free (ctv);
+ }
+ hmap_destroy (&t->clabels_values_map);
+ free (t->clabels_values);
+
+ free (t->sum_vars);
+ free (t->caption);
+ free (t->corner);
+ free (t->title);
+ ctables_chisq_destroy (t->chisq);
+ ctables_pairwise_destroy (t->pairwise);
+ free (t);
+}
+
+static void
+ctables_destroy (struct ctables *ct)
+{
+ if (!ct)
+ return;
+
+ struct ctables_postcompute *pc, *next_pc;
+ HMAP_FOR_EACH_SAFE (pc, next_pc, struct ctables_postcompute, hmap_node,
+ &ct->postcomputes)
+ {
+ free (pc->name);
+ msg_location_destroy (pc->location);
+ ctables_pcexpr_destroy (pc->expr);
+ free (pc->label);
+ if (pc->specs)
+ {
+ ctables_summary_spec_set_uninit (pc->specs);
+ free (pc->specs);
+ }
+ hmap_delete (&ct->postcomputes, &pc->hmap_node);
+ free (pc);
+ }
+ hmap_destroy (&ct->postcomputes);