const struct variable *clabels_example;
struct hmap clabels_values_map;
union value *clabels_values;
+ size_t n_clabels_values;
enum pivot_axis_type slabels_axis;
bool slabels_visible;
opposite axis or PIVOT_AXIS_LAYER. Only one of them will differ.
*/
enum pivot_axis_type label_axis[PIVOT_N_AXES];
+ enum pivot_axis_type clabels_from_axis;
/* Indexed by variable dictionary index. */
struct ctables_categories **categories;
}
}
-static double
+static double UNUSED
ctables_summary_value (const struct ctables_cell *cell,
union ctables_summary *s,
const struct ctables_summary_spec *ss)
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 void
-ctables_table_output_same_axis (struct ctables *ct, struct ctables_table *t)
+ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t)
{
struct pivot_table *pt = pivot_table_create__ (
(t->title
pivot_table_set_caption (
pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
- pivot_table_set_look (pt, ct->look);
- struct pivot_dimension *d[PIVOT_N_AXES];
- for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ if (t->summary_axis != t->slabels_axis)
{
- static const char *names[] = {
- [PIVOT_AXIS_ROW] = N_("Rows"),
- [PIVOT_AXIS_COLUMN] = N_("Columns"),
- [PIVOT_AXIS_LAYER] = N_("Layers"),
- };
- d[a] = (t->axes[a] || a == t->summary_axis
- ? pivot_dimension_create (pt, a, names[a])
- : NULL);
- if (!d[a])
- continue;
-
- assert (t->axes[a]);
-
- struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
-
- struct ctables_cell *cell;
- size_t n = 0;
- HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
- if (!cell->hide)
- sorted[n++] = cell;
- assert (n <= t->cells.count);
-
- struct ctables_cell_sort_aux aux = { .t = t, .a = a };
- sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
-
- size_t max_depth = 0;
- for (size_t j = 0; j < t->stacks[a].n; j++)
- if (t->stacks[a].nests[j].n > max_depth)
- max_depth = t->stacks[a].nests[j].n;
+ struct pivot_dimension *d = pivot_dimension_create (
+ pt, t->slabels_axis, N_("Summaries"));
+ const struct ctables_summary_spec_set *specs = &t->summary_specs;
+ for (size_t i = 0; i < specs->n; i++)
+ pivot_category_create_leaf (
+ d->root, pivot_value_new_text (specs->specs[i].label));
+ }
- struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
- struct pivot_category *top = NULL;
- int prev_leaf = 0;
- for (size_t j = 0; j < n; j++)
+ if (t->clabels_example)
+ {
+ struct pivot_dimension *d = pivot_dimension_create (
+ pt, t->label_axis[t->clabels_from_axis],
+ t->clabels_from_axis == PIVOT_AXIS_ROW
+ ? N_("Row Categories")
+ : N_("Column Categories"));
+ const struct variable *var = t->clabels_example;
+ const struct ctables_categories *c = t->categories[var_get_dict_index (var)];
+ for (size_t i = 0; i < t->n_clabels_values; i++)
{
- struct ctables_cell *cell = sorted[j];
- const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
-
- size_t n_common = 0;
- bool new_subtable = false;
- if (j > 0)
+ const union value *value = &t->clabels_values[i];
+ const struct ctables_category *cat = ctables_categories_match (c, value, var);
+ if (!cat)
{
- struct ctables_cell *prev = sorted[j - 1];
- if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
- {
- for (; n_common < nest->n; n_common++)
- if (n_common != nest->scale_idx
- && (prev->axes[a].cvs[n_common].category
- != cell->axes[a].cvs[n_common].category
- || !value_equal (&prev->axes[a].cvs[n_common].value,
- &cell->axes[a].cvs[n_common].value,
- var_get_type (nest->vars[n_common]))))
- break;
- }
- else
- new_subtable = true;
- }
- else
- new_subtable = true;
-
- if (new_subtable)
- {
- enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
- top = d[a]->root;
- if (vlabel != CTVL_NONE)
- top = pivot_category_create_group__ (
- top, pivot_value_new_variable (nest->vars[0]));
- }
- if (n_common == nest->n)
- {
- cell->axes[a].leaf = prev_leaf;
+ /* XXX probably missing */
continue;
}
-
- for (size_t k = n_common; k < nest->n; k++)
- {
- struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
-
- struct pivot_value *label
- = (k == nest->scale_idx ? NULL
- : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
- || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
- || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
- ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
- SIZE_MAX)
- : pivot_value_new_var_value (nest->vars[k],
- &cell->axes[a].cvs[k].value));
- if (k == nest->n - 1)
- {
- if (a == t->summary_axis)
- {
- if (label)
- parent = pivot_category_create_group__ (parent, label);
- const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
- for (size_t m = 0; m < specs->n; m++)
- {
- int leaf = pivot_category_create_leaf (
- parent, pivot_value_new_text (specs->specs[m].label));
- if (m == 0)
- prev_leaf = leaf;
- }
- }
- else
- {
- /* This assertion is true as long as the summary axis
- is the axis where the summaries are displayed. */
- assert (label);
-
- prev_leaf = pivot_category_create_leaf (parent, label);
- }
- break;
- }
-
- if (label)
- parent = pivot_category_create_group__ (parent, label);
-
- enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
- if (vlabel != CTVL_NONE)
- parent = pivot_category_create_group__ (
- parent, pivot_value_new_variable (nest->vars[k + 1]));
- groups[k] = parent;
- }
-
- cell->axes[a].leaf = prev_leaf;
- }
- free (sorted);
- free (groups);
- }
- struct ctables_cell *cell;
- HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
- {
- if (cell->hide)
- continue;
-
- const struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[cell->axes[t->summary_axis].stack_idx];
- const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
- for (size_t j = 0; j < specs->n; j++)
- {
- size_t dindexes[3];
- size_t n_dindexes = 0;
-
- for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
- if (d[a])
- {
- int leaf = cell->axes[a].leaf;
- if (a == t->summary_axis)
- leaf += j;
- dindexes[n_dindexes++] = leaf;
- }
-
- double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
- struct pivot_value *value = pivot_value_new_number (d);
- value->numeric.format = specs->specs[j].format;
- pivot_table_put (pt, dindexes, n_dindexes, value);
+ pivot_category_create_leaf (d->root, ctables_category_create_label (
+ cat, t->clabels_example, value));
}
}
- pivot_table_submit (pt);
-}
-
-static void
-ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t)
-{
- struct pivot_table *pt = pivot_table_create__ (
- (t->title
- ? pivot_value_new_user_text (t->title, SIZE_MAX)
- : pivot_value_new_text (N_("Custom Tables"))),
- "Custom Tables");
- if (t->caption)
- pivot_table_set_caption (
- pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
- if (t->corner)
- pivot_table_set_caption (
- pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
-
pivot_table_set_look (pt, ct->look);
struct pivot_dimension *d[PIVOT_N_AXES];
for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
assert (t->axes[a]);
struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
+ size_t n_sorted = 0;
struct ctables_cell *cell;
- size_t n = 0;
HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
if (!cell->hide)
- sorted[n++] = cell;
- assert (n <= t->cells.count);
+ sorted[n_sorted++] = cell;
+ assert (n_sorted <= t->cells.count);
struct ctables_cell_sort_aux aux = { .t = t, .a = a };
- sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
+ sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
size_t max_depth = 0;
for (size_t j = 0; j < t->stacks[a].n; j++)
if (t->stacks[a].nests[j].n > max_depth)
max_depth = t->stacks[a].nests[j].n;
- struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
- struct pivot_category *top = NULL;
- int prev_leaf = 0;
- for (size_t j = 0; j < n; j++)
- {
- struct ctables_cell *cell = sorted[j];
- const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
+ /* Pivot categories:
- /* Pivot categories:
+ - variable label for nest->vars[0], if vlabel != CTVL_NONE
+ - category for nest->vars[0], if nest->scale_idx != 0
+ - variable label for nest->vars[1], if vlabel != CTVL_NONE
+ - category for nest->vars[1], if nest->scale_idx != 1
+ ...
+ - variable label for nest->vars[n - 1], if vlabel != CTVL_NONE
+ - category for nest->vars[n - 1], if t->label_axis[a] == a && nest->scale_idx != n - 1.
+ - summary function, if 'a == t->slabels_axis && a ==
+ t->summary_axis'.
- - variable label for nest->vars[0], if vlabel != CTVL_NONE
- - category for nest->vars[0]
- - variable label for nest->vars[1], if vlabel != CTVL_NONE
- - category for nest->vars[1]
- ...
- - variable label for nest->vars[nest->n - 1], if vlabel != CTVL_NONE
- - category for nest->vars[nest->n - 1], unless axis A's
- labels are moved to another axis
- - summary function, if 'a == t->summary_axis && t->slabels_axis ==
- t->summary_axis'
+ Additional dimensions:
- Additional dimensions:
+ - If 'a == t->slabels_axis && a != t->summary_axis', add a summary
+ dimension.
+ - If 't->label_axis[b] == a' for some 'b != a', add a category
+ dimension to 'a'.
+ */
- - If 't->slabels_axis != t->summary_axis', add a summary dimension
- to t->slabels_axis.
- - If 't->row_labels ==
+ struct ctables_level
+ {
+ enum ctables_level_type
+ {
+ CTL_VAR, /* Variable label for nest->vars[var_idx]. */
+ CTL_CATEGORY, /* Category for nest->vars[var_idx]. */
+ CTL_SUMMARY, /* Summary functions. */
+ }
+ type;
- */
+ size_t var_idx;
+ };
+ struct ctables_level *levels = xnmalloc (1 + 2 * max_depth, sizeof *levels);
+ size_t n_levels = 0;
- size_t n_common = 0;
- bool new_subtable = false;
- if (j > 0)
+ struct pivot_category **groups = xnmalloc (1 + 2 * max_depth, sizeof *groups);
+ int prev_leaf = 0;
+ for (size_t j = 0; j < n_sorted; j++)
+ {
+ struct ctables_cell *cell = sorted[j];
+ struct ctables_cell *prev = j > 0 ? sorted[j - 1] : NULL;
+ const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
+
+ bool new_subtable = !prev || prev->axes[a].stack_idx != cell->axes[a].stack_idx;
+ if (new_subtable)
{
- struct ctables_cell *prev = sorted[j - 1];
- if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
+ n_levels = 0;
+ printf ("%s levels:", pivot_axis_type_to_string (a));
+ for (size_t k = 0; k < nest->n; k++)
{
- for (; n_common < nest->n; n_common++)
- if (n_common != nest->scale_idx
- && (prev->axes[a].cvs[n_common].category
- != cell->axes[a].cvs[n_common].category
- || !value_equal (&prev->axes[a].cvs[n_common].value,
- &cell->axes[a].cvs[n_common].value,
- var_get_type (nest->vars[n_common]))))
- break;
+ enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k])];
+ if (vlabel != CTVL_NONE)
+ {
+ printf (" var(%s)", var_get_name (nest->vars[k]));
+ levels[n_levels++] = (struct ctables_level) {
+ .type = CTL_VAR,
+ .var_idx = k,
+ };
+ }
+
+ if (nest->scale_idx != k
+ && (k != nest->n - 1 || t->label_axis[a] == a))
+ {
+ printf (" category(%s)", var_get_name (nest->vars[k]));
+ levels[n_levels++] = (struct ctables_level) {
+ .type = CTL_CATEGORY,
+ .var_idx = k,
+ };
+ }
}
- else
- new_subtable = true;
- }
- else
- new_subtable = true;
- if (new_subtable)
- {
- enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
- top = d[a]->root;
- if (vlabel != CTVL_NONE)
- top = pivot_category_create_group__ (
- top, pivot_value_new_variable (nest->vars[0]));
- }
- if (n_common == nest->n)
- {
- cell->axes[a].leaf = prev_leaf;
- continue;
+ if (a == t->slabels_axis && a == t->summary_axis)
+ {
+ printf (" summary");
+ levels[n_levels++] = (struct ctables_level) {
+ .type = CTL_SUMMARY,
+ .var_idx = SIZE_MAX,
+ };
+ }
+ printf ("\n");
}
- for (size_t k = n_common; k < nest->n; k++)
+ size_t n_common = 0;
+ if (!new_subtable)
{
- struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
-
- struct pivot_value *label
- = (k == nest->scale_idx ? NULL
- : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
- || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
- || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
- ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
- SIZE_MAX)
- : pivot_value_new_var_value (nest->vars[k],
- &cell->axes[a].cvs[k].value));
- if (k == nest->n - 1)
+ for (; n_common < n_levels; n_common++)
{
- if (a == t->slabels_axis)
+ const struct ctables_level *level = &levels[n_common];
+ if (level->type == CTL_CATEGORY)
{
- if (label)
- parent = pivot_category_create_group__ (parent, label);
- const struct ctables_summary_spec_set *specs = &t->summary_specs;
- for (size_t m = 0; m < specs->n; m++)
+ size_t var_idx = level->var_idx;
+ if (prev->axes[a].cvs[var_idx].category
+ != cell->axes[a].cvs[var_idx].category)
{
- int leaf = pivot_category_create_leaf (
- parent, pivot_value_new_text (specs->specs[m].label));
- if (m == 0)
- prev_leaf = leaf;
+ break;
+ }
+ else if (!value_equal (&prev->axes[a].cvs[var_idx].value,
+ &cell->axes[a].cvs[var_idx].value,
+ var_get_type (nest->vars[var_idx])))
+ {
+ break;
}
}
- else
- {
- prev_leaf = pivot_category_create_leaf (parent, label ? label : pivot_value_new_user_text ("text", SIZE_MAX));
- }
- break;
}
+ }
- if (label)
- parent = pivot_category_create_group__ (parent, label);
+ for (size_t k = n_common; k < n_levels; k++)
+ {
+ const struct ctables_level *level = &levels[k];
+ struct pivot_category *parent = k ? groups[k - 1] : d[a]->root;
+ if (level->type == CTL_SUMMARY)
+ {
+ const struct ctables_summary_spec_set *specs = &t->summary_specs;
+ for (size_t m = 0; m < specs->n; m++)
+ pivot_category_create_leaf (
+ parent, pivot_value_new_text (specs->specs[m].label));
+ }
+ else
+ {
+ const struct variable *var = nest->vars[level->var_idx];
+ struct pivot_value *label;
+ if (level->type == CTL_VAR)
+ label = pivot_value_new_variable (var);
+ else if (level->type == CTL_CATEGORY)
+ {
+ const struct ctables_cell_value *cv = &cell->axes[a].cvs[level->var_idx];
+ label = ctables_category_create_label (cv->category,
+ var, &cv->value);
+ }
+ else
+ NOT_REACHED ();
- enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
- if (vlabel != CTVL_NONE)
- parent = pivot_category_create_group__ (
- parent, pivot_value_new_variable (nest->vars[k + 1]));
- groups[k] = parent;
+ if (k == n_levels - 1)
+ pivot_category_create_leaf (parent, label);
+ else
+ groups[k] = pivot_category_create_group__ (parent, label);
+ }
}
cell->axes[a].leaf = prev_leaf;
size_t i = 0;
HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
t->clabels_values[i++] = clv->value;
+ t->n_clabels_values = n;
assert (i == n);
sort (t->clabels_values, n, sizeof *t->clabels_values,
if (t->clabels_example)
ctables_sort_clabels_values (t);
- if (t->summary_axis == t->slabels_axis)
- ctables_table_output_same_axis (ct, ct->tables[i]);
- else
- ctables_table_output_different_axis (ct, ct->tables[i]);
+ ctables_table_output_different_axis (ct, ct->tables[i]);
}
return proc_commit (ds);
}
if (label_pos == a)
return true;
+ t->clabels_from_axis = a;
+
const char *subcommand_name = a == PIVOT_AXIS_ROW ? "ROWLABELS" : "COLLABELS";
const char *pos_name = label_pos == PIVOT_AXIS_LAYER ? "LAYER" : "OPPOSITE";
subcommand_name, pos_name);
return false;
}
+ if (n0->n - 1 == n0->scale_idx)
+ {
+ msg (SE, _("%s=%s requires the variables to be moved to be categorical, "
+ "but %s is a scale variable."),
+ subcommand_name, pos_name, var_get_name (v0));
+ return false;
+ }
for (size_t i = 1; i < stack->n; i++)
{
const struct variable *vi = ni->vars[ni->n - 1];
struct ctables_categories *ci = t->categories[var_get_dict_index (vi)];
+ if (ni->n - 1 == ni->scale_idx)
+ {
+ msg (SE, _("%s=%s requires the variables to be moved to be "
+ "categorical, but %s is a scale variable."),
+ subcommand_name, pos_name, var_get_name (vi));
+ return false;
+ }
if (var_get_width (v0) != var_get_width (vi))
{
msg (SE, _("%s=%s requires the variables to be "
[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN,
[PIVOT_AXIS_LAYER] = PIVOT_AXIS_LAYER,
},
+ .clabels_from_axis = PIVOT_AXIS_LAYER,
.categories = categories,
.n_categories = n_vars,
.cilevel = 95,