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/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/mrset.h"
25 #include "data/value-labels.h"
26 #include "language/command.h"
27 #include "language/lexer/format-parser.h"
28 #include "language/lexer/lexer.h"
29 #include "language/lexer/variable-parser.h"
30 #include "libpspp/array.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/hash-functions.h"
33 #include "libpspp/hmap.h"
34 #include "libpspp/message.h"
35 #include "libpspp/string-array.h"
36 #include "math/moments.h"
37 #include "output/pivot-table.h"
39 #include "gl/minmax.h"
40 #include "gl/xalloc.h"
43 #define _(msgid) gettext (msgid)
44 #define N_(msgid) (msgid)
48 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
49 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
50 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
51 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
55 - unweighted summaries (U*)
56 - lower confidence limits (*.LCL)
57 - upper confidence limits (*.UCL)
58 - standard error (*.SE)
61 /* All variables. */ \
62 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
63 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
64 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
65 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
66 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
67 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
68 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
69 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
86 /* Scale variables, totals, and subtotals. */ \
87 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
88 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
89 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
90 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
91 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
92 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
97 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
98 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
99 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
100 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
101 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
102 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
103 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
104 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
105 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
106 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
107 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
108 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
109 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
111 /* Multiple response sets. */ \
112 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
113 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
114 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
115 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
116 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
117 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
118 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
135 enum ctables_summary_function
137 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
143 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
144 N_CTSF_FUNCTIONS = SUMMARIES
148 enum ctables_domain_type
150 /* Within a section, where stacked variables divide one section from
152 CTDT_TABLE, /* All layers of a whole section. */
153 CTDT_LAYER, /* One layer within a section. */
154 CTDT_LAYERROW, /* Row in one layer within a section. */
155 CTDT_LAYERCOL, /* Column in one layer within a section. */
157 /* Within a subtable, where a subtable pairs an innermost row variable with
158 an innermost column variable within a single layer. */
159 CTDT_SUBTABLE, /* Whole subtable. */
160 CTDT_ROW, /* Row within a subtable. */
161 CTDT_COL, /* Column within a subtable. */
165 struct ctables_domain
167 struct hmap_node node;
169 const struct ctables_cell *example;
175 enum ctables_summary_variant
184 /* In struct ctables's 'cells' hmap. Indexed by all the values in all the
185 axes (except the scalar variable, if any). */
186 struct hmap_node node;
188 /* The domains that contain this cell. */
189 struct ctables_domain *domains[N_CTDTS];
192 enum ctables_summary_variant sv;
197 struct ctables_cell_value
199 const struct ctables_category *category;
207 union ctables_summary *summaries;
212 struct pivot_table_look *look;
214 /* If this is NULL, zeros are displayed using the normal print format.
215 Otherwise, this string is displayed. */
218 /* If this is NULL, missing values are displayed using the normal print
219 format. Otherwise, this string is displayed. */
222 /* Indexed by variable dictionary index. */
223 enum ctables_vlabel *vlabels;
225 bool mrsets_count_duplicates; /* MRSETS. */
226 bool smissing_listwise; /* SMISSING. */
227 struct variable *base_weight; /* WEIGHT. */
228 int hide_threshold; /* HIDESMALLCOUNTS. */
230 struct ctables_table **tables;
234 struct ctables_postcompute
236 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
237 const char *name; /* Name, without leading &. */
239 struct ctables_postcompute_expr *expr;
242 bool hide_source_cats;
245 struct ctables_postcompute_expr
247 enum ctables_postcompute_op
255 /* XXX SUBTOTAL and HSUBTOTAL */
268 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
273 XXX what about string ranges? */
276 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
277 struct ctables_postcompute_expr *subs[2];
281 struct ctables_summary_spec_set
283 struct ctables_summary_spec *specs;
287 struct variable *var;
290 static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
291 const struct ctables_summary_spec_set *);
292 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
294 /* A nested sequence of variables, e.g. a > b > c. */
297 struct variable **vars;
300 size_t *domains[N_CTDTS];
301 size_t n_domains[N_CTDTS];
303 struct ctables_summary_spec_set specs[N_CSVS];
306 /* A stack of nestings, e.g. nest1 + nest2 + ... + nestN. */
309 struct ctables_nest *nests;
315 struct hmap_node node;
321 struct ctables_axis *axes[PIVOT_N_AXES];
322 struct ctables_stack stacks[PIVOT_N_AXES];
323 enum pivot_axis_type summary_axis;
324 struct ctables_summary_spec_set summary_specs;
326 struct hmap domains[N_CTDTS];
328 const struct variable *clabels_example;
329 struct hmap clabels_values_map;
330 union value *clabels_values;
332 enum pivot_axis_type slabels_axis;
333 bool slabels_visible;
335 /* The innermost category labels for axis 'a' appear on axis label_axis[a].
337 Most commonly, label_axis[a] == a, and in particular we always have
338 label_axis{PIVOT_AXIS_LAYER] == PIVOT_AXIS_LAYER.
340 If ROWLABELS or COLLABELS is specified, then one of
341 label_axis[PIVOT_AXIS_ROW] or label_axis[PIVOT_AXIS_COLUMN] can be the
342 opposite axis or PIVOT_AXIS_LAYER. Only one of them will differ.
344 enum pivot_axis_type label_axis[PIVOT_N_AXES];
346 /* Indexed by variable dictionary index. */
347 struct ctables_categories **categories;
356 struct ctables_chisq *chisq;
357 struct ctables_pairwise *pairwise;
365 struct variable *var;
366 const struct mrset *mrset;
370 static const struct fmt_spec *
371 ctables_var_get_print_format (const struct ctables_var *var)
373 return (var->is_mrset
374 ? var_get_print_format (var->mrset->vars[0])
375 : var_get_print_format (var->var));
379 ctables_var_name (const struct ctables_var *var)
381 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
384 struct ctables_categories
387 struct ctables_category *cats;
392 struct ctables_category
394 enum ctables_category_type
412 struct ctables_category *subtotal;
416 double number; /* CCT_NUMBER. */
417 char *string; /* CCT_STRING. */
418 double range[2]; /* CCT_RANGE. */
419 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
421 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
424 bool include_missing;
428 enum ctables_summary_function sort_function;
429 struct variable *sort_var;
436 ctables_category_uninit (struct ctables_category *cat)
456 free (cat->total_label);
467 ctables_category_equal (const struct ctables_category *a,
468 const struct ctables_category *b)
470 if (a->type != b->type)
476 return a->number == b->number;
479 return strcmp (a->string, b->string);
482 return a->range[0] == b->range[0] && a->range[1] == b->range[1];
491 return !strcmp (a->total_label, b->total_label);
496 return (a->include_missing == b->include_missing
497 && a->sort_ascending == b->sort_ascending
498 && a->sort_function == b->sort_function
499 && a->sort_var == b->sort_var
500 && a->percentile == b->percentile);
507 ctables_categories_unref (struct ctables_categories *c)
512 assert (c->n_refs > 0);
516 for (size_t i = 0; i < c->n_cats; i++)
517 ctables_category_uninit (&c->cats[i]);
523 ctables_categories_equal (const struct ctables_categories *a,
524 const struct ctables_categories *b)
526 if (a->n_cats != b->n_cats || a->show_empty != b->show_empty)
529 for (size_t i = 0; i < a->n_cats; i++)
530 if (!ctables_category_equal (&a->cats[i], &b->cats[i]))
536 /* Chi-square test (SIGTEST). */
544 /* Pairwise comparison test (COMPARETEST). */
545 struct ctables_pairwise
547 enum { PROP, MEAN } type;
550 bool meansvariance_allcats;
552 enum { BONFERRONI = 1, BH } adjust;
576 struct ctables_var var;
578 struct ctables_summary_spec_set specs[N_CSVS];
582 struct ctables_axis *subs[2];
585 struct msg_location *loc;
588 static void ctables_axis_destroy (struct ctables_axis *);
597 enum ctables_function_availability
599 CTFA_ALL, /* Any variables. */
600 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
601 CTFA_MRSETS, /* Only multiple-response sets */
604 struct ctables_summary_spec
606 enum ctables_summary_function function;
607 double percentile; /* CTSF_PTILE only. */
609 struct fmt_spec format; /* XXX extra CTABLES formats */
614 ctables_summary_spec_clone (struct ctables_summary_spec *dst,
615 const struct ctables_summary_spec *src)
618 dst->label = xstrdup (src->label);
622 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
629 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
630 const struct ctables_summary_spec_set *src)
632 struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
633 for (size_t i = 0; i < src->n; i++)
634 ctables_summary_spec_clone (&specs[i], &src->specs[i]);
636 *dst = (struct ctables_summary_spec_set) {
645 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
647 for (size_t i = 0; i < set->n; i++)
648 ctables_summary_spec_uninit (&set->specs[i]);
653 parse_col_width (struct lexer *lexer, const char *name, double *width)
655 lex_match (lexer, T_EQUALS);
656 if (lex_match_id (lexer, "DEFAULT"))
658 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
660 *width = lex_number (lexer);
670 parse_bool (struct lexer *lexer, bool *b)
672 if (lex_match_id (lexer, "NO"))
674 else if (lex_match_id (lexer, "YES"))
678 lex_error_expecting (lexer, "YES", "NO");
684 static enum ctables_function_availability
685 ctables_function_availability (enum ctables_summary_function f)
687 static enum ctables_function_availability availability[] = {
688 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
693 return availability[f];
697 parse_ctables_summary_function (struct lexer *lexer,
698 enum ctables_summary_function *f)
702 enum ctables_summary_function function;
703 struct substring name;
705 static struct pair names[] = {
706 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
707 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
710 /* The .COUNT suffix may be omitted. */
711 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
712 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
713 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
714 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
715 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
716 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
717 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
721 if (!lex_force_id (lexer))
724 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
725 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
727 *f = names[i].function;
732 lex_error (lexer, _("Expecting summary function name."));
737 ctables_axis_destroy (struct ctables_axis *axis)
745 for (size_t i = 0; i < N_CSVS; i++)
746 ctables_summary_spec_set_uninit (&axis->specs[i]);
751 ctables_axis_destroy (axis->subs[0]);
752 ctables_axis_destroy (axis->subs[1]);
755 msg_location_destroy (axis->loc);
759 static struct ctables_axis *
760 ctables_axis_new_nonterminal (enum ctables_axis_op op,
761 struct ctables_axis *sub0,
762 struct ctables_axis *sub1,
763 struct lexer *lexer, int start_ofs)
765 struct ctables_axis *axis = xmalloc (sizeof *axis);
766 *axis = (struct ctables_axis) {
768 .subs = { sub0, sub1 },
769 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
774 struct ctables_axis_parse_ctx
777 struct dictionary *dict;
779 struct ctables_table *t;
782 static struct fmt_spec
783 ctables_summary_default_format (enum ctables_summary_function function,
784 const struct ctables_var *var)
786 static const enum ctables_format default_formats[] = {
787 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
791 switch (default_formats[function])
794 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
797 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
800 return *ctables_var_get_print_format (var);
808 ctables_summary_default_label (enum ctables_summary_function function,
811 static const char *default_labels[] = {
812 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
817 return (function == CTSF_PTILE
818 ? xasprintf (_("Percentile %.2f"), percentile)
819 : xstrdup (gettext (default_labels[function])));
823 ctables_summary_function_name (enum ctables_summary_function function)
825 static const char *names[] = {
826 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
830 return names[function];
834 add_summary_spec (struct ctables_axis *axis,
835 enum ctables_summary_function function, double percentile,
836 const char *label, const struct fmt_spec *format,
837 const struct msg_location *loc, enum ctables_summary_variant sv)
839 if (axis->op == CTAO_VAR)
841 const char *function_name = ctables_summary_function_name (function);
842 const char *var_name = ctables_var_name (&axis->var);
843 switch (ctables_function_availability (function))
846 if (!axis->var.is_mrset)
848 msg_at (SE, loc, _("Summary function %s applies only to multiple "
849 "response sets."), function_name);
850 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
860 _("Summary function %s applies only to scale variables."),
862 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
872 struct ctables_summary_spec_set *set = &axis->specs[sv];
873 if (set->n >= set->allocated)
874 set->specs = x2nrealloc (set->specs, &set->allocated,
877 struct ctables_summary_spec *dst = &set->specs[set->n++];
878 *dst = (struct ctables_summary_spec) {
879 .function = function,
880 .percentile = percentile,
881 .label = xstrdup (label),
882 .format = (format ? *format
883 : ctables_summary_default_format (function, &axis->var)),
889 for (size_t i = 0; i < 2; i++)
890 if (!add_summary_spec (axis->subs[i], function, percentile, label,
897 static struct ctables_axis *ctables_axis_parse_stack (
898 struct ctables_axis_parse_ctx *);
901 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
902 struct ctables_var *var)
904 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
906 *var = (struct ctables_var) {
908 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
912 lex_error (lexer, _("'%s' does not name a multiple-response set "
913 "in the active file dictionary."),
914 lex_tokcstr (lexer));
922 *var = (struct ctables_var) {
924 .var = parse_variable (lexer, dict),
926 return var->var != NULL;
930 static struct ctables_axis *
931 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
933 if (lex_match (ctx->lexer, T_LPAREN))
935 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
936 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
938 ctables_axis_destroy (sub);
944 if (!lex_force_id (ctx->lexer))
947 int start_ofs = lex_ofs (ctx->lexer);
948 struct ctables_var var;
949 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
952 struct ctables_axis *axis = xmalloc (sizeof *axis);
953 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
955 /* XXX should figure out default measures by reading data */
956 axis->scale = (var.is_mrset ? false
957 : lex_match_phrase (ctx->lexer, "[S]") ? true
958 : lex_match_phrase (ctx->lexer, "[C]") ? false
959 : var_get_measure (var.var) == MEASURE_SCALE);
960 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
961 lex_ofs (ctx->lexer) - 1);
966 has_digit (const char *s)
968 return s[strcspn (s, "0123456789")] != '\0';
971 static struct ctables_axis *
972 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
974 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
975 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
978 enum ctables_summary_variant sv = CSV_CELL;
981 int start_ofs = lex_ofs (ctx->lexer);
983 /* Parse function. */
984 enum ctables_summary_function function;
985 if (!parse_ctables_summary_function (ctx->lexer, &function))
988 /* Parse percentile. */
989 double percentile = 0;
990 if (function == CTSF_PTILE)
992 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
994 percentile = lex_number (ctx->lexer);
995 lex_get (ctx->lexer);
1000 if (lex_is_string (ctx->lexer))
1002 label = ss_xstrdup (lex_tokss (ctx->lexer));
1003 lex_get (ctx->lexer);
1006 label = ctables_summary_default_label (function, percentile);
1009 struct fmt_spec format;
1010 const struct fmt_spec *formatp;
1011 if (lex_token (ctx->lexer) == T_ID
1012 && has_digit (lex_tokcstr (ctx->lexer)))
1014 if (!parse_format_specifier (ctx->lexer, &format)
1015 || !fmt_check_output (&format)
1016 || !fmt_check_type_compat (&format, VAL_NUMERIC))
1026 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
1027 lex_ofs (ctx->lexer) - 1);
1028 add_summary_spec (sub, function, percentile, label, formatp, loc, sv);
1030 msg_location_destroy (loc);
1032 lex_match (ctx->lexer, T_COMMA);
1033 if (sv == CSV_CELL && lex_match_id (ctx->lexer, "TOTALS"))
1035 if (!lex_force_match (ctx->lexer, T_LBRACK))
1039 else if (lex_match (ctx->lexer, T_RBRACK))
1041 if (sv == CSV_TOTAL && !lex_force_match (ctx->lexer, T_RBRACK))
1048 ctables_axis_destroy (sub);
1052 static const struct ctables_axis *
1053 find_scale (const struct ctables_axis *axis)
1057 else if (axis->op == CTAO_VAR)
1061 assert (!axis->var.is_mrset);
1069 for (size_t i = 0; i < 2; i++)
1071 const struct ctables_axis *scale = find_scale (axis->subs[i]);
1079 static const struct ctables_axis *
1080 find_categorical_summary_spec (const struct ctables_axis *axis)
1084 else if (axis->op == CTAO_VAR)
1085 return !axis->scale && axis->specs[CSV_CELL].n ? axis : NULL;
1088 for (size_t i = 0; i < 2; i++)
1090 const struct ctables_axis *sum
1091 = find_categorical_summary_spec (axis->subs[i]);
1099 static struct ctables_axis *
1100 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1102 int start_ofs = lex_ofs (ctx->lexer);
1103 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1107 while (lex_match (ctx->lexer, T_GT))
1109 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1113 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1114 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1116 const struct ctables_axis *outer_scale = find_scale (lhs);
1117 const struct ctables_axis *inner_scale = find_scale (rhs);
1118 if (outer_scale && inner_scale)
1120 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1121 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1122 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1123 ctables_axis_destroy (nest);
1127 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1130 msg_at (SE, nest->loc,
1131 _("Summaries may only be requested for categorical variables "
1132 "at the innermost nesting level."));
1133 msg_at (SN, outer_sum->loc,
1134 _("This outer categorical variable has a summary."));
1135 ctables_axis_destroy (nest);
1145 static struct ctables_axis *
1146 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1148 int start_ofs = lex_ofs (ctx->lexer);
1149 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1153 while (lex_match (ctx->lexer, T_PLUS))
1155 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1159 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1160 ctx->lexer, start_ofs);
1167 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1168 struct ctables *ct, struct ctables_table *t,
1169 enum pivot_axis_type a)
1171 if (lex_token (lexer) == T_BY
1172 || lex_token (lexer) == T_SLASH
1173 || lex_token (lexer) == T_ENDCMD)
1176 struct ctables_axis_parse_ctx ctx = {
1182 t->axes[a] = ctables_axis_parse_stack (&ctx);
1183 return t->axes[a] != NULL;
1187 ctables_chisq_destroy (struct ctables_chisq *chisq)
1193 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1199 ctables_table_destroy (struct ctables_table *t)
1204 for (size_t i = 0; i < t->n_categories; i++)
1205 ctables_categories_unref (t->categories[i]);
1206 free (t->categories);
1208 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1209 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1210 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1214 ctables_chisq_destroy (t->chisq);
1215 ctables_pairwise_destroy (t->pairwise);
1220 ctables_destroy (struct ctables *ct)
1225 pivot_table_look_unref (ct->look);
1229 for (size_t i = 0; i < ct->n_tables; i++)
1230 ctables_table_destroy (ct->tables[i]);
1235 static struct ctables_category
1236 cct_range (double low, double high)
1238 return (struct ctables_category) {
1240 .range = { low, high }
1245 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1246 struct ctables_table *t)
1248 if (!lex_match_id (lexer, "VARIABLES"))
1250 lex_match (lexer, T_EQUALS);
1252 struct variable **vars;
1254 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1257 struct ctables_categories *c = xmalloc (sizeof *c);
1258 *c = (struct ctables_categories) { .n_refs = n_vars };
1259 for (size_t i = 0; i < n_vars; i++)
1261 struct ctables_categories **cp
1262 = &t->categories[var_get_dict_index (vars[i])];
1263 ctables_categories_unref (*cp);
1268 size_t allocated_cats = 0;
1269 if (lex_match (lexer, T_LBRACK))
1273 if (c->n_cats >= allocated_cats)
1274 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1276 struct ctables_category *cat = &c->cats[c->n_cats];
1277 if (lex_match_id (lexer, "OTHERNM"))
1278 cat->type = CCT_OTHERNM;
1279 else if (lex_match_id (lexer, "MISSING"))
1280 cat->type = CCT_MISSING;
1281 else if (lex_match_id (lexer, "SUBTOTAL"))
1282 *cat = (struct ctables_category)
1283 { .type = CCT_SUBTOTAL, .total_label = NULL };
1284 else if (lex_match_id (lexer, "HSUBTOTAL"))
1285 *cat = (struct ctables_category)
1286 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1287 else if (lex_match_id (lexer, "LO"))
1289 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1291 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1294 else if (lex_is_number (lexer))
1296 double number = lex_number (lexer);
1298 if (lex_match_id (lexer, "THRU"))
1300 cat->type = CCT_RANGE;
1301 cat->range[0] = number;
1302 if (lex_match_id (lexer, "HI"))
1303 *cat = cct_range (number, DBL_MAX);
1306 if (!lex_force_num (lexer))
1308 *cat = cct_range (number, lex_number (lexer));
1313 *cat = (struct ctables_category) {
1318 else if (lex_is_string (lexer))
1320 *cat = (struct ctables_category) {
1322 .string = ss_xstrdup (lex_tokss (lexer)),
1328 lex_error (lexer, NULL);
1332 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1334 if (lex_match (lexer, T_EQUALS))
1336 if (!lex_force_string (lexer))
1339 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1343 cat->total_label = xstrdup (_("Subtotal"));
1347 lex_match (lexer, T_COMMA);
1349 while (!lex_match (lexer, T_RBRACK));
1352 struct ctables_category cat = {
1354 .include_missing = false,
1355 .sort_ascending = true,
1357 bool show_totals = false;
1358 char *total_label = NULL;
1359 bool totals_before = false;
1360 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1362 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1364 lex_match (lexer, T_EQUALS);
1365 if (lex_match_id (lexer, "A"))
1366 cat.sort_ascending = true;
1367 else if (lex_match_id (lexer, "D"))
1368 cat.sort_ascending = false;
1371 lex_error_expecting (lexer, "A", "D");
1375 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1377 lex_match (lexer, T_EQUALS);
1378 if (lex_match_id (lexer, "VALUE"))
1379 cat.type = CCT_VALUE;
1380 else if (lex_match_id (lexer, "LABEL"))
1381 cat.type = CCT_LABEL;
1384 cat.type = CCT_FUNCTION;
1385 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1388 if (lex_match (lexer, T_LPAREN))
1390 cat.sort_var = parse_variable (lexer, dict);
1394 if (cat.sort_function == CTSF_PTILE)
1396 lex_match (lexer, T_COMMA);
1397 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1399 cat.percentile = lex_number (lexer);
1403 if (!lex_force_match (lexer, T_RPAREN))
1406 else if (ctables_function_availability (cat.sort_function)
1409 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1414 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1416 lex_match (lexer, T_EQUALS);
1417 if (lex_match_id (lexer, "INCLUDE"))
1418 cat.include_missing = true;
1419 else if (lex_match_id (lexer, "EXCLUDE"))
1420 cat.include_missing = false;
1423 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1427 else if (lex_match_id (lexer, "TOTAL"))
1429 lex_match (lexer, T_EQUALS);
1430 if (!parse_bool (lexer, &show_totals))
1433 else if (lex_match_id (lexer, "LABEL"))
1435 lex_match (lexer, T_EQUALS);
1436 if (!lex_force_string (lexer))
1439 total_label = ss_xstrdup (lex_tokss (lexer));
1442 else if (lex_match_id (lexer, "POSITION"))
1444 lex_match (lexer, T_EQUALS);
1445 if (lex_match_id (lexer, "BEFORE"))
1446 totals_before = true;
1447 else if (lex_match_id (lexer, "AFTER"))
1448 totals_before = false;
1451 lex_error_expecting (lexer, "BEFORE", "AFTER");
1455 else if (lex_match_id (lexer, "EMPTY"))
1457 lex_match (lexer, T_EQUALS);
1458 if (lex_match_id (lexer, "INCLUDE"))
1459 c->show_empty = true;
1460 else if (lex_match_id (lexer, "EXCLUDE"))
1461 c->show_empty = false;
1464 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1471 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1472 "TOTAL", "LABEL", "POSITION", "EMPTY");
1474 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1481 if (c->n_cats >= allocated_cats)
1482 c->cats = x2nrealloc (c->cats, &allocated_cats,
1484 c->cats[c->n_cats++] = cat;
1489 if (c->n_cats >= allocated_cats)
1490 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1492 struct ctables_category *totals;
1495 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1496 totals = &c->cats[0];
1499 totals = &c->cats[c->n_cats];
1502 *totals = (struct ctables_category) {
1504 .total_label = total_label ? total_label : xstrdup (_("Total")),
1508 struct ctables_category *subtotal = NULL;
1509 for (size_t i = totals_before ? 0 : c->n_cats;
1510 totals_before ? i < c->n_cats : i-- > 0;
1511 totals_before ? i++ : 0)
1513 struct ctables_category *cat = &c->cats[i];
1521 cat->subtotal = subtotal;
1541 ctables_nest_uninit (struct ctables_nest *nest)
1548 ctables_stack_uninit (struct ctables_stack *stack)
1552 for (size_t i = 0; i < stack->n; i++)
1553 ctables_nest_uninit (&stack->nests[i]);
1554 free (stack->nests);
1558 static struct ctables_stack
1559 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1566 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1567 for (size_t i = 0; i < s0.n; i++)
1568 for (size_t j = 0; j < s1.n; j++)
1570 const struct ctables_nest *a = &s0.nests[i];
1571 const struct ctables_nest *b = &s1.nests[j];
1573 size_t allocate = a->n + b->n;
1574 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1575 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1577 for (size_t k = 0; k < a->n; k++)
1578 vars[n++] = a->vars[k];
1579 for (size_t k = 0; k < b->n; k++)
1580 vars[n++] = b->vars[k];
1581 assert (n == allocate);
1583 const struct ctables_nest *summary_src;
1584 if (!a->specs[CSV_CELL].var)
1586 else if (!b->specs[CSV_CELL].var)
1591 struct ctables_nest *new = &stack.nests[stack.n++];
1592 *new = (struct ctables_nest) {
1594 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1595 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1599 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1600 ctables_summary_spec_set_clone (&new->specs[sv], &summary_src->specs[sv]);
1602 ctables_stack_uninit (&s0);
1603 ctables_stack_uninit (&s1);
1607 static struct ctables_stack
1608 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1610 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1611 for (size_t i = 0; i < s0.n; i++)
1612 stack.nests[stack.n++] = s0.nests[i];
1613 for (size_t i = 0; i < s1.n; i++)
1614 stack.nests[stack.n++] = s1.nests[i];
1615 assert (stack.n == s0.n + s1.n);
1621 static struct ctables_stack
1622 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1625 return (struct ctables_stack) { .n = 0 };
1630 assert (!a->var.is_mrset);
1632 struct variable **vars = xmalloc (sizeof *vars);
1635 struct ctables_nest *nest = xmalloc (sizeof *nest);
1636 *nest = (struct ctables_nest) {
1639 .scale_idx = a->scale ? 0 : SIZE_MAX,
1641 if (a->specs[CSV_CELL].n || a->scale)
1642 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1644 ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
1645 nest->specs[sv].var = a->var.var;
1647 return (struct ctables_stack) { .nests = nest, .n = 1 };
1650 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1651 enumerate_fts (axis_type, a->subs[1]));
1654 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1655 enumerate_fts (axis_type, a->subs[1]));
1661 union ctables_summary
1663 /* COUNT, VALIDN, TOTALN. */
1670 /* MINIMUM, MAXIMUM, RANGE. */
1677 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1678 struct moments1 *moments;
1680 /* XXX percentiles, median, mode, multiple response */
1684 ctables_summary_init (union ctables_summary *s,
1685 const struct ctables_summary_spec *ss)
1687 switch (ss->function)
1691 case CTSF_ROWPCT_COUNT:
1692 case CTSF_COLPCT_COUNT:
1693 case CTSF_TABLEPCT_COUNT:
1694 case CTSF_SUBTABLEPCT_COUNT:
1695 case CTSF_LAYERPCT_COUNT:
1696 case CTSF_LAYERROWPCT_COUNT:
1697 case CTSF_LAYERCOLPCT_COUNT:
1698 case CTSF_ROWPCT_VALIDN:
1699 case CTSF_COLPCT_VALIDN:
1700 case CTSF_TABLEPCT_VALIDN:
1701 case CTSF_SUBTABLEPCT_VALIDN:
1702 case CTSF_LAYERPCT_VALIDN:
1703 case CTSF_LAYERROWPCT_VALIDN:
1704 case CTSF_LAYERCOLPCT_VALIDN:
1705 case CTSF_ROWPCT_TOTALN:
1706 case CTSF_COLPCT_TOTALN:
1707 case CTSF_TABLEPCT_TOTALN:
1708 case CTSF_SUBTABLEPCT_TOTALN:
1709 case CTSF_LAYERPCT_TOTALN:
1710 case CTSF_LAYERROWPCT_TOTALN:
1711 case CTSF_LAYERCOLPCT_TOTALN:
1716 s->missing = s->valid = 0;
1722 s->min = s->max = SYSMIS;
1730 case CTSF_ROWPCT_SUM:
1731 case CTSF_COLPCT_SUM:
1732 case CTSF_TABLEPCT_SUM:
1733 case CTSF_SUBTABLEPCT_SUM:
1734 case CTSF_LAYERPCT_SUM:
1735 case CTSF_LAYERROWPCT_SUM:
1736 case CTSF_LAYERCOLPCT_SUM:
1737 s->moments = moments1_create (MOMENT_VARIANCE);
1746 case CTSF_RESPONSES:
1747 case CTSF_ROWPCT_RESPONSES:
1748 case CTSF_COLPCT_RESPONSES:
1749 case CTSF_TABLEPCT_RESPONSES:
1750 case CTSF_SUBTABLEPCT_RESPONSES:
1751 case CTSF_LAYERPCT_RESPONSES:
1752 case CTSF_LAYERROWPCT_RESPONSES:
1753 case CTSF_LAYERCOLPCT_RESPONSES:
1754 case CTSF_ROWPCT_RESPONSES_COUNT:
1755 case CTSF_COLPCT_RESPONSES_COUNT:
1756 case CTSF_TABLEPCT_RESPONSES_COUNT:
1757 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1758 case CTSF_LAYERPCT_RESPONSES_COUNT:
1759 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1760 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1761 case CTSF_ROWPCT_COUNT_RESPONSES:
1762 case CTSF_COLPCT_COUNT_RESPONSES:
1763 case CTSF_TABLEPCT_COUNT_RESPONSES:
1764 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1765 case CTSF_LAYERPCT_COUNT_RESPONSES:
1766 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1767 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1773 ctables_summary_uninit (union ctables_summary *s,
1774 const struct ctables_summary_spec *ss)
1776 switch (ss->function)
1780 case CTSF_ROWPCT_COUNT:
1781 case CTSF_COLPCT_COUNT:
1782 case CTSF_TABLEPCT_COUNT:
1783 case CTSF_SUBTABLEPCT_COUNT:
1784 case CTSF_LAYERPCT_COUNT:
1785 case CTSF_LAYERROWPCT_COUNT:
1786 case CTSF_LAYERCOLPCT_COUNT:
1787 case CTSF_ROWPCT_VALIDN:
1788 case CTSF_COLPCT_VALIDN:
1789 case CTSF_TABLEPCT_VALIDN:
1790 case CTSF_SUBTABLEPCT_VALIDN:
1791 case CTSF_LAYERPCT_VALIDN:
1792 case CTSF_LAYERROWPCT_VALIDN:
1793 case CTSF_LAYERCOLPCT_VALIDN:
1794 case CTSF_ROWPCT_TOTALN:
1795 case CTSF_COLPCT_TOTALN:
1796 case CTSF_TABLEPCT_TOTALN:
1797 case CTSF_SUBTABLEPCT_TOTALN:
1798 case CTSF_LAYERPCT_TOTALN:
1799 case CTSF_LAYERROWPCT_TOTALN:
1800 case CTSF_LAYERCOLPCT_TOTALN:
1817 case CTSF_ROWPCT_SUM:
1818 case CTSF_COLPCT_SUM:
1819 case CTSF_TABLEPCT_SUM:
1820 case CTSF_SUBTABLEPCT_SUM:
1821 case CTSF_LAYERPCT_SUM:
1822 case CTSF_LAYERROWPCT_SUM:
1823 case CTSF_LAYERCOLPCT_SUM:
1824 moments1_destroy (s->moments);
1833 case CTSF_RESPONSES:
1834 case CTSF_ROWPCT_RESPONSES:
1835 case CTSF_COLPCT_RESPONSES:
1836 case CTSF_TABLEPCT_RESPONSES:
1837 case CTSF_SUBTABLEPCT_RESPONSES:
1838 case CTSF_LAYERPCT_RESPONSES:
1839 case CTSF_LAYERROWPCT_RESPONSES:
1840 case CTSF_LAYERCOLPCT_RESPONSES:
1841 case CTSF_ROWPCT_RESPONSES_COUNT:
1842 case CTSF_COLPCT_RESPONSES_COUNT:
1843 case CTSF_TABLEPCT_RESPONSES_COUNT:
1844 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1845 case CTSF_LAYERPCT_RESPONSES_COUNT:
1846 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1847 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1848 case CTSF_ROWPCT_COUNT_RESPONSES:
1849 case CTSF_COLPCT_COUNT_RESPONSES:
1850 case CTSF_TABLEPCT_COUNT_RESPONSES:
1851 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1852 case CTSF_LAYERPCT_COUNT_RESPONSES:
1853 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1854 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1860 ctables_summary_add (union ctables_summary *s,
1861 const struct ctables_summary_spec *ss,
1862 const struct variable *var, const union value *value,
1865 switch (ss->function)
1869 case CTSF_ROWPCT_COUNT:
1870 case CTSF_COLPCT_COUNT:
1871 case CTSF_TABLEPCT_COUNT:
1872 case CTSF_SUBTABLEPCT_COUNT:
1873 case CTSF_LAYERPCT_COUNT:
1874 case CTSF_LAYERROWPCT_COUNT:
1875 case CTSF_LAYERCOLPCT_COUNT:
1876 case CTSF_ROWPCT_VALIDN:
1877 case CTSF_COLPCT_VALIDN:
1878 case CTSF_TABLEPCT_VALIDN:
1879 case CTSF_SUBTABLEPCT_VALIDN:
1880 case CTSF_LAYERPCT_VALIDN:
1881 case CTSF_LAYERROWPCT_VALIDN:
1882 case CTSF_LAYERCOLPCT_VALIDN:
1883 case CTSF_ROWPCT_TOTALN:
1884 case CTSF_COLPCT_TOTALN:
1885 case CTSF_TABLEPCT_TOTALN:
1886 case CTSF_SUBTABLEPCT_TOTALN:
1887 case CTSF_LAYERPCT_TOTALN:
1888 case CTSF_LAYERROWPCT_TOTALN:
1889 case CTSF_LAYERCOLPCT_TOTALN:
1894 if (var_is_value_missing (var, value))
1895 s->missing += weight;
1903 if (!var_is_value_missing (var, value))
1905 assert (!var_is_alpha (var)); /* XXX? */
1906 if (s->min == SYSMIS || value->f < s->min)
1908 if (s->max == SYSMIS || value->f > s->max)
1918 case CTSF_ROWPCT_SUM:
1919 case CTSF_COLPCT_SUM:
1920 case CTSF_TABLEPCT_SUM:
1921 case CTSF_SUBTABLEPCT_SUM:
1922 case CTSF_LAYERPCT_SUM:
1923 case CTSF_LAYERROWPCT_SUM:
1924 case CTSF_LAYERCOLPCT_SUM:
1925 moments1_add (s->moments, value->f, weight);
1934 case CTSF_RESPONSES:
1935 case CTSF_ROWPCT_RESPONSES:
1936 case CTSF_COLPCT_RESPONSES:
1937 case CTSF_TABLEPCT_RESPONSES:
1938 case CTSF_SUBTABLEPCT_RESPONSES:
1939 case CTSF_LAYERPCT_RESPONSES:
1940 case CTSF_LAYERROWPCT_RESPONSES:
1941 case CTSF_LAYERCOLPCT_RESPONSES:
1942 case CTSF_ROWPCT_RESPONSES_COUNT:
1943 case CTSF_COLPCT_RESPONSES_COUNT:
1944 case CTSF_TABLEPCT_RESPONSES_COUNT:
1945 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1946 case CTSF_LAYERPCT_RESPONSES_COUNT:
1947 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1948 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1949 case CTSF_ROWPCT_COUNT_RESPONSES:
1950 case CTSF_COLPCT_COUNT_RESPONSES:
1951 case CTSF_TABLEPCT_COUNT_RESPONSES:
1952 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1953 case CTSF_LAYERPCT_COUNT_RESPONSES:
1954 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1955 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1961 ctables_summary_value (const struct ctables_cell *cell,
1962 union ctables_summary *s,
1963 const struct ctables_summary_spec *ss)
1965 switch (ss->function)
1971 case CTSF_SUBTABLEPCT_COUNT:
1972 return cell->domains[CTDT_SUBTABLE]->valid ? s->valid / cell->domains[CTDT_SUBTABLE]->valid * 100 : SYSMIS;
1974 case CTSF_ROWPCT_COUNT:
1975 return cell->domains[CTDT_ROW]->valid ? s->valid / cell->domains[CTDT_ROW]->valid * 100 : SYSMIS;
1977 case CTSF_COLPCT_COUNT:
1978 return cell->domains[CTDT_COL]->valid ? s->valid / cell->domains[CTDT_COL]->valid * 100 : SYSMIS;
1980 case CTSF_TABLEPCT_COUNT:
1981 return cell->domains[CTDT_TABLE]->valid ? s->valid / cell->domains[CTDT_TABLE]->valid * 100 : SYSMIS;
1983 case CTSF_LAYERPCT_COUNT:
1984 return cell->domains[CTDT_LAYER]->valid ? s->valid / cell->domains[CTDT_LAYER]->valid * 100 : SYSMIS;
1986 case CTSF_LAYERROWPCT_COUNT:
1987 return cell->domains[CTDT_LAYERROW]->valid ? s->valid / cell->domains[CTDT_LAYERROW]->valid * 100 : SYSMIS;
1989 case CTSF_LAYERCOLPCT_COUNT:
1990 return cell->domains[CTDT_LAYERCOL]->valid ? s->valid / cell->domains[CTDT_LAYERCOL]->valid * 100 : SYSMIS;
1992 case CTSF_ROWPCT_VALIDN:
1993 case CTSF_COLPCT_VALIDN:
1994 case CTSF_TABLEPCT_VALIDN:
1995 case CTSF_SUBTABLEPCT_VALIDN:
1996 case CTSF_LAYERPCT_VALIDN:
1997 case CTSF_LAYERROWPCT_VALIDN:
1998 case CTSF_LAYERCOLPCT_VALIDN:
1999 case CTSF_ROWPCT_TOTALN:
2000 case CTSF_COLPCT_TOTALN:
2001 case CTSF_TABLEPCT_TOTALN:
2002 case CTSF_SUBTABLEPCT_TOTALN:
2003 case CTSF_LAYERPCT_TOTALN:
2004 case CTSF_LAYERROWPCT_TOTALN:
2005 case CTSF_LAYERCOLPCT_TOTALN:
2010 return s->valid + s->missing;
2023 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
2028 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
2034 double weight, variance;
2035 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
2036 return calc_semean (variance, weight);
2042 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2043 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
2048 double weight, mean;
2049 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
2050 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
2056 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2060 case CTSF_ROWPCT_SUM:
2061 case CTSF_COLPCT_SUM:
2062 case CTSF_TABLEPCT_SUM:
2063 case CTSF_SUBTABLEPCT_SUM:
2064 case CTSF_LAYERPCT_SUM:
2065 case CTSF_LAYERROWPCT_SUM:
2066 case CTSF_LAYERCOLPCT_SUM:
2075 case CTSF_RESPONSES:
2076 case CTSF_ROWPCT_RESPONSES:
2077 case CTSF_COLPCT_RESPONSES:
2078 case CTSF_TABLEPCT_RESPONSES:
2079 case CTSF_SUBTABLEPCT_RESPONSES:
2080 case CTSF_LAYERPCT_RESPONSES:
2081 case CTSF_LAYERROWPCT_RESPONSES:
2082 case CTSF_LAYERCOLPCT_RESPONSES:
2083 case CTSF_ROWPCT_RESPONSES_COUNT:
2084 case CTSF_COLPCT_RESPONSES_COUNT:
2085 case CTSF_TABLEPCT_RESPONSES_COUNT:
2086 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2087 case CTSF_LAYERPCT_RESPONSES_COUNT:
2088 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2089 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2090 case CTSF_ROWPCT_COUNT_RESPONSES:
2091 case CTSF_COLPCT_COUNT_RESPONSES:
2092 case CTSF_TABLEPCT_COUNT_RESPONSES:
2093 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2094 case CTSF_LAYERPCT_COUNT_RESPONSES:
2095 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2096 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2103 struct ctables_cell_sort_aux
2105 const struct ctables_table *t;
2106 enum pivot_axis_type a;
2110 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2112 const struct ctables_cell_sort_aux *aux = aux_;
2113 struct ctables_cell *const *ap = a_;
2114 struct ctables_cell *const *bp = b_;
2115 const struct ctables_cell *a = *ap;
2116 const struct ctables_cell *b = *bp;
2118 size_t a_idx = a->axes[aux->a].stack_idx;
2119 size_t b_idx = b->axes[aux->a].stack_idx;
2121 return a_idx < b_idx ? -1 : 1;
2123 const struct ctables_nest *nest = &aux->t->stacks[aux->a].nests[a_idx];
2124 for (size_t i = 0; i < nest->n; i++)
2125 if (i != nest->scale_idx)
2127 const struct variable *var = nest->vars[i];
2128 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2129 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2130 if (a_cv->category != b_cv->category)
2131 return a_cv->category > b_cv->category ? 1 : -1;
2133 const union value *a_val = &a_cv->value;
2134 const union value *b_val = &b_cv->value;
2135 switch (a_cv->category->type)
2142 /* Must be equal. */
2149 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2157 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2159 return a_cv->category->sort_ascending ? cmp : -cmp;
2165 const char *a_label = var_lookup_value_label (var, a_val);
2166 const char *b_label = var_lookup_value_label (var, b_val);
2168 ? (b_label ? strcmp (a_label, b_label) : 1)
2169 : (b_label ? -1 : value_compare_3way (
2170 a_val, b_val, var_get_width (var))));
2172 return a_cv->category->sort_ascending ? cmp : -cmp;
2186 For each ctables_table:
2187 For each combination of row vars:
2188 For each combination of column vars:
2189 For each combination of layer vars:
2191 Make a table of row values:
2192 Sort entries by row values
2193 Assign a 0-based index to each actual value
2194 Construct a dimension
2195 Make a table of column values
2196 Make a table of layer values
2198 Fill the table entry using the indexes from before.
2201 static struct ctables_domain *
2202 ctables_domain_insert (struct ctables_table *t, struct ctables_cell *cell,
2203 enum ctables_domain_type domain)
2206 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2208 size_t idx = cell->axes[a].stack_idx;
2209 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2210 hash = hash_int (idx, hash);
2211 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2213 size_t v_idx = nest->domains[domain][i];
2214 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2215 var_get_width (nest->vars[v_idx]), hash);
2219 struct ctables_domain *d;
2220 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &t->domains[domain])
2222 const struct ctables_cell *df = d->example;
2223 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2225 size_t idx = cell->axes[a].stack_idx;
2226 if (idx != df->axes[a].stack_idx)
2229 const struct ctables_nest *nest = &t->stacks[a].nests[idx];
2230 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2232 size_t v_idx = nest->domains[domain][i];
2233 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2234 &cell->axes[a].cvs[v_idx].value,
2235 var_get_width (nest->vars[v_idx])))
2244 d = xmalloc (sizeof *d);
2245 *d = (struct ctables_domain) { .example = cell };
2246 hmap_insert (&t->domains[domain], &d->node, hash);
2250 static const struct ctables_category *
2251 ctables_categories_match (const struct ctables_categories *c,
2252 const union value *v, const struct variable *var)
2254 const struct ctables_category *othernm = NULL;
2255 for (size_t i = c->n_cats; i-- > 0; )
2257 const struct ctables_category *cat = &c->cats[i];
2261 if (cat->number == v->f)
2269 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2270 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2275 if (var_is_value_missing (var, v))
2292 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2297 return var_is_value_missing (var, v) ? NULL : othernm;
2300 static const struct ctables_category *
2301 ctables_categories_total (const struct ctables_categories *c)
2303 const struct ctables_category *first = &c->cats[0];
2304 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2305 return (first->type == CCT_TOTAL ? first
2306 : last->type == CCT_TOTAL ? last
2310 static struct ctables_cell *
2311 ctables_cell_insert__ (struct ctables_table *t, const struct ccase *c,
2312 size_t ix[PIVOT_N_AXES],
2313 const struct ctables_category *cats[PIVOT_N_AXES][10])
2315 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2318 enum ctables_summary_variant sv = CSV_CELL;
2319 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2321 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2322 hash = hash_int (ix[a], hash);
2323 for (size_t i = 0; i < nest->n; i++)
2324 if (i != nest->scale_idx)
2326 hash = hash_pointer (cats[a][i], hash);
2327 if (cats[a][i]->type != CCT_TOTAL
2328 && cats[a][i]->type != CCT_SUBTOTAL
2329 && cats[a][i]->type != CCT_HSUBTOTAL)
2330 hash = value_hash (case_data (c, nest->vars[i]),
2331 var_get_width (nest->vars[i]), hash);
2337 struct ctables_cell *cell;
2338 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &t->cells)
2340 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2342 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2343 if (cell->axes[a].stack_idx != ix[a])
2345 for (size_t i = 0; i < nest->n; i++)
2346 if (i != nest->scale_idx
2347 && (cats[a][i] != cell->axes[a].cvs[i].category
2348 || (cats[a][i]->type != CCT_TOTAL
2349 && cats[a][i]->type != CCT_SUBTOTAL
2350 && cats[a][i]->type != CCT_HSUBTOTAL
2351 && !value_equal (case_data (c, nest->vars[i]),
2352 &cell->axes[a].cvs[i].value,
2353 var_get_width (nest->vars[i])))))
2362 cell = xmalloc (sizeof *cell);
2365 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2367 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2368 cell->axes[a].stack_idx = ix[a];
2369 cell->axes[a].cvs = (nest->n
2370 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2372 for (size_t i = 0; i < nest->n; i++)
2374 if (i != nest->scale_idx)
2376 const struct ctables_category *subtotal = cats[a][i]->subtotal;
2377 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2381 cell->axes[a].cvs[i].category = cats[a][i];
2382 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2383 var_get_width (nest->vars[i]));
2387 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2388 cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
2389 for (size_t i = 0; i < specs->n; i++)
2390 ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
2391 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2392 cell->domains[dt] = ctables_domain_insert (t, cell, dt);
2393 hmap_insert (&t->cells, &cell->node, hash);
2398 ctables_cell_add__ (struct ctables_table *t, const struct ccase *c,
2399 size_t ix[PIVOT_N_AXES],
2400 const struct ctables_category *cats[PIVOT_N_AXES][10],
2403 struct ctables_cell *cell = ctables_cell_insert__ (t, c, ix, cats);
2404 const struct ctables_nest *ss = &t->stacks[t->summary_axis].nests[ix[t->summary_axis]];
2406 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2407 for (size_t i = 0; i < specs->n; i++)
2408 ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
2409 case_data (c, specs->var), weight);
2410 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2411 cell->domains[dt]->valid += weight;
2415 recurse_totals (struct ctables_table *t, const struct ccase *c,
2416 size_t ix[PIVOT_N_AXES],
2417 const struct ctables_category *cats[PIVOT_N_AXES][10],
2419 enum pivot_axis_type start_axis, size_t start_nest)
2421 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2423 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2424 for (size_t i = start_nest; i < nest->n; i++)
2426 if (i == nest->scale_idx)
2429 const struct variable *var = nest->vars[i];
2431 const struct ctables_category *total = ctables_categories_total (
2432 t->categories[var_get_dict_index (var)]);
2435 const struct ctables_category *save = cats[a][i];
2437 ctables_cell_add__ (t, c, ix, cats, weight);
2438 recurse_totals (t, c, ix, cats, weight, a, i + 1);
2447 ctables_cell_insert (struct ctables_table *t,
2448 const struct ccase *c,
2449 size_t ir, size_t ic, size_t il,
2452 size_t ix[PIVOT_N_AXES] = {
2453 [PIVOT_AXIS_ROW] = ir,
2454 [PIVOT_AXIS_COLUMN] = ic,
2455 [PIVOT_AXIS_LAYER] = il,
2458 const struct ctables_category *cats[PIVOT_N_AXES][10];
2459 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2461 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2462 for (size_t i = 0; i < nest->n; i++)
2464 if (i == nest->scale_idx)
2467 const struct variable *var = nest->vars[i];
2468 const union value *value = case_data (c, var);
2470 if (var_is_numeric (var) && value->f == SYSMIS)
2473 cats[a][i] = ctables_categories_match (
2474 t->categories[var_get_dict_index (var)], value, var);
2480 ctables_cell_add__ (t, c, ix, cats, weight);
2482 recurse_totals (t, c, ix, cats, weight, 0, 0);
2484 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2486 const struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2487 for (size_t i = 0; i < nest->n; i++)
2489 if (i == nest->scale_idx)
2492 const struct ctables_category *save = cats[a][i];
2495 cats[a][i] = save->subtotal;
2496 ctables_cell_add__ (t, c, ix, cats, weight);
2505 const struct ctables_summary_spec_set *set;
2510 merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
2512 const struct ctables_summary_spec *as = &a->set->specs[a->ofs];
2513 const struct ctables_summary_spec *bs = &b->set->specs[b->ofs];
2514 if (as->function != bs->function)
2515 return as->function > bs->function ? 1 : -1;
2516 else if (as->percentile != bs->percentile)
2517 return as->percentile < bs->percentile ? 1 : -1;
2518 return strcmp (as->label, bs->label);
2522 ctables_table_output_same_axis (struct ctables *ct, struct ctables_table *t)
2524 struct pivot_table *pt = pivot_table_create__ (
2526 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2527 : pivot_value_new_text (N_("Custom Tables"))),
2530 pivot_table_set_caption (
2531 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2533 pivot_table_set_caption (
2534 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2536 pivot_table_set_look (pt, ct->look);
2537 struct pivot_dimension *d[PIVOT_N_AXES];
2538 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2540 static const char *names[] = {
2541 [PIVOT_AXIS_ROW] = N_("Rows"),
2542 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2543 [PIVOT_AXIS_LAYER] = N_("Layers"),
2545 d[a] = (t->axes[a] || a == t->summary_axis
2546 ? pivot_dimension_create (pt, a, names[a])
2551 assert (t->axes[a]);
2553 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2555 struct ctables_cell *cell;
2557 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2560 assert (n <= t->cells.count);
2562 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2563 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2565 size_t max_depth = 0;
2566 for (size_t j = 0; j < t->stacks[a].n; j++)
2567 if (t->stacks[a].nests[j].n > max_depth)
2568 max_depth = t->stacks[a].nests[j].n;
2570 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2571 struct pivot_category *top = NULL;
2573 for (size_t j = 0; j < n; j++)
2575 struct ctables_cell *cell = sorted[j];
2576 const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
2578 size_t n_common = 0;
2579 bool new_subtable = false;
2582 struct ctables_cell *prev = sorted[j - 1];
2583 if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
2585 for (; n_common < nest->n; n_common++)
2586 if (n_common != nest->scale_idx
2587 && (prev->axes[a].cvs[n_common].category
2588 != cell->axes[a].cvs[n_common].category
2589 || !value_equal (&prev->axes[a].cvs[n_common].value,
2590 &cell->axes[a].cvs[n_common].value,
2591 var_get_type (nest->vars[n_common]))))
2595 new_subtable = true;
2598 new_subtable = true;
2602 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
2604 if (vlabel != CTVL_NONE)
2605 top = pivot_category_create_group__ (
2606 top, pivot_value_new_variable (nest->vars[0]));
2608 if (n_common == nest->n)
2610 cell->axes[a].leaf = prev_leaf;
2614 for (size_t k = n_common; k < nest->n; k++)
2616 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2618 struct pivot_value *label
2619 = (k == nest->scale_idx ? NULL
2620 : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
2621 || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
2622 || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
2623 ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
2625 : pivot_value_new_var_value (nest->vars[k],
2626 &cell->axes[a].cvs[k].value));
2627 if (k == nest->n - 1)
2629 if (a == t->summary_axis)
2632 parent = pivot_category_create_group__ (parent, label);
2633 const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
2634 for (size_t m = 0; m < specs->n; m++)
2636 int leaf = pivot_category_create_leaf (
2637 parent, pivot_value_new_text (specs->specs[m].label));
2644 /* This assertion is true as long as the summary axis
2645 is the axis where the summaries are displayed. */
2648 prev_leaf = pivot_category_create_leaf (parent, label);
2654 parent = pivot_category_create_group__ (parent, label);
2656 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
2657 if (vlabel != CTVL_NONE)
2658 parent = pivot_category_create_group__ (
2659 parent, pivot_value_new_variable (nest->vars[k + 1]));
2663 cell->axes[a].leaf = prev_leaf;
2668 struct ctables_cell *cell;
2669 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2674 const struct ctables_nest *nest = &t->stacks[t->summary_axis].nests[cell->axes[t->summary_axis].stack_idx];
2675 const struct ctables_summary_spec_set *specs = &nest->specs[cell->sv];
2676 for (size_t j = 0; j < specs->n; j++)
2679 size_t n_dindexes = 0;
2681 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2684 int leaf = cell->axes[a].leaf;
2685 if (a == t->summary_axis)
2687 dindexes[n_dindexes++] = leaf;
2690 double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
2691 struct pivot_value *value = pivot_value_new_number (d);
2692 value->numeric.format = specs->specs[j].format;
2693 pivot_table_put (pt, dindexes, n_dindexes, value);
2697 pivot_table_submit (pt);
2701 ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t)
2703 struct pivot_table *pt = pivot_table_create__ (
2705 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2706 : pivot_value_new_text (N_("Custom Tables"))),
2709 pivot_table_set_caption (
2710 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2712 pivot_table_set_caption (
2713 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2715 pivot_table_set_look (pt, ct->look);
2716 struct pivot_dimension *d[PIVOT_N_AXES];
2717 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2719 static const char *names[] = {
2720 [PIVOT_AXIS_ROW] = N_("Rows"),
2721 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2722 [PIVOT_AXIS_LAYER] = N_("Layers"),
2724 d[a] = (t->axes[a] || a == t->summary_axis
2725 ? pivot_dimension_create (pt, a, names[a])
2730 assert (t->axes[a]);
2732 struct ctables_cell **sorted = xnmalloc (t->cells.count, sizeof *sorted);
2734 struct ctables_cell *cell;
2736 HMAP_FOR_EACH (cell, struct ctables_cell, node, &t->cells)
2739 assert (n <= t->cells.count);
2741 struct ctables_cell_sort_aux aux = { .t = t, .a = a };
2742 sort (sorted, n, sizeof *sorted, ctables_cell_compare_3way, &aux);
2744 size_t max_depth = 0;
2745 for (size_t j = 0; j < t->stacks[a].n; j++)
2746 if (t->stacks[a].nests[j].n > max_depth)
2747 max_depth = t->stacks[a].nests[j].n;
2749 struct pivot_category **groups = xnmalloc (max_depth, sizeof *groups);
2750 struct pivot_category *top = NULL;
2752 for (size_t j = 0; j < n; j++)
2754 struct ctables_cell *cell = sorted[j];
2755 const struct ctables_nest *nest = &t->stacks[a].nests[cell->axes[a].stack_idx];
2757 /* Pivot categories:
2759 - variable label for nest->vars[0], if vlabel != CTVL_NONE
2760 - category for nest->vars[0]
2761 - variable label for nest->vars[1], if vlabel != CTVL_NONE
2762 - category for nest->vars[1]
2764 - variable label for nest->vars[nest->n - 1], if vlabel != CTVL_NONE
2765 - category for nest->vars[nest->n - 1], unless t->label_axis[a] != a.
2766 - summary function, if 'a == t->slabels_axis && a ==
2769 Additional dimensions:
2771 - If 'a == t->slabels_axis && a != t->summary_axis', add a summary
2773 - If 't->label_axis[b] == a' for some 'b != a', add a category
2777 size_t n_common = 0;
2778 bool new_subtable = false;
2781 struct ctables_cell *prev = sorted[j - 1];
2782 if (prev->axes[a].stack_idx == cell->axes[a].stack_idx)
2784 for (; n_common < nest->n; n_common++)
2785 if (n_common != nest->scale_idx
2786 && (prev->axes[a].cvs[n_common].category
2787 != cell->axes[a].cvs[n_common].category
2788 || !value_equal (&prev->axes[a].cvs[n_common].value,
2789 &cell->axes[a].cvs[n_common].value,
2790 var_get_type (nest->vars[n_common]))))
2794 new_subtable = true;
2797 new_subtable = true;
2801 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[0])];
2803 if (vlabel != CTVL_NONE)
2804 top = pivot_category_create_group__ (
2805 top, pivot_value_new_variable (nest->vars[0]));
2807 if (n_common == nest->n)
2809 cell->axes[a].leaf = prev_leaf;
2813 for (size_t k = n_common; k < nest->n; k++)
2815 struct pivot_category *parent = k > 0 ? groups[k - 1] : top;
2817 struct pivot_value *label
2818 = (k == nest->scale_idx ? NULL
2819 : (cell->axes[a].cvs[k].category->type == CCT_TOTAL
2820 || cell->axes[a].cvs[k].category->type == CCT_SUBTOTAL
2821 || cell->axes[a].cvs[k].category->type == CCT_HSUBTOTAL)
2822 ? pivot_value_new_user_text (cell->axes[a].cvs[k].category->total_label,
2824 : pivot_value_new_var_value (nest->vars[k],
2825 &cell->axes[a].cvs[k].value));
2826 if (k == nest->n - 1)
2828 if (a == t->slabels_axis)
2831 parent = pivot_category_create_group__ (parent, label);
2832 const struct ctables_summary_spec_set *specs = &t->summary_specs;
2833 for (size_t m = 0; m < specs->n; m++)
2835 int leaf = pivot_category_create_leaf (
2836 parent, pivot_value_new_text (specs->specs[m].label));
2843 prev_leaf = pivot_category_create_leaf (parent, label ? label : pivot_value_new_user_text ("text", SIZE_MAX));
2849 parent = pivot_category_create_group__ (parent, label);
2851 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k + 1])];
2852 if (vlabel != CTVL_NONE)
2853 parent = pivot_category_create_group__ (
2854 parent, pivot_value_new_variable (nest->vars[k + 1]));
2858 cell->axes[a].leaf = prev_leaf;
2863 pivot_table_submit (pt);
2868 ctables_prepare_table (struct ctables_table *t)
2870 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2873 t->stacks[a] = enumerate_fts (a, t->axes[a]);
2875 for (size_t j = 0; j < t->stacks[a].n; j++)
2877 struct ctables_nest *nest = &t->stacks[a].nests[j];
2878 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2880 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
2881 nest->n_domains[dt] = 0;
2883 for (size_t k = 0; k < nest->n; k++)
2885 if (k == nest->scale_idx)
2894 if (a != PIVOT_AXIS_LAYER)
2901 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
2902 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
2903 : a == PIVOT_AXIS_ROW)
2905 if (k == nest->n - 1
2906 || (nest->scale_idx == nest->n - 1
2907 && k == nest->n - 2))
2913 if (a == PIVOT_AXIS_COLUMN)
2918 if (a == PIVOT_AXIS_ROW)
2923 nest->domains[dt][nest->n_domains[dt]++] = k;
2930 struct ctables_nest *nest = xmalloc (sizeof *nest);
2931 *nest = (struct ctables_nest) { .n = 0 };
2932 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
2935 struct ctables_stack *stack = &t->stacks[t->summary_axis];
2936 for (size_t i = 0; i < stack->n; i++)
2938 struct ctables_nest *nest = &stack->nests[i];
2939 if (!nest->specs[CSV_CELL].n)
2941 struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
2942 specs->specs = xmalloc (sizeof *specs->specs);
2945 enum ctables_summary_function function
2946 = specs->var ? CTSF_MEAN : CTSF_COUNT;
2947 struct ctables_var var = { .is_mrset = false, .var = specs->var };
2949 *specs->specs = (struct ctables_summary_spec) {
2950 .function = function,
2951 .format = ctables_summary_default_format (function, &var),
2952 .label = ctables_summary_default_label (function, 0),
2955 specs->var = nest->vars[0];
2957 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
2958 &nest->specs[CSV_CELL]);
2960 else if (!nest->specs[CSV_TOTAL].n)
2961 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
2962 &nest->specs[CSV_CELL]);
2965 struct ctables_summary_spec_set *merged = &t->summary_specs;
2966 struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
2968 for (size_t j = 0; j < stack->n; j++)
2970 const struct ctables_nest *nest = &stack->nests[j];
2972 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
2973 items[n_left++] = (struct merge_item) { .set = &nest->specs[sv] };
2978 struct merge_item min = items[0];
2979 for (size_t j = 1; j < n_left; j++)
2980 if (merge_item_compare_3way (&items[j], &min) < 0)
2983 if (merged->n >= merged->allocated)
2984 merged->specs = x2nrealloc (merged->specs, &merged->allocated,
2985 sizeof *merged->specs);
2986 merged->specs[merged->n++] = min.set->specs[min.ofs];
2988 for (size_t j = 0; j < n_left; )
2990 if (merge_item_compare_3way (&items[j], &min) == 0)
2992 struct merge_item *item = &items[j];
2993 item->set->specs[item->ofs].axis_idx = merged->n - 1;
2994 if (++item->ofs >= item->set->n)
2996 items[j] = items[--n_left];
3005 for (size_t j = 0; j < merged->n; j++)
3006 printf ("%s\n", ctables_summary_function_name (merged->specs[j].function));
3008 for (size_t j = 0; j < stack->n; j++)
3010 const struct ctables_nest *nest = &stack->nests[j];
3011 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3013 const struct ctables_summary_spec_set *specs = &nest->specs[sv];
3014 for (size_t k = 0; k < specs->n; k++)
3015 printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
3016 specs->specs[k].axis_idx);
3024 ctables_insert_clabels_values (struct ctables_table *t, const struct ccase *c,
3025 enum pivot_axis_type a)
3027 struct ctables_stack *stack = &t->stacks[a];
3028 for (size_t i = 0; i < stack->n; i++)
3030 const struct ctables_nest *nest = &stack->nests[i];
3031 const struct variable *v = nest->vars[nest->n - 1];
3032 int width = var_get_width (v);
3033 const union value *value = case_data (c, v);
3034 unsigned int hash = value_hash (value, width, 0);
3036 struct ctables_value *clv;
3037 HMAP_FOR_EACH_WITH_HASH (clv, struct ctables_value, node, hash,
3038 &t->clabels_values_map)
3039 if (value_equal (value, &clv->value, width))
3042 clv = xmalloc (sizeof *clv);
3043 value_clone (&clv->value, value, width);
3044 hmap_insert (&t->clabels_values_map, &clv->node, hash);
3051 compare_clabels_values_3way (const void *a_, const void *b_, const void *width_)
3053 const union value *a = a_;
3054 const union value *b = b_;
3055 const int *width = width_;
3056 return value_compare_3way (a, b, *width);
3060 ctables_sort_clabels_values (struct ctables_table *t)
3062 int width = var_get_width (t->clabels_example);
3064 size_t n = hmap_count (&t->clabels_values_map);
3065 t->clabels_values = xnmalloc (n, sizeof *t->clabels_values);
3067 const struct ctables_value *clv;
3069 HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
3070 t->clabels_values[i++] = clv->value;
3073 sort (t->clabels_values, n, sizeof *t->clabels_values,
3074 compare_clabels_values_3way, &width);
3078 ctables_execute (struct dataset *ds, struct ctables *ct)
3080 struct casereader *input = casereader_create_filter_weight (proc_open (ds),
3083 bool warn_on_invalid = true;
3084 double total_weight = 0;
3085 for (struct ccase *c = casereader_read (input); c;
3086 case_unref (c), c = casereader_read (input))
3088 double weight = dict_get_case_weight (dataset_dict (ds), c,
3090 total_weight += weight;
3092 for (size_t i = 0; i < ct->n_tables; i++)
3094 struct ctables_table *t = ct->tables[i];
3096 for (size_t ir = 0; ir < t->stacks[PIVOT_AXIS_ROW].n; ir++)
3097 for (size_t ic = 0; ic < t->stacks[PIVOT_AXIS_COLUMN].n; ic++)
3098 for (size_t il = 0; il < t->stacks[PIVOT_AXIS_LAYER].n; il++)
3099 ctables_cell_insert (t, c, ir, ic, il, weight);
3101 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3102 if (t->label_axis[a] != a)
3103 ctables_insert_clabels_values (t, c, a);
3106 casereader_destroy (input);
3108 for (size_t i = 0; i < ct->n_tables; i++)
3110 struct ctables_table *t = ct->tables[i];
3112 if (t->clabels_example)
3113 ctables_sort_clabels_values (t);
3115 if (t->summary_axis == t->slabels_axis)
3116 ctables_table_output_same_axis (ct, ct->tables[i]);
3118 ctables_table_output_different_axis (ct, ct->tables[i]);
3120 return proc_commit (ds);
3124 ctables_check_label_position (struct ctables_table *t, enum pivot_axis_type a)
3126 enum pivot_axis_type label_pos = t->label_axis[a];
3130 const char *subcommand_name = a == PIVOT_AXIS_ROW ? "ROWLABELS" : "COLLABELS";
3131 const char *pos_name = label_pos == PIVOT_AXIS_LAYER ? "LAYER" : "OPPOSITE";
3133 const struct ctables_stack *stack = &t->stacks[a];
3137 const struct ctables_nest *n0 = &stack->nests[0];
3139 const struct variable *v0 = n0->vars[n0->n - 1];
3140 struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
3141 t->clabels_example = v0;
3143 for (size_t i = 0; i < c0->n_cats; i++)
3144 if (c0->cats[i].type == CCT_FUNCTION)
3146 msg (SE, _("%s=%s is not allowed with sorting based "
3147 "on a summary function."),
3148 subcommand_name, pos_name);
3152 for (size_t i = 1; i < stack->n; i++)
3154 const struct ctables_nest *ni = &stack->nests[i];
3156 const struct variable *vi = ni->vars[ni->n - 1];
3157 struct ctables_categories *ci = t->categories[var_get_dict_index (vi)];
3159 if (var_get_width (v0) != var_get_width (vi))
3161 msg (SE, _("%s=%s requires the variables to be "
3162 "moved to have the same width, but %s has "
3163 "width %d and %s has width %d."),
3164 subcommand_name, pos_name,
3165 var_get_name (v0), var_get_width (v0),
3166 var_get_name (vi), var_get_width (vi));
3169 if (!val_labs_equal (var_get_value_labels (v0),
3170 var_get_value_labels (vi)))
3172 msg (SE, _("%s=%s requires the variables to be "
3173 "moved to have the same value labels, but %s "
3174 "and %s have different value labels."),
3175 subcommand_name, pos_name,
3176 var_get_name (v0), var_get_name (vi));
3179 if (!ctables_categories_equal (c0, ci))
3181 msg (SE, _("%s=%s requires the variables to be "
3182 "moved to have the same category "
3183 "specifications, but %s and %s have different "
3184 "category specifications."),
3185 subcommand_name, pos_name,
3186 var_get_name (v0), var_get_name (vi));
3195 cmd_ctables (struct lexer *lexer, struct dataset *ds)
3197 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3198 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
3199 enum settings_value_show tvars = settings_get_show_variables ();
3200 for (size_t i = 0; i < n_vars; i++)
3201 vlabels[i] = (enum ctables_vlabel) tvars;
3203 struct ctables *ct = xmalloc (sizeof *ct);
3204 *ct = (struct ctables) {
3205 .look = pivot_table_look_unshare (pivot_table_look_ref (
3206 pivot_table_look_get_default ())),
3208 .hide_threshold = 5,
3210 ct->look->omit_empty = false;
3212 if (!lex_force_match (lexer, T_SLASH))
3215 while (!lex_match_id (lexer, "TABLE"))
3217 if (lex_match_id (lexer, "FORMAT"))
3219 double widths[2] = { SYSMIS, SYSMIS };
3220 double units_per_inch = 72.0;
3222 while (lex_token (lexer) != T_SLASH)
3224 if (lex_match_id (lexer, "MINCOLWIDTH"))
3226 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
3229 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
3231 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
3234 else if (lex_match_id (lexer, "UNITS"))
3236 lex_match (lexer, T_EQUALS);
3237 if (lex_match_id (lexer, "POINTS"))
3238 units_per_inch = 72.0;
3239 else if (lex_match_id (lexer, "INCHES"))
3240 units_per_inch = 1.0;
3241 else if (lex_match_id (lexer, "CM"))
3242 units_per_inch = 2.54;
3245 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
3249 else if (lex_match_id (lexer, "EMPTY"))
3254 lex_match (lexer, T_EQUALS);
3255 if (lex_match_id (lexer, "ZERO"))
3257 /* Nothing to do. */
3259 else if (lex_match_id (lexer, "BLANK"))
3260 ct->zero = xstrdup ("");
3261 else if (lex_force_string (lexer))
3263 ct->zero = ss_xstrdup (lex_tokss (lexer));
3269 else if (lex_match_id (lexer, "MISSING"))
3271 lex_match (lexer, T_EQUALS);
3272 if (!lex_force_string (lexer))
3276 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
3277 ? ss_xstrdup (lex_tokss (lexer))
3283 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
3284 "UNITS", "EMPTY", "MISSING");
3289 if (widths[0] != SYSMIS && widths[1] != SYSMIS
3290 && widths[0] > widths[1])
3292 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
3296 for (size_t i = 0; i < 2; i++)
3297 if (widths[i] != SYSMIS)
3299 int *wr = ct->look->width_ranges[TABLE_HORZ];
3300 wr[i] = widths[i] / units_per_inch * 96.0;
3305 else if (lex_match_id (lexer, "VLABELS"))
3307 if (!lex_force_match_id (lexer, "VARIABLES"))
3309 lex_match (lexer, T_EQUALS);
3311 struct variable **vars;
3313 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
3317 if (!lex_force_match_id (lexer, "DISPLAY"))
3322 lex_match (lexer, T_EQUALS);
3324 enum ctables_vlabel vlabel;
3325 if (lex_match_id (lexer, "DEFAULT"))
3326 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
3327 else if (lex_match_id (lexer, "NAME"))
3329 else if (lex_match_id (lexer, "LABEL"))
3330 vlabel = CTVL_LABEL;
3331 else if (lex_match_id (lexer, "BOTH"))
3333 else if (lex_match_id (lexer, "NONE"))
3337 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
3343 for (size_t i = 0; i < n_vars; i++)
3344 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
3347 else if (lex_match_id (lexer, "MRSETS"))
3349 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
3351 lex_match (lexer, T_EQUALS);
3352 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
3355 else if (lex_match_id (lexer, "SMISSING"))
3357 if (lex_match_id (lexer, "VARIABLE"))
3358 ct->smissing_listwise = false;
3359 else if (lex_match_id (lexer, "LISTWISE"))
3360 ct->smissing_listwise = true;
3363 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
3368 else if (lex_match_id (lexer, "WEIGHT"))
3370 if (!lex_force_match_id (lexer, "VARIABLE"))
3372 lex_match (lexer, T_EQUALS);
3373 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
3374 if (!ct->base_weight)
3377 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
3379 if (!lex_force_match_id (lexer, "COUNT"))
3381 lex_match (lexer, T_EQUALS);
3382 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
3384 ct->hide_threshold = lex_integer (lexer);
3389 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
3390 "SMISSING", "PCOMPUTE", "PPROPERTIES",
3391 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
3395 if (!lex_force_match (lexer, T_SLASH))
3399 size_t allocated_tables = 0;
3402 if (ct->n_tables >= allocated_tables)
3403 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
3404 sizeof *ct->tables);
3406 struct ctables_category *cat = xmalloc (sizeof *cat);
3407 *cat = (struct ctables_category) {
3409 .include_missing = false,
3410 .sort_ascending = true,
3413 struct ctables_categories *c = xmalloc (sizeof *c);
3414 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3415 *c = (struct ctables_categories) {
3421 struct ctables_categories **categories = xnmalloc (n_vars,
3422 sizeof *categories);
3423 for (size_t i = 0; i < n_vars; i++)
3426 struct ctables_table *t = xmalloc (sizeof *t);
3427 *t = (struct ctables_table) {
3428 .cells = HMAP_INITIALIZER (t->cells),
3429 .slabels_axis = PIVOT_AXIS_COLUMN,
3430 .slabels_visible = true,
3431 .clabels_values_map = HMAP_INITIALIZER (t->clabels_values_map),
3433 [PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW,
3434 [PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN,
3435 [PIVOT_AXIS_LAYER] = PIVOT_AXIS_LAYER,
3437 .categories = categories,
3438 .n_categories = n_vars,
3441 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
3442 hmap_init (&t->domains[dt]);
3443 ct->tables[ct->n_tables++] = t;
3445 lex_match (lexer, T_EQUALS);
3446 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
3448 if (lex_match (lexer, T_BY))
3450 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3451 ct, t, PIVOT_AXIS_COLUMN))
3454 if (lex_match (lexer, T_BY))
3456 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3457 ct, t, PIVOT_AXIS_LAYER))
3462 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
3463 && !t->axes[PIVOT_AXIS_LAYER])
3465 lex_error (lexer, _("At least one variable must be specified."));
3469 const struct ctables_axis *scales[PIVOT_N_AXES];
3470 size_t n_scales = 0;
3471 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3473 scales[a] = find_scale (t->axes[a]);
3479 msg (SE, _("Scale variables may appear only on one axis."));
3480 if (scales[PIVOT_AXIS_ROW])
3481 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
3482 _("This scale variable appears on the rows axis."));
3483 if (scales[PIVOT_AXIS_COLUMN])
3484 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
3485 _("This scale variable appears on the columns axis."));
3486 if (scales[PIVOT_AXIS_LAYER])
3487 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
3488 _("This scale variable appears on the layer axis."));
3492 const struct ctables_axis *summaries[PIVOT_N_AXES];
3493 size_t n_summaries = 0;
3494 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3496 summaries[a] = (scales[a]
3498 : find_categorical_summary_spec (t->axes[a]));
3502 if (n_summaries > 1)
3504 msg (SE, _("Summaries may appear only on one axis."));
3505 if (summaries[PIVOT_AXIS_ROW])
3506 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3507 _("This variable on the rows axis has a summary."));
3508 if (summaries[PIVOT_AXIS_COLUMN])
3509 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3510 _("This variable on the columns axis has a summary."));
3511 if (summaries[PIVOT_AXIS_LAYER])
3512 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3513 _("This variable on the layers axis has a summary."));
3516 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3517 if (n_summaries ? summaries[a] : t->axes[a])
3519 t->summary_axis = a;
3523 if (lex_token (lexer) == T_ENDCMD)
3525 if (!lex_force_match (lexer, T_SLASH))
3528 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3530 if (lex_match_id (lexer, "SLABELS"))
3532 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3534 if (lex_match_id (lexer, "POSITION"))
3536 lex_match (lexer, T_EQUALS);
3537 if (lex_match_id (lexer, "COLUMN"))
3538 t->slabels_axis = PIVOT_AXIS_COLUMN;
3539 else if (lex_match_id (lexer, "ROW"))
3540 t->slabels_axis = PIVOT_AXIS_ROW;
3541 else if (lex_match_id (lexer, "LAYER"))
3542 t->slabels_axis = PIVOT_AXIS_LAYER;
3545 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3549 else if (lex_match_id (lexer, "VISIBLE"))
3551 lex_match (lexer, T_EQUALS);
3552 if (!parse_bool (lexer, &t->slabels_visible))
3557 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3562 else if (lex_match_id (lexer, "CLABELS"))
3564 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3566 if (lex_match_id (lexer, "AUTO"))
3568 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW;
3569 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN;
3571 else if (lex_match_id (lexer, "ROWLABELS"))
3573 lex_match (lexer, T_EQUALS);
3574 if (lex_match_id (lexer, "OPPOSITE"))
3575 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_COLUMN;
3576 else if (lex_match_id (lexer, "LAYER"))
3577 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_LAYER;
3580 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3584 else if (lex_match_id (lexer, "COLLABELS"))
3586 lex_match (lexer, T_EQUALS);
3587 if (lex_match_id (lexer, "OPPOSITE"))
3588 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_ROW;
3589 else if (lex_match_id (lexer, "LAYER"))
3590 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_LAYER;
3593 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3599 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3605 else if (lex_match_id (lexer, "CRITERIA"))
3607 if (!lex_force_match_id (lexer, "CILEVEL"))
3609 lex_match (lexer, T_EQUALS);
3611 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3613 t->cilevel = lex_number (lexer);
3616 else if (lex_match_id (lexer, "CATEGORIES"))
3618 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3621 else if (lex_match_id (lexer, "TITLES"))
3626 if (lex_match_id (lexer, "CAPTION"))
3627 textp = &t->caption;
3628 else if (lex_match_id (lexer, "CORNER"))
3630 else if (lex_match_id (lexer, "TITLE"))
3634 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
3637 lex_match (lexer, T_EQUALS);
3639 struct string s = DS_EMPTY_INITIALIZER;
3640 while (lex_is_string (lexer))
3642 if (!ds_is_empty (&s))
3643 ds_put_byte (&s, ' ');
3644 ds_put_substring (&s, lex_tokss (lexer));
3648 *textp = ds_steal_cstr (&s);
3650 while (lex_token (lexer) != T_SLASH
3651 && lex_token (lexer) != T_ENDCMD);
3653 else if (lex_match_id (lexer, "SIGTEST"))
3657 t->chisq = xmalloc (sizeof *t->chisq);
3658 *t->chisq = (struct ctables_chisq) {
3660 .include_mrsets = true,
3661 .all_visible = true,
3667 if (lex_match_id (lexer, "TYPE"))
3669 lex_match (lexer, T_EQUALS);
3670 if (!lex_force_match_id (lexer, "CHISQUARE"))
3673 else if (lex_match_id (lexer, "ALPHA"))
3675 lex_match (lexer, T_EQUALS);
3676 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
3678 t->chisq->alpha = lex_number (lexer);
3681 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3683 lex_match (lexer, T_EQUALS);
3684 if (parse_bool (lexer, &t->chisq->include_mrsets))
3687 else if (lex_match_id (lexer, "CATEGORIES"))
3689 lex_match (lexer, T_EQUALS);
3690 if (lex_match_id (lexer, "ALLVISIBLE"))
3691 t->chisq->all_visible = true;
3692 else if (lex_match_id (lexer, "SUBTOTALS"))
3693 t->chisq->all_visible = false;
3696 lex_error_expecting (lexer,
3697 "ALLVISIBLE", "SUBTOTALS");
3703 lex_error_expecting (lexer, "TYPE", "ALPHA",
3704 "INCLUDEMRSETS", "CATEGORIES");
3708 while (lex_token (lexer) != T_SLASH
3709 && lex_token (lexer) != T_ENDCMD);
3711 else if (lex_match_id (lexer, "COMPARETEST"))
3715 t->pairwise = xmalloc (sizeof *t->pairwise);
3716 *t->pairwise = (struct ctables_pairwise) {
3718 .alpha = { .05, .05 },
3719 .adjust = BONFERRONI,
3720 .include_mrsets = true,
3721 .meansvariance_allcats = true,
3722 .all_visible = true,
3731 if (lex_match_id (lexer, "TYPE"))
3733 lex_match (lexer, T_EQUALS);
3734 if (lex_match_id (lexer, "PROP"))
3735 t->pairwise->type = PROP;
3736 else if (lex_match_id (lexer, "MEAN"))
3737 t->pairwise->type = MEAN;
3740 lex_error_expecting (lexer, "PROP", "MEAN");
3744 else if (lex_match_id (lexer, "ALPHA"))
3746 lex_match (lexer, T_EQUALS);
3748 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3750 double a0 = lex_number (lexer);
3753 lex_match (lexer, T_COMMA);
3754 if (lex_is_number (lexer))
3756 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3758 double a1 = lex_number (lexer);
3761 t->pairwise->alpha[0] = MIN (a0, a1);
3762 t->pairwise->alpha[1] = MAX (a0, a1);
3765 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3767 else if (lex_match_id (lexer, "ADJUST"))
3769 lex_match (lexer, T_EQUALS);
3770 if (lex_match_id (lexer, "BONFERRONI"))
3771 t->pairwise->adjust = BONFERRONI;
3772 else if (lex_match_id (lexer, "BH"))
3773 t->pairwise->adjust = BH;
3774 else if (lex_match_id (lexer, "NONE"))
3775 t->pairwise->adjust = 0;
3778 lex_error_expecting (lexer, "BONFERRONI", "BH",
3783 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3785 lex_match (lexer, T_EQUALS);
3786 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3789 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3791 lex_match (lexer, T_EQUALS);
3792 if (lex_match_id (lexer, "ALLCATS"))
3793 t->pairwise->meansvariance_allcats = true;
3794 else if (lex_match_id (lexer, "TESTEDCATS"))
3795 t->pairwise->meansvariance_allcats = false;
3798 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
3802 else if (lex_match_id (lexer, "CATEGORIES"))
3804 lex_match (lexer, T_EQUALS);
3805 if (lex_match_id (lexer, "ALLVISIBLE"))
3806 t->pairwise->all_visible = true;
3807 else if (lex_match_id (lexer, "SUBTOTALS"))
3808 t->pairwise->all_visible = false;
3811 lex_error_expecting (lexer, "ALLVISIBLE",
3816 else if (lex_match_id (lexer, "MERGE"))
3818 lex_match (lexer, T_EQUALS);
3819 if (!parse_bool (lexer, &t->pairwise->merge))
3822 else if (lex_match_id (lexer, "STYLE"))
3824 lex_match (lexer, T_EQUALS);
3825 if (lex_match_id (lexer, "APA"))
3826 t->pairwise->apa_style = true;
3827 else if (lex_match_id (lexer, "SIMPLE"))
3828 t->pairwise->apa_style = false;
3831 lex_error_expecting (lexer, "APA", "SIMPLE");
3835 else if (lex_match_id (lexer, "SHOWSIG"))
3837 lex_match (lexer, T_EQUALS);
3838 if (!parse_bool (lexer, &t->pairwise->show_sig))
3843 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
3844 "INCLUDEMRSETS", "MEANSVARIANCE",
3845 "CATEGORIES", "MERGE", "STYLE",
3850 while (lex_token (lexer) != T_SLASH
3851 && lex_token (lexer) != T_ENDCMD);
3855 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
3856 "CRITERIA", "CATEGORIES", "TITLES",
3857 "SIGTEST", "COMPARETEST");
3861 if (!lex_match (lexer, T_SLASH))
3865 if (t->label_axis[PIVOT_AXIS_ROW] != PIVOT_AXIS_ROW
3866 && t->label_axis[PIVOT_AXIS_COLUMN] != PIVOT_AXIS_COLUMN)
3868 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
3872 ctables_prepare_table (t);
3874 ctables_check_label_position (t, PIVOT_AXIS_ROW);
3875 ctables_check_label_position (t, PIVOT_AXIS_COLUMN);
3877 while (lex_token (lexer) != T_ENDCMD);
3879 bool ok = ctables_execute (ds, ct);
3880 ctables_destroy (ct);
3881 return ok ? CMD_SUCCESS : CMD_FAILURE;
3884 ctables_destroy (ct);