1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/casereader.h"
22 #include "data/casewriter.h"
23 #include "data/dataset.h"
24 #include "data/dictionary.h"
25 #include "data/mrset.h"
26 #include "data/subcase.h"
27 #include "data/value-labels.h"
28 #include "language/command.h"
29 #include "language/lexer/format-parser.h"
30 #include "language/lexer/lexer.h"
31 #include "language/lexer/variable-parser.h"
32 #include "libpspp/array.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/hash-functions.h"
35 #include "libpspp/hmap.h"
36 #include "libpspp/message.h"
37 #include "libpspp/string-array.h"
38 #include "math/mode.h"
39 #include "math/moments.h"
40 #include "math/percentiles.h"
41 #include "math/sort.h"
42 #include "output/pivot-table.h"
44 #include "gl/minmax.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) (msgid)
53 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
54 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
55 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
56 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
60 - unweighted summaries (U*)
61 - lower confidence limits (*.LCL)
62 - upper confidence limits (*.UCL)
63 - standard error (*.SE)
66 /* All variables. */ \
67 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
68 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
69 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
85 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
86 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
87 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
88 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
89 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
91 /* Scale variables, totals, and subtotals. */ \
92 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
97 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
98 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
100 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
103 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
104 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
105 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
106 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
107 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
108 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
109 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
110 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
111 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
112 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
113 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
114 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
116 /* Multiple response sets. */ \
117 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
118 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
134 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
135 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
136 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
137 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
138 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
140 enum ctables_summary_function
142 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
148 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
149 N_CTSF_FUNCTIONS = SUMMARIES
153 enum ctables_domain_type
155 /* Within a section, where stacked variables divide one section from
157 CTDT_TABLE, /* All layers of a whole section. */
158 CTDT_LAYER, /* One layer within a section. */
159 CTDT_LAYERROW, /* Row in one layer within a section. */
160 CTDT_LAYERCOL, /* Column in one layer within a section. */
162 /* Within a subtable, where a subtable pairs an innermost row variable with
163 an innermost column variable within a single layer. */
164 CTDT_SUBTABLE, /* Whole subtable. */
165 CTDT_ROW, /* Row within a subtable. */
166 CTDT_COL, /* Column within a subtable. */
170 struct ctables_domain
172 struct hmap_node node;
174 const struct ctables_cell *example;
176 double d_valid; /* Dictionary weight. */
178 double e_valid; /* Effective weight */
182 enum ctables_summary_variant
191 /* In struct ctables's 'cells' hmap. Indexed by all the values in all the
192 axes (except the scalar variable, if any). */
193 struct hmap_node node;
195 /* The domains that contain this cell. */
196 bool contributes_to_domains;
197 struct ctables_domain *domains[N_CTDTS];
200 enum ctables_summary_variant sv;
205 struct ctables_cell_value
207 const struct ctables_category *category;
215 union ctables_summary *summaries;
220 struct pivot_table_look *look;
222 /* If this is NULL, zeros are displayed using the normal print format.
223 Otherwise, this string is displayed. */
226 /* If this is NULL, missing values are displayed using the normal print
227 format. Otherwise, this string is displayed. */
230 /* Indexed by variable dictionary index. */
231 enum ctables_vlabel *vlabels;
233 bool mrsets_count_duplicates; /* MRSETS. */
234 bool smissing_listwise; /* SMISSING. */
235 struct variable *e_weight; /* WEIGHT. */
236 int hide_threshold; /* HIDESMALLCOUNTS. */
238 struct ctables_table **tables;
242 struct ctables_postcompute
244 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
245 const char *name; /* Name, without leading &. */
247 struct ctables_postcompute_expr *expr;
250 bool hide_source_cats;
253 struct ctables_postcompute_expr
255 enum ctables_postcompute_op
263 /* XXX SUBTOTAL and HSUBTOTAL */
276 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
281 XXX what about string ranges? */
284 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
285 struct ctables_postcompute_expr *subs[2];
289 struct ctables_summary_spec_set
291 struct ctables_summary_spec *specs;
295 struct variable *var;
298 static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
299 const struct ctables_summary_spec_set *);
300 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
302 /* A nested sequence of variables, e.g. a > b > c. */
305 struct variable **vars;
308 size_t *domains[N_CTDTS];
309 size_t n_domains[N_CTDTS];
311 struct ctables_summary_spec_set specs[N_CSVS];
314 /* A stack of nestings, e.g. nest1 + nest2 + ... + nestN. */
317 struct ctables_nest *nests;
323 struct hmap_node node;
330 struct ctables_axis *axes[PIVOT_N_AXES];
331 struct ctables_stack stacks[PIVOT_N_AXES];
332 enum pivot_axis_type summary_axis;
333 struct ctables_summary_spec_set summary_specs;
335 struct hmap domains[N_CTDTS];
337 const struct variable *clabels_example;
338 struct hmap clabels_values_map;
339 struct ctables_value **clabels_values;
340 size_t n_clabels_values;
342 enum pivot_axis_type slabels_axis;
343 bool slabels_visible;
345 /* The innermost category labels for axis 'a' appear on axis label_axis[a].
347 Most commonly, label_axis[a] == a, and in particular we always have
348 label_axis{PIVOT_AXIS_LAYER] == PIVOT_AXIS_LAYER.
350 If ROWLABELS or COLLABELS is specified, then one of
351 label_axis[PIVOT_AXIS_ROW] or label_axis[PIVOT_AXIS_COLUMN] can be the
352 opposite axis or PIVOT_AXIS_LAYER. Only one of them will differ.
354 enum pivot_axis_type label_axis[PIVOT_N_AXES];
355 enum pivot_axis_type clabels_from_axis;
357 /* Indexed by variable dictionary index. */
358 struct ctables_categories **categories;
367 struct ctables_chisq *chisq;
368 struct ctables_pairwise *pairwise;
376 struct variable *var;
377 const struct mrset *mrset;
381 static const struct fmt_spec *
382 ctables_var_get_print_format (const struct ctables_var *var)
384 return (var->is_mrset
385 ? var_get_print_format (var->mrset->vars[0])
386 : var_get_print_format (var->var));
390 ctables_var_name (const struct ctables_var *var)
392 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
395 struct ctables_categories
398 struct ctables_category *cats;
403 struct ctables_category
405 enum ctables_category_type
423 struct ctables_category *subtotal;
427 double number; /* CCT_NUMBER. */
428 char *string; /* CCT_STRING. */
429 double range[2]; /* CCT_RANGE. */
430 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
432 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
435 bool include_missing;
439 enum ctables_summary_function sort_function;
440 struct variable *sort_var;
447 ctables_category_uninit (struct ctables_category *cat)
467 free (cat->total_label);
478 ctables_category_equal (const struct ctables_category *a,
479 const struct ctables_category *b)
481 if (a->type != b->type)
487 return a->number == b->number;
490 return strcmp (a->string, b->string);
493 return a->range[0] == b->range[0] && a->range[1] == b->range[1];
502 return !strcmp (a->total_label, b->total_label);
507 return (a->include_missing == b->include_missing
508 && a->sort_ascending == b->sort_ascending
509 && a->sort_function == b->sort_function
510 && a->sort_var == b->sort_var
511 && a->percentile == b->percentile);
518 ctables_categories_unref (struct ctables_categories *c)
523 assert (c->n_refs > 0);
527 for (size_t i = 0; i < c->n_cats; i++)
528 ctables_category_uninit (&c->cats[i]);
534 ctables_categories_equal (const struct ctables_categories *a,
535 const struct ctables_categories *b)
537 if (a->n_cats != b->n_cats || a->show_empty != b->show_empty)
540 for (size_t i = 0; i < a->n_cats; i++)
541 if (!ctables_category_equal (&a->cats[i], &b->cats[i]))
547 /* Chi-square test (SIGTEST). */
555 /* Pairwise comparison test (COMPARETEST). */
556 struct ctables_pairwise
558 enum { PROP, MEAN } type;
561 bool meansvariance_allcats;
563 enum { BONFERRONI = 1, BH } adjust;
587 struct ctables_var var;
589 struct ctables_summary_spec_set specs[N_CSVS];
593 struct ctables_axis *subs[2];
596 struct msg_location *loc;
599 static void ctables_axis_destroy (struct ctables_axis *);
608 enum ctables_function_availability
610 CTFA_ALL, /* Any variables. */
611 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
612 CTFA_MRSETS, /* Only multiple-response sets */
615 struct ctables_summary_spec
617 enum ctables_summary_function function;
618 double percentile; /* CTSF_PTILE only. */
620 struct fmt_spec format; /* XXX extra CTABLES formats */
625 ctables_summary_spec_clone (struct ctables_summary_spec *dst,
626 const struct ctables_summary_spec *src)
629 dst->label = xstrdup (src->label);
633 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
640 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
641 const struct ctables_summary_spec_set *src)
643 struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
644 for (size_t i = 0; i < src->n; i++)
645 ctables_summary_spec_clone (&specs[i], &src->specs[i]);
647 *dst = (struct ctables_summary_spec_set) {
656 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
658 for (size_t i = 0; i < set->n; i++)
659 ctables_summary_spec_uninit (&set->specs[i]);
664 parse_col_width (struct lexer *lexer, const char *name, double *width)
666 lex_match (lexer, T_EQUALS);
667 if (lex_match_id (lexer, "DEFAULT"))
669 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
671 *width = lex_number (lexer);
681 parse_bool (struct lexer *lexer, bool *b)
683 if (lex_match_id (lexer, "NO"))
685 else if (lex_match_id (lexer, "YES"))
689 lex_error_expecting (lexer, "YES", "NO");
695 static enum ctables_function_availability
696 ctables_function_availability (enum ctables_summary_function f)
698 static enum ctables_function_availability availability[] = {
699 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
704 return availability[f];
708 parse_ctables_summary_function (struct lexer *lexer,
709 enum ctables_summary_function *f)
713 enum ctables_summary_function function;
714 struct substring name;
716 static struct pair names[] = {
717 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
718 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
721 /* The .COUNT suffix may be omitted. */
722 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
723 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
724 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
725 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
726 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
727 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
728 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
732 if (!lex_force_id (lexer))
735 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
736 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
738 *f = names[i].function;
743 lex_error (lexer, _("Expecting summary function name."));
748 ctables_axis_destroy (struct ctables_axis *axis)
756 for (size_t i = 0; i < N_CSVS; i++)
757 ctables_summary_spec_set_uninit (&axis->specs[i]);
762 ctables_axis_destroy (axis->subs[0]);
763 ctables_axis_destroy (axis->subs[1]);
766 msg_location_destroy (axis->loc);
770 static struct ctables_axis *
771 ctables_axis_new_nonterminal (enum ctables_axis_op op,
772 struct ctables_axis *sub0,
773 struct ctables_axis *sub1,
774 struct lexer *lexer, int start_ofs)
776 struct ctables_axis *axis = xmalloc (sizeof *axis);
777 *axis = (struct ctables_axis) {
779 .subs = { sub0, sub1 },
780 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
785 struct ctables_axis_parse_ctx
788 struct dictionary *dict;
790 struct ctables_table *t;
793 static struct fmt_spec
794 ctables_summary_default_format (enum ctables_summary_function function,
795 const struct ctables_var *var)
797 static const enum ctables_format default_formats[] = {
798 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
802 switch (default_formats[function])
805 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
808 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
811 return *ctables_var_get_print_format (var);
819 ctables_summary_default_label (enum ctables_summary_function function,
822 static const char *default_labels[] = {
823 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
828 return (function == CTSF_PTILE
829 ? xasprintf (_("Percentile %.2f"), percentile)
830 : xstrdup (gettext (default_labels[function])));
834 ctables_summary_function_name (enum ctables_summary_function function)
836 static const char *names[] = {
837 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
841 return names[function];
845 add_summary_spec (struct ctables_axis *axis,
846 enum ctables_summary_function function, double percentile,
847 const char *label, const struct fmt_spec *format,
848 const struct msg_location *loc, enum ctables_summary_variant sv)
850 if (axis->op == CTAO_VAR)
852 const char *function_name = ctables_summary_function_name (function);
853 const char *var_name = ctables_var_name (&axis->var);
854 switch (ctables_function_availability (function))
857 if (!axis->var.is_mrset)
859 msg_at (SE, loc, _("Summary function %s applies only to multiple "
860 "response sets."), function_name);
861 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
871 _("Summary function %s applies only to scale variables."),
873 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
883 struct ctables_summary_spec_set *set = &axis->specs[sv];
884 if (set->n >= set->allocated)
885 set->specs = x2nrealloc (set->specs, &set->allocated,
888 struct ctables_summary_spec *dst = &set->specs[set->n++];
889 *dst = (struct ctables_summary_spec) {
890 .function = function,
891 .percentile = percentile,
892 .label = xstrdup (label),
893 .format = (format ? *format
894 : ctables_summary_default_format (function, &axis->var)),
900 for (size_t i = 0; i < 2; i++)
901 if (!add_summary_spec (axis->subs[i], function, percentile, label,
908 static struct ctables_axis *ctables_axis_parse_stack (
909 struct ctables_axis_parse_ctx *);
912 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
913 struct ctables_var *var)
915 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
917 *var = (struct ctables_var) {
919 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
923 lex_error (lexer, _("'%s' does not name a multiple-response set "
924 "in the active file dictionary."),
925 lex_tokcstr (lexer));
933 *var = (struct ctables_var) {
935 .var = parse_variable (lexer, dict),
937 return var->var != NULL;
941 static struct ctables_axis *
942 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
944 if (lex_match (ctx->lexer, T_LPAREN))
946 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
947 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
949 ctables_axis_destroy (sub);
955 if (!lex_force_id (ctx->lexer))
958 int start_ofs = lex_ofs (ctx->lexer);
959 struct ctables_var var;
960 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
963 struct ctables_axis *axis = xmalloc (sizeof *axis);
964 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
966 /* XXX should figure out default measures by reading data */
967 axis->scale = (var.is_mrset ? false
968 : lex_match_phrase (ctx->lexer, "[S]") ? true
969 : lex_match_phrase (ctx->lexer, "[C]") ? false
970 : var_get_measure (var.var) == MEASURE_SCALE);
971 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
972 lex_ofs (ctx->lexer) - 1);
977 has_digit (const char *s)
979 return s[strcspn (s, "0123456789")] != '\0';
982 static struct ctables_axis *
983 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
985 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
986 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
989 enum ctables_summary_variant sv = CSV_CELL;
992 int start_ofs = lex_ofs (ctx->lexer);
994 /* Parse function. */
995 enum ctables_summary_function function;
996 if (!parse_ctables_summary_function (ctx->lexer, &function))
999 /* Parse percentile. */
1000 double percentile = 0;
1001 if (function == CTSF_PTILE)
1003 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
1005 percentile = lex_number (ctx->lexer);
1006 lex_get (ctx->lexer);
1011 if (lex_is_string (ctx->lexer))
1013 label = ss_xstrdup (lex_tokss (ctx->lexer));
1014 lex_get (ctx->lexer);
1017 label = ctables_summary_default_label (function, percentile);
1020 struct fmt_spec format;
1021 const struct fmt_spec *formatp;
1022 if (lex_token (ctx->lexer) == T_ID
1023 && has_digit (lex_tokcstr (ctx->lexer)))
1025 if (!parse_format_specifier (ctx->lexer, &format)
1026 || !fmt_check_output (&format)
1027 || !fmt_check_type_compat (&format, VAL_NUMERIC))
1037 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
1038 lex_ofs (ctx->lexer) - 1);
1039 add_summary_spec (sub, function, percentile, label, formatp, loc, sv);
1041 msg_location_destroy (loc);
1043 lex_match (ctx->lexer, T_COMMA);
1044 if (sv == CSV_CELL && lex_match_id (ctx->lexer, "TOTALS"))
1046 if (!lex_force_match (ctx->lexer, T_LBRACK))
1050 else if (lex_match (ctx->lexer, T_RBRACK))
1052 if (sv == CSV_TOTAL && !lex_force_match (ctx->lexer, T_RBRACK))
1059 ctables_axis_destroy (sub);
1063 static const struct ctables_axis *
1064 find_scale (const struct ctables_axis *axis)
1068 else if (axis->op == CTAO_VAR)
1072 assert (!axis->var.is_mrset);
1080 for (size_t i = 0; i < 2; i++)
1082 const struct ctables_axis *scale = find_scale (axis->subs[i]);
1090 static const struct ctables_axis *
1091 find_categorical_summary_spec (const struct ctables_axis *axis)
1095 else if (axis->op == CTAO_VAR)
1096 return !axis->scale && axis->specs[CSV_CELL].n ? axis : NULL;
1099 for (size_t i = 0; i < 2; i++)
1101 const struct ctables_axis *sum
1102 = find_categorical_summary_spec (axis->subs[i]);
1110 static struct ctables_axis *
1111 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1113 int start_ofs = lex_ofs (ctx->lexer);
1114 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1118 while (lex_match (ctx->lexer, T_GT))
1120 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1124 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1125 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1127 const struct ctables_axis *outer_scale = find_scale (lhs);
1128 const struct ctables_axis *inner_scale = find_scale (rhs);
1129 if (outer_scale && inner_scale)
1131 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1132 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1133 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1134 ctables_axis_destroy (nest);
1138 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1141 msg_at (SE, nest->loc,
1142 _("Summaries may only be requested for categorical variables "
1143 "at the innermost nesting level."));
1144 msg_at (SN, outer_sum->loc,
1145 _("This outer categorical variable has a summary."));
1146 ctables_axis_destroy (nest);
1156 static struct ctables_axis *
1157 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1159 int start_ofs = lex_ofs (ctx->lexer);
1160 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1164 while (lex_match (ctx->lexer, T_PLUS))
1166 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1170 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1171 ctx->lexer, start_ofs);
1178 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1179 struct ctables *ct, struct ctables_table *t,
1180 enum pivot_axis_type a)
1182 if (lex_token (lexer) == T_BY
1183 || lex_token (lexer) == T_SLASH
1184 || lex_token (lexer) == T_ENDCMD)
1187 struct ctables_axis_parse_ctx ctx = {
1193 t->axes[a] = ctables_axis_parse_stack (&ctx);
1194 return t->axes[a] != NULL;
1198 ctables_chisq_destroy (struct ctables_chisq *chisq)
1204 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1210 ctables_table_destroy (struct ctables_table *t)
1215 for (size_t i = 0; i < t->n_categories; i++)
1216 ctables_categories_unref (t->categories[i]);
1217 free (t->categories);
1219 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1220 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1221 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1225 ctables_chisq_destroy (t->chisq);
1226 ctables_pairwise_destroy (t->pairwise);
1231 ctables_destroy (struct ctables *ct)
1236 pivot_table_look_unref (ct->look);
1240 for (size_t i = 0; i < ct->n_tables; i++)
1241 ctables_table_destroy (ct->tables[i]);
1246 static struct ctables_category
1247 cct_range (double low, double high)
1249 return (struct ctables_category) {
1251 .range = { low, high }
1256 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1257 struct ctables_table *t)
1259 if (!lex_match_id (lexer, "VARIABLES"))
1261 lex_match (lexer, T_EQUALS);
1263 struct variable **vars;
1265 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1268 struct ctables_categories *c = xmalloc (sizeof *c);
1269 *c = (struct ctables_categories) { .n_refs = n_vars };
1270 for (size_t i = 0; i < n_vars; i++)
1272 struct ctables_categories **cp
1273 = &t->categories[var_get_dict_index (vars[i])];
1274 ctables_categories_unref (*cp);
1279 size_t allocated_cats = 0;
1280 if (lex_match (lexer, T_LBRACK))
1284 if (c->n_cats >= allocated_cats)
1285 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1287 struct ctables_category *cat = &c->cats[c->n_cats];
1288 if (lex_match_id (lexer, "OTHERNM"))
1289 cat->type = CCT_OTHERNM;
1290 else if (lex_match_id (lexer, "MISSING"))
1291 cat->type = CCT_MISSING;
1292 else if (lex_match_id (lexer, "SUBTOTAL"))
1293 *cat = (struct ctables_category)
1294 { .type = CCT_SUBTOTAL, .total_label = NULL };
1295 else if (lex_match_id (lexer, "HSUBTOTAL"))
1296 *cat = (struct ctables_category)
1297 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1298 else if (lex_match_id (lexer, "LO"))
1300 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1302 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1305 else if (lex_is_number (lexer))
1307 double number = lex_number (lexer);
1309 if (lex_match_id (lexer, "THRU"))
1311 cat->type = CCT_RANGE;
1312 cat->range[0] = number;
1313 if (lex_match_id (lexer, "HI"))
1314 *cat = cct_range (number, DBL_MAX);
1317 if (!lex_force_num (lexer))
1319 *cat = cct_range (number, lex_number (lexer));
1324 *cat = (struct ctables_category) {
1329 else if (lex_is_string (lexer))
1331 *cat = (struct ctables_category) {
1333 .string = ss_xstrdup (lex_tokss (lexer)),
1339 lex_error (lexer, NULL);
1343 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1345 if (lex_match (lexer, T_EQUALS))
1347 if (!lex_force_string (lexer))
1350 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1354 cat->total_label = xstrdup (_("Subtotal"));
1358 lex_match (lexer, T_COMMA);
1360 while (!lex_match (lexer, T_RBRACK));
1363 struct ctables_category cat = {
1365 .include_missing = false,
1366 .sort_ascending = true,
1368 bool show_totals = false;
1369 char *total_label = NULL;
1370 bool totals_before = false;
1371 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1373 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1375 lex_match (lexer, T_EQUALS);
1376 if (lex_match_id (lexer, "A"))
1377 cat.sort_ascending = true;
1378 else if (lex_match_id (lexer, "D"))
1379 cat.sort_ascending = false;
1382 lex_error_expecting (lexer, "A", "D");
1386 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1388 lex_match (lexer, T_EQUALS);
1389 if (lex_match_id (lexer, "VALUE"))
1390 cat.type = CCT_VALUE;
1391 else if (lex_match_id (lexer, "LABEL"))
1392 cat.type = CCT_LABEL;
1395 cat.type = CCT_FUNCTION;
1396 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1399 if (lex_match (lexer, T_LPAREN))
1401 cat.sort_var = parse_variable (lexer, dict);
1405 if (cat.sort_function == CTSF_PTILE)
1407 lex_match (lexer, T_COMMA);
1408 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1410 cat.percentile = lex_number (lexer);
1414 if (!lex_force_match (lexer, T_RPAREN))
1417 else if (ctables_function_availability (cat.sort_function)
1420 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1425 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1427 lex_match (lexer, T_EQUALS);
1428 if (lex_match_id (lexer, "INCLUDE"))
1429 cat.include_missing = true;
1430 else if (lex_match_id (lexer, "EXCLUDE"))
1431 cat.include_missing = false;
1434 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1438 else if (lex_match_id (lexer, "TOTAL"))
1440 lex_match (lexer, T_EQUALS);
1441 if (!parse_bool (lexer, &show_totals))
1444 else if (lex_match_id (lexer, "LABEL"))
1446 lex_match (lexer, T_EQUALS);
1447 if (!lex_force_string (lexer))
1450 total_label = ss_xstrdup (lex_tokss (lexer));
1453 else if (lex_match_id (lexer, "POSITION"))
1455 lex_match (lexer, T_EQUALS);
1456 if (lex_match_id (lexer, "BEFORE"))
1457 totals_before = true;
1458 else if (lex_match_id (lexer, "AFTER"))
1459 totals_before = false;
1462 lex_error_expecting (lexer, "BEFORE", "AFTER");
1466 else if (lex_match_id (lexer, "EMPTY"))
1468 lex_match (lexer, T_EQUALS);
1469 if (lex_match_id (lexer, "INCLUDE"))
1470 c->show_empty = true;
1471 else if (lex_match_id (lexer, "EXCLUDE"))
1472 c->show_empty = false;
1475 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1482 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1483 "TOTAL", "LABEL", "POSITION", "EMPTY");
1485 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1492 if (c->n_cats >= allocated_cats)
1493 c->cats = x2nrealloc (c->cats, &allocated_cats,
1495 c->cats[c->n_cats++] = cat;
1500 if (c->n_cats >= allocated_cats)
1501 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1503 struct ctables_category *totals;
1506 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1507 totals = &c->cats[0];
1510 totals = &c->cats[c->n_cats];
1513 *totals = (struct ctables_category) {
1515 .total_label = total_label ? total_label : xstrdup (_("Total")),
1519 struct ctables_category *subtotal = NULL;
1520 for (size_t i = totals_before ? 0 : c->n_cats;
1521 totals_before ? i < c->n_cats : i-- > 0;
1522 totals_before ? i++ : 0)
1524 struct ctables_category *cat = &c->cats[i];
1532 cat->subtotal = subtotal;
1552 ctables_nest_uninit (struct ctables_nest *nest)
1559 ctables_stack_uninit (struct ctables_stack *stack)
1563 for (size_t i = 0; i < stack->n; i++)
1564 ctables_nest_uninit (&stack->nests[i]);
1565 free (stack->nests);
1569 static struct ctables_stack
1570 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1577 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1578 for (size_t i = 0; i < s0.n; i++)
1579 for (size_t j = 0; j < s1.n; j++)
1581 const struct ctables_nest *a = &s0.nests[i];
1582 const struct ctables_nest *b = &s1.nests[j];
1584 size_t allocate = a->n + b->n;
1585 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1586 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1588 for (size_t k = 0; k < a->n; k++)
1589 vars[n++] = a->vars[k];
1590 for (size_t k = 0; k < b->n; k++)
1591 vars[n++] = b->vars[k];
1592 assert (n == allocate);
1594 const struct ctables_nest *summary_src;
1595 if (!a->specs[CSV_CELL].var)
1597 else if (!b->specs[CSV_CELL].var)
1602 struct ctables_nest *new = &stack.nests[stack.n++];
1603 *new = (struct ctables_nest) {
1605 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1606 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1610 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1611 ctables_summary_spec_set_clone (&new->specs[sv], &summary_src->specs[sv]);
1613 ctables_stack_uninit (&s0);
1614 ctables_stack_uninit (&s1);
1618 static struct ctables_stack
1619 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1621 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1622 for (size_t i = 0; i < s0.n; i++)
1623 stack.nests[stack.n++] = s0.nests[i];
1624 for (size_t i = 0; i < s1.n; i++)
1625 stack.nests[stack.n++] = s1.nests[i];
1626 assert (stack.n == s0.n + s1.n);
1632 static struct ctables_stack
1633 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1636 return (struct ctables_stack) { .n = 0 };
1641 assert (!a->var.is_mrset);
1643 struct variable **vars = xmalloc (sizeof *vars);
1646 struct ctables_nest *nest = xmalloc (sizeof *nest);
1647 *nest = (struct ctables_nest) {
1650 .scale_idx = a->scale ? 0 : SIZE_MAX,
1652 if (a->specs[CSV_CELL].n || a->scale)
1653 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1655 ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
1656 nest->specs[sv].var = a->var.var;
1658 return (struct ctables_stack) { .nests = nest, .n = 1 };
1661 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1662 enumerate_fts (axis_type, a->subs[1]));
1665 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1666 enumerate_fts (axis_type, a->subs[1]));
1672 union ctables_summary
1674 /* COUNT, VALIDN, TOTALN. */
1681 /* MINIMUM, MAXIMUM, RANGE. */
1688 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1689 struct moments1 *moments;
1691 /* MEDIAN, MODE, PTILE. */
1694 struct casewriter *writer;
1699 /* XXX multiple response */
1703 ctables_summary_init (union ctables_summary *s,
1704 const struct ctables_summary_spec *ss)
1706 switch (ss->function)
1710 case CTSF_ROWPCT_COUNT:
1711 case CTSF_COLPCT_COUNT:
1712 case CTSF_TABLEPCT_COUNT:
1713 case CTSF_SUBTABLEPCT_COUNT:
1714 case CTSF_LAYERPCT_COUNT:
1715 case CTSF_LAYERROWPCT_COUNT:
1716 case CTSF_LAYERCOLPCT_COUNT:
1717 case CTSF_ROWPCT_VALIDN:
1718 case CTSF_COLPCT_VALIDN:
1719 case CTSF_TABLEPCT_VALIDN:
1720 case CTSF_SUBTABLEPCT_VALIDN:
1721 case CTSF_LAYERPCT_VALIDN:
1722 case CTSF_LAYERROWPCT_VALIDN:
1723 case CTSF_LAYERCOLPCT_VALIDN:
1724 case CTSF_ROWPCT_TOTALN:
1725 case CTSF_COLPCT_TOTALN:
1726 case CTSF_TABLEPCT_TOTALN:
1727 case CTSF_SUBTABLEPCT_TOTALN:
1728 case CTSF_LAYERPCT_TOTALN:
1729 case CTSF_LAYERROWPCT_TOTALN:
1730 case CTSF_LAYERCOLPCT_TOTALN:
1736 s->missing = s->valid = 0;
1742 s->min = s->max = SYSMIS;
1750 case CTSF_ROWPCT_SUM:
1751 case CTSF_COLPCT_SUM:
1752 case CTSF_TABLEPCT_SUM:
1753 case CTSF_SUBTABLEPCT_SUM:
1754 case CTSF_LAYERPCT_SUM:
1755 case CTSF_LAYERROWPCT_SUM:
1756 case CTSF_LAYERCOLPCT_SUM:
1757 s->moments = moments1_create (MOMENT_VARIANCE);
1764 struct caseproto *proto = caseproto_create ();
1765 proto = caseproto_add_width (proto, 0);
1766 proto = caseproto_add_width (proto, 0);
1768 struct subcase ordering;
1769 subcase_init (&ordering, 0, 0, SC_ASCEND);
1770 s->writer = sort_create_writer (&ordering, proto);
1771 subcase_uninit (&ordering);
1772 caseproto_unref (proto);
1779 case CTSF_RESPONSES:
1780 case CTSF_ROWPCT_RESPONSES:
1781 case CTSF_COLPCT_RESPONSES:
1782 case CTSF_TABLEPCT_RESPONSES:
1783 case CTSF_SUBTABLEPCT_RESPONSES:
1784 case CTSF_LAYERPCT_RESPONSES:
1785 case CTSF_LAYERROWPCT_RESPONSES:
1786 case CTSF_LAYERCOLPCT_RESPONSES:
1787 case CTSF_ROWPCT_RESPONSES_COUNT:
1788 case CTSF_COLPCT_RESPONSES_COUNT:
1789 case CTSF_TABLEPCT_RESPONSES_COUNT:
1790 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1791 case CTSF_LAYERPCT_RESPONSES_COUNT:
1792 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1793 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1794 case CTSF_ROWPCT_COUNT_RESPONSES:
1795 case CTSF_COLPCT_COUNT_RESPONSES:
1796 case CTSF_TABLEPCT_COUNT_RESPONSES:
1797 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1798 case CTSF_LAYERPCT_COUNT_RESPONSES:
1799 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1800 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1806 ctables_summary_uninit (union ctables_summary *s,
1807 const struct ctables_summary_spec *ss)
1809 switch (ss->function)
1813 case CTSF_ROWPCT_COUNT:
1814 case CTSF_COLPCT_COUNT:
1815 case CTSF_TABLEPCT_COUNT:
1816 case CTSF_SUBTABLEPCT_COUNT:
1817 case CTSF_LAYERPCT_COUNT:
1818 case CTSF_LAYERROWPCT_COUNT:
1819 case CTSF_LAYERCOLPCT_COUNT:
1820 case CTSF_ROWPCT_VALIDN:
1821 case CTSF_COLPCT_VALIDN:
1822 case CTSF_TABLEPCT_VALIDN:
1823 case CTSF_SUBTABLEPCT_VALIDN:
1824 case CTSF_LAYERPCT_VALIDN:
1825 case CTSF_LAYERROWPCT_VALIDN:
1826 case CTSF_LAYERCOLPCT_VALIDN:
1827 case CTSF_ROWPCT_TOTALN:
1828 case CTSF_COLPCT_TOTALN:
1829 case CTSF_TABLEPCT_TOTALN:
1830 case CTSF_SUBTABLEPCT_TOTALN:
1831 case CTSF_LAYERPCT_TOTALN:
1832 case CTSF_LAYERROWPCT_TOTALN:
1833 case CTSF_LAYERCOLPCT_TOTALN:
1851 case CTSF_ROWPCT_SUM:
1852 case CTSF_COLPCT_SUM:
1853 case CTSF_TABLEPCT_SUM:
1854 case CTSF_SUBTABLEPCT_SUM:
1855 case CTSF_LAYERPCT_SUM:
1856 case CTSF_LAYERROWPCT_SUM:
1857 case CTSF_LAYERCOLPCT_SUM:
1858 moments1_destroy (s->moments);
1864 casewriter_destroy (s->writer);
1867 case CTSF_RESPONSES:
1868 case CTSF_ROWPCT_RESPONSES:
1869 case CTSF_COLPCT_RESPONSES:
1870 case CTSF_TABLEPCT_RESPONSES:
1871 case CTSF_SUBTABLEPCT_RESPONSES:
1872 case CTSF_LAYERPCT_RESPONSES:
1873 case CTSF_LAYERROWPCT_RESPONSES:
1874 case CTSF_LAYERCOLPCT_RESPONSES:
1875 case CTSF_ROWPCT_RESPONSES_COUNT:
1876 case CTSF_COLPCT_RESPONSES_COUNT:
1877 case CTSF_TABLEPCT_RESPONSES_COUNT:
1878 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1879 case CTSF_LAYERPCT_RESPONSES_COUNT:
1880 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1881 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1882 case CTSF_ROWPCT_COUNT_RESPONSES:
1883 case CTSF_COLPCT_COUNT_RESPONSES:
1884 case CTSF_TABLEPCT_COUNT_RESPONSES:
1885 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1886 case CTSF_LAYERPCT_COUNT_RESPONSES:
1887 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1888 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1894 ctables_summary_add (union ctables_summary *s,
1895 const struct ctables_summary_spec *ss,
1896 const struct variable *var, const union value *value,
1897 double d_weight, double e_weight)
1899 switch (ss->function)
1904 if (var_is_value_missing (var, value))
1905 s->missing += d_weight;
1907 s->valid += d_weight;
1911 case CTSF_ROWPCT_COUNT:
1912 case CTSF_COLPCT_COUNT:
1913 case CTSF_TABLEPCT_COUNT:
1914 case CTSF_SUBTABLEPCT_COUNT:
1915 case CTSF_LAYERPCT_COUNT:
1916 case CTSF_LAYERROWPCT_COUNT:
1917 case CTSF_LAYERCOLPCT_COUNT:
1918 case CTSF_ROWPCT_VALIDN:
1919 case CTSF_COLPCT_VALIDN:
1920 case CTSF_TABLEPCT_VALIDN:
1921 case CTSF_SUBTABLEPCT_VALIDN:
1922 case CTSF_LAYERPCT_VALIDN:
1923 case CTSF_LAYERROWPCT_VALIDN:
1924 case CTSF_LAYERCOLPCT_VALIDN:
1925 case CTSF_ROWPCT_TOTALN:
1926 case CTSF_COLPCT_TOTALN:
1927 case CTSF_TABLEPCT_TOTALN:
1928 case CTSF_SUBTABLEPCT_TOTALN:
1929 case CTSF_LAYERPCT_TOTALN:
1930 case CTSF_LAYERROWPCT_TOTALN:
1931 case CTSF_LAYERCOLPCT_TOTALN:
1935 if (var_is_value_missing (var, value))
1936 s->missing += e_weight;
1938 s->valid += e_weight;
1944 if (!var_is_value_missing (var, value))
1946 assert (!var_is_alpha (var)); /* XXX? */
1947 if (s->min == SYSMIS || value->f < s->min)
1949 if (s->max == SYSMIS || value->f > s->max)
1959 case CTSF_ROWPCT_SUM:
1960 case CTSF_COLPCT_SUM:
1961 case CTSF_TABLEPCT_SUM:
1962 case CTSF_SUBTABLEPCT_SUM:
1963 case CTSF_LAYERPCT_SUM:
1964 case CTSF_LAYERROWPCT_SUM:
1965 case CTSF_LAYERCOLPCT_SUM:
1966 if (!var_is_value_missing (var, value))
1967 moments1_add (s->moments, value->f, e_weight);
1973 if (var_is_value_missing (var, value))
1975 s->ovalid += e_weight;
1977 struct ccase *c = case_create (casewriter_get_proto (s->writer));
1978 *case_num_rw_idx (c, 0) = value->f;
1979 *case_num_rw_idx (c, 1) = e_weight;
1980 casewriter_write (s->writer, c);
1984 case CTSF_RESPONSES:
1985 case CTSF_ROWPCT_RESPONSES:
1986 case CTSF_COLPCT_RESPONSES:
1987 case CTSF_TABLEPCT_RESPONSES:
1988 case CTSF_SUBTABLEPCT_RESPONSES:
1989 case CTSF_LAYERPCT_RESPONSES:
1990 case CTSF_LAYERROWPCT_RESPONSES:
1991 case CTSF_LAYERCOLPCT_RESPONSES:
1992 case CTSF_ROWPCT_RESPONSES_COUNT:
1993 case CTSF_COLPCT_RESPONSES_COUNT:
1994 case CTSF_TABLEPCT_RESPONSES_COUNT:
1995 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1996 case CTSF_LAYERPCT_RESPONSES_COUNT:
1997 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1998 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1999 case CTSF_ROWPCT_COUNT_RESPONSES:
2000 case CTSF_COLPCT_COUNT_RESPONSES:
2001 case CTSF_TABLEPCT_COUNT_RESPONSES:
2002 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2003 case CTSF_LAYERPCT_COUNT_RESPONSES:
2004 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2005 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2010 static enum ctables_domain_type
2011 ctables_function_domain (enum ctables_summary_function function)
2033 case CTSF_RESPONSES:
2036 case CTSF_COLPCT_COUNT:
2037 case CTSF_COLPCT_COUNT_RESPONSES:
2038 case CTSF_COLPCT_RESPONSES:
2039 case CTSF_COLPCT_RESPONSES_COUNT:
2040 case CTSF_COLPCT_SUM:
2041 case CTSF_COLPCT_TOTALN:
2042 case CTSF_COLPCT_VALIDN:
2045 case CTSF_LAYERCOLPCT_COUNT:
2046 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2047 case CTSF_LAYERCOLPCT_RESPONSES:
2048 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2049 case CTSF_LAYERCOLPCT_SUM:
2050 case CTSF_LAYERCOLPCT_TOTALN:
2051 case CTSF_LAYERCOLPCT_VALIDN:
2052 return CTDT_LAYERCOL;
2054 case CTSF_LAYERPCT_COUNT:
2055 case CTSF_LAYERPCT_COUNT_RESPONSES:
2056 case CTSF_LAYERPCT_RESPONSES:
2057 case CTSF_LAYERPCT_RESPONSES_COUNT:
2058 case CTSF_LAYERPCT_SUM:
2059 case CTSF_LAYERPCT_TOTALN:
2060 case CTSF_LAYERPCT_VALIDN:
2063 case CTSF_LAYERROWPCT_COUNT:
2064 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2065 case CTSF_LAYERROWPCT_RESPONSES:
2066 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2067 case CTSF_LAYERROWPCT_SUM:
2068 case CTSF_LAYERROWPCT_TOTALN:
2069 case CTSF_LAYERROWPCT_VALIDN:
2070 return CTDT_LAYERROW;
2072 case CTSF_ROWPCT_COUNT:
2073 case CTSF_ROWPCT_COUNT_RESPONSES:
2074 case CTSF_ROWPCT_RESPONSES:
2075 case CTSF_ROWPCT_RESPONSES_COUNT:
2076 case CTSF_ROWPCT_SUM:
2077 case CTSF_ROWPCT_TOTALN:
2078 case CTSF_ROWPCT_VALIDN:
2081 case CTSF_SUBTABLEPCT_COUNT:
2082 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2083 case CTSF_SUBTABLEPCT_RESPONSES:
2084 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2085 case CTSF_SUBTABLEPCT_SUM:
2086 case CTSF_SUBTABLEPCT_TOTALN:
2087 case CTSF_SUBTABLEPCT_VALIDN:
2088 return CTDT_SUBTABLE;
2090 case CTSF_TABLEPCT_COUNT:
2091 case CTSF_TABLEPCT_COUNT_RESPONSES:
2092 case CTSF_TABLEPCT_RESPONSES:
2093 case CTSF_TABLEPCT_RESPONSES_COUNT:
2094 case CTSF_TABLEPCT_SUM:
2095 case CTSF_TABLEPCT_TOTALN:
2096 case CTSF_TABLEPCT_VALIDN:
2104 ctables_summary_value (const struct ctables_cell *cell,
2105 union ctables_summary *s,
2106 const struct ctables_summary_spec *ss)
2108 switch (ss->function)
2114 case CTSF_ROWPCT_COUNT:
2115 case CTSF_COLPCT_COUNT:
2116 case CTSF_TABLEPCT_COUNT:
2117 case CTSF_SUBTABLEPCT_COUNT:
2118 case CTSF_LAYERPCT_COUNT:
2119 case CTSF_LAYERROWPCT_COUNT:
2120 case CTSF_LAYERCOLPCT_COUNT:
2122 enum ctables_domain_type d = ctables_function_domain (ss->function);
2123 return (cell->domains[d]->e_valid
2124 ? s->valid / cell->domains[d]->e_valid * 100
2128 case CTSF_ROWPCT_VALIDN:
2129 case CTSF_COLPCT_VALIDN:
2130 case CTSF_TABLEPCT_VALIDN:
2131 case CTSF_SUBTABLEPCT_VALIDN:
2132 case CTSF_LAYERPCT_VALIDN:
2133 case CTSF_LAYERROWPCT_VALIDN:
2134 case CTSF_LAYERCOLPCT_VALIDN:
2135 case CTSF_ROWPCT_TOTALN:
2136 case CTSF_COLPCT_TOTALN:
2137 case CTSF_TABLEPCT_TOTALN:
2138 case CTSF_SUBTABLEPCT_TOTALN:
2139 case CTSF_LAYERPCT_TOTALN:
2140 case CTSF_LAYERROWPCT_TOTALN:
2141 case CTSF_LAYERCOLPCT_TOTALN:
2149 return s->valid + s->missing;
2162 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
2167 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
2173 double weight, variance;
2174 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
2175 return calc_semean (variance, weight);
2181 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2182 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
2187 double weight, mean;
2188 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
2189 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
2195 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2199 case CTSF_ROWPCT_SUM:
2200 case CTSF_COLPCT_SUM:
2201 case CTSF_TABLEPCT_SUM:
2202 case CTSF_SUBTABLEPCT_SUM:
2203 case CTSF_LAYERPCT_SUM:
2204 case CTSF_LAYERROWPCT_SUM:
2205 case CTSF_LAYERCOLPCT_SUM:
2212 struct casereader *reader = casewriter_make_reader (s->writer);
2215 struct percentile *ptile = percentile_create (
2216 ss->function == CTSF_PTILE ? ss->percentile : 0.5, s->ovalid);
2217 struct order_stats *os = &ptile->parent;
2218 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2219 s->ovalue = percentile_calculate (ptile, PC_HAVERAGE);
2220 statistic_destroy (&ptile->parent.parent);
2227 struct casereader *reader = casewriter_make_reader (s->writer);
2230 struct mode *mode = mode_create ();
2231 struct order_stats *os = &mode->parent;
2232 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2233 s->ovalue = mode->mode;
2234 statistic_destroy (&mode->parent.parent);
2238 case CTSF_RESPONSES:
2239 case CTSF_ROWPCT_RESPONSES:
2240 case CTSF_COLPCT_RESPONSES:
2241 case CTSF_TABLEPCT_RESPONSES:
2242 case CTSF_SUBTABLEPCT_RESPONSES:
2243 case CTSF_LAYERPCT_RESPONSES:
2244 case CTSF_LAYERROWPCT_RESPONSES:
2245 case CTSF_LAYERCOLPCT_RESPONSES:
2246 case CTSF_ROWPCT_RESPONSES_COUNT:
2247 case CTSF_COLPCT_RESPONSES_COUNT:
2248 case CTSF_TABLEPCT_RESPONSES_COUNT:
2249 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2250 case CTSF_LAYERPCT_RESPONSES_COUNT:
2251 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2252 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2253 case CTSF_ROWPCT_COUNT_RESPONSES:
2254 case CTSF_COLPCT_COUNT_RESPONSES:
2255 case CTSF_TABLEPCT_COUNT_RESPONSES:
2256 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2257 case CTSF_LAYERPCT_COUNT_RESPONSES:
2258 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2259 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2266 struct ctables_cell_sort_aux
2268 const struct ctables_table *t;
2269 enum pivot_axis_type a;
2273 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2275 const struct ctables_cell_sort_aux *aux = aux_;
2276 struct ctables_cell *const *ap = a_;
2277 struct ctables_cell *const *bp = b_;
2278 const struct ctables_cell *a = *ap;
2279 const struct ctables_cell *b = *bp;
2281 size_t a_idx = a->axes[aux->a].nest_idx;
2282 size_t b_idx = b->axes[aux->a].nest_idx;
2284 return a_idx < b_idx ? -1 : 1;
2286 const struct ctables_nest *nest = &aux->t->stacks[aux->a].nests[a_idx];
2287 for (size_t i = 0; i < nest->n; i++)
2288 if (i != nest->scale_idx)
2290 const struct variable *var = nest->vars[i];
2291 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2292 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2293 if (a_cv->category != b_cv->category)
2294 return a_cv->category > b_cv->category ? 1 : -1;
2296 const union value *a_val = &a_cv->value;
2297 const union value *b_val = &b_cv->value;
2298 switch (a_cv->category->type)
2305 /* Must be equal. */
2312 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2320 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2322 return a_cv->category->sort_ascending ? cmp : -cmp;
2328 const char *a_label = var_lookup_value_label (var, a_val);
2329 const char *b_label = var_lookup_value_label (var, b_val);
2331 ? (b_label ? strcmp (a_label, b_label) : 1)
2332 : (b_label ? -1 : value_compare_3way (
2333 a_val, b_val, var_get_width (var))));
2335 return a_cv->category->sort_ascending ? cmp : -cmp;
2349 For each ctables_table:
2350 For each combination of row vars:
2351 For each combination of column vars:
2352 For each combination of layer vars:
2354 Make a table of row values:
2355 Sort entries by row values
2356 Assign a 0-based index to each actual value
2357 Construct a dimension
2358 Make a table of column values
2359 Make a table of layer values
2361 Fill the table entry using the indexes from before.
2364 static struct ctables_domain *
2365 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *cell,
2366 enum ctables_domain_type domain)
2369 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2371 size_t idx = cell->axes[a].nest_idx;
2372 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2373 hash = hash_int (idx, hash);
2374 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2376 size_t v_idx = nest->domains[domain][i];
2377 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2378 var_get_width (nest->vars[v_idx]), hash);
2382 struct ctables_domain *d;
2383 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
2385 const struct ctables_cell *df = d->example;
2386 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2388 size_t idx = cell->axes[a].nest_idx;
2389 if (idx != df->axes[a].nest_idx)
2392 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2393 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2395 size_t v_idx = nest->domains[domain][i];
2396 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2397 &cell->axes[a].cvs[v_idx].value,
2398 var_get_width (nest->vars[v_idx])))
2407 d = xmalloc (sizeof *d);
2408 *d = (struct ctables_domain) { .example = cell };
2409 hmap_insert (&t->domains[domain], &d->node, hash);
2413 static const struct ctables_category *
2414 ctables_categories_match (const struct ctables_categories *c,
2415 const union value *v, const struct variable *var)
2417 const struct ctables_category *othernm = NULL;
2418 for (size_t i = c->n_cats; i-- > 0; )
2420 const struct ctables_category *cat = &c->cats[i];
2424 if (cat->number == v->f)
2432 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2433 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2438 if (var_is_value_missing (var, v))
2455 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2460 return var_is_value_missing (var, v) ? NULL : othernm;
2463 static const struct ctables_category *
2464 ctables_categories_total (const struct ctables_categories *c)
2466 const struct ctables_category *first = &c->cats[0];
2467 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2468 return (first->type == CCT_TOTAL ? first
2469 : last->type == CCT_TOTAL ? last
2473 static struct ctables_cell *
2474 ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c,
2475 size_t ix[PIVOT_N_AXES],
2476 const struct ctables_category *cats[PIVOT_N_AXES][10])
2478 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2481 enum ctables_summary_variant sv = CSV_CELL;
2482 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2484 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2485 hash = hash_int (ix[a], hash);
2486 for (size_t i = 0; i < nest->n; i++)
2487 if (i != nest->scale_idx)
2489 hash = hash_pointer (cats[a][i], hash);
2490 if (cats[a][i]->type != CCT_TOTAL
2491 && cats[a][i]->type != CCT_SUBTOTAL
2492 && cats[a][i]->type != CCT_HSUBTOTAL)
2493 hash = value_hash (case_data (c, nest->vars[i]),
2494 var_get_width (nest->vars[i]), hash);
2500 struct ctables_cell *cell;
2501 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &t->cells)
2503 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2505 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2506 if (cell->axes[a].nest_idx != ix[a])
2508 for (size_t i = 0; i < nest->n; i++)
2509 if (i != nest->scale_idx
2510 && (cats[a][i] != cell->axes[a].cvs[i].category
2511 || (cats[a][i]->type != CCT_TOTAL
2512 && cats[a][i]->type != CCT_SUBTOTAL
2513 && cats[a][i]->type != CCT_HSUBTOTAL
2514 && !value_equal (case_data (c, nest->vars[i]),
2515 &cell->axes[a].cvs[i].value,
2516 var_get_width (nest->vars[i])))))
2525 cell = xmalloc (sizeof *cell);
2528 cell->contributes_to_domains = true;
2529 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2531 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2532 cell->axes[a].nest_idx = ix[a];
2533 cell->axes[a].cvs = (nest->n
2534 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2536 for (size_t i = 0; i < nest->n; i++)
2538 const struct ctables_category *cat = cats[a][i];
2540 if (i != nest->scale_idx)
2542 const struct ctables_category *subtotal = cat->subtotal;
2543 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2546 if (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
2547 cell->contributes_to_domains = false;
2550 cell->axes[a].cvs[i].category = cat;
2551 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2552 var_get_width (nest->vars[i]));
2556 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2557 cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
2558 for (size_t i = 0; i < specs->n; i++)
2559 ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
2560 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2561 cell->domains[dt] = ctables_domain_insert (t, cell, dt);
2562 hmap_insert (&t->cells, &cell->node, hash);
2567 ctables_cell_add__ (struct ctables_table *t, const struct ccase *c,
2568 size_t ix[PIVOT_N_AXES],
2569 const struct ctables_category *cats[PIVOT_N_AXES][10],
2570 double d_weight, double e_weight)
2572 struct ctables_cell *cell = ctables_cell_insert__ (t, c, ix, cats);
2573 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2575 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2576 for (size_t i = 0; i < specs->n; i++)
2577 ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
2578 case_data (c, specs->var), d_weight, e_weight);
2579 if (cell->contributes_to_domains)
2581 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2583 cell->domains[dt]->d_valid += d_weight;
2584 cell->domains[dt]->e_valid += e_weight;
2590 recurse_totals (struct ctables_table *t, const struct ccase *c,
2591 size_t ix[PIVOT_N_AXES],
2592 const struct ctables_category *cats[PIVOT_N_AXES][10],
2593 double d_weight, double e_weight,
2594 enum pivot_axis_type start_axis, size_t start_nest)
2596 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2598 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2599 for (size_t i = start_nest; i < nest->n; i++)
2601 if (i == nest->scale_idx)
2604 const struct variable *var = nest->vars[i];
2606 const struct ctables_category *total = ctables_categories_total (
2607 t->categories[var_get_dict_index (var)]);
2610 const struct ctables_category *save = cats[a][i];
2612 ctables_cell_add__ (t, c, ix, cats, d_weight, e_weight);
2613 recurse_totals (t, c, ix, cats, d_weight, e_weight, a, i + 1);
2622 recurse_subtotals (struct ctables_table *t, const struct ccase *c,
2623 size_t ix[PIVOT_N_AXES],
2624 const struct ctables_category *cats[PIVOT_N_AXES][10],
2625 double d_weight, double e_weight,
2626 enum pivot_axis_type start_axis, size_t start_nest)
2628 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2630 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2631 for (size_t i = start_nest; i < nest->n; i++)
2633 if (i == nest->scale_idx)
2636 const struct ctables_category *save = cats[a][i];
2639 cats[a][i] = save->subtotal;
2640 ctables_cell_add__ (t, c, ix, cats, d_weight, e_weight);
2641 recurse_subtotals (t, c, ix, cats, d_weight, e_weight, a, i + 1);
2650 ctables_cell_insert (struct ctables_table *t,
2651 const struct ccase *c,
2652 size_t ix[PIVOT_N_AXES],
2653 double d_weight, double e_weight)
2655 const struct ctables_category *cats[PIVOT_N_AXES][10];
2656 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2658 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2659 for (size_t i = 0; i < nest->n; i++)
2661 if (i == nest->scale_idx)
2664 const struct variable *var = nest->vars[i];
2665 const union value *value = case_data (c, var);
2667 if (var_is_numeric (var) && value->f == SYSMIS)
2670 cats[a][i] = ctables_categories_match (
2671 t->categories[var_get_dict_index (var)], value, var);
2677 ctables_cell_add__ (t, c, ix, cats, d_weight, e_weight);
2679 recurse_totals (t, c, ix, cats, d_weight, e_weight, 0, 0);
2680 recurse_subtotals (t, c, ix, cats, d_weight, e_weight, 0, 0);
2685 const struct ctables_summary_spec_set *set;
2690 merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
2692 const struct ctables_summary_spec *as = &a->set->specs[a->ofs];
2693 const struct ctables_summary_spec *bs = &b->set->specs[b->ofs];
2694 if (as->function != bs->function)
2695 return as->function > bs->function ? 1 : -1;
2696 else if (as->percentile != bs->percentile)
2697 return as->percentile < bs->percentile ? 1 : -1;
2698 return strcmp (as->label, bs->label);
2701 static struct pivot_value *
2702 ctables_category_create_label (const struct ctables_category *cat,
2703 const struct variable *var,
2704 const union value *value)
2706 return (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL
2707 ? pivot_value_new_user_text (cat->total_label, SIZE_MAX)
2708 : pivot_value_new_var_value (var, value));
2711 static struct ctables_value *
2712 ctables_value_find__ (struct ctables_table *t, const union value *value,
2713 int width, unsigned int hash)
2715 struct ctables_value *clv;
2716 HMAP_FOR_EACH_WITH_HASH (clv, struct ctables_value, node,
2717 hash, &t->clabels_values_map)
2718 if (value_equal (value, &clv->value, width))
2723 static struct ctables_value *
2724 ctables_value_find (struct ctables_table *t,
2725 const union value *value, int width)
2727 return ctables_value_find__ (t, value, width,
2728 value_hash (value, width, 0));
2732 ctables_table_output (struct ctables *ct, struct ctables_table *t)
2734 struct pivot_table *pt = pivot_table_create__ (
2736 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2737 : pivot_value_new_text (N_("Custom Tables"))),
2740 pivot_table_set_caption (
2741 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2743 pivot_table_set_caption (
2744 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2746 bool summary_dimension = (t->summary_axis != t->slabels_axis
2747 || (!t->slabels_visible
2748 && t->summary_specs.n > 1));
2749 if (summary_dimension)
2751 struct pivot_dimension *d = pivot_dimension_create (
2752 pt, t->slabels_axis, N_("Statistics"));
2753 const struct ctables_summary_spec_set *specs = &t->summary_specs;
2754 if (!t->slabels_visible)
2755 d->hide_all_labels = true;
2756 for (size_t i = 0; i < specs->n; i++)
2757 pivot_category_create_leaf (
2758 d->root, pivot_value_new_text (specs->specs[i].label));
2761 bool categories_dimension = t->clabels_example != NULL;
2762 if (categories_dimension)
2764 struct pivot_dimension *d = pivot_dimension_create (
2765 pt, t->label_axis[t->clabels_from_axis],
2766 t->clabels_from_axis == PIVOT_AXIS_ROW
2767 ? N_("Row Categories")
2768 : N_("Column Categories"));
2769 const struct variable *var = t->clabels_example;
2770 const struct ctables_categories *c = t->categories[var_get_dict_index (var)];
2771 for (size_t i = 0; i < t->n_clabels_values; i++)
2773 const struct ctables_value *value = t->clabels_values[i];
2774 const struct ctables_category *cat = ctables_categories_match (c, &value->value, var);
2775 assert (cat != NULL);
2776 pivot_category_create_leaf (d->root, ctables_category_create_label (
2777 cat, t->clabels_example, &value->value));
2781 pivot_table_set_look (pt, ct->look);
2782 struct pivot_dimension *d[PIVOT_N_AXES];
2783 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2785 static const char *names[] = {
2786 [PIVOT_AXIS_ROW] = N_("Rows"),
2787 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2788 [PIVOT_AXIS_LAYER] = N_("Layers"),
2790 d[a] = (t->axes[a] || a == t->summary_axis
2791 ? pivot_dimension_create (pt, a, names[a])
2796 assert (t->axes[a]);
2798 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2799 size_t n_sorted = 0;
2801 struct ctables_cell *cell;
2802 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2804 sorted[n_sorted++] = cell;
2805 assert (n_sorted <= t->cells.count);
2807 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2808 sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
2810 size_t max_depth = 0;
2811 for (size_t j = 0; j < t->stacks[a].n; j++)
2812 if (t->stacks[a].nests[j].n > max_depth)
2813 max_depth = t->stacks[a].nests[j].n;
2815 /* Pivot categories:
2817 - variable label for nest->vars[0], if vlabel != CTVL_NONE
2818 - category for nest->vars[0], if nest->scale_idx != 0
2819 - variable label for nest->vars[1], if vlabel != CTVL_NONE
2820 - category for nest->vars[1], if nest->scale_idx != 1
2822 - variable label for nest->vars[n - 1], if vlabel != CTVL_NONE
2823 - category for nest->vars[n - 1], if t->label_axis[a] == a && nest->scale_idx != n - 1.
2824 - summary function, if 'a == t->slabels_axis && a ==
2827 Additional dimensions:
2829 - If 'a == t->slabels_axis && a != t->summary_axis', add a summary
2831 - If 't->label_axis[b] == a' for some 'b != a', add a category
2835 struct ctables_level
2837 enum ctables_level_type
2839 CTL_VAR, /* Variable label for nest->vars[var_idx]. */
2840 CTL_CATEGORY, /* Category for nest->vars[var_idx]. */
2841 CTL_SUMMARY, /* Summary functions. */
2847 struct ctables_level *levels = xnmalloc (1 + 2 * max_depth, sizeof *levels);
2848 size_t n_levels = 0;
2850 struct pivot_category **groups = xnmalloc (1 + 2 * max_depth, sizeof *groups);
2852 for (size_t j = 0; j < n_sorted; j++)
2854 struct ctables_cell *cell = sorted[j];
2855 struct ctables_cell *prev = j > 0 ? sorted[j - 1] : NULL;
2856 const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].nest_idx];
2858 bool new_subtable = !prev || prev->axes[a].nest_idx != cell->axes[a].nest_idx;
2862 for (size_t k = 0; k < nest->n; k++)
2864 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k])];
2865 if (vlabel != CTVL_NONE)
2867 levels[n_levels++] = (struct ctables_level) {
2873 if (nest->scale_idx != k
2874 && (k != nest->n - 1 || t->label_axis[a] == a))
2876 levels[n_levels++] = (struct ctables_level) {
2877 .type = CTL_CATEGORY,
2883 if (!summary_dimension && a == t->slabels_axis)
2885 levels[n_levels++] = (struct ctables_level) {
2886 .type = CTL_SUMMARY,
2887 .var_idx = SIZE_MAX,
2892 size_t n_common = 0;
2895 for (; n_common < n_levels; n_common++)
2897 const struct ctables_level *level = &levels[n_common];
2898 if (level->type == CTL_CATEGORY)
2900 size_t var_idx = level->var_idx;
2901 const struct ctables_category *c = cell->axes[a].cvs[var_idx].category;
2902 if (prev->axes[a].cvs[var_idx].category != c)
2904 else if (c->type != CCT_SUBTOTAL
2905 && c->type != CCT_HSUBTOTAL
2906 && c->type != CCT_TOTAL
2907 && !value_equal (&prev->axes[a].cvs[var_idx].value,
2908 &cell->axes[a].cvs[var_idx].value,
2909 var_get_type (nest->vars[var_idx])))
2915 for (size_t k = n_common; k < n_levels; k++)
2917 const struct ctables_level *level = &levels[k];
2918 struct pivot_category *parent = k ? groups[k - 1] : d[a]->root;
2919 if (level->type == CTL_SUMMARY)
2921 assert (k == n_levels - 1);
2923 const struct ctables_summary_spec_set *specs = &t->summary_specs;
2924 for (size_t m = 0; m < specs->n; m++)
2926 int leaf = pivot_category_create_leaf (
2927 parent, pivot_value_new_text (specs->specs[m].label));
2934 const struct variable *var = nest->vars[level->var_idx];
2935 struct pivot_value *label;
2936 if (level->type == CTL_VAR)
2937 label = pivot_value_new_variable (var);
2938 else if (level->type == CTL_CATEGORY)
2940 const struct ctables_cell_value *cv = &cell->axes[a].cvs[level->var_idx];
2941 label = ctables_category_create_label (cv->category,
2947 if (k == n_levels - 1)
2948 prev_leaf = pivot_category_create_leaf (parent, label);
2950 groups[k] = pivot_category_create_group__ (parent, label);
2954 cell->axes[a].leaf = prev_leaf;
2960 struct ctables_cell *cell;
2961 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2966 const struct ctables_nest *specs_nest = &t->stacks[t->summary_axis].nests[cell->axes[t->summary_axis].nest_idx];
2967 const struct ctables_summary_spec_set *specs = &specs_nest->specs[cell->sv];
2968 for (size_t j = 0; j < specs->n; j++)
2971 size_t n_dindexes = 0;
2973 if (summary_dimension)
2974 dindexes[n_dindexes++] = specs->specs[j].axis_idx;
2976 if (categories_dimension)
2978 const struct ctables_nest *clabels_nest = &t->stacks[t->clabels_from_axis].nests[cell->axes[t->clabels_from_axis].nest_idx];
2979 const struct variable *var = clabels_nest->vars[clabels_nest->n - 1];
2980 const union value *value = &cell->axes[t->clabels_from_axis].cvs[clabels_nest->n - 1].value;
2981 const struct ctables_value *ctv = ctables_value_find (t, value, var_get_width (var));
2982 assert (ctv != NULL);
2983 dindexes[n_dindexes++] = ctv->leaf;
2986 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2989 int leaf = cell->axes[a].leaf;
2990 if (a == t->summary_axis && !summary_dimension)
2992 dindexes[n_dindexes++] = leaf;
2995 double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
2996 struct pivot_value *value = pivot_value_new_number (d);
2997 value->numeric.format = specs->specs[j].format;
2998 pivot_table_put (pt, dindexes, n_dindexes, value);
3002 pivot_table_submit (pt);
3006 ctables_check_label_position (struct ctables_table *t, enum pivot_axis_type a)
3008 enum pivot_axis_type label_pos = t->label_axis[a];
3012 t->clabels_from_axis = a;
3014 const char *subcommand_name = a == PIVOT_AXIS_ROW ? "ROWLABELS" : "COLLABELS";
3015 const char *pos_name = label_pos == PIVOT_AXIS_LAYER ? "LAYER" : "OPPOSITE";
3017 const struct ctables_stack *stack = &t->stacks[a];
3021 const struct ctables_nest *n0 = &stack->nests[0];
3023 const struct variable *v0 = n0->vars[n0->n - 1];
3024 struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
3025 t->clabels_example = v0;
3027 for (size_t i = 0; i < c0->n_cats; i++)
3028 if (c0->cats[i].type == CCT_FUNCTION)
3030 msg (SE, _("%s=%s is not allowed with sorting based "
3031 "on a summary function."),
3032 subcommand_name, pos_name);
3035 if (n0->n - 1 == n0->scale_idx)
3037 msg (SE, _("%s=%s requires the variables to be moved to be categorical, "
3038 "but %s is a scale variable."),
3039 subcommand_name, pos_name, var_get_name (v0));
3043 for (size_t i = 1; i < stack->n; i++)
3045 const struct ctables_nest *ni = &stack->nests[i];
3047 const struct variable *vi = ni->vars[ni->n - 1];
3048 struct ctables_categories *ci = t->categories[var_get_dict_index (vi)];
3050 if (ni->n - 1 == ni->scale_idx)
3052 msg (SE, _("%s=%s requires the variables to be moved to be "
3053 "categorical, but %s is a scale variable."),
3054 subcommand_name, pos_name, var_get_name (vi));
3057 if (var_get_width (v0) != var_get_width (vi))
3059 msg (SE, _("%s=%s requires the variables to be "
3060 "moved to have the same width, but %s has "
3061 "width %d and %s has width %d."),
3062 subcommand_name, pos_name,
3063 var_get_name (v0), var_get_width (v0),
3064 var_get_name (vi), var_get_width (vi));
3067 if (!val_labs_equal (var_get_value_labels (v0),
3068 var_get_value_labels (vi)))
3070 msg (SE, _("%s=%s requires the variables to be "
3071 "moved to have the same value labels, but %s "
3072 "and %s have different value labels."),
3073 subcommand_name, pos_name,
3074 var_get_name (v0), var_get_name (vi));
3077 if (!ctables_categories_equal (c0, ci))
3079 msg (SE, _("%s=%s requires the variables to be "
3080 "moved to have the same category "
3081 "specifications, but %s and %s have different "
3082 "category specifications."),
3083 subcommand_name, pos_name,
3084 var_get_name (v0), var_get_name (vi));
3093 ctables_prepare_table (struct ctables_table *t)
3095 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3098 t->stacks[a] = enumerate_fts (a, t->axes[a]);
3100 for (size_t j = 0; j < t->stacks[a].n; j++)
3102 struct ctables_nest *nest = &t->stacks[a].nests[j];
3103 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
3105 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
3106 nest->n_domains[dt] = 0;
3108 for (size_t k = 0; k < nest->n; k++)
3110 if (k == nest->scale_idx)
3119 if (a != PIVOT_AXIS_LAYER)
3126 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
3127 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
3128 : a == PIVOT_AXIS_ROW)
3130 if (k == nest->n - 1
3131 || (nest->scale_idx == nest->n - 1
3132 && k == nest->n - 2))
3138 if (a == PIVOT_AXIS_COLUMN)
3143 if (a == PIVOT_AXIS_ROW)
3148 nest->domains[dt][nest->n_domains[dt]++] = k;
3155 struct ctables_nest *nest = xmalloc (sizeof *nest);
3156 *nest = (struct ctables_nest) { .n = 0 };
3157 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
3160 struct ctables_stack *stack = &t->stacks[t->summary_axis];
3161 for (size_t i = 0; i < stack->n; i++)
3163 struct ctables_nest *nest = &stack->nests[i];
3164 if (!nest->specs[CSV_CELL].n)
3166 struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
3167 specs->specs = xmalloc (sizeof *specs->specs);
3170 enum ctables_summary_function function
3171 = specs->var ? CTSF_MEAN : CTSF_COUNT;
3172 struct ctables_var var = { .is_mrset = false, .var = specs->var };
3174 *specs->specs = (struct ctables_summary_spec) {
3175 .function = function,
3176 .format = ctables_summary_default_format (function, &var),
3177 .label = ctables_summary_default_label (function, 0),
3180 specs->var = nest->vars[0];
3182 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3183 &nest->specs[CSV_CELL]);
3185 else if (!nest->specs[CSV_TOTAL].n)
3186 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3187 &nest->specs[CSV_CELL]);
3190 struct ctables_summary_spec_set *merged = &t->summary_specs;
3191 struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
3193 for (size_t j = 0; j < stack->n; j++)
3195 const struct ctables_nest *nest = &stack->nests[j];
3197 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3198 items[n_left++] = (struct merge_item) { .set = &nest->specs[sv] };
3203 struct merge_item min = items[0];
3204 for (size_t j = 1; j < n_left; j++)
3205 if (merge_item_compare_3way (&items[j], &min) < 0)
3208 if (merged->n >= merged->allocated)
3209 merged->specs = x2nrealloc (merged->specs, &merged->allocated,
3210 sizeof *merged->specs);
3211 merged->specs[merged->n++] = min.set->specs[min.ofs];
3213 for (size_t j = 0; j < n_left; )
3215 if (merge_item_compare_3way (&items[j], &min) == 0)
3217 struct merge_item *item = &items[j];
3218 item->set->specs[item->ofs].axis_idx = merged->n - 1;
3219 if (++item->ofs >= item->set->n)
3221 items[j] = items[--n_left];
3230 for (size_t j = 0; j < merged->n; j++)
3231 printf ("%s\n", ctables_summary_function_name (merged->specs[j].function));
3233 for (size_t j = 0; j < stack->n; j++)
3235 const struct ctables_nest *nest = &stack->nests[j];
3236 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3238 const struct ctables_summary_spec_set *specs = &nest->specs[sv];
3239 for (size_t k = 0; k < specs->n; k++)
3240 printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
3241 specs->specs[k].axis_idx);
3247 return (ctables_check_label_position (t, PIVOT_AXIS_ROW)
3248 && ctables_check_label_position (t, PIVOT_AXIS_COLUMN));
3252 ctables_insert_clabels_values (struct ctables_table *t, const struct ccase *c,
3253 enum pivot_axis_type a)
3255 struct ctables_stack *stack = &t->stacks[a];
3256 for (size_t i = 0; i < stack->n; i++)
3258 const struct ctables_nest *nest = &stack->nests[i];
3259 const struct variable *var = nest->vars[nest->n - 1];
3260 int width = var_get_width (var);
3261 const union value *value = case_data (c, var);
3263 if (var_is_numeric (var) && value->f == SYSMIS)
3266 if (!ctables_categories_match (t->categories [var_get_dict_index (var)],
3270 unsigned int hash = value_hash (value, width, 0);
3272 struct ctables_value *clv = ctables_value_find__ (t, value, width, hash);
3275 clv = xmalloc (sizeof *clv);
3276 value_clone (&clv->value, value, width);
3277 hmap_insert (&t->clabels_values_map, &clv->node, hash);
3283 compare_clabels_values_3way (const void *a_, const void *b_, const void *width_)
3285 const struct ctables_value *const *ap = a_;
3286 const struct ctables_value *const *bp = b_;
3287 const struct ctables_value *a = *ap;
3288 const struct ctables_value *b = *bp;
3289 const int *width = width_;
3290 return value_compare_3way (&a->value, &b->value, *width);
3294 ctables_sort_clabels_values (struct ctables_table *t)
3296 int width = var_get_width (t->clabels_example);
3298 size_t n = hmap_count (&t->clabels_values_map);
3299 t->clabels_values = xnmalloc (n, sizeof *t->clabels_values);
3301 struct ctables_value *clv;
3303 HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
3304 t->clabels_values[i++] = clv;
3305 t->n_clabels_values = n;
3308 sort (t->clabels_values, n, sizeof *t->clabels_values,
3309 compare_clabels_values_3way, &width);
3311 for (size_t i = 0; i < n; i++)
3312 t->clabels_values[i]->leaf = i;
3316 add_empty_categories (struct ctables_table *t, const struct ccase *c,
3317 size_t ix[PIVOT_N_AXES],
3318 enum pivot_axis_type start_axis, size_t start_nest)
3320 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
3322 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
3323 for (size_t i = start_nest; i < nest->n; i++)
3325 if (i == nest->scale_idx)
3328 const struct ctables_category *save = cats[a][i];
3331 cats[a][i] = save->subtotal;
3332 ctables_cell_add__ (t, c, ix, cats, d_weight, e_weight);
3333 recurse_subtotals (t, c, ix, cats, d_weight, e_weight, a, i + 1);
3342 ctables_execute (struct dataset *ds, struct ctables *ct)
3344 for (size_t i = 0; i < ct->n_tables; i++)
3346 struct ctables_table *t = ct->tables[i];
3348 for (size_t ir = 0; ir < t->stacks[PIVOT_AXIS_ROW].n; ir++)
3349 for (size_t ic = 0; ic < t->stacks[PIVOT_AXIS_COLUMN].n; ic++)
3350 for (size_t il = 0; il < t->stacks[PIVOT_AXIS_LAYER].n; il++)
3352 size_t ix[PIVOT_N_AXES] = {
3353 [PIVOT_AXIS_ROW] = ir,
3354 [PIVOT_AXIS_COLUMN] = ic,
3355 [PIVOT_AXIS_LAYER] = il,
3358 add_empty_categories (t, ix, 0, 0);
3362 struct casereader *input = proc_open (ds);
3363 bool warn_on_invalid = true;
3364 for (struct ccase *c = casereader_read (input); c;
3365 case_unref (c), c = casereader_read (input))
3367 double d_weight = dict_get_case_weight (dataset_dict (ds), c,
3369 double e_weight = (ct->e_weight
3370 ? var_force_valid_weight (ct->e_weight,
3371 case_num (c, ct->e_weight),
3375 for (size_t i = 0; i < ct->n_tables; i++)
3377 struct ctables_table *t = ct->tables[i];
3379 for (size_t ir = 0; ir < t->stacks[PIVOT_AXIS_ROW].n; ir++)
3380 for (size_t ic = 0; ic < t->stacks[PIVOT_AXIS_COLUMN].n; ic++)
3381 for (size_t il = 0; il < t->stacks[PIVOT_AXIS_LAYER].n; il++)
3383 size_t ix[PIVOT_N_AXES] = {
3384 [PIVOT_AXIS_ROW] = ir,
3385 [PIVOT_AXIS_COLUMN] = ic,
3386 [PIVOT_AXIS_LAYER] = il,
3389 ctables_cell_insert (t, c, ix, d_weight, e_weight);
3392 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3393 if (t->label_axis[a] != a)
3394 ctables_insert_clabels_values (t, c, a);
3397 casereader_destroy (input);
3399 for (size_t i = 0; i < ct->n_tables; i++)
3401 struct ctables_table *t = ct->tables[i];
3403 if (t->clabels_example)
3404 ctables_sort_clabels_values (t);
3406 ctables_table_output (ct, ct->tables[i]);
3408 return proc_commit (ds);
3412 cmd_ctables (struct lexer *lexer, struct dataset *ds)
3414 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3415 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
3416 enum settings_value_show tvars = settings_get_show_variables ();
3417 for (size_t i = 0; i < n_vars; i++)
3418 vlabels[i] = (enum ctables_vlabel) tvars;
3420 struct ctables *ct = xmalloc (sizeof *ct);
3421 *ct = (struct ctables) {
3422 .look = pivot_table_look_unshare (pivot_table_look_ref (
3423 pivot_table_look_get_default ())),
3425 .hide_threshold = 5,
3427 ct->look->omit_empty = false;
3429 if (!lex_force_match (lexer, T_SLASH))
3432 while (!lex_match_id (lexer, "TABLE"))
3434 if (lex_match_id (lexer, "FORMAT"))
3436 double widths[2] = { SYSMIS, SYSMIS };
3437 double units_per_inch = 72.0;
3439 while (lex_token (lexer) != T_SLASH)
3441 if (lex_match_id (lexer, "MINCOLWIDTH"))
3443 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
3446 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
3448 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
3451 else if (lex_match_id (lexer, "UNITS"))
3453 lex_match (lexer, T_EQUALS);
3454 if (lex_match_id (lexer, "POINTS"))
3455 units_per_inch = 72.0;
3456 else if (lex_match_id (lexer, "INCHES"))
3457 units_per_inch = 1.0;
3458 else if (lex_match_id (lexer, "CM"))
3459 units_per_inch = 2.54;
3462 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
3466 else if (lex_match_id (lexer, "EMPTY"))
3471 lex_match (lexer, T_EQUALS);
3472 if (lex_match_id (lexer, "ZERO"))
3474 /* Nothing to do. */
3476 else if (lex_match_id (lexer, "BLANK"))
3477 ct->zero = xstrdup ("");
3478 else if (lex_force_string (lexer))
3480 ct->zero = ss_xstrdup (lex_tokss (lexer));
3486 else if (lex_match_id (lexer, "MISSING"))
3488 lex_match (lexer, T_EQUALS);
3489 if (!lex_force_string (lexer))
3493 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
3494 ? ss_xstrdup (lex_tokss (lexer))
3500 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
3501 "UNITS", "EMPTY", "MISSING");
3506 if (widths[0] != SYSMIS && widths[1] != SYSMIS
3507 && widths[0] > widths[1])
3509 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
3513 for (size_t i = 0; i < 2; i++)
3514 if (widths[i] != SYSMIS)
3516 int *wr = ct->look->width_ranges[TABLE_HORZ];
3517 wr[i] = widths[i] / units_per_inch * 96.0;
3522 else if (lex_match_id (lexer, "VLABELS"))
3524 if (!lex_force_match_id (lexer, "VARIABLES"))
3526 lex_match (lexer, T_EQUALS);
3528 struct variable **vars;
3530 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
3534 if (!lex_force_match_id (lexer, "DISPLAY"))
3539 lex_match (lexer, T_EQUALS);
3541 enum ctables_vlabel vlabel;
3542 if (lex_match_id (lexer, "DEFAULT"))
3543 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
3544 else if (lex_match_id (lexer, "NAME"))
3546 else if (lex_match_id (lexer, "LABEL"))
3547 vlabel = CTVL_LABEL;
3548 else if (lex_match_id (lexer, "BOTH"))
3550 else if (lex_match_id (lexer, "NONE"))
3554 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
3560 for (size_t i = 0; i < n_vars; i++)
3561 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
3564 else if (lex_match_id (lexer, "MRSETS"))
3566 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
3568 lex_match (lexer, T_EQUALS);
3569 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
3572 else if (lex_match_id (lexer, "SMISSING"))
3574 if (lex_match_id (lexer, "VARIABLE"))
3575 ct->smissing_listwise = false;
3576 else if (lex_match_id (lexer, "LISTWISE"))
3577 ct->smissing_listwise = true;
3580 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
3585 else if (lex_match_id (lexer, "WEIGHT"))
3587 if (!lex_force_match_id (lexer, "VARIABLE"))
3589 lex_match (lexer, T_EQUALS);
3590 ct->e_weight = parse_variable (lexer, dataset_dict (ds));
3594 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
3596 if (!lex_force_match_id (lexer, "COUNT"))
3598 lex_match (lexer, T_EQUALS);
3599 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
3601 ct->hide_threshold = lex_integer (lexer);
3606 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
3607 "SMISSING", "PCOMPUTE", "PPROPERTIES",
3608 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
3612 if (!lex_force_match (lexer, T_SLASH))
3616 size_t allocated_tables = 0;
3619 if (ct->n_tables >= allocated_tables)
3620 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
3621 sizeof *ct->tables);
3623 struct ctables_category *cat = xmalloc (sizeof *cat);
3624 *cat = (struct ctables_category) {
3626 .include_missing = false,
3627 .sort_ascending = true,
3630 struct ctables_categories *c = xmalloc (sizeof *c);
3631 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3632 *c = (struct ctables_categories) {
3638 struct ctables_categories **categories = xnmalloc (n_vars,
3639 sizeof *categories);
3640 for (size_t i = 0; i < n_vars; i++)
3643 struct ctables_table *t = xmalloc (sizeof *t);
3644 *t = (struct ctables_table) {
3645 .cells = HMAP_INITIALIZER (t->cells),
3646 .slabels_axis = PIVOT_AXIS_COLUMN,
3647 .slabels_visible = true,
3648 .clabels_values_map = HMAP_INITIALIZER (t->clabels_values_map),
3650 [PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW,
3651 [PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN,
3652 [PIVOT_AXIS_LAYER] = PIVOT_AXIS_LAYER,
3654 .clabels_from_axis = PIVOT_AXIS_LAYER,
3655 .categories = categories,
3656 .n_categories = n_vars,
3659 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
3660 hmap_init (&t->domains[dt]);
3661 ct->tables[ct->n_tables++] = t;
3663 lex_match (lexer, T_EQUALS);
3664 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
3666 if (lex_match (lexer, T_BY))
3668 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3669 ct, t, PIVOT_AXIS_COLUMN))
3672 if (lex_match (lexer, T_BY))
3674 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3675 ct, t, PIVOT_AXIS_LAYER))
3680 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
3681 && !t->axes[PIVOT_AXIS_LAYER])
3683 lex_error (lexer, _("At least one variable must be specified."));
3687 const struct ctables_axis *scales[PIVOT_N_AXES];
3688 size_t n_scales = 0;
3689 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3691 scales[a] = find_scale (t->axes[a]);
3697 msg (SE, _("Scale variables may appear only on one axis."));
3698 if (scales[PIVOT_AXIS_ROW])
3699 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
3700 _("This scale variable appears on the rows axis."));
3701 if (scales[PIVOT_AXIS_COLUMN])
3702 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
3703 _("This scale variable appears on the columns axis."));
3704 if (scales[PIVOT_AXIS_LAYER])
3705 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
3706 _("This scale variable appears on the layer axis."));
3710 const struct ctables_axis *summaries[PIVOT_N_AXES];
3711 size_t n_summaries = 0;
3712 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3714 summaries[a] = (scales[a]
3716 : find_categorical_summary_spec (t->axes[a]));
3720 if (n_summaries > 1)
3722 msg (SE, _("Summaries may appear only on one axis."));
3723 if (summaries[PIVOT_AXIS_ROW])
3724 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3725 _("This variable on the rows axis has a summary."));
3726 if (summaries[PIVOT_AXIS_COLUMN])
3727 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3728 _("This variable on the columns axis has a summary."));
3729 if (summaries[PIVOT_AXIS_LAYER])
3730 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3731 _("This variable on the layers axis has a summary."));
3734 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3735 if (n_summaries ? summaries[a] : t->axes[a])
3737 t->summary_axis = a;
3741 if (lex_token (lexer) == T_ENDCMD)
3743 if (!ctables_prepare_table (t))
3747 if (!lex_force_match (lexer, T_SLASH))
3750 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3752 if (lex_match_id (lexer, "SLABELS"))
3754 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3756 if (lex_match_id (lexer, "POSITION"))
3758 lex_match (lexer, T_EQUALS);
3759 if (lex_match_id (lexer, "COLUMN"))
3760 t->slabels_axis = PIVOT_AXIS_COLUMN;
3761 else if (lex_match_id (lexer, "ROW"))
3762 t->slabels_axis = PIVOT_AXIS_ROW;
3763 else if (lex_match_id (lexer, "LAYER"))
3764 t->slabels_axis = PIVOT_AXIS_LAYER;
3767 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3771 else if (lex_match_id (lexer, "VISIBLE"))
3773 lex_match (lexer, T_EQUALS);
3774 if (!parse_bool (lexer, &t->slabels_visible))
3779 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3784 else if (lex_match_id (lexer, "CLABELS"))
3786 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3788 if (lex_match_id (lexer, "AUTO"))
3790 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW;
3791 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN;
3793 else if (lex_match_id (lexer, "ROWLABELS"))
3795 lex_match (lexer, T_EQUALS);
3796 if (lex_match_id (lexer, "OPPOSITE"))
3797 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_COLUMN;
3798 else if (lex_match_id (lexer, "LAYER"))
3799 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_LAYER;
3802 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3806 else if (lex_match_id (lexer, "COLLABELS"))
3808 lex_match (lexer, T_EQUALS);
3809 if (lex_match_id (lexer, "OPPOSITE"))
3810 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_ROW;
3811 else if (lex_match_id (lexer, "LAYER"))
3812 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_LAYER;
3815 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3821 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3827 else if (lex_match_id (lexer, "CRITERIA"))
3829 if (!lex_force_match_id (lexer, "CILEVEL"))
3831 lex_match (lexer, T_EQUALS);
3833 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3835 t->cilevel = lex_number (lexer);
3838 else if (lex_match_id (lexer, "CATEGORIES"))
3840 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3843 else if (lex_match_id (lexer, "TITLES"))
3848 if (lex_match_id (lexer, "CAPTION"))
3849 textp = &t->caption;
3850 else if (lex_match_id (lexer, "CORNER"))
3852 else if (lex_match_id (lexer, "TITLE"))
3856 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
3859 lex_match (lexer, T_EQUALS);
3861 struct string s = DS_EMPTY_INITIALIZER;
3862 while (lex_is_string (lexer))
3864 if (!ds_is_empty (&s))
3865 ds_put_byte (&s, ' ');
3866 ds_put_substring (&s, lex_tokss (lexer));
3870 *textp = ds_steal_cstr (&s);
3872 while (lex_token (lexer) != T_SLASH
3873 && lex_token (lexer) != T_ENDCMD);
3875 else if (lex_match_id (lexer, "SIGTEST"))
3879 t->chisq = xmalloc (sizeof *t->chisq);
3880 *t->chisq = (struct ctables_chisq) {
3882 .include_mrsets = true,
3883 .all_visible = true,
3889 if (lex_match_id (lexer, "TYPE"))
3891 lex_match (lexer, T_EQUALS);
3892 if (!lex_force_match_id (lexer, "CHISQUARE"))
3895 else if (lex_match_id (lexer, "ALPHA"))
3897 lex_match (lexer, T_EQUALS);
3898 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
3900 t->chisq->alpha = lex_number (lexer);
3903 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3905 lex_match (lexer, T_EQUALS);
3906 if (parse_bool (lexer, &t->chisq->include_mrsets))
3909 else if (lex_match_id (lexer, "CATEGORIES"))
3911 lex_match (lexer, T_EQUALS);
3912 if (lex_match_id (lexer, "ALLVISIBLE"))
3913 t->chisq->all_visible = true;
3914 else if (lex_match_id (lexer, "SUBTOTALS"))
3915 t->chisq->all_visible = false;
3918 lex_error_expecting (lexer,
3919 "ALLVISIBLE", "SUBTOTALS");
3925 lex_error_expecting (lexer, "TYPE", "ALPHA",
3926 "INCLUDEMRSETS", "CATEGORIES");
3930 while (lex_token (lexer) != T_SLASH
3931 && lex_token (lexer) != T_ENDCMD);
3933 else if (lex_match_id (lexer, "COMPARETEST"))
3937 t->pairwise = xmalloc (sizeof *t->pairwise);
3938 *t->pairwise = (struct ctables_pairwise) {
3940 .alpha = { .05, .05 },
3941 .adjust = BONFERRONI,
3942 .include_mrsets = true,
3943 .meansvariance_allcats = true,
3944 .all_visible = true,
3953 if (lex_match_id (lexer, "TYPE"))
3955 lex_match (lexer, T_EQUALS);
3956 if (lex_match_id (lexer, "PROP"))
3957 t->pairwise->type = PROP;
3958 else if (lex_match_id (lexer, "MEAN"))
3959 t->pairwise->type = MEAN;
3962 lex_error_expecting (lexer, "PROP", "MEAN");
3966 else if (lex_match_id (lexer, "ALPHA"))
3968 lex_match (lexer, T_EQUALS);
3970 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3972 double a0 = lex_number (lexer);
3975 lex_match (lexer, T_COMMA);
3976 if (lex_is_number (lexer))
3978 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3980 double a1 = lex_number (lexer);
3983 t->pairwise->alpha[0] = MIN (a0, a1);
3984 t->pairwise->alpha[1] = MAX (a0, a1);
3987 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3989 else if (lex_match_id (lexer, "ADJUST"))
3991 lex_match (lexer, T_EQUALS);
3992 if (lex_match_id (lexer, "BONFERRONI"))
3993 t->pairwise->adjust = BONFERRONI;
3994 else if (lex_match_id (lexer, "BH"))
3995 t->pairwise->adjust = BH;
3996 else if (lex_match_id (lexer, "NONE"))
3997 t->pairwise->adjust = 0;
4000 lex_error_expecting (lexer, "BONFERRONI", "BH",
4005 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
4007 lex_match (lexer, T_EQUALS);
4008 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
4011 else if (lex_match_id (lexer, "MEANSVARIANCE"))
4013 lex_match (lexer, T_EQUALS);
4014 if (lex_match_id (lexer, "ALLCATS"))
4015 t->pairwise->meansvariance_allcats = true;
4016 else if (lex_match_id (lexer, "TESTEDCATS"))
4017 t->pairwise->meansvariance_allcats = false;
4020 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
4024 else if (lex_match_id (lexer, "CATEGORIES"))
4026 lex_match (lexer, T_EQUALS);
4027 if (lex_match_id (lexer, "ALLVISIBLE"))
4028 t->pairwise->all_visible = true;
4029 else if (lex_match_id (lexer, "SUBTOTALS"))
4030 t->pairwise->all_visible = false;
4033 lex_error_expecting (lexer, "ALLVISIBLE",
4038 else if (lex_match_id (lexer, "MERGE"))
4040 lex_match (lexer, T_EQUALS);
4041 if (!parse_bool (lexer, &t->pairwise->merge))
4044 else if (lex_match_id (lexer, "STYLE"))
4046 lex_match (lexer, T_EQUALS);
4047 if (lex_match_id (lexer, "APA"))
4048 t->pairwise->apa_style = true;
4049 else if (lex_match_id (lexer, "SIMPLE"))
4050 t->pairwise->apa_style = false;
4053 lex_error_expecting (lexer, "APA", "SIMPLE");
4057 else if (lex_match_id (lexer, "SHOWSIG"))
4059 lex_match (lexer, T_EQUALS);
4060 if (!parse_bool (lexer, &t->pairwise->show_sig))
4065 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
4066 "INCLUDEMRSETS", "MEANSVARIANCE",
4067 "CATEGORIES", "MERGE", "STYLE",
4072 while (lex_token (lexer) != T_SLASH
4073 && lex_token (lexer) != T_ENDCMD);
4077 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
4078 "CRITERIA", "CATEGORIES", "TITLES",
4079 "SIGTEST", "COMPARETEST");
4083 if (!lex_match (lexer, T_SLASH))
4087 if (t->label_axis[PIVOT_AXIS_ROW] != PIVOT_AXIS_ROW
4088 && t->label_axis[PIVOT_AXIS_COLUMN] != PIVOT_AXIS_COLUMN)
4090 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
4094 if (!ctables_prepare_table (t))
4097 while (lex_token (lexer) != T_ENDCMD);
4099 bool ok = ctables_execute (ds, ct);
4100 ctables_destroy (ct);
4101 return ok ? CMD_SUCCESS : CMD_FAILURE;
4104 ctables_destroy (ct);