+ /* Create array of pointers to items. */
+ struct arc_item **items = xmalloc (n_items * sizeof *items);
+ struct arc_item *item;
+ size_t j = 0;
+ HMAP_FOR_EACH (item, struct arc_item, hmap_node, &spec->items->ht)
+ items[j++] = item;
+ assert (j == n_items);
+
+ /* Sort array by value. */
+ sort (items, n_items, sizeof *items, compare_arc_items, &direction);
+
+ /* Assign recoded values in sorted order. */
+ for (j = 0; j < n_items; j++)
+ items[j]->to = j + 1;
+
+ if (print && (!group || i == 0))
+ {
+ struct pivot_value *title
+ = (group
+ ? pivot_value_new_text (N_("Recoding grouped variables."))
+ : spec->label && spec->label[0]
+ ? pivot_value_new_text_format (N_("Recoding %s into %s (%s)."),
+ spec->src_name,
+ var_get_name (spec->dst),
+ spec->label)
+ : pivot_value_new_text_format (N_("Recoding %s into %s."),
+ spec->src_name,
+ var_get_name (spec->dst)));
+ struct pivot_table *table = pivot_table_create__ (title, "Recoding");
+
+ pivot_dimension_create (
+ table, PIVOT_AXIS_COLUMN, N_("Attributes"),
+ N_("New Value"), N_("Value Label"));
+
+ struct pivot_dimension *old_values = pivot_dimension_create (
+ table, PIVOT_AXIS_ROW, N_("Old Value"));
+ old_values->root->show_label = true;
+
+ for (size_t k = 0; k < n_items; k++)
+ {
+ const struct arc_item *item = items[k];
+ int old_value_idx = pivot_category_create_leaf (
+ old_values->root, pivot_value_new_value (
+ &item->from, item->width,
+ (item->width
+ ? &(struct fmt_spec) { .type = FMT_F, .w = item->width }
+ : &spec->format),
+ dict_get_encoding (dict)));
+ pivot_table_put2 (table, 0, old_value_idx,
+ pivot_value_new_integer (item->to));
+
+ const char *value_label = item->value_label;
+ if (value_label && value_label[0])
+ pivot_table_put2 (table, 1, old_value_idx,
+ pivot_value_new_user_text (value_label, -1));
+ }
+
+ pivot_table_submit (table);
+ }
+
+ /* Assign user-missing values.
+
+ User-missing values in the source variable(s) must be marked
+ as user-missing values in the destination variable. There
+ might be an arbitrary number of missing values, since the
+ source variable might have a range. Our sort function always
+ puts missing values together at the top of the range, so that
+ means that we can use a missing value range to cover all of
+ the user-missing values in any case (but we avoid it unless
+ necessary because user-missing value ranges are an obscure
+ feature). */
+ size_t n_missing = n_items;
+ for (size_t k = 0; k < n_items; k++)
+ if (!items[n_items - k - 1]->missing)
+ {
+ n_missing = k;
+ break;
+ }
+ if (n_missing > 0)
+ {
+ size_t lo = n_items - (n_missing - 1);
+ size_t hi = n_items;
+
+ struct missing_values mv;
+ mv_init (&mv, 0);
+ if (n_missing > 3)
+ mv_add_range (&mv, lo, hi);