add examples
[pspp] / src / language / stats / ctables.c
index 0024ac28efd0bc9a388aa2c0451c628956ddca76..7d2f15df5a853a715c410c1721a857d706203b5a 100644 (file)
 #define _(msgid) gettext (msgid)
 #define N_(msgid) (msgid)
 
+struct ctables;
+
+/* The three forms of weighting supported by CTABLES. */
 enum ctables_weighting
   {
-    CTW_EFFECTIVE,
-    CTW_DICTIONARY,
-    CTW_UNWEIGHTED
+    CTW_EFFECTIVE,             /* Effective base weight (WEIGHT subcommand). */
+    CTW_DICTIONARY,            /* Dictionary weight. */
+    CTW_UNWEIGHTED             /* No weight. */
 #define N_CTWS 3
   };
 \f
@@ -95,16 +98,22 @@ static const char *ctables_area_type_name[N_CTATS] = {
   [CTAT_COL] = "COL",
 };
 
+/* Summary statistics for an area. */
 struct ctables_area
   {
     struct hmap_node node;
-
     const struct ctables_cell *example;
 
+    /* Sequence number used for CTSF_ID. */
     size_t sequence;
+
+    /* Weights for CTSF_areaPCT_COUNT, CTSF_areaPCT_VALIDN, and
+       CTSF_areaPCT_TOTALN. */
     double count[N_CTWS];
     double valid[N_CTWS];
     double total[N_CTWS];
+
+    /* Sums for CTSF_areaPCT_SUM. */
     struct ctables_sum *sums;
   };
 
@@ -138,9 +147,9 @@ enum ctables_function_type
 
 enum ctables_format
   {
-    CTF_COUNT,
-    CTF_PERCENT,
-    CTF_GENERAL
+    CTF_COUNT,                  /* F40.0. */
+    CTF_PERCENT,                /* PCT40.1. */
+    CTF_GENERAL                 /* Variable's print format. */
   };
 
 enum ctables_function_availability
@@ -318,9 +327,7 @@ ctables_summary_function_label__ (enum ctables_summary_function function,
   switch (function)
     {
     case CTSF_COUNT:
-      return (d ? N_("Count")
-              : w ? N_("Adjusted Count")
-              : N_("Unweighted Count"));
+      return d ? N_("Count") : w ? N_("Adjusted Count") : N_("Unweighted Count");
 
     case CTSF_areaPCT_COUNT:
       switch (a)
@@ -461,8 +468,8 @@ struct ctables_summary_spec
     struct fmt_spec format;
     bool is_ctables_format;       /* Is 'format' one of CTEF_*? */
 
-    size_t axis_idx;
-    size_t sum_var_idx;
+    size_t axis_idx;            /* Leaf index if summary dimension in use. */
+    size_t sum_var_idx;         /* Offset into 'sums' in ctables_area. */
   };
 
 static void
@@ -619,7 +626,6 @@ struct ctables_pcexpr
     struct msg_location *location;
   };
 
-struct ctables;
 static struct ctables_postcompute *ctables_find_postcompute (struct ctables *,
                                                              const char *name);
 
@@ -3474,6 +3480,50 @@ ctables_value_find (struct ctables_table *t,
   return ctables_value_find__ (t, value, width,
                                value_hash (value, width, 0));
 }
+
+static int
+compare_ctables_values_3way (const void *a_, const void *b_, const void *width_)
+{
+  const struct ctables_value *const *ap = a_;
+  const struct ctables_value *const *bp = b_;
+  const struct ctables_value *a = *ap;
+  const struct ctables_value *b = *bp;
+  const int *width = width_;
+  return value_compare_3way (&a->value, &b->value, *width);
+}
+
+static void
+ctables_sort_clabels_values (struct ctables_table *t)
+{
+  const struct variable *v0 = t->clabels_example;
+  int width = var_get_width (v0);
+
+  struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
+  if (c0->show_empty)
+    {
+      const struct val_labs *val_labs = var_get_value_labels (v0);
+      for (const struct val_lab *vl = val_labs_first (val_labs); vl;
+           vl = val_labs_next (val_labs, vl))
+        if (ctables_categories_match (c0, &vl->value, v0))
+          ctables_value_insert (t, &vl->value, width);
+    }
+
+  size_t n = hmap_count (&t->clabels_values_map);
+  t->clabels_values = xnmalloc (n, sizeof *t->clabels_values);
+
+  struct ctables_value *clv;
+  size_t i = 0;
+  HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
+    t->clabels_values[i++] = clv;
+  t->n_clabels_values = n;
+  assert (i == n);
+
+  sort (t->clabels_values, n, sizeof *t->clabels_values,
+        compare_ctables_values_3way, &width);
+
+  for (size_t i = 0; i < n; i++)
+    t->clabels_values[i]->leaf = i;
+}
 \f
 struct ctables
   {
@@ -3504,139 +3554,451 @@ struct ctables
     struct ctables_table **tables;
     size_t n_tables;
   };
-
-/* 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)
+\f
+static double
+ctpo_add (double a, double b)
 {
-  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;
+  return a + b;
 }
 
-static bool
-parse_bool (struct lexer *lexer, bool *b)
+static double
+ctpo_sub (double a, double 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;
+  return a - b;
 }
 
-static void
-ctables_chisq_destroy (struct ctables_chisq *chisq)
+static double
+ctpo_mul (double a, double b)
 {
-  free (chisq);
+  return a * b;
 }
 
-static void
-ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
+static double
+ctpo_div (double a, double b)
 {
-  free (pairwise);
+  return b ? a / b : SYSMIS;
 }
 
-static void
-ctables_table_destroy (struct ctables_table *t)
+static double
+ctpo_pow (double a, double b)
 {
-  if (!t)
-    return;
+  int save_errno = errno;
+  errno = 0;
+  double result = pow (a, b);
+  if (errno)
+    result = SYSMIS;
+  errno = save_errno;
+  return result;
+}
 
-  for (size_t i = 0; i < t->n_sections; i++)
-    ctables_section_uninit (&t->sections[i]);
-  free (t->sections);
+static double
+ctpo_neg (double a, double b UNUSED)
+{
+  return -a;
+}
 
-  for (size_t i = 0; i < t->n_categories; i++)
-    ctables_categories_unref (t->categories[i]);
-  free (t->categories);
+struct ctables_pcexpr_evaluate_ctx
+  {
+    const struct ctables_cell *cell;
+    const struct ctables_section *section;
+    const struct ctables_categories *cats;
+    enum pivot_axis_type pc_a;
+    size_t pc_a_idx;
+    size_t summary_idx;
+    enum fmt_type parse_format;
+  };
 
-  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);
+static double ctables_pcexpr_evaluate (
+  const struct ctables_pcexpr_evaluate_ctx *, const struct ctables_pcexpr *);
 
-  struct ctables_value *ctv, *next_ctv;
-  HMAP_FOR_EACH_SAFE (ctv, next_ctv, struct ctables_value, node,
-                      &t->clabels_values_map)
+static double
+ctables_pcexpr_evaluate_nonterminal (
+  const struct ctables_pcexpr_evaluate_ctx *ctx,
+  const struct ctables_pcexpr *e, size_t n_args,
+  double evaluate (double, double))
+{
+  double args[2] = { 0, 0 };
+  for (size_t i = 0; i < n_args; i++)
     {
-      value_destroy (&ctv->value, var_get_width (t->clabels_example));
-      hmap_delete (&t->clabels_values_map, &ctv->node);
-      free (ctv);
+      args[i] = ctables_pcexpr_evaluate (ctx, e->subs[i]);
+      if (!isfinite (args[i]) || args[i] == SYSMIS)
+        return SYSMIS;
     }
-  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);
+  return evaluate (args[0], args[1]);
 }
 
-static void
-ctables_destroy (struct ctables *ct)
+static double
+ctables_pcexpr_evaluate_category (const struct ctables_pcexpr_evaluate_ctx *ctx,
+                                  const struct ctables_cell_value *pc_cv)
 {
-  if (!ct)
-    return;
+  const struct ctables_section *s = ctx->section;
 
-  struct ctables_postcompute *pc, *next_pc;
-  HMAP_FOR_EACH_SAFE (pc, next_pc, struct ctables_postcompute, hmap_node,
-                      &ct->postcomputes)
+  size_t hash = 0;
+  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
     {
-      free (pc->name);
-      msg_location_destroy (pc->location);
-      ctables_pcexpr_destroy (pc->expr);
-      free (pc->label);
-      if (pc->specs)
+      const struct ctables_nest *nest = s->nests[a];
+      for (size_t i = 0; i < nest->n; i++)
+        if (i != nest->scale_idx)
+          {
+            const struct ctables_cell_value *cv
+              = (a == ctx->pc_a && i == ctx->pc_a_idx ? pc_cv
+                 : &ctx->cell->axes[a].cvs[i]);
+            hash = hash_pointer (cv->category, hash);
+            if (cv->category->type != CCT_TOTAL
+                && cv->category->type != CCT_SUBTOTAL
+                && cv->category->type != CCT_POSTCOMPUTE)
+              hash = value_hash (&cv->value,
+                                 var_get_width (nest->vars[i]), hash);
+          }
+    }
+
+  struct ctables_cell *tc;
+  HMAP_FOR_EACH_WITH_HASH (tc, struct ctables_cell, node, hash, &s->cells)
+    {
+      for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
         {
-          ctables_summary_spec_set_uninit (pc->specs);
-          free (pc->specs);
+          const struct ctables_nest *nest = s->nests[a];
+          for (size_t i = 0; i < nest->n; i++)
+            if (i != nest->scale_idx)
+              {
+                const struct ctables_cell_value *p_cv
+                  = (a == ctx->pc_a && i == ctx->pc_a_idx ? pc_cv
+                     : &ctx->cell->axes[a].cvs[i]);
+                const struct ctables_cell_value *t_cv = &tc->axes[a].cvs[i];
+                if (p_cv->category != t_cv->category
+                    || (p_cv->category->type != CCT_TOTAL
+                        && p_cv->category->type != CCT_SUBTOTAL
+                        && p_cv->category->type != CCT_POSTCOMPUTE
+                        && !value_equal (&p_cv->value,
+                                         &t_cv->value,
+                                         var_get_width (nest->vars[i]))))
+                  goto not_equal;
+              }
         }
-      hmap_delete (&ct->postcomputes, &pc->hmap_node);
-      free (pc);
+
+      goto found;
+
+    not_equal: ;
     }
-  hmap_destroy (&ct->postcomputes);
+  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);
 
   fmt_settings_uninit (&ct->ctables_formats);
   pivot_table_look_unref (ct->look);
@@ -3670,7 +4032,7 @@ static bool
 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
                                 struct ctables *ct, struct ctables_table *t)
 {
-  if (!lex_match_id (lexer, "VARIABLES"))
+  if (!lex_force_match_id (lexer, "VARIABLES"))
     return false;
   lex_match (lexer, T_EQUALS);
 
@@ -3846,550 +4208,238 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
             c->show_empty = false;
           else
             {
-              lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
-              goto error;
-            }
-        }
-      else
-        {
-          if (!c->n_cats)
-            lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
-                                 "TOTAL", "LABEL", "POSITION", "EMPTY");
-          else
-            lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
-          goto error;
-        }
-    }
-
-  if (!c->n_cats)
-    {
-      if (c->n_cats >= allocated_cats)
-        c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
-      c->cats[c->n_cats++] = cat;
-    }
-
-  if (show_totals)
-    {
-      if (c->n_cats >= allocated_cats)
-        c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
-
-      struct ctables_category *totals;
-      if (totals_before)
-        {
-          insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
-          totals = &c->cats[0];
-        }
-      else
-        totals = &c->cats[c->n_cats];
-      c->n_cats++;
-
-      *totals = (struct ctables_category) {
-        .type = CCT_TOTAL,
-        .total_label = total_label ? total_label : xstrdup (_("Total")),
-      };
-    }
-
-  struct ctables_category *subtotal = NULL;
-  for (size_t i = totals_before ? 0 : c->n_cats;
-       totals_before ? i < c->n_cats : i-- > 0;
-       totals_before ? i++ : 0)
-    {
-      struct ctables_category *cat = &c->cats[i];
-      switch (cat->type)
-        {
-        case CCT_NUMBER:
-        case CCT_STRING:
-        case CCT_NRANGE:
-        case CCT_SRANGE:
-        case CCT_MISSING:
-        case CCT_OTHERNM:
-          cat->subtotal = subtotal;
-          break;
-
-        case CCT_POSTCOMPUTE:
-          break;
-
-        case CCT_SUBTOTAL:
-          subtotal = cat;
-          break;
-
-        case CCT_TOTAL:
-        case CCT_VALUE:
-        case CCT_LABEL:
-        case CCT_FUNCTION:
-        case CCT_EXCLUDED_MISSING:
-          break;
-        }
-    }
-
-  if (cats_start_ofs != -1)
-    {
-      for (size_t i = 0; i < c->n_cats; i++)
-        {
-          struct ctables_category *cat = &c->cats[i];
-          switch (cat->type)
-            {
-            case CCT_POSTCOMPUTE:
-              cat->parse_format = parse_strings ? common_format->type : FMT_F;
-              struct msg_location *cats_location
-                = lex_ofs_location (lexer, cats_start_ofs, cats_end_ofs);
-              bool ok = ctables_recursive_check_postcompute (
-                dict, cat->pc->expr, cat, c, cats_location);
-              msg_location_destroy (cats_location);
-              if (!ok)
-                goto error;
-              break;
-
-            case CCT_NUMBER:
-            case CCT_NRANGE:
-              for (size_t j = 0; j < n_vars; j++)
-                if (var_is_alpha (vars[j]))
-                  {
-                    msg_at (SE, cat->location,
-                            _("This category specification may be applied "
-                              "only to numeric variables, but this "
-                              "subcommand tries to apply it to string "
-                              "variable %s."),
-                            var_get_name (vars[j]));
-                    goto error;
-                  }
-              break;
-
-            case CCT_STRING:
-              if (parse_strings)
-                {
-                  double n;
-                  if (!parse_category_string (cat->location, cat->string, dict,
-                                              common_format->type, &n))
-                    goto error;
-
-                  ss_dealloc (&cat->string);
-
-                  cat->type = CCT_NUMBER;
-                  cat->number = n;
-                }
-              else if (!all_strings (vars, n_vars, cat))
-                goto error;
-              break;
-
-            case CCT_SRANGE:
-              if (parse_strings)
-                {
-                  double n[2];
-
-                  if (!cat->srange[0].string)
-                    n[0] = -DBL_MAX;
-                  else if (!parse_category_string (cat->location,
-                                                   cat->srange[0], dict,
-                                                   common_format->type, &n[0]))
-                    goto error;
-
-                  if (!cat->srange[1].string)
-                    n[1] = DBL_MAX;
-                  else if (!parse_category_string (cat->location,
-                                                   cat->srange[1], dict,
-                                                   common_format->type, &n[1]))
-                    goto error;
-
-                  ss_dealloc (&cat->srange[0]);
-                  ss_dealloc (&cat->srange[1]);
-
-                  cat->type = CCT_NRANGE;
-                  cat->nrange[0] = n[0];
-                  cat->nrange[1] = n[1];
-                }
-              else if (!all_strings (vars, n_vars, cat))
-                goto error;
-              break;
-
-            case CCT_MISSING:
-            case CCT_OTHERNM:
-            case CCT_SUBTOTAL:
-            case CCT_TOTAL:
-            case CCT_VALUE:
-            case CCT_LABEL:
-            case CCT_FUNCTION:
-            case CCT_EXCLUDED_MISSING:
-              break;
-            }
-        }
-    }
-
-  free (vars);
-  return true;
-
-error:
-  free (vars);
-  return false;
-}
-\f
-
-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->weighting != bs->weighting)
-    return as->weighting > bs->weighting ? 1 : -1;
-  else if (as->calc_area != bs->calc_area)
-    return as->calc_area > bs->calc_area ? 1 : -1;
-  else if (as->percentile != bs->percentile)
-    return as->percentile < bs->percentile ? 1 : -1;
-
-  const char *as_label = as->label ? as->label : "";
-  const char *bs_label = bs->label ? bs->label : "";
-  return strcmp (as_label, bs_label);
-}
-
-static void
-ctables_table_add_section (struct ctables_table *t, enum pivot_axis_type a,
-                           size_t ix[PIVOT_N_AXES])
-{
-  if (a < PIVOT_N_AXES)
-    {
-      size_t limit = MAX (t->stacks[a].n, 1);
-      for (ix[a] = 0; ix[a] < limit; ix[a]++)
-        ctables_table_add_section (t, a + 1, ix);
-    }
-  else
-    {
-      struct ctables_section *s = &t->sections[t->n_sections++];
-      *s = (struct ctables_section) {
-        .table = t,
-        .cells = HMAP_INITIALIZER (s->cells),
-      };
-      for (a = 0; a < PIVOT_N_AXES; a++)
-        if (t->stacks[a].n)
-          {
-            struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
-            s->nests[a] = nest;
-            s->occurrences[a] = xnmalloc (nest->n, sizeof *s->occurrences[a]);
-            for (size_t i = 0; i < nest->n; i++)
-              hmap_init (&s->occurrences[a][i]);
+              lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
+              goto error;
+            }
+        }
+      else
+        {
+          if (!c->n_cats)
+            lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
+                                 "TOTAL", "LABEL", "POSITION", "EMPTY");
+          else
+            lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
+          goto error;
         }
-      for (enum ctables_area_type at = 0; at < N_CTATS; at++)
-        hmap_init (&s->areas[at]);
     }
-}
-
-static double
-ctpo_add (double a, double b)
-{
-  return a + b;
-}
-
-static double
-ctpo_sub (double a, double b)
-{
-  return a - b;
-}
-
-static double
-ctpo_mul (double a, double b)
-{
-  return a * b;
-}
-
-static double
-ctpo_div (double a, double b)
-{
-  return b ? a / b : SYSMIS;
-}
-
-static double
-ctpo_pow (double a, double b)
-{
-  int save_errno = errno;
-  errno = 0;
-  double result = pow (a, b);
-  if (errno)
-    result = SYSMIS;
-  errno = save_errno;
-  return result;
-}
-
-static double
-ctpo_neg (double a, double b UNUSED)
-{
-  return -a;
-}
-
-struct ctables_pcexpr_evaluate_ctx
-  {
-    const struct ctables_cell *cell;
-    const struct ctables_section *section;
-    const struct ctables_categories *cats;
-    enum pivot_axis_type pc_a;
-    size_t pc_a_idx;
-    size_t summary_idx;
-    enum fmt_type parse_format;
-  };
 
-static double ctables_pcexpr_evaluate (
-  const struct ctables_pcexpr_evaluate_ctx *, const struct ctables_pcexpr *);
-
-static double
-ctables_pcexpr_evaluate_nonterminal (
-  const struct ctables_pcexpr_evaluate_ctx *ctx,
-  const struct ctables_pcexpr *e, size_t n_args,
-  double evaluate (double, double))
-{
-  double args[2] = { 0, 0 };
-  for (size_t i = 0; i < n_args; i++)
+  if (!c->n_cats)
     {
-      args[i] = ctables_pcexpr_evaluate (ctx, e->subs[i]);
-      if (!isfinite (args[i]) || args[i] == SYSMIS)
-        return SYSMIS;
+      if (c->n_cats >= allocated_cats)
+        c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
+      c->cats[c->n_cats++] = cat;
     }
-  return evaluate (args[0], args[1]);
-}
 
-static double
-ctables_pcexpr_evaluate_category (const struct ctables_pcexpr_evaluate_ctx *ctx,
-                                  const struct ctables_cell_value *pc_cv)
-{
-  const struct ctables_section *s = ctx->section;
-
-  size_t hash = 0;
-  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+  if (show_totals)
     {
-      const struct ctables_nest *nest = s->nests[a];
-      for (size_t i = 0; i < nest->n; i++)
-        if (i != nest->scale_idx)
-          {
-            const struct ctables_cell_value *cv
-              = (a == ctx->pc_a && i == ctx->pc_a_idx ? pc_cv
-                 : &ctx->cell->axes[a].cvs[i]);
-            hash = hash_pointer (cv->category, hash);
-            if (cv->category->type != CCT_TOTAL
-                && cv->category->type != CCT_SUBTOTAL
-                && cv->category->type != CCT_POSTCOMPUTE)
-              hash = value_hash (&cv->value,
-                                 var_get_width (nest->vars[i]), hash);
-          }
-    }
+      if (c->n_cats >= allocated_cats)
+        c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
 
-  struct ctables_cell *tc;
-  HMAP_FOR_EACH_WITH_HASH (tc, struct ctables_cell, node, hash, &s->cells)
-    {
-      for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+      struct ctables_category *totals;
+      if (totals_before)
         {
-          const struct ctables_nest *nest = s->nests[a];
-          for (size_t i = 0; i < nest->n; i++)
-            if (i != nest->scale_idx)
-              {
-                const struct ctables_cell_value *p_cv
-                  = (a == ctx->pc_a && i == ctx->pc_a_idx ? pc_cv
-                     : &ctx->cell->axes[a].cvs[i]);
-                const struct ctables_cell_value *t_cv = &tc->axes[a].cvs[i];
-                if (p_cv->category != t_cv->category
-                    || (p_cv->category->type != CCT_TOTAL
-                        && p_cv->category->type != CCT_SUBTOTAL
-                        && p_cv->category->type != CCT_POSTCOMPUTE
-                        && !value_equal (&p_cv->value,
-                                         &t_cv->value,
-                                         var_get_width (nest->vars[i]))))
-                  goto not_equal;
-              }
+          insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
+          totals = &c->cats[0];
         }
+      else
+        totals = &c->cats[c->n_cats];
+      c->n_cats++;
 
-      goto found;
-
-    not_equal: ;
+      *totals = (struct ctables_category) {
+        .type = CCT_TOTAL,
+        .total_label = total_label ? total_label : xstrdup (_("Total")),
+      };
     }
-  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)
+  struct ctables_category *subtotal = NULL;
+  for (size_t i = totals_before ? 0 : c->n_cats;
+       totals_before ? i < c->n_cats : i-- > 0;
+       totals_before ? i++ : 0)
     {
-    case CTPO_CONSTANT:
-      return e->number;
+      struct ctables_category *cat = &c->cats[i];
+      switch (cat->type)
+        {
+        case CCT_NUMBER:
+        case CCT_STRING:
+        case CCT_NRANGE:
+        case CCT_SRANGE:
+        case CCT_MISSING:
+        case CCT_OTHERNM:
+          cat->subtotal = subtotal;
+          break;
 
-    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);
+        case CCT_POSTCOMPUTE:
+          break;
 
-        struct hmap *occurrences = &ctx->section->occurrences[ctx->pc_a][ctx->pc_a_idx];
-        const struct ctables_occurrence *o;
+        case CCT_SUBTOTAL:
+          subtotal = cat;
+          break;
 
-        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 CCT_TOTAL:
+        case CCT_VALUE:
+        case CCT_LABEL:
+        case CCT_FUNCTION:
+        case CCT_EXCLUDED_MISSING:
+          break;
+        }
+    }
 
-    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);
-      }
+  if (cats_start_ofs != -1)
+    {
+      for (size_t i = 0; i < c->n_cats; i++)
+        {
+          struct ctables_category *cat = &c->cats[i];
+          switch (cat->type)
+            {
+            case CCT_POSTCOMPUTE:
+              cat->parse_format = parse_strings ? common_format->type : FMT_F;
+              struct msg_location *cats_location
+                = lex_ofs_location (lexer, cats_start_ofs, cats_end_ofs);
+              bool ok = ctables_recursive_check_postcompute (
+                dict, cat->pc->expr, cat, c, cats_location);
+              msg_location_destroy (cats_location);
+              if (!ok)
+                goto error;
+              break;
 
-    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, ' ');
-          }
+            case CCT_NUMBER:
+            case CCT_NRANGE:
+              for (size_t j = 0; j < n_vars; j++)
+                if (var_is_alpha (vars[j]))
+                  {
+                    msg_at (SE, cat->location,
+                            _("This category specification may be applied "
+                              "only to numeric variables, but this "
+                              "subcommand tries to apply it to string "
+                              "variable %s."),
+                            var_get_name (vars[j]));
+                    goto error;
+                  }
+              break;
 
-        const struct ctables_category *category
-          = ctables_find_category_for_postcompute (
-            ctx->section->table->ctables->dict,
-            ctx->cats, ctx->parse_format, e);
-        assert (category != NULL);
+            case CCT_STRING:
+              if (parse_strings)
+                {
+                  double n;
+                  if (!parse_category_string (cat->location, cat->string, dict,
+                                              common_format->type, &n))
+                    goto error;
 
-        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 ();
+                  ss_dealloc (&cat->string);
 
-        double retval = ctables_pcexpr_evaluate_category (ctx, &cv);
-        free (s);
-        return retval;
-      }
+                  cat->type = CCT_NUMBER;
+                  cat->number = n;
+                }
+              else if (!all_strings (vars, n_vars, cat))
+                goto error;
+              break;
 
-    case CTPO_ADD:
-      return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_add);
+            case CCT_SRANGE:
+              if (parse_strings)
+                {
+                  double n[2];
 
-    case CTPO_SUB:
-      return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_sub);
+                  if (!cat->srange[0].string)
+                    n[0] = -DBL_MAX;
+                  else if (!parse_category_string (cat->location,
+                                                   cat->srange[0], dict,
+                                                   common_format->type, &n[0]))
+                    goto error;
 
-    case CTPO_MUL:
-      return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_mul);
+                  if (!cat->srange[1].string)
+                    n[1] = DBL_MAX;
+                  else if (!parse_category_string (cat->location,
+                                                   cat->srange[1], dict,
+                                                   common_format->type, &n[1]))
+                    goto error;
 
-    case CTPO_DIV:
-      return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_div);
+                  ss_dealloc (&cat->srange[0]);
+                  ss_dealloc (&cat->srange[1]);
 
-    case CTPO_POW:
-      return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_pow);
+                  cat->type = CCT_NRANGE;
+                  cat->nrange[0] = n[0];
+                  cat->nrange[1] = n[1];
+                }
+              else if (!all_strings (vars, n_vars, cat))
+                goto error;
+              break;
 
-    case CTPO_NEG:
-      return ctables_pcexpr_evaluate_nonterminal (ctx, e, 1, ctpo_neg);
+            case CCT_MISSING:
+            case CCT_OTHERNM:
+            case CCT_SUBTOTAL:
+            case CCT_TOTAL:
+            case CCT_VALUE:
+            case CCT_LABEL:
+            case CCT_FUNCTION:
+            case CCT_EXCLUDED_MISSING:
+              break;
+            }
+        }
     }
 
-  NOT_REACHED ();
+  free (vars);
+  return true;
+
+error:
+  free (vars);
+  return false;
 }
+\f
 
-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;
-              }
+struct merge_item
+  {
+    const struct ctables_summary_spec_set *set;
+    size_t ofs;
+  };
 
-            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;
-          }
-      }
+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->weighting != bs->weighting)
+    return as->weighting > bs->weighting ? 1 : -1;
+  else if (as->calc_area != bs->calc_area)
+    return as->calc_area > bs->calc_area ? 1 : -1;
+  else if (as->percentile != bs->percentile)
+    return as->percentile < bs->percentile ? 1 : -1;
 
-  assert (pc_cat != NULL);
-  return pc_cat;
+  const char *as_label = as->label ? as->label : "";
+  const char *bs_label = bs->label ? bs->label : "";
+  return strcmp (as_label, bs_label);
 }
 
-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)
+static void
+ctables_table_add_section (struct ctables_table *t, enum pivot_axis_type a,
+                           size_t ix[PIVOT_N_AXES])
 {
-  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)
+  if (a < PIVOT_N_AXES)
     {
-      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;
-            }
+      size_t limit = MAX (t->stacks[a].n, 1);
+      for (ix[a] = 0; ix[a] < limit; ix[a]++)
+        ctables_table_add_section (t, a + 1, ix);
+    }
+  else
+    {
+      struct ctables_section *s = &t->sections[t->n_sections++];
+      *s = (struct ctables_section) {
+        .table = t,
+        .cells = HMAP_INITIALIZER (s->cells),
+      };
+      for (a = 0; a < PIVOT_N_AXES; a++)
+        if (t->stacks[a].n)
+          {
+            struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
+            s->nests[a] = nest;
+            s->occurrences[a] = xnmalloc (nest->n, sizeof *s->occurrences[a]);
+            for (size_t i = 0; i < nest->n; i++)
+              hmap_init (&s->occurrences[a][i]);
         }
+      for (enum ctables_area_type at = 0; at < N_CTATS; at++)
+        hmap_init (&s->areas[at]);
     }
-
-  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);
 }
 
 static char *
@@ -5194,8 +5244,8 @@ ctables_prepare_table (struct ctables_table *t)
           if (merge_item_compare_3way (&items[j], &min) == 0)
             {
               struct merge_item *item = &items[j];
-              item->set->specs[item->ofs].axis_idx = merged->n - 1;
-              if (++item->ofs >= item->set->n)
+              item->set->specs[item->ofs++].axis_idx = merged->n - 1;
+              if (item->ofs >= item->set->n)
                 {
                   items[j] = items[--n_left];
                   continue;
@@ -5234,50 +5284,6 @@ ctables_insert_clabels_values (struct ctables_table *t, const struct ccase *c,
     }
 }
 
-static int
-compare_ctables_values_3way (const void *a_, const void *b_, const void *width_)
-{
-  const struct ctables_value *const *ap = a_;
-  const struct ctables_value *const *bp = b_;
-  const struct ctables_value *a = *ap;
-  const struct ctables_value *b = *bp;
-  const int *width = width_;
-  return value_compare_3way (&a->value, &b->value, *width);
-}
-
-static void
-ctables_sort_clabels_values (struct ctables_table *t)
-{
-  const struct variable *v0 = t->clabels_example;
-  int width = var_get_width (v0);
-
-  struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
-  if (c0->show_empty)
-    {
-      const struct val_labs *val_labs = var_get_value_labels (v0);
-      for (const struct val_lab *vl = val_labs_first (val_labs); vl;
-           vl = val_labs_next (val_labs, vl))
-        if (ctables_categories_match (c0, &vl->value, v0))
-          ctables_value_insert (t, &vl->value, width);
-    }
-
-  size_t n = hmap_count (&t->clabels_values_map);
-  t->clabels_values = xnmalloc (n, sizeof *t->clabels_values);
-
-  struct ctables_value *clv;
-  size_t i = 0;
-  HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
-    t->clabels_values[i++] = clv;
-  t->n_clabels_values = n;
-  assert (i == n);
-
-  sort (t->clabels_values, n, sizeof *t->clabels_values,
-        compare_ctables_values_3way, &width);
-
-  for (size_t i = 0; i < n; i++)
-    t->clabels_values[i]->leaf = i;
-}
-
 static void
 ctables_add_category_occurrences (const struct variable *var,
                                   struct hmap *occurrences,