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/>. */
22 #include "data/casereader.h"
23 #include "data/casewriter.h"
24 #include "data/dataset.h"
25 #include "data/dictionary.h"
26 #include "data/mrset.h"
27 #include "data/subcase.h"
28 #include "data/value-labels.h"
29 #include "language/command.h"
30 #include "language/lexer/format-parser.h"
31 #include "language/lexer/lexer.h"
32 #include "language/lexer/variable-parser.h"
33 #include "libpspp/array.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/hash-functions.h"
36 #include "libpspp/hmap.h"
37 #include "libpspp/i18n.h"
38 #include "libpspp/message.h"
39 #include "libpspp/string-array.h"
40 #include "math/mode.h"
41 #include "math/moments.h"
42 #include "math/percentiles.h"
43 #include "math/sort.h"
44 #include "output/pivot-table.h"
46 #include "gl/minmax.h"
47 #include "gl/xalloc.h"
50 #define _(msgid) gettext (msgid)
51 #define N_(msgid) (msgid)
55 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
56 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
57 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
58 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
62 - unweighted summaries (U*)
63 - lower confidence limits (*.LCL)
64 - upper confidence limits (*.UCL)
65 - standard error (*.SE)
68 /* All variables. */ \
69 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
70 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
71 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
85 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
86 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
87 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
88 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
89 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
90 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
91 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
93 /* Scale variables, totals, and subtotals. */ \
94 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
97 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
98 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
100 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
103 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
104 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
105 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
106 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
107 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
108 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
109 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
110 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
111 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
112 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
113 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
114 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
115 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
116 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
118 /* Multiple response sets. */ \
119 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
120 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
134 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
135 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
136 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
137 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
138 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
139 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
140 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
142 enum ctables_summary_function
144 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
150 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
151 N_CTSF_FUNCTIONS = SUMMARIES
155 static bool ctables_summary_function_is_count (enum ctables_summary_function);
157 enum ctables_domain_type
159 /* Within a section, where stacked variables divide one section from
161 CTDT_TABLE, /* All layers of a whole section. */
162 CTDT_LAYER, /* One layer within a section. */
163 CTDT_LAYERROW, /* Row in one layer within a section. */
164 CTDT_LAYERCOL, /* Column in one layer within a section. */
166 /* Within a subtable, where a subtable pairs an innermost row variable with
167 an innermost column variable within a single layer. */
168 CTDT_SUBTABLE, /* Whole subtable. */
169 CTDT_ROW, /* Row within a subtable. */
170 CTDT_COL, /* Column within a subtable. */
174 struct ctables_domain
176 struct hmap_node node;
178 const struct ctables_cell *example;
180 double d_valid; /* Dictionary weight. */
182 double e_valid; /* Effective weight */
186 enum ctables_summary_variant
195 /* In struct ctables_section's 'cells' hmap. Indexed by all the values in
196 all the axes (except the scalar variable, if any). */
197 struct hmap_node node;
199 /* The domains that contain this cell. */
200 bool contributes_to_domains;
201 struct ctables_domain *domains[N_CTDTS];
205 enum ctables_summary_variant sv;
207 struct ctables_cell_axis
209 struct ctables_cell_value
211 const struct ctables_category *category;
219 union ctables_summary *summaries;
224 const struct dictionary *dict;
225 struct pivot_table_look *look;
227 /* If this is NULL, zeros are displayed using the normal print format.
228 Otherwise, this string is displayed. */
231 /* If this is NULL, missing values are displayed using the normal print
232 format. Otherwise, this string is displayed. */
235 /* Indexed by variable dictionary index. */
236 enum ctables_vlabel *vlabels;
238 struct hmap postcomputes; /* Contains "struct ctables_postcompute"s. */
240 bool mrsets_count_duplicates; /* MRSETS. */
241 bool smissing_listwise; /* SMISSING. */
242 struct variable *e_weight; /* WEIGHT. */
243 int hide_threshold; /* HIDESMALLCOUNTS. */
245 struct ctables_table **tables;
249 static struct ctables_postcompute *ctables_find_postcompute (struct ctables *,
252 struct ctables_postcompute
254 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
255 char *name; /* Name, without leading &. */
257 struct msg_location *location; /* Location of definition. */
258 struct ctables_pcexpr *expr;
260 struct ctables_summary_spec_set *specs;
261 bool hide_source_cats;
264 struct ctables_pcexpr
274 enum ctables_postcompute_op
277 CTPO_CONSTANT, /* 5 */
278 CTPO_CAT_NUMBER, /* [5] */
279 CTPO_CAT_STRING, /* ["STRING"] */
280 CTPO_CAT_RANGE, /* [LO THRU 5] */
281 CTPO_CAT_MISSING, /* MISSING */
282 CTPO_CAT_OTHERNM, /* OTHERNM */
283 CTPO_CAT_SUBTOTAL, /* SUBTOTAL */
284 CTPO_CAT_TOTAL, /* TOTAL */
298 /* CTPO_CAT_NUMBER. */
301 /* CTPO_CAT_STRING. */
304 /* CTPO_CAT_RANGE. */
307 /* CTPO_CAT_SUBTOTAL. */
308 size_t subtotal_index;
310 /* Two elements: CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW.
311 One element: CTPO_NEG. */
312 struct ctables_pcexpr *subs[2];
315 /* Source location. */
316 struct msg_location *location;
319 static void ctables_pcexpr_destroy (struct ctables_pcexpr *);
320 static struct ctables_pcexpr *ctables_pcexpr_allocate_binary (
321 enum ctables_postcompute_op, struct ctables_pcexpr *sub0,
322 struct ctables_pcexpr *sub1);
324 struct ctables_summary_spec_set
326 struct ctables_summary_spec *specs;
330 struct variable *var;
333 static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
334 const struct ctables_summary_spec_set *);
335 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
337 /* A nested sequence of variables, e.g. a > b > c. */
340 struct variable **vars;
343 size_t *domains[N_CTDTS];
344 size_t n_domains[N_CTDTS];
346 struct ctables_summary_spec_set specs[N_CSVS];
349 /* A stack of nestings, e.g. nest1 + nest2 + ... + nestN. */
352 struct ctables_nest *nests;
358 struct hmap_node node;
363 struct ctables_occurrence
365 struct hmap_node node;
369 struct ctables_section
371 struct ctables_table *table;
372 struct ctables_nest *nests[PIVOT_N_AXES];
373 struct hmap *occurrences[PIVOT_N_AXES];
374 struct hmap cells; /* Contains "struct ctable_cell"s. */
375 struct hmap domains[N_CTDTS]; /* Contains "struct ctable_domain"s. */
380 struct ctables *ctables;
381 struct ctables_axis *axes[PIVOT_N_AXES];
382 struct ctables_stack stacks[PIVOT_N_AXES];
383 struct ctables_section *sections;
385 enum pivot_axis_type summary_axis;
386 struct ctables_summary_spec_set summary_specs;
388 const struct variable *clabels_example;
389 struct hmap clabels_values_map;
390 struct ctables_value **clabels_values;
391 size_t n_clabels_values;
393 enum pivot_axis_type slabels_axis;
394 bool slabels_visible;
396 /* The innermost category labels for axis 'a' appear on axis label_axis[a].
398 Most commonly, label_axis[a] == a, and in particular we always have
399 label_axis{PIVOT_AXIS_LAYER] == PIVOT_AXIS_LAYER.
401 If ROWLABELS or COLLABELS is specified, then one of
402 label_axis[PIVOT_AXIS_ROW] or label_axis[PIVOT_AXIS_COLUMN] can be the
403 opposite axis or PIVOT_AXIS_LAYER. Only one of them will differ.
405 enum pivot_axis_type label_axis[PIVOT_N_AXES];
406 enum pivot_axis_type clabels_from_axis;
408 /* Indexed by variable dictionary index. */
409 struct ctables_categories **categories;
418 struct ctables_chisq *chisq;
419 struct ctables_pairwise *pairwise;
427 struct variable *var;
428 const struct mrset *mrset;
432 static const struct fmt_spec *
433 ctables_var_get_print_format (const struct ctables_var *var)
435 return (var->is_mrset
436 ? var_get_print_format (var->mrset->vars[0])
437 : var_get_print_format (var->var));
441 ctables_var_name (const struct ctables_var *var)
443 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
446 struct ctables_categories
449 struct ctables_category *cats;
454 struct ctables_category
456 enum ctables_category_type
458 /* Explicit category lists. */
466 /* Totals and subtotals. */
470 /* Implicit category lists. */
477 struct ctables_category *subtotal;
483 double number; /* CCT_NUMBER. */
484 char *string; /* CCT_STRING. */
485 double range[2]; /* CCT_RANGE. */
489 char *total_label; /* CCT_SUBTOTAL, CCT_TOTAL. */
490 bool hide_subcategories; /* CCT_SUBTOTAL. */
493 const struct ctables_postcompute *pc; /* CCT_POSTCOMPUTE. */
495 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
498 bool include_missing;
502 enum ctables_summary_function sort_function;
503 struct variable *sort_var;
508 /* Source location. This is null for CCT_TOTAL, CCT_VALUE, CCT_LABEL,
510 struct msg_location *location;
514 ctables_category_uninit (struct ctables_category *cat)
525 case CCT_POSTCOMPUTE:
534 free (cat->total_label);
545 ctables_category_equal (const struct ctables_category *a,
546 const struct ctables_category *b)
548 if (a->type != b->type)
554 return a->number == b->number;
557 return strcmp (a->string, b->string);
560 return a->range[0] == b->range[0] && a->range[1] == b->range[1];
566 case CCT_POSTCOMPUTE:
567 return a->pc == b->pc;
571 return !strcmp (a->total_label, b->total_label);
576 return (a->include_missing == b->include_missing
577 && a->sort_ascending == b->sort_ascending
578 && a->sort_function == b->sort_function
579 && a->sort_var == b->sort_var
580 && a->percentile == b->percentile);
587 ctables_categories_unref (struct ctables_categories *c)
592 assert (c->n_refs > 0);
596 for (size_t i = 0; i < c->n_cats; i++)
597 ctables_category_uninit (&c->cats[i]);
603 ctables_categories_equal (const struct ctables_categories *a,
604 const struct ctables_categories *b)
606 if (a->n_cats != b->n_cats || a->show_empty != b->show_empty)
609 for (size_t i = 0; i < a->n_cats; i++)
610 if (!ctables_category_equal (&a->cats[i], &b->cats[i]))
616 /* Chi-square test (SIGTEST). */
624 /* Pairwise comparison test (COMPARETEST). */
625 struct ctables_pairwise
627 enum { PROP, MEAN } type;
630 bool meansvariance_allcats;
632 enum { BONFERRONI = 1, BH } adjust;
656 struct ctables_var var;
658 struct ctables_summary_spec_set specs[N_CSVS];
662 struct ctables_axis *subs[2];
665 struct msg_location *loc;
668 static void ctables_axis_destroy (struct ctables_axis *);
677 enum ctables_function_availability
679 CTFA_ALL, /* Any variables. */
680 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
681 CTFA_MRSETS, /* Only multiple-response sets */
684 struct ctables_summary_spec
686 enum ctables_summary_function function;
687 double percentile; /* CTSF_PTILE only. */
689 struct fmt_spec format; /* XXX extra CTABLES formats */
694 ctables_summary_spec_clone (struct ctables_summary_spec *dst,
695 const struct ctables_summary_spec *src)
698 dst->label = xstrdup (src->label);
702 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
709 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
710 const struct ctables_summary_spec_set *src)
712 struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
713 for (size_t i = 0; i < src->n; i++)
714 ctables_summary_spec_clone (&specs[i], &src->specs[i]);
716 *dst = (struct ctables_summary_spec_set) {
725 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
727 for (size_t i = 0; i < set->n; i++)
728 ctables_summary_spec_uninit (&set->specs[i]);
733 parse_col_width (struct lexer *lexer, const char *name, double *width)
735 lex_match (lexer, T_EQUALS);
736 if (lex_match_id (lexer, "DEFAULT"))
738 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
740 *width = lex_number (lexer);
750 parse_bool (struct lexer *lexer, bool *b)
752 if (lex_match_id (lexer, "NO"))
754 else if (lex_match_id (lexer, "YES"))
758 lex_error_expecting (lexer, "YES", "NO");
764 static enum ctables_function_availability
765 ctables_function_availability (enum ctables_summary_function f)
767 static enum ctables_function_availability availability[] = {
768 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
773 return availability[f];
777 ctables_summary_function_is_count (enum ctables_summary_function f)
779 static const bool is_count[N_CTSF_FUNCTIONS] = {
781 [CTSF_ECOUNT] = true,
782 [CTSF_ROWPCT_COUNT] = true,
783 [CTSF_COLPCT_COUNT] = true,
784 [CTSF_TABLEPCT_COUNT] = true,
785 [CTSF_SUBTABLEPCT_COUNT] = true,
786 [CTSF_LAYERPCT_COUNT] = true,
787 [CTSF_LAYERROWPCT_COUNT] = true,
788 [CTSF_LAYERCOLPCT_COUNT] = true,
789 [CTSF_ROWPCT_RESPONSES_COUNT] = true,
790 [CTSF_COLPCT_RESPONSES_COUNT] = true,
791 [CTSF_TABLEPCT_RESPONSES_COUNT] = true,
792 [CTSF_SUBTABLEPCT_RESPONSES_COUNT] = true,
793 [CTSF_LAYERPCT_RESPONSES_COUNT] = true,
794 [CTSF_LAYERROWPCT_RESPONSES_COUNT] = true,
795 [CTSF_LAYERCOLPCT_RESPONSES_COUNT] = true,
796 [CTSF_ROWPCT_COUNT_RESPONSES] = true,
797 [CTSF_COLPCT_COUNT_RESPONSES] = true,
798 [CTSF_TABLEPCT_COUNT_RESPONSES] = true,
799 [CTSF_SUBTABLEPCT_COUNT_RESPONSES] = true,
800 [CTSF_LAYERPCT_COUNT_RESPONSES] = true,
801 [CTSF_LAYERROWPCT_COUNT_RESPONSES] = true,
802 [CTSF_LAYERCOLPCT_COUNT_RESPONSES] = true,
809 parse_ctables_summary_function (struct lexer *lexer,
810 enum ctables_summary_function *f)
814 enum ctables_summary_function function;
815 struct substring name;
817 static struct pair names[] = {
818 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
819 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
822 /* The .COUNT suffix may be omitted. */
823 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
824 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
825 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
826 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
827 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
828 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
829 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
833 if (!lex_force_id (lexer))
836 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
837 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
839 *f = names[i].function;
844 lex_error (lexer, _("Expecting summary function name."));
849 ctables_axis_destroy (struct ctables_axis *axis)
857 for (size_t i = 0; i < N_CSVS; i++)
858 ctables_summary_spec_set_uninit (&axis->specs[i]);
863 ctables_axis_destroy (axis->subs[0]);
864 ctables_axis_destroy (axis->subs[1]);
867 msg_location_destroy (axis->loc);
871 static struct ctables_axis *
872 ctables_axis_new_nonterminal (enum ctables_axis_op op,
873 struct ctables_axis *sub0,
874 struct ctables_axis *sub1,
875 struct lexer *lexer, int start_ofs)
877 struct ctables_axis *axis = xmalloc (sizeof *axis);
878 *axis = (struct ctables_axis) {
880 .subs = { sub0, sub1 },
881 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
886 struct ctables_axis_parse_ctx
889 struct dictionary *dict;
891 struct ctables_table *t;
894 static struct fmt_spec
895 ctables_summary_default_format (enum ctables_summary_function function,
896 const struct ctables_var *var)
898 static const enum ctables_format default_formats[] = {
899 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
903 switch (default_formats[function])
906 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
909 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
912 return *ctables_var_get_print_format (var);
920 ctables_summary_default_label (enum ctables_summary_function function,
923 static const char *default_labels[] = {
924 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
929 return (function == CTSF_PTILE
930 ? xasprintf (_("Percentile %.2f"), percentile)
931 : xstrdup (gettext (default_labels[function])));
935 ctables_summary_function_name (enum ctables_summary_function function)
937 static const char *names[] = {
938 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
942 return names[function];
946 add_summary_spec (struct ctables_axis *axis,
947 enum ctables_summary_function function, double percentile,
948 const char *label, const struct fmt_spec *format,
949 const struct msg_location *loc, enum ctables_summary_variant sv)
951 if (axis->op == CTAO_VAR)
953 const char *function_name = ctables_summary_function_name (function);
954 const char *var_name = ctables_var_name (&axis->var);
955 switch (ctables_function_availability (function))
958 if (!axis->var.is_mrset)
960 msg_at (SE, loc, _("Summary function %s applies only to multiple "
961 "response sets."), function_name);
962 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
972 _("Summary function %s applies only to scale variables."),
974 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
984 struct ctables_summary_spec_set *set = &axis->specs[sv];
985 if (set->n >= set->allocated)
986 set->specs = x2nrealloc (set->specs, &set->allocated,
989 struct ctables_summary_spec *dst = &set->specs[set->n++];
990 *dst = (struct ctables_summary_spec) {
991 .function = function,
992 .percentile = percentile,
993 .label = xstrdup (label),
994 .format = (format ? *format
995 : ctables_summary_default_format (function, &axis->var)),
1001 for (size_t i = 0; i < 2; i++)
1002 if (!add_summary_spec (axis->subs[i], function, percentile, label,
1009 static struct ctables_axis *ctables_axis_parse_stack (
1010 struct ctables_axis_parse_ctx *);
1013 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
1014 struct ctables_var *var)
1016 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
1018 *var = (struct ctables_var) {
1020 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
1024 lex_error (lexer, _("'%s' does not name a multiple-response set "
1025 "in the active file dictionary."),
1026 lex_tokcstr (lexer));
1034 *var = (struct ctables_var) {
1036 .var = parse_variable (lexer, dict),
1038 return var->var != NULL;
1042 static struct ctables_axis *
1043 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
1045 if (lex_match (ctx->lexer, T_LPAREN))
1047 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
1048 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
1050 ctables_axis_destroy (sub);
1056 if (!lex_force_id (ctx->lexer))
1059 int start_ofs = lex_ofs (ctx->lexer);
1060 struct ctables_var var;
1061 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
1064 struct ctables_axis *axis = xmalloc (sizeof *axis);
1065 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
1067 /* XXX should figure out default measures by reading data */
1068 axis->scale = (var.is_mrset ? false
1069 : lex_match_phrase (ctx->lexer, "[S]") ? true
1070 : lex_match_phrase (ctx->lexer, "[C]") ? false
1071 : var_get_measure (var.var) == MEASURE_SCALE);
1072 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
1073 lex_ofs (ctx->lexer) - 1);
1078 has_digit (const char *s)
1080 return s[strcspn (s, "0123456789")] != '\0';
1083 static struct ctables_axis *
1084 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
1086 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
1087 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
1090 enum ctables_summary_variant sv = CSV_CELL;
1093 int start_ofs = lex_ofs (ctx->lexer);
1095 /* Parse function. */
1096 enum ctables_summary_function function;
1097 if (!parse_ctables_summary_function (ctx->lexer, &function))
1100 /* Parse percentile. */
1101 double percentile = 0;
1102 if (function == CTSF_PTILE)
1104 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
1106 percentile = lex_number (ctx->lexer);
1107 lex_get (ctx->lexer);
1112 if (lex_is_string (ctx->lexer))
1114 label = ss_xstrdup (lex_tokss (ctx->lexer));
1115 lex_get (ctx->lexer);
1118 label = ctables_summary_default_label (function, percentile);
1121 struct fmt_spec format;
1122 const struct fmt_spec *formatp;
1123 if (lex_token (ctx->lexer) == T_ID
1124 && has_digit (lex_tokcstr (ctx->lexer)))
1126 if (!parse_format_specifier (ctx->lexer, &format)
1127 || !fmt_check_output (&format)
1128 || !fmt_check_type_compat (&format, VAL_NUMERIC))
1138 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
1139 lex_ofs (ctx->lexer) - 1);
1140 add_summary_spec (sub, function, percentile, label, formatp, loc, sv);
1142 msg_location_destroy (loc);
1144 lex_match (ctx->lexer, T_COMMA);
1145 if (sv == CSV_CELL && lex_match_id (ctx->lexer, "TOTALS"))
1147 if (!lex_force_match (ctx->lexer, T_LBRACK))
1151 else if (lex_match (ctx->lexer, T_RBRACK))
1153 if (sv == CSV_TOTAL && !lex_force_match (ctx->lexer, T_RBRACK))
1160 ctables_axis_destroy (sub);
1164 static const struct ctables_axis *
1165 find_scale (const struct ctables_axis *axis)
1169 else if (axis->op == CTAO_VAR)
1173 assert (!axis->var.is_mrset);
1181 for (size_t i = 0; i < 2; i++)
1183 const struct ctables_axis *scale = find_scale (axis->subs[i]);
1191 static const struct ctables_axis *
1192 find_categorical_summary_spec (const struct ctables_axis *axis)
1196 else if (axis->op == CTAO_VAR)
1197 return !axis->scale && axis->specs[CSV_CELL].n ? axis : NULL;
1200 for (size_t i = 0; i < 2; i++)
1202 const struct ctables_axis *sum
1203 = find_categorical_summary_spec (axis->subs[i]);
1211 static struct ctables_axis *
1212 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1214 int start_ofs = lex_ofs (ctx->lexer);
1215 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1219 while (lex_match (ctx->lexer, T_GT))
1221 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1225 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1226 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1228 const struct ctables_axis *outer_scale = find_scale (lhs);
1229 const struct ctables_axis *inner_scale = find_scale (rhs);
1230 if (outer_scale && inner_scale)
1232 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1233 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1234 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1235 ctables_axis_destroy (nest);
1239 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1242 msg_at (SE, nest->loc,
1243 _("Summaries may only be requested for categorical variables "
1244 "at the innermost nesting level."));
1245 msg_at (SN, outer_sum->loc,
1246 _("This outer categorical variable has a summary."));
1247 ctables_axis_destroy (nest);
1257 static struct ctables_axis *
1258 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1260 int start_ofs = lex_ofs (ctx->lexer);
1261 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1265 while (lex_match (ctx->lexer, T_PLUS))
1267 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1271 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1272 ctx->lexer, start_ofs);
1279 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1280 struct ctables *ct, struct ctables_table *t,
1281 enum pivot_axis_type a)
1283 if (lex_token (lexer) == T_BY
1284 || lex_token (lexer) == T_SLASH
1285 || lex_token (lexer) == T_ENDCMD)
1288 struct ctables_axis_parse_ctx ctx = {
1294 t->axes[a] = ctables_axis_parse_stack (&ctx);
1295 return t->axes[a] != NULL;
1299 ctables_chisq_destroy (struct ctables_chisq *chisq)
1305 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1311 ctables_table_destroy (struct ctables_table *t)
1316 for (size_t i = 0; i < t->n_categories; i++)
1317 ctables_categories_unref (t->categories[i]);
1318 free (t->categories);
1320 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1321 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1322 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1326 ctables_chisq_destroy (t->chisq);
1327 ctables_pairwise_destroy (t->pairwise);
1332 ctables_destroy (struct ctables *ct)
1337 pivot_table_look_unref (ct->look);
1341 for (size_t i = 0; i < ct->n_tables; i++)
1342 ctables_table_destroy (ct->tables[i]);
1347 static struct ctables_category
1348 cct_range (double low, double high)
1350 return (struct ctables_category) {
1352 .range = { low, high }
1357 ctables_table_parse_subtotal (struct lexer *lexer, bool hide_subcategories,
1358 struct ctables_category *cat)
1361 if (lex_match (lexer, T_EQUALS))
1363 if (!lex_force_string (lexer))
1366 total_label = ss_xstrdup (lex_tokss (lexer));
1370 total_label = xstrdup (_("Subtotal"));
1372 *cat = (struct ctables_category) {
1373 .type = CCT_SUBTOTAL,
1374 .hide_subcategories = hide_subcategories,
1375 .total_label = total_label
1381 ctables_table_parse_explicit_category (struct lexer *lexer, struct ctables *ct,
1382 struct ctables_category *cat)
1384 if (lex_match_id (lexer, "OTHERNM"))
1385 *cat = (struct ctables_category) { .type = CCT_OTHERNM };
1386 else if (lex_match_id (lexer, "MISSING"))
1387 *cat = (struct ctables_category) { .type = CCT_MISSING };
1388 else if (lex_match_id (lexer, "SUBTOTAL"))
1389 return ctables_table_parse_subtotal (lexer, false, cat);
1390 else if (lex_match_id (lexer, "HSUBTOTAL"))
1391 return ctables_table_parse_subtotal (lexer, true, cat);
1392 else if (lex_match_id (lexer, "LO"))
1394 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1396 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1399 else if (lex_is_number (lexer))
1401 double number = lex_number (lexer);
1403 if (lex_match_id (lexer, "THRU"))
1405 if (lex_match_id (lexer, "HI"))
1406 *cat = cct_range (number, DBL_MAX);
1409 if (!lex_force_num (lexer))
1411 *cat = cct_range (number, lex_number (lexer));
1416 *cat = (struct ctables_category) {
1421 else if (lex_is_string (lexer))
1423 *cat = (struct ctables_category) {
1425 .string = ss_xstrdup (lex_tokss (lexer)),
1429 else if (lex_match (lexer, T_AND))
1431 if (!lex_force_id (lexer))
1433 struct ctables_postcompute *pc = ctables_find_postcompute (
1434 ct, lex_tokcstr (lexer));
1437 struct msg_location *loc = lex_get_location (lexer, -1, 0);
1438 msg_at (SE, loc, _("Unknown postcompute &%s."),
1439 lex_tokcstr (lexer));
1440 msg_location_destroy (loc);
1445 *cat = (struct ctables_category) { .type = CCT_POSTCOMPUTE, .pc = pc };
1449 lex_error (lexer, NULL);
1456 static struct ctables_category *
1457 ctables_find_category_for_postcompute (const struct ctables_categories *cats,
1458 const struct ctables_pcexpr *e)
1460 struct ctables_category *best = NULL;
1461 size_t n_subtotals = 0;
1462 for (size_t i = 0; i < cats->n_cats; i++)
1464 struct ctables_category *cat = &cats->cats[i];
1467 case CTPO_CAT_NUMBER:
1468 if (cat->type == CCT_NUMBER && cat->number == e->number)
1472 case CTPO_CAT_STRING:
1473 if (cat->type == CCT_STRING && !strcmp (cat->string, e->string))
1477 case CTPO_CAT_RANGE:
1478 if (cat->type == CCT_RANGE
1479 && cat->range[0] == e->range[0]
1480 && cat->range[1] == e->range[1])
1484 case CTPO_CAT_MISSING:
1485 if (cat->type == CCT_MISSING)
1489 case CTPO_CAT_OTHERNM:
1490 if (cat->type == CCT_OTHERNM)
1494 case CTPO_CAT_SUBTOTAL:
1495 if (cat->type == CCT_SUBTOTAL)
1498 if (e->subtotal_index == n_subtotals)
1500 else if (e->subtotal_index == 0)
1505 case CTPO_CAT_TOTAL:
1506 if (cat->type == CCT_TOTAL)
1520 if (e->op == CTPO_CAT_SUBTOTAL && e->subtotal_index == 0 && n_subtotals > 1)
1526 ctables_recursive_check_postcompute (const struct ctables_pcexpr *e,
1527 struct ctables_category *pc_cat,
1528 const struct ctables_categories *cats,
1529 const struct msg_location *cats_location)
1533 case CTPO_CAT_NUMBER:
1534 case CTPO_CAT_STRING:
1535 case CTPO_CAT_RANGE:
1536 case CTPO_CAT_MISSING:
1537 case CTPO_CAT_OTHERNM:
1538 case CTPO_CAT_SUBTOTAL:
1539 case CTPO_CAT_TOTAL:
1541 struct ctables_category *cat = ctables_find_category_for_postcompute (
1545 if (e->op == CTPO_CAT_SUBTOTAL && e->subtotal_index == 0)
1547 size_t n_subtotals = 0;
1548 for (size_t i = 0; i < cats->n_cats; i++)
1549 n_subtotals += cats->cats[i].type == CCT_SUBTOTAL;
1550 if (n_subtotals > 1)
1552 msg_at (SE, cats_location,
1553 ngettext ("These categories include %zu instance "
1554 "of SUBTOTAL or HSUBTOTAL, so references "
1555 "from computed categories must refer to "
1556 "subtotals by position.",
1557 "These categories include %zu instances "
1558 "of SUBTOTAL or HSUBTOTAL, so references "
1559 "from computed categories must refer to "
1560 "subtotals by position.",
1563 msg_at (SN, e->location,
1564 _("This is the reference that lacks a position."));
1569 msg_at (SE, pc_cat->location,
1570 _("Computed category &%s references a category not included "
1571 "in the category list."),
1573 msg_at (SN, e->location, _("This is the missing category."));
1574 msg_at (SN, cats_location,
1575 _("To fix the problem, add the missing category to the "
1576 "list of categories here."));
1579 if (pc_cat->pc->hide_source_cats)
1593 for (size_t i = 0; i < 2; i++)
1594 if (e->subs[i] && !ctables_recursive_check_postcompute (
1595 e->subs[i], pc_cat, cats, cats_location))
1605 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1606 struct ctables *ct, struct ctables_table *t)
1608 if (!lex_match_id (lexer, "VARIABLES"))
1610 lex_match (lexer, T_EQUALS);
1612 struct variable **vars;
1614 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1617 struct ctables_categories *c = xmalloc (sizeof *c);
1618 *c = (struct ctables_categories) { .n_refs = n_vars, .show_empty = true };
1619 for (size_t i = 0; i < n_vars; i++)
1621 struct ctables_categories **cp
1622 = &t->categories[var_get_dict_index (vars[i])];
1623 ctables_categories_unref (*cp);
1628 size_t allocated_cats = 0;
1629 if (lex_match (lexer, T_LBRACK))
1631 int cats_start_ofs = lex_ofs (lexer);
1634 if (c->n_cats >= allocated_cats)
1635 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1637 int start_ofs = lex_ofs (lexer);
1638 struct ctables_category *cat = &c->cats[c->n_cats];
1639 if (!ctables_table_parse_explicit_category (lexer, ct, cat))
1641 cat->location = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1);
1644 lex_match (lexer, T_COMMA);
1646 while (!lex_match (lexer, T_RBRACK));
1648 struct msg_location *cats_location
1649 = lex_ofs_location (lexer, cats_start_ofs, lex_ofs (lexer) - 1);
1650 for (size_t i = 0; i < c->n_cats; i++)
1652 struct ctables_category *cat = &c->cats[i];
1653 if (cat->type == CCT_POSTCOMPUTE
1654 && !ctables_recursive_check_postcompute (cat->pc->expr, cat,
1660 struct ctables_category cat = {
1662 .include_missing = false,
1663 .sort_ascending = true,
1665 bool show_totals = false;
1666 char *total_label = NULL;
1667 bool totals_before = false;
1668 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1670 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1672 lex_match (lexer, T_EQUALS);
1673 if (lex_match_id (lexer, "A"))
1674 cat.sort_ascending = true;
1675 else if (lex_match_id (lexer, "D"))
1676 cat.sort_ascending = false;
1679 lex_error_expecting (lexer, "A", "D");
1683 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1685 lex_match (lexer, T_EQUALS);
1686 if (lex_match_id (lexer, "VALUE"))
1687 cat.type = CCT_VALUE;
1688 else if (lex_match_id (lexer, "LABEL"))
1689 cat.type = CCT_LABEL;
1692 cat.type = CCT_FUNCTION;
1693 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1696 if (lex_match (lexer, T_LPAREN))
1698 cat.sort_var = parse_variable (lexer, dict);
1702 if (cat.sort_function == CTSF_PTILE)
1704 lex_match (lexer, T_COMMA);
1705 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1707 cat.percentile = lex_number (lexer);
1711 if (!lex_force_match (lexer, T_RPAREN))
1714 else if (ctables_function_availability (cat.sort_function)
1717 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1722 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1724 lex_match (lexer, T_EQUALS);
1725 if (lex_match_id (lexer, "INCLUDE"))
1726 cat.include_missing = true;
1727 else if (lex_match_id (lexer, "EXCLUDE"))
1728 cat.include_missing = false;
1731 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1735 else if (lex_match_id (lexer, "TOTAL"))
1737 lex_match (lexer, T_EQUALS);
1738 if (!parse_bool (lexer, &show_totals))
1741 else if (lex_match_id (lexer, "LABEL"))
1743 lex_match (lexer, T_EQUALS);
1744 if (!lex_force_string (lexer))
1747 total_label = ss_xstrdup (lex_tokss (lexer));
1750 else if (lex_match_id (lexer, "POSITION"))
1752 lex_match (lexer, T_EQUALS);
1753 if (lex_match_id (lexer, "BEFORE"))
1754 totals_before = true;
1755 else if (lex_match_id (lexer, "AFTER"))
1756 totals_before = false;
1759 lex_error_expecting (lexer, "BEFORE", "AFTER");
1763 else if (lex_match_id (lexer, "EMPTY"))
1765 lex_match (lexer, T_EQUALS);
1766 if (lex_match_id (lexer, "INCLUDE"))
1767 c->show_empty = true;
1768 else if (lex_match_id (lexer, "EXCLUDE"))
1769 c->show_empty = false;
1772 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1779 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1780 "TOTAL", "LABEL", "POSITION", "EMPTY");
1782 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1789 if (c->n_cats >= allocated_cats)
1790 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1791 c->cats[c->n_cats++] = cat;
1796 if (c->n_cats >= allocated_cats)
1797 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1799 struct ctables_category *totals;
1802 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1803 totals = &c->cats[0];
1806 totals = &c->cats[c->n_cats];
1809 *totals = (struct ctables_category) {
1811 .total_label = total_label ? total_label : xstrdup (_("Total")),
1815 struct ctables_category *subtotal = NULL;
1816 for (size_t i = totals_before ? 0 : c->n_cats;
1817 totals_before ? i < c->n_cats : i-- > 0;
1818 totals_before ? i++ : 0)
1820 struct ctables_category *cat = &c->cats[i];
1828 cat->subtotal = subtotal;
1831 case CCT_POSTCOMPUTE:
1850 ctables_nest_uninit (struct ctables_nest *nest)
1857 ctables_stack_uninit (struct ctables_stack *stack)
1861 for (size_t i = 0; i < stack->n; i++)
1862 ctables_nest_uninit (&stack->nests[i]);
1863 free (stack->nests);
1867 static struct ctables_stack
1868 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1875 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1876 for (size_t i = 0; i < s0.n; i++)
1877 for (size_t j = 0; j < s1.n; j++)
1879 const struct ctables_nest *a = &s0.nests[i];
1880 const struct ctables_nest *b = &s1.nests[j];
1882 size_t allocate = a->n + b->n;
1883 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1884 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1886 for (size_t k = 0; k < a->n; k++)
1887 vars[n++] = a->vars[k];
1888 for (size_t k = 0; k < b->n; k++)
1889 vars[n++] = b->vars[k];
1890 assert (n == allocate);
1892 const struct ctables_nest *summary_src;
1893 if (!a->specs[CSV_CELL].var)
1895 else if (!b->specs[CSV_CELL].var)
1900 struct ctables_nest *new = &stack.nests[stack.n++];
1901 *new = (struct ctables_nest) {
1903 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1904 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1908 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1909 ctables_summary_spec_set_clone (&new->specs[sv], &summary_src->specs[sv]);
1911 ctables_stack_uninit (&s0);
1912 ctables_stack_uninit (&s1);
1916 static struct ctables_stack
1917 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1919 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1920 for (size_t i = 0; i < s0.n; i++)
1921 stack.nests[stack.n++] = s0.nests[i];
1922 for (size_t i = 0; i < s1.n; i++)
1923 stack.nests[stack.n++] = s1.nests[i];
1924 assert (stack.n == s0.n + s1.n);
1930 static struct ctables_stack
1931 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1934 return (struct ctables_stack) { .n = 0 };
1939 assert (!a->var.is_mrset);
1941 struct variable **vars = xmalloc (sizeof *vars);
1944 struct ctables_nest *nest = xmalloc (sizeof *nest);
1945 *nest = (struct ctables_nest) {
1948 .scale_idx = a->scale ? 0 : SIZE_MAX,
1950 if (a->specs[CSV_CELL].n || a->scale)
1951 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1953 ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
1954 nest->specs[sv].var = a->var.var;
1956 return (struct ctables_stack) { .nests = nest, .n = 1 };
1959 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1960 enumerate_fts (axis_type, a->subs[1]));
1963 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1964 enumerate_fts (axis_type, a->subs[1]));
1970 union ctables_summary
1972 /* COUNT, VALIDN, TOTALN. */
1979 /* MINIMUM, MAXIMUM, RANGE. */
1986 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1987 struct moments1 *moments;
1989 /* MEDIAN, MODE, PTILE. */
1992 struct casewriter *writer;
1997 /* XXX multiple response */
2001 ctables_summary_init (union ctables_summary *s,
2002 const struct ctables_summary_spec *ss)
2004 switch (ss->function)
2008 case CTSF_ROWPCT_COUNT:
2009 case CTSF_COLPCT_COUNT:
2010 case CTSF_TABLEPCT_COUNT:
2011 case CTSF_SUBTABLEPCT_COUNT:
2012 case CTSF_LAYERPCT_COUNT:
2013 case CTSF_LAYERROWPCT_COUNT:
2014 case CTSF_LAYERCOLPCT_COUNT:
2015 case CTSF_ROWPCT_VALIDN:
2016 case CTSF_COLPCT_VALIDN:
2017 case CTSF_TABLEPCT_VALIDN:
2018 case CTSF_SUBTABLEPCT_VALIDN:
2019 case CTSF_LAYERPCT_VALIDN:
2020 case CTSF_LAYERROWPCT_VALIDN:
2021 case CTSF_LAYERCOLPCT_VALIDN:
2022 case CTSF_ROWPCT_TOTALN:
2023 case CTSF_COLPCT_TOTALN:
2024 case CTSF_TABLEPCT_TOTALN:
2025 case CTSF_SUBTABLEPCT_TOTALN:
2026 case CTSF_LAYERPCT_TOTALN:
2027 case CTSF_LAYERROWPCT_TOTALN:
2028 case CTSF_LAYERCOLPCT_TOTALN:
2034 s->missing = s->valid = 0;
2040 s->min = s->max = SYSMIS;
2048 case CTSF_ROWPCT_SUM:
2049 case CTSF_COLPCT_SUM:
2050 case CTSF_TABLEPCT_SUM:
2051 case CTSF_SUBTABLEPCT_SUM:
2052 case CTSF_LAYERPCT_SUM:
2053 case CTSF_LAYERROWPCT_SUM:
2054 case CTSF_LAYERCOLPCT_SUM:
2055 s->moments = moments1_create (MOMENT_VARIANCE);
2062 struct caseproto *proto = caseproto_create ();
2063 proto = caseproto_add_width (proto, 0);
2064 proto = caseproto_add_width (proto, 0);
2066 struct subcase ordering;
2067 subcase_init (&ordering, 0, 0, SC_ASCEND);
2068 s->writer = sort_create_writer (&ordering, proto);
2069 subcase_uninit (&ordering);
2070 caseproto_unref (proto);
2077 case CTSF_RESPONSES:
2078 case CTSF_ROWPCT_RESPONSES:
2079 case CTSF_COLPCT_RESPONSES:
2080 case CTSF_TABLEPCT_RESPONSES:
2081 case CTSF_SUBTABLEPCT_RESPONSES:
2082 case CTSF_LAYERPCT_RESPONSES:
2083 case CTSF_LAYERROWPCT_RESPONSES:
2084 case CTSF_LAYERCOLPCT_RESPONSES:
2085 case CTSF_ROWPCT_RESPONSES_COUNT:
2086 case CTSF_COLPCT_RESPONSES_COUNT:
2087 case CTSF_TABLEPCT_RESPONSES_COUNT:
2088 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2089 case CTSF_LAYERPCT_RESPONSES_COUNT:
2090 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2091 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2092 case CTSF_ROWPCT_COUNT_RESPONSES:
2093 case CTSF_COLPCT_COUNT_RESPONSES:
2094 case CTSF_TABLEPCT_COUNT_RESPONSES:
2095 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2096 case CTSF_LAYERPCT_COUNT_RESPONSES:
2097 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2098 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2104 ctables_summary_uninit (union ctables_summary *s,
2105 const struct ctables_summary_spec *ss)
2107 switch (ss->function)
2111 case CTSF_ROWPCT_COUNT:
2112 case CTSF_COLPCT_COUNT:
2113 case CTSF_TABLEPCT_COUNT:
2114 case CTSF_SUBTABLEPCT_COUNT:
2115 case CTSF_LAYERPCT_COUNT:
2116 case CTSF_LAYERROWPCT_COUNT:
2117 case CTSF_LAYERCOLPCT_COUNT:
2118 case CTSF_ROWPCT_VALIDN:
2119 case CTSF_COLPCT_VALIDN:
2120 case CTSF_TABLEPCT_VALIDN:
2121 case CTSF_SUBTABLEPCT_VALIDN:
2122 case CTSF_LAYERPCT_VALIDN:
2123 case CTSF_LAYERROWPCT_VALIDN:
2124 case CTSF_LAYERCOLPCT_VALIDN:
2125 case CTSF_ROWPCT_TOTALN:
2126 case CTSF_COLPCT_TOTALN:
2127 case CTSF_TABLEPCT_TOTALN:
2128 case CTSF_SUBTABLEPCT_TOTALN:
2129 case CTSF_LAYERPCT_TOTALN:
2130 case CTSF_LAYERROWPCT_TOTALN:
2131 case CTSF_LAYERCOLPCT_TOTALN:
2149 case CTSF_ROWPCT_SUM:
2150 case CTSF_COLPCT_SUM:
2151 case CTSF_TABLEPCT_SUM:
2152 case CTSF_SUBTABLEPCT_SUM:
2153 case CTSF_LAYERPCT_SUM:
2154 case CTSF_LAYERROWPCT_SUM:
2155 case CTSF_LAYERCOLPCT_SUM:
2156 moments1_destroy (s->moments);
2162 casewriter_destroy (s->writer);
2165 case CTSF_RESPONSES:
2166 case CTSF_ROWPCT_RESPONSES:
2167 case CTSF_COLPCT_RESPONSES:
2168 case CTSF_TABLEPCT_RESPONSES:
2169 case CTSF_SUBTABLEPCT_RESPONSES:
2170 case CTSF_LAYERPCT_RESPONSES:
2171 case CTSF_LAYERROWPCT_RESPONSES:
2172 case CTSF_LAYERCOLPCT_RESPONSES:
2173 case CTSF_ROWPCT_RESPONSES_COUNT:
2174 case CTSF_COLPCT_RESPONSES_COUNT:
2175 case CTSF_TABLEPCT_RESPONSES_COUNT:
2176 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2177 case CTSF_LAYERPCT_RESPONSES_COUNT:
2178 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2179 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2180 case CTSF_ROWPCT_COUNT_RESPONSES:
2181 case CTSF_COLPCT_COUNT_RESPONSES:
2182 case CTSF_TABLEPCT_COUNT_RESPONSES:
2183 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2184 case CTSF_LAYERPCT_COUNT_RESPONSES:
2185 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2186 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2192 ctables_summary_add (union ctables_summary *s,
2193 const struct ctables_summary_spec *ss,
2194 const struct variable *var, const union value *value,
2195 double d_weight, double e_weight)
2197 switch (ss->function)
2202 if (var_is_value_missing (var, value))
2203 s->missing += d_weight;
2205 s->valid += d_weight;
2209 case CTSF_ROWPCT_COUNT:
2210 case CTSF_COLPCT_COUNT:
2211 case CTSF_TABLEPCT_COUNT:
2212 case CTSF_SUBTABLEPCT_COUNT:
2213 case CTSF_LAYERPCT_COUNT:
2214 case CTSF_LAYERROWPCT_COUNT:
2215 case CTSF_LAYERCOLPCT_COUNT:
2216 case CTSF_ROWPCT_VALIDN:
2217 case CTSF_COLPCT_VALIDN:
2218 case CTSF_TABLEPCT_VALIDN:
2219 case CTSF_SUBTABLEPCT_VALIDN:
2220 case CTSF_LAYERPCT_VALIDN:
2221 case CTSF_LAYERROWPCT_VALIDN:
2222 case CTSF_LAYERCOLPCT_VALIDN:
2223 case CTSF_ROWPCT_TOTALN:
2224 case CTSF_COLPCT_TOTALN:
2225 case CTSF_TABLEPCT_TOTALN:
2226 case CTSF_SUBTABLEPCT_TOTALN:
2227 case CTSF_LAYERPCT_TOTALN:
2228 case CTSF_LAYERROWPCT_TOTALN:
2229 case CTSF_LAYERCOLPCT_TOTALN:
2233 if (var_is_value_missing (var, value))
2234 s->missing += e_weight;
2236 s->valid += e_weight;
2242 if (!var_is_value_missing (var, value))
2244 assert (!var_is_alpha (var)); /* XXX? */
2245 if (s->min == SYSMIS || value->f < s->min)
2247 if (s->max == SYSMIS || value->f > s->max)
2257 case CTSF_ROWPCT_SUM:
2258 case CTSF_COLPCT_SUM:
2259 case CTSF_TABLEPCT_SUM:
2260 case CTSF_SUBTABLEPCT_SUM:
2261 case CTSF_LAYERPCT_SUM:
2262 case CTSF_LAYERROWPCT_SUM:
2263 case CTSF_LAYERCOLPCT_SUM:
2264 if (!var_is_value_missing (var, value))
2265 moments1_add (s->moments, value->f, e_weight);
2271 if (var_is_value_missing (var, value))
2273 s->ovalid += e_weight;
2275 struct ccase *c = case_create (casewriter_get_proto (s->writer));
2276 *case_num_rw_idx (c, 0) = value->f;
2277 *case_num_rw_idx (c, 1) = e_weight;
2278 casewriter_write (s->writer, c);
2282 case CTSF_RESPONSES:
2283 case CTSF_ROWPCT_RESPONSES:
2284 case CTSF_COLPCT_RESPONSES:
2285 case CTSF_TABLEPCT_RESPONSES:
2286 case CTSF_SUBTABLEPCT_RESPONSES:
2287 case CTSF_LAYERPCT_RESPONSES:
2288 case CTSF_LAYERROWPCT_RESPONSES:
2289 case CTSF_LAYERCOLPCT_RESPONSES:
2290 case CTSF_ROWPCT_RESPONSES_COUNT:
2291 case CTSF_COLPCT_RESPONSES_COUNT:
2292 case CTSF_TABLEPCT_RESPONSES_COUNT:
2293 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2294 case CTSF_LAYERPCT_RESPONSES_COUNT:
2295 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2296 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2297 case CTSF_ROWPCT_COUNT_RESPONSES:
2298 case CTSF_COLPCT_COUNT_RESPONSES:
2299 case CTSF_TABLEPCT_COUNT_RESPONSES:
2300 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2301 case CTSF_LAYERPCT_COUNT_RESPONSES:
2302 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2303 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2308 static enum ctables_domain_type
2309 ctables_function_domain (enum ctables_summary_function function)
2331 case CTSF_RESPONSES:
2334 case CTSF_COLPCT_COUNT:
2335 case CTSF_COLPCT_COUNT_RESPONSES:
2336 case CTSF_COLPCT_RESPONSES:
2337 case CTSF_COLPCT_RESPONSES_COUNT:
2338 case CTSF_COLPCT_SUM:
2339 case CTSF_COLPCT_TOTALN:
2340 case CTSF_COLPCT_VALIDN:
2343 case CTSF_LAYERCOLPCT_COUNT:
2344 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2345 case CTSF_LAYERCOLPCT_RESPONSES:
2346 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2347 case CTSF_LAYERCOLPCT_SUM:
2348 case CTSF_LAYERCOLPCT_TOTALN:
2349 case CTSF_LAYERCOLPCT_VALIDN:
2350 return CTDT_LAYERCOL;
2352 case CTSF_LAYERPCT_COUNT:
2353 case CTSF_LAYERPCT_COUNT_RESPONSES:
2354 case CTSF_LAYERPCT_RESPONSES:
2355 case CTSF_LAYERPCT_RESPONSES_COUNT:
2356 case CTSF_LAYERPCT_SUM:
2357 case CTSF_LAYERPCT_TOTALN:
2358 case CTSF_LAYERPCT_VALIDN:
2361 case CTSF_LAYERROWPCT_COUNT:
2362 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2363 case CTSF_LAYERROWPCT_RESPONSES:
2364 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2365 case CTSF_LAYERROWPCT_SUM:
2366 case CTSF_LAYERROWPCT_TOTALN:
2367 case CTSF_LAYERROWPCT_VALIDN:
2368 return CTDT_LAYERROW;
2370 case CTSF_ROWPCT_COUNT:
2371 case CTSF_ROWPCT_COUNT_RESPONSES:
2372 case CTSF_ROWPCT_RESPONSES:
2373 case CTSF_ROWPCT_RESPONSES_COUNT:
2374 case CTSF_ROWPCT_SUM:
2375 case CTSF_ROWPCT_TOTALN:
2376 case CTSF_ROWPCT_VALIDN:
2379 case CTSF_SUBTABLEPCT_COUNT:
2380 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2381 case CTSF_SUBTABLEPCT_RESPONSES:
2382 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2383 case CTSF_SUBTABLEPCT_SUM:
2384 case CTSF_SUBTABLEPCT_TOTALN:
2385 case CTSF_SUBTABLEPCT_VALIDN:
2386 return CTDT_SUBTABLE;
2388 case CTSF_TABLEPCT_COUNT:
2389 case CTSF_TABLEPCT_COUNT_RESPONSES:
2390 case CTSF_TABLEPCT_RESPONSES:
2391 case CTSF_TABLEPCT_RESPONSES_COUNT:
2392 case CTSF_TABLEPCT_SUM:
2393 case CTSF_TABLEPCT_TOTALN:
2394 case CTSF_TABLEPCT_VALIDN:
2402 ctables_summary_value (const struct ctables_cell *cell,
2403 union ctables_summary *s,
2404 const struct ctables_summary_spec *ss)
2406 switch (ss->function)
2412 case CTSF_ROWPCT_COUNT:
2413 case CTSF_COLPCT_COUNT:
2414 case CTSF_TABLEPCT_COUNT:
2415 case CTSF_SUBTABLEPCT_COUNT:
2416 case CTSF_LAYERPCT_COUNT:
2417 case CTSF_LAYERROWPCT_COUNT:
2418 case CTSF_LAYERCOLPCT_COUNT:
2420 enum ctables_domain_type d = ctables_function_domain (ss->function);
2421 return (cell->domains[d]->e_valid
2422 ? s->valid / cell->domains[d]->e_valid * 100
2426 case CTSF_ROWPCT_VALIDN:
2427 case CTSF_COLPCT_VALIDN:
2428 case CTSF_TABLEPCT_VALIDN:
2429 case CTSF_SUBTABLEPCT_VALIDN:
2430 case CTSF_LAYERPCT_VALIDN:
2431 case CTSF_LAYERROWPCT_VALIDN:
2432 case CTSF_LAYERCOLPCT_VALIDN:
2433 case CTSF_ROWPCT_TOTALN:
2434 case CTSF_COLPCT_TOTALN:
2435 case CTSF_TABLEPCT_TOTALN:
2436 case CTSF_SUBTABLEPCT_TOTALN:
2437 case CTSF_LAYERPCT_TOTALN:
2438 case CTSF_LAYERROWPCT_TOTALN:
2439 case CTSF_LAYERCOLPCT_TOTALN:
2447 return s->valid + s->missing;
2460 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
2465 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
2471 double weight, variance;
2472 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
2473 return calc_semean (variance, weight);
2479 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2480 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
2485 double weight, mean;
2486 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
2487 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
2493 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2497 case CTSF_ROWPCT_SUM:
2498 case CTSF_COLPCT_SUM:
2499 case CTSF_TABLEPCT_SUM:
2500 case CTSF_SUBTABLEPCT_SUM:
2501 case CTSF_LAYERPCT_SUM:
2502 case CTSF_LAYERROWPCT_SUM:
2503 case CTSF_LAYERCOLPCT_SUM:
2510 struct casereader *reader = casewriter_make_reader (s->writer);
2513 struct percentile *ptile = percentile_create (
2514 ss->function == CTSF_PTILE ? ss->percentile : 0.5, s->ovalid);
2515 struct order_stats *os = &ptile->parent;
2516 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2517 s->ovalue = percentile_calculate (ptile, PC_HAVERAGE);
2518 statistic_destroy (&ptile->parent.parent);
2525 struct casereader *reader = casewriter_make_reader (s->writer);
2528 struct mode *mode = mode_create ();
2529 struct order_stats *os = &mode->parent;
2530 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2531 s->ovalue = mode->mode;
2532 statistic_destroy (&mode->parent.parent);
2536 case CTSF_RESPONSES:
2537 case CTSF_ROWPCT_RESPONSES:
2538 case CTSF_COLPCT_RESPONSES:
2539 case CTSF_TABLEPCT_RESPONSES:
2540 case CTSF_SUBTABLEPCT_RESPONSES:
2541 case CTSF_LAYERPCT_RESPONSES:
2542 case CTSF_LAYERROWPCT_RESPONSES:
2543 case CTSF_LAYERCOLPCT_RESPONSES:
2544 case CTSF_ROWPCT_RESPONSES_COUNT:
2545 case CTSF_COLPCT_RESPONSES_COUNT:
2546 case CTSF_TABLEPCT_RESPONSES_COUNT:
2547 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2548 case CTSF_LAYERPCT_RESPONSES_COUNT:
2549 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2550 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2551 case CTSF_ROWPCT_COUNT_RESPONSES:
2552 case CTSF_COLPCT_COUNT_RESPONSES:
2553 case CTSF_TABLEPCT_COUNT_RESPONSES:
2554 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2555 case CTSF_LAYERPCT_COUNT_RESPONSES:
2556 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2557 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2564 struct ctables_cell_sort_aux
2566 const struct ctables_nest *nest;
2567 enum pivot_axis_type a;
2571 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2573 const struct ctables_cell_sort_aux *aux = aux_;
2574 struct ctables_cell *const *ap = a_;
2575 struct ctables_cell *const *bp = b_;
2576 const struct ctables_cell *a = *ap;
2577 const struct ctables_cell *b = *bp;
2579 const struct ctables_nest *nest = aux->nest;
2580 for (size_t i = 0; i < nest->n; i++)
2581 if (i != nest->scale_idx)
2583 const struct variable *var = nest->vars[i];
2584 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2585 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2586 if (a_cv->category != b_cv->category)
2587 return a_cv->category > b_cv->category ? 1 : -1;
2589 const union value *a_val = &a_cv->value;
2590 const union value *b_val = &b_cv->value;
2591 switch (a_cv->category->type)
2597 case CCT_POSTCOMPUTE:
2598 /* Must be equal. */
2605 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2613 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2615 return a_cv->category->sort_ascending ? cmp : -cmp;
2621 const char *a_label = var_lookup_value_label (var, a_val);
2622 const char *b_label = var_lookup_value_label (var, b_val);
2624 ? (b_label ? strcmp (a_label, b_label) : 1)
2625 : (b_label ? -1 : value_compare_3way (
2626 a_val, b_val, var_get_width (var))));
2628 return a_cv->category->sort_ascending ? cmp : -cmp;
2642 For each ctables_table:
2643 For each combination of row vars:
2644 For each combination of column vars:
2645 For each combination of layer vars:
2647 Make a table of row values:
2648 Sort entries by row values
2649 Assign a 0-based index to each actual value
2650 Construct a dimension
2651 Make a table of column values
2652 Make a table of layer values
2654 Fill the table entry using the indexes from before.
2657 static struct ctables_domain *
2658 ctables_domain_insert (struct ctables_section *s, struct ctables_cell *cell,
2659 enum ctables_domain_type domain)
2662 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2664 const struct ctables_nest *nest = s->nests[a];
2665 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2667 size_t v_idx = nest->domains[domain][i];
2668 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2669 var_get_width (nest->vars[v_idx]), hash);
2673 struct ctables_domain *d;
2674 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &s->domains[domain])
2676 const struct ctables_cell *df = d->example;
2677 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2679 const struct ctables_nest *nest = s->nests[a];
2680 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2682 size_t v_idx = nest->domains[domain][i];
2683 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2684 &cell->axes[a].cvs[v_idx].value,
2685 var_get_width (nest->vars[v_idx])))
2694 d = xmalloc (sizeof *d);
2695 *d = (struct ctables_domain) { .example = cell };
2696 hmap_insert (&s->domains[domain], &d->node, hash);
2700 static const struct ctables_category *
2701 ctables_categories_match (const struct ctables_categories *c,
2702 const union value *v, const struct variable *var)
2704 const struct ctables_category *othernm = NULL;
2705 for (size_t i = c->n_cats; i-- > 0; )
2707 const struct ctables_category *cat = &c->cats[i];
2711 if (cat->number == v->f)
2719 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2720 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2725 if (var_is_value_missing (var, v))
2729 case CCT_POSTCOMPUTE:
2744 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2749 return var_is_value_missing (var, v) ? NULL : othernm;
2752 static const struct ctables_category *
2753 ctables_categories_total (const struct ctables_categories *c)
2755 const struct ctables_category *first = &c->cats[0];
2756 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2757 return (first->type == CCT_TOTAL ? first
2758 : last->type == CCT_TOTAL ? last
2762 static struct ctables_cell *
2763 ctables_cell_insert__ (struct ctables_section *s, const struct ccase *c,
2764 const struct ctables_category *cats[PIVOT_N_AXES][10])
2767 enum ctables_summary_variant sv = CSV_CELL;
2768 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2770 const struct ctables_nest *nest = s->nests[a];
2771 for (size_t i = 0; i < nest->n; i++)
2772 if (i != nest->scale_idx)
2774 hash = hash_pointer (cats[a][i], hash);
2775 if (cats[a][i]->type != CCT_TOTAL
2776 && cats[a][i]->type != CCT_SUBTOTAL
2777 && cats[a][i]->type != CCT_POSTCOMPUTE)
2778 hash = value_hash (case_data (c, nest->vars[i]),
2779 var_get_width (nest->vars[i]), hash);
2785 struct ctables_cell *cell;
2786 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &s->cells)
2788 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2790 const struct ctables_nest *nest = s->nests[a];
2791 for (size_t i = 0; i < nest->n; i++)
2792 if (i != nest->scale_idx
2793 && (cats[a][i] != cell->axes[a].cvs[i].category
2794 || (cats[a][i]->type != CCT_TOTAL
2795 && cats[a][i]->type != CCT_SUBTOTAL
2796 && cats[a][i]->type != CCT_POSTCOMPUTE
2797 && !value_equal (case_data (c, nest->vars[i]),
2798 &cell->axes[a].cvs[i].value,
2799 var_get_width (nest->vars[i])))))
2808 cell = xmalloc (sizeof *cell);
2811 cell->contributes_to_domains = true;
2812 cell->postcompute = false;
2813 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2815 const struct ctables_nest *nest = s->nests[a];
2816 cell->axes[a].cvs = (nest->n
2817 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2819 for (size_t i = 0; i < nest->n; i++)
2821 const struct ctables_category *cat = cats[a][i];
2822 if (i != nest->scale_idx)
2824 const struct ctables_category *subtotal = cat->subtotal;
2825 if (cat->hide || (subtotal && subtotal->hide_subcategories))
2828 if (cat->type == CCT_TOTAL
2829 || cat->type == CCT_SUBTOTAL
2830 || cat->type == CCT_POSTCOMPUTE)
2831 cell->contributes_to_domains = false;
2832 if (cat->type == CCT_POSTCOMPUTE)
2833 cell->postcompute = true;
2836 cell->axes[a].cvs[i].category = cat;
2837 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2838 var_get_width (nest->vars[i]));
2842 const struct ctables_nest *ss = s->nests[s->table->summary_axis];
2843 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2844 cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
2845 for (size_t i = 0; i < specs->n; i++)
2846 ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
2847 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2848 cell->domains[dt] = ctables_domain_insert (s, cell, dt);
2849 hmap_insert (&s->cells, &cell->node, hash);
2854 ctables_cell_add__ (struct ctables_section *s, const struct ccase *c,
2855 const struct ctables_category *cats[PIVOT_N_AXES][10],
2856 double d_weight, double e_weight)
2858 struct ctables_cell *cell = ctables_cell_insert__ (s, c, cats);
2859 const struct ctables_nest *ss = s->nests[s->table->summary_axis];
2861 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2862 for (size_t i = 0; i < specs->n; i++)
2863 ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
2864 case_data (c, specs->var), d_weight, e_weight);
2865 if (cell->contributes_to_domains)
2867 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2869 cell->domains[dt]->d_valid += d_weight;
2870 cell->domains[dt]->e_valid += e_weight;
2876 recurse_totals (struct ctables_section *s, const struct ccase *c,
2877 const struct ctables_category *cats[PIVOT_N_AXES][10],
2878 double d_weight, double e_weight,
2879 enum pivot_axis_type start_axis, size_t start_nest)
2881 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2883 const struct ctables_nest *nest = s->nests[a];
2884 for (size_t i = start_nest; i < nest->n; i++)
2886 if (i == nest->scale_idx)
2889 const struct variable *var = nest->vars[i];
2891 const struct ctables_category *total = ctables_categories_total (
2892 s->table->categories[var_get_dict_index (var)]);
2895 const struct ctables_category *save = cats[a][i];
2897 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2898 recurse_totals (s, c, cats, d_weight, e_weight, a, i + 1);
2907 recurse_subtotals (struct ctables_section *s, const struct ccase *c,
2908 const struct ctables_category *cats[PIVOT_N_AXES][10],
2909 double d_weight, double e_weight,
2910 enum pivot_axis_type start_axis, size_t start_nest)
2912 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2914 const struct ctables_nest *nest = s->nests[a];
2915 for (size_t i = start_nest; i < nest->n; i++)
2917 if (i == nest->scale_idx)
2920 const struct ctables_category *save = cats[a][i];
2923 cats[a][i] = save->subtotal;
2924 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2925 recurse_subtotals (s, c, cats, d_weight, e_weight, a, i + 1);
2934 ctables_add_occurrence (const struct variable *var,
2935 const union value *value,
2936 struct hmap *occurrences)
2938 int width = var_get_width (var);
2939 unsigned int hash = value_hash (value, width, 0);
2941 struct ctables_occurrence *o;
2942 HMAP_FOR_EACH_WITH_HASH (o, struct ctables_occurrence, node, hash,
2944 if (value_equal (value, &o->value, width))
2947 o = xmalloc (sizeof *o);
2948 value_clone (&o->value, value, width);
2949 hmap_insert (occurrences, &o->node, hash);
2953 ctables_cell_insert (struct ctables_section *s,
2954 const struct ccase *c,
2955 double d_weight, double e_weight)
2957 const struct ctables_category *cats[PIVOT_N_AXES][10]; /* XXX */
2958 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2960 const struct ctables_nest *nest = s->nests[a];
2961 for (size_t i = 0; i < nest->n; i++)
2963 if (i == nest->scale_idx)
2966 const struct variable *var = nest->vars[i];
2967 const union value *value = case_data (c, var);
2969 if (var_is_numeric (var) && value->f == SYSMIS)
2972 cats[a][i] = ctables_categories_match (
2973 s->table->categories[var_get_dict_index (var)], value, var);
2979 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2981 const struct ctables_nest *nest = s->nests[a];
2982 for (size_t i = 0; i < nest->n; i++)
2983 if (i != nest->scale_idx)
2985 const struct variable *var = nest->vars[i];
2986 const union value *value = case_data (c, var);
2987 ctables_add_occurrence (var, value, &s->occurrences[a][i]);
2991 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2993 recurse_totals (s, c, cats, d_weight, e_weight, 0, 0);
2994 recurse_subtotals (s, c, cats, d_weight, e_weight, 0, 0);
2999 const struct ctables_summary_spec_set *set;
3004 merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
3006 const struct ctables_summary_spec *as = &a->set->specs[a->ofs];
3007 const struct ctables_summary_spec *bs = &b->set->specs[b->ofs];
3008 if (as->function != bs->function)
3009 return as->function > bs->function ? 1 : -1;
3010 else if (as->percentile != bs->percentile)
3011 return as->percentile < bs->percentile ? 1 : -1;
3012 return strcmp (as->label, bs->label);
3015 static struct pivot_value *
3016 ctables_category_create_label (const struct ctables_category *cat,
3017 const struct variable *var,
3018 const union value *value)
3020 return (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL
3021 ? pivot_value_new_user_text (cat->total_label, SIZE_MAX)
3022 : cat->type == CCT_POSTCOMPUTE && cat->pc->label
3023 ? pivot_value_new_user_text (cat->pc->label, SIZE_MAX)
3024 : pivot_value_new_var_value (var, value));
3027 static struct ctables_value *
3028 ctables_value_find__ (struct ctables_table *t, const union value *value,
3029 int width, unsigned int hash)
3031 struct ctables_value *clv;
3032 HMAP_FOR_EACH_WITH_HASH (clv, struct ctables_value, node,
3033 hash, &t->clabels_values_map)
3034 if (value_equal (value, &clv->value, width))
3040 ctables_value_insert (struct ctables_table *t, const union value *value,
3043 unsigned int hash = value_hash (value, width, 0);
3044 struct ctables_value *clv = ctables_value_find__ (t, value, width, hash);
3047 clv = xmalloc (sizeof *clv);
3048 value_clone (&clv->value, value, width);
3049 hmap_insert (&t->clabels_values_map, &clv->node, hash);
3053 static struct ctables_value *
3054 ctables_value_find (struct ctables_table *t,
3055 const union value *value, int width)
3057 return ctables_value_find__ (t, value, width,
3058 value_hash (value, width, 0));
3062 ctables_table_add_section (struct ctables_table *t, enum pivot_axis_type a,
3063 size_t ix[PIVOT_N_AXES])
3065 if (a < PIVOT_N_AXES)
3067 size_t limit = MAX (t->stacks[a].n, 1);
3068 for (ix[a] = 0; ix[a] < limit; ix[a]++)
3069 ctables_table_add_section (t, a + 1, ix);
3073 struct ctables_section *s = &t->sections[t->n_sections++];
3074 *s = (struct ctables_section) {
3076 .cells = HMAP_INITIALIZER (s->cells),
3078 for (a = 0; a < PIVOT_N_AXES; a++)
3081 struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
3083 s->occurrences[a] = xnmalloc (nest->n, sizeof *s->occurrences[a]);
3084 for (size_t i = 0; i < nest->n; i++)
3085 hmap_init (&s->occurrences[a][i]);
3087 for (size_t i = 0; i < N_CTDTS; i++)
3088 hmap_init (&s->domains[i]);
3093 ctpo_add (double a, double b)
3099 ctpo_sub (double a, double b)
3105 ctpo_mul (double a, double b)
3111 ctpo_div (double a, double b)
3113 return b ? a / b : SYSMIS;
3117 ctpo_pow (double a, double b)
3119 int save_errno = errno;
3121 double result = pow (a, b);
3129 ctpo_neg (double a, double b UNUSED)
3134 struct ctables_pcexpr_evaluate_ctx
3136 const struct ctables_cell *cell;
3137 const struct ctables_section *section;
3138 const struct ctables_categories *cats;
3139 enum pivot_axis_type pc_a;
3143 static double ctables_pcexpr_evaluate (
3144 const struct ctables_pcexpr_evaluate_ctx *, const struct ctables_pcexpr *);
3147 ctables_pcexpr_evaluate_nonterminal (
3148 const struct ctables_pcexpr_evaluate_ctx *ctx,
3149 const struct ctables_pcexpr *e, size_t n_args,
3150 double evaluate (double, double))
3152 double args[2] = { 0, 0 };
3153 for (size_t i = 0; i < n_args; i++)
3155 args[i] = ctables_pcexpr_evaluate (ctx, e->subs[i]);
3156 if (!isfinite (args[i]) || args[i] == SYSMIS)
3159 return evaluate (args[0], args[1]);
3163 ctables_pcexpr_evaluate_category (const struct ctables_pcexpr_evaluate_ctx *ctx,
3164 const struct ctables_cell_value *pc_cv)
3166 const struct ctables_section *s = ctx->section;
3169 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3171 const struct ctables_nest *nest = s->nests[a];
3172 for (size_t i = 0; i < nest->n; i++)
3173 if (i != nest->scale_idx)
3175 const struct ctables_cell_value *cv
3176 = (a == ctx->pc_a && i == ctx->pc_a_idx ? pc_cv
3177 : &ctx->cell->axes[a].cvs[i]);
3178 hash = hash_pointer (cv->category, hash);
3179 if (cv->category->type != CCT_TOTAL
3180 && cv->category->type != CCT_SUBTOTAL
3181 && cv->category->type != CCT_POSTCOMPUTE)
3182 hash = value_hash (&cv->value,
3183 var_get_width (nest->vars[i]), hash);
3187 struct ctables_cell *tc;
3188 HMAP_FOR_EACH_WITH_HASH (tc, struct ctables_cell, node, hash, &s->cells)
3190 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3192 const struct ctables_nest *nest = s->nests[a];
3193 for (size_t i = 0; i < nest->n; i++)
3194 if (i != nest->scale_idx)
3196 const struct ctables_cell_value *p_cv
3197 = (a == ctx->pc_a && i == ctx->pc_a_idx ? pc_cv
3198 : &ctx->cell->axes[a].cvs[i]);
3199 const struct ctables_cell_value *t_cv = &tc->axes[a].cvs[i];
3200 if (p_cv->category != t_cv->category
3201 || (p_cv->category->type != CCT_TOTAL
3202 && p_cv->category->type != CCT_SUBTOTAL
3203 && p_cv->category->type != CCT_POSTCOMPUTE
3204 && !value_equal (&p_cv->value,
3206 var_get_width (nest->vars[i]))))
3218 const struct ctables_table *t = s->table;
3219 const struct ctables_nest *specs_nest = s->nests[t->summary_axis];
3220 const struct ctables_summary_spec_set *specs = &specs_nest->specs[tc->sv];
3221 size_t j = 0 /* XXX */;
3222 return ctables_summary_value (tc, &tc->summaries[j], &specs->specs[j]);
3226 ctables_pcexpr_evaluate (const struct ctables_pcexpr_evaluate_ctx *ctx,
3227 const struct ctables_pcexpr *e)
3234 case CTPO_CAT_RANGE:
3236 struct ctables_cell_value cv = {
3237 .category = ctables_find_category_for_postcompute (ctx->cats, e)
3239 assert (cv.category != NULL);
3241 struct hmap *occurrences = &ctx->section->occurrences[ctx->pc_a][ctx->pc_a_idx];
3242 const struct ctables_occurrence *o;
3245 const struct variable *var = ctx->section->nests[ctx->pc_a]->vars[ctx->pc_a_idx];
3246 HMAP_FOR_EACH (o, struct ctables_occurrence, node, occurrences)
3247 if (ctables_categories_match (ctx->cats, &o->value, var) == cv.category)
3249 cv.value = o->value;
3250 sum += ctables_pcexpr_evaluate_category (ctx, &cv);
3255 case CTPO_CAT_NUMBER:
3256 case CTPO_CAT_STRING:
3257 case CTPO_CAT_MISSING:
3258 case CTPO_CAT_OTHERNM:
3259 case CTPO_CAT_SUBTOTAL:
3260 case CTPO_CAT_TOTAL:
3262 struct ctables_cell_value cv = {
3263 .category = ctables_find_category_for_postcompute (ctx->cats, e),
3264 .value = { .f = e->number },
3266 assert (cv.category != NULL);
3267 return ctables_pcexpr_evaluate_category (ctx, &cv);
3271 return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_add);
3274 return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_sub);
3277 return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_mul);
3280 return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_div);
3283 return ctables_pcexpr_evaluate_nonterminal (ctx, e, 2, ctpo_pow);
3286 return ctables_pcexpr_evaluate_nonterminal (ctx, e, 1, ctpo_neg);
3293 ctables_cell_calculate_postcompute (const struct ctables_section *s,
3294 const struct ctables_cell *cell)
3296 enum pivot_axis_type pc_a;
3298 const struct ctables_postcompute *pc;
3299 for (pc_a = 0; ; pc_a++)
3301 assert (pc_a < PIVOT_N_AXES);
3302 for (pc_a_idx = 0; pc_a_idx < s->nests[pc_a]->n; pc_a_idx++)
3304 const struct ctables_cell_value *cv = &cell->axes[pc_a].cvs[pc_a_idx];
3305 if (cv->category->type == CCT_POSTCOMPUTE)
3307 pc = cv->category->pc;
3314 const struct variable *var = s->nests[pc_a]->vars[pc_a_idx];
3315 const struct ctables_categories *cats = s->table->categories[
3316 var_get_dict_index (var)];
3317 struct ctables_pcexpr_evaluate_ctx ctx = {
3322 .pc_a_idx = pc_a_idx,
3324 return ctables_pcexpr_evaluate (&ctx, pc->expr);
3328 ctables_table_output (struct ctables *ct, struct ctables_table *t)
3330 struct pivot_table *pt = pivot_table_create__ (
3332 ? pivot_value_new_user_text (t->title, SIZE_MAX)
3333 : pivot_value_new_text (N_("Custom Tables"))),
3336 pivot_table_set_caption (
3337 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
3339 pivot_table_set_caption (
3340 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
3342 bool summary_dimension = (t->summary_axis != t->slabels_axis
3343 || (!t->slabels_visible
3344 && t->summary_specs.n > 1));
3345 if (summary_dimension)
3347 struct pivot_dimension *d = pivot_dimension_create (
3348 pt, t->slabels_axis, N_("Statistics"));
3349 const struct ctables_summary_spec_set *specs = &t->summary_specs;
3350 if (!t->slabels_visible)
3351 d->hide_all_labels = true;
3352 for (size_t i = 0; i < specs->n; i++)
3353 pivot_category_create_leaf (
3354 d->root, pivot_value_new_text (specs->specs[i].label));
3357 bool categories_dimension = t->clabels_example != NULL;
3358 if (categories_dimension)
3360 struct pivot_dimension *d = pivot_dimension_create (
3361 pt, t->label_axis[t->clabels_from_axis],
3362 t->clabels_from_axis == PIVOT_AXIS_ROW
3363 ? N_("Row Categories")
3364 : N_("Column Categories"));
3365 const struct variable *var = t->clabels_example;
3366 const struct ctables_categories *c = t->categories[var_get_dict_index (var)];
3367 for (size_t i = 0; i < t->n_clabels_values; i++)
3369 const struct ctables_value *value = t->clabels_values[i];
3370 const struct ctables_category *cat = ctables_categories_match (c, &value->value, var);
3371 assert (cat != NULL);
3372 pivot_category_create_leaf (d->root, ctables_category_create_label (
3373 cat, t->clabels_example, &value->value));
3377 pivot_table_set_look (pt, ct->look);
3378 struct pivot_dimension *d[PIVOT_N_AXES];
3379 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3381 static const char *names[] = {
3382 [PIVOT_AXIS_ROW] = N_("Rows"),
3383 [PIVOT_AXIS_COLUMN] = N_("Columns"),
3384 [PIVOT_AXIS_LAYER] = N_("Layers"),
3386 d[a] = (t->axes[a] || a == t->summary_axis
3387 ? pivot_dimension_create (pt, a, names[a])
3392 assert (t->axes[a]);
3394 for (size_t i = 0; i < t->stacks[a].n; i++)
3396 struct ctables_nest *nest = &t->stacks[a].nests[i];
3397 struct ctables_section **sections = xnmalloc (t->n_sections,
3399 size_t n_sections = 0;
3401 size_t n_total_cells = 0;
3402 size_t max_depth = 0;
3403 for (size_t j = 0; j < t->n_sections; j++)
3404 if (t->sections[j].nests[a] == nest)
3406 struct ctables_section *s = &t->sections[j];
3407 sections[n_sections++] = s;
3408 n_total_cells += s->cells.count;
3410 size_t depth = s->nests[a]->n;
3411 max_depth = MAX (depth, max_depth);
3414 struct ctables_cell **sorted = xnmalloc (n_total_cells,
3416 size_t n_sorted = 0;
3418 for (size_t j = 0; j < n_sections; j++)
3420 struct ctables_section *s = sections[j];
3422 struct ctables_cell *cell;
3423 HMAP_FOR_EACH (cell, struct ctables_cell, node, &s->cells)
3425 sorted[n_sorted++] = cell;
3426 assert (n_sorted <= n_total_cells);
3429 struct ctables_cell_sort_aux aux = { .nest = nest, .a = a };
3430 sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
3432 struct ctables_level
3434 enum ctables_level_type
3436 CTL_VAR, /* Variable label for nest->vars[var_idx]. */
3437 CTL_CATEGORY, /* Category for nest->vars[var_idx]. */
3438 CTL_SUMMARY, /* Summary functions. */
3442 enum settings_value_show vlabel; /* CTL_VAR only. */
3445 struct ctables_level *levels = xnmalloc (1 + 2 * max_depth, sizeof *levels);
3446 size_t n_levels = 0;
3447 for (size_t k = 0; k < nest->n; k++)
3449 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k])];
3450 if (vlabel != CTVL_NONE)
3452 levels[n_levels++] = (struct ctables_level) {
3454 .vlabel = (enum settings_value_show) vlabel,
3459 if (nest->scale_idx != k
3460 && (k != nest->n - 1 || t->label_axis[a] == a))
3462 levels[n_levels++] = (struct ctables_level) {
3463 .type = CTL_CATEGORY,
3469 if (!summary_dimension && a == t->slabels_axis)
3471 levels[n_levels++] = (struct ctables_level) {
3472 .type = CTL_SUMMARY,
3473 .var_idx = SIZE_MAX,
3477 /* Pivot categories:
3479 - variable label for nest->vars[0], if vlabel != CTVL_NONE
3480 - category for nest->vars[0], if nest->scale_idx != 0
3481 - variable label for nest->vars[1], if vlabel != CTVL_NONE
3482 - category for nest->vars[1], if nest->scale_idx != 1
3484 - variable label for nest->vars[n - 1], if vlabel != CTVL_NONE
3485 - category for nest->vars[n - 1], if t->label_axis[a] == a && nest->scale_idx != n - 1.
3486 - summary function, if 'a == t->slabels_axis && a ==
3489 Additional dimensions:
3491 - If 'a == t->slabels_axis && a != t->summary_axis', add a summary
3493 - If 't->label_axis[b] == a' for some 'b != a', add a category
3498 struct pivot_category **groups = xnmalloc (1 + 2 * max_depth, sizeof *groups);
3500 for (size_t j = 0; j < n_sorted; j++)
3502 struct ctables_cell *cell = sorted[j];
3503 struct ctables_cell *prev = j > 0 ? sorted[j - 1] : NULL;
3505 size_t n_common = 0;
3508 for (; n_common < n_levels; n_common++)
3510 const struct ctables_level *level = &levels[n_common];
3511 if (level->type == CTL_CATEGORY)
3513 size_t var_idx = level->var_idx;
3514 const struct ctables_category *c = cell->axes[a].cvs[var_idx].category;
3515 if (prev->axes[a].cvs[var_idx].category != c)
3517 else if (c->type != CCT_SUBTOTAL
3518 && c->type != CCT_TOTAL
3519 && c->type != CCT_POSTCOMPUTE
3520 && !value_equal (&prev->axes[a].cvs[var_idx].value,
3521 &cell->axes[a].cvs[var_idx].value,
3522 var_get_type (nest->vars[var_idx])))
3528 for (size_t k = n_common; k < n_levels; k++)
3530 const struct ctables_level *level = &levels[k];
3531 struct pivot_category *parent = k ? groups[k - 1] : d[a]->root;
3532 if (level->type == CTL_SUMMARY)
3534 assert (k == n_levels - 1);
3536 const struct ctables_summary_spec_set *specs = &t->summary_specs;
3537 for (size_t m = 0; m < specs->n; m++)
3539 int leaf = pivot_category_create_leaf (
3540 parent, pivot_value_new_text (specs->specs[m].label));
3547 const struct variable *var = nest->vars[level->var_idx];
3548 struct pivot_value *label;
3549 if (level->type == CTL_VAR)
3551 label = pivot_value_new_variable (var);
3552 label->variable.show = level->vlabel;
3554 else if (level->type == CTL_CATEGORY)
3556 const struct ctables_cell_value *cv = &cell->axes[a].cvs[level->var_idx];
3557 label = ctables_category_create_label (cv->category,
3563 if (k == n_levels - 1)
3564 prev_leaf = pivot_category_create_leaf (parent, label);
3566 groups[k] = pivot_category_create_group__ (parent, label);
3570 cell->axes[a].leaf = prev_leaf;
3577 for (size_t i = 0; i < t->n_sections; i++)
3579 struct ctables_section *s = &t->sections[i];
3581 struct ctables_cell *cell;
3582 HMAP_FOR_EACH (cell, struct ctables_cell, node, &s->cells)
3587 const struct ctables_nest *specs_nest = s->nests[t->summary_axis];
3588 const struct ctables_summary_spec_set *specs = &specs_nest->specs[cell->sv];
3589 for (size_t j = 0; j < specs->n; j++)
3592 size_t n_dindexes = 0;
3594 if (summary_dimension)
3595 dindexes[n_dindexes++] = specs->specs[j].axis_idx;
3597 if (categories_dimension)
3599 const struct ctables_nest *clabels_nest = s->nests[t->clabels_from_axis];
3600 const struct variable *var = clabels_nest->vars[clabels_nest->n - 1];
3601 const union value *value = &cell->axes[t->clabels_from_axis].cvs[clabels_nest->n - 1].value;
3602 const struct ctables_value *ctv = ctables_value_find (t, value, var_get_width (var));
3605 dindexes[n_dindexes++] = ctv->leaf;
3608 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3611 int leaf = cell->axes[a].leaf;
3612 if (a == t->summary_axis && !summary_dimension)
3614 dindexes[n_dindexes++] = leaf;
3617 const struct ctables_summary_spec *ss = &specs->specs[j];
3619 double d = (cell->postcompute
3620 ? ctables_cell_calculate_postcompute (s, cell)
3621 : ctables_summary_value (cell, &cell->summaries[j], ss));
3622 struct pivot_value *value;
3623 if (ct->hide_threshold != 0
3624 && d < ct->hide_threshold
3625 && (cell->postcompute
3627 : ctables_summary_function_is_count (ss->function)))
3629 value = pivot_value_new_user_text_nocopy (
3630 xasprintf ("<%d", ct->hide_threshold));
3632 else if (d == 0 && ct->zero)
3633 value = pivot_value_new_user_text (ct->zero, SIZE_MAX);
3634 else if (d == SYSMIS && ct->missing)
3635 value = pivot_value_new_user_text (ct->missing, SIZE_MAX);
3638 value = pivot_value_new_number (d);
3639 value->numeric.format = specs->specs[j].format;
3641 pivot_table_put (pt, dindexes, n_dindexes, value);
3646 pivot_table_submit (pt);
3650 ctables_check_label_position (struct ctables_table *t, enum pivot_axis_type a)
3652 enum pivot_axis_type label_pos = t->label_axis[a];
3656 t->clabels_from_axis = a;
3658 const char *subcommand_name = a == PIVOT_AXIS_ROW ? "ROWLABELS" : "COLLABELS";
3659 const char *pos_name = label_pos == PIVOT_AXIS_LAYER ? "LAYER" : "OPPOSITE";
3661 const struct ctables_stack *stack = &t->stacks[a];
3665 const struct ctables_nest *n0 = &stack->nests[0];
3667 const struct variable *v0 = n0->vars[n0->n - 1];
3668 struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
3669 t->clabels_example = v0;
3671 for (size_t i = 0; i < c0->n_cats; i++)
3672 if (c0->cats[i].type == CCT_FUNCTION)
3674 msg (SE, _("%s=%s is not allowed with sorting based "
3675 "on a summary function."),
3676 subcommand_name, pos_name);
3679 if (n0->n - 1 == n0->scale_idx)
3681 msg (SE, _("%s=%s requires the variables to be moved to be categorical, "
3682 "but %s is a scale variable."),
3683 subcommand_name, pos_name, var_get_name (v0));
3687 for (size_t i = 1; i < stack->n; i++)
3689 const struct ctables_nest *ni = &stack->nests[i];
3691 const struct variable *vi = ni->vars[ni->n - 1];
3692 struct ctables_categories *ci = t->categories[var_get_dict_index (vi)];
3694 if (ni->n - 1 == ni->scale_idx)
3696 msg (SE, _("%s=%s requires the variables to be moved to be "
3697 "categorical, but %s is a scale variable."),
3698 subcommand_name, pos_name, var_get_name (vi));
3701 if (var_get_width (v0) != var_get_width (vi))
3703 msg (SE, _("%s=%s requires the variables to be "
3704 "moved to have the same width, but %s has "
3705 "width %d and %s has width %d."),
3706 subcommand_name, pos_name,
3707 var_get_name (v0), var_get_width (v0),
3708 var_get_name (vi), var_get_width (vi));
3711 if (!val_labs_equal (var_get_value_labels (v0),
3712 var_get_value_labels (vi)))
3714 msg (SE, _("%s=%s requires the variables to be "
3715 "moved to have the same value labels, but %s "
3716 "and %s have different value labels."),
3717 subcommand_name, pos_name,
3718 var_get_name (v0), var_get_name (vi));
3721 if (!ctables_categories_equal (c0, ci))
3723 msg (SE, _("%s=%s requires the variables to be "
3724 "moved to have the same category "
3725 "specifications, but %s and %s have different "
3726 "category specifications."),
3727 subcommand_name, pos_name,
3728 var_get_name (v0), var_get_name (vi));
3737 ctables_prepare_table (struct ctables_table *t)
3739 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3742 t->stacks[a] = enumerate_fts (a, t->axes[a]);
3744 for (size_t j = 0; j < t->stacks[a].n; j++)
3746 struct ctables_nest *nest = &t->stacks[a].nests[j];
3747 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
3749 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
3750 nest->n_domains[dt] = 0;
3752 for (size_t k = 0; k < nest->n; k++)
3754 if (k == nest->scale_idx)
3763 if (a != PIVOT_AXIS_LAYER)
3770 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
3771 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
3772 : a == PIVOT_AXIS_ROW)
3774 if (k == nest->n - 1
3775 || (nest->scale_idx == nest->n - 1
3776 && k == nest->n - 2))
3782 if (a == PIVOT_AXIS_COLUMN)
3787 if (a == PIVOT_AXIS_ROW)
3792 nest->domains[dt][nest->n_domains[dt]++] = k;
3799 struct ctables_nest *nest = xmalloc (sizeof *nest);
3800 *nest = (struct ctables_nest) { .n = 0 };
3801 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
3804 struct ctables_stack *stack = &t->stacks[t->summary_axis];
3805 for (size_t i = 0; i < stack->n; i++)
3807 struct ctables_nest *nest = &stack->nests[i];
3808 if (!nest->specs[CSV_CELL].n)
3810 struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
3811 specs->specs = xmalloc (sizeof *specs->specs);
3814 enum ctables_summary_function function
3815 = specs->var ? CTSF_MEAN : CTSF_COUNT;
3816 struct ctables_var var = { .is_mrset = false, .var = specs->var };
3818 *specs->specs = (struct ctables_summary_spec) {
3819 .function = function,
3820 .format = ctables_summary_default_format (function, &var),
3821 .label = ctables_summary_default_label (function, 0),
3824 specs->var = nest->vars[0];
3826 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3827 &nest->specs[CSV_CELL]);
3829 else if (!nest->specs[CSV_TOTAL].n)
3830 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3831 &nest->specs[CSV_CELL]);
3834 struct ctables_summary_spec_set *merged = &t->summary_specs;
3835 struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
3837 for (size_t j = 0; j < stack->n; j++)
3839 const struct ctables_nest *nest = &stack->nests[j];
3841 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3842 items[n_left++] = (struct merge_item) { .set = &nest->specs[sv] };
3847 struct merge_item min = items[0];
3848 for (size_t j = 1; j < n_left; j++)
3849 if (merge_item_compare_3way (&items[j], &min) < 0)
3852 if (merged->n >= merged->allocated)
3853 merged->specs = x2nrealloc (merged->specs, &merged->allocated,
3854 sizeof *merged->specs);
3855 merged->specs[merged->n++] = min.set->specs[min.ofs];
3857 for (size_t j = 0; j < n_left; )
3859 if (merge_item_compare_3way (&items[j], &min) == 0)
3861 struct merge_item *item = &items[j];
3862 item->set->specs[item->ofs].axis_idx = merged->n - 1;
3863 if (++item->ofs >= item->set->n)
3865 items[j] = items[--n_left];
3874 for (size_t j = 0; j < merged->n; j++)
3875 printf ("%s\n", ctables_summary_function_name (merged->specs[j].function));
3877 for (size_t j = 0; j < stack->n; j++)
3879 const struct ctables_nest *nest = &stack->nests[j];
3880 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3882 const struct ctables_summary_spec_set *specs = &nest->specs[sv];
3883 for (size_t k = 0; k < specs->n; k++)
3884 printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
3885 specs->specs[k].axis_idx);
3891 return (ctables_check_label_position (t, PIVOT_AXIS_ROW)
3892 && ctables_check_label_position (t, PIVOT_AXIS_COLUMN));
3896 ctables_insert_clabels_values (struct ctables_table *t, const struct ccase *c,
3897 enum pivot_axis_type a)
3899 struct ctables_stack *stack = &t->stacks[a];
3900 for (size_t i = 0; i < stack->n; i++)
3902 const struct ctables_nest *nest = &stack->nests[i];
3903 const struct variable *var = nest->vars[nest->n - 1];
3904 const union value *value = case_data (c, var);
3906 if (var_is_numeric (var) && value->f == SYSMIS)
3909 if (ctables_categories_match (t->categories [var_get_dict_index (var)],
3911 ctables_value_insert (t, value, var_get_width (var));
3916 compare_clabels_values_3way (const void *a_, const void *b_, const void *width_)
3918 const struct ctables_value *const *ap = a_;
3919 const struct ctables_value *const *bp = b_;
3920 const struct ctables_value *a = *ap;
3921 const struct ctables_value *b = *bp;
3922 const int *width = width_;
3923 return value_compare_3way (&a->value, &b->value, *width);
3927 ctables_sort_clabels_values (struct ctables_table *t)
3929 const struct variable *v0 = t->clabels_example;
3930 int width = var_get_width (v0);
3932 struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
3935 const struct val_labs *val_labs = var_get_value_labels (v0);
3936 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3937 vl = val_labs_next (val_labs, vl))
3938 if (ctables_categories_match (c0, &vl->value, v0))
3939 ctables_value_insert (t, &vl->value, width);
3942 size_t n = hmap_count (&t->clabels_values_map);
3943 t->clabels_values = xnmalloc (n, sizeof *t->clabels_values);
3945 struct ctables_value *clv;
3947 HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
3948 t->clabels_values[i++] = clv;
3949 t->n_clabels_values = n;
3952 sort (t->clabels_values, n, sizeof *t->clabels_values,
3953 compare_clabels_values_3way, &width);
3955 for (size_t i = 0; i < n; i++)
3956 t->clabels_values[i]->leaf = i;
3960 ctables_add_category_occurrences (const struct variable *var,
3961 struct hmap *occurrences,
3962 const struct ctables_categories *cats)
3964 const struct val_labs *val_labs = var_get_value_labels (var);
3966 for (size_t i = 0; i < cats->n_cats; i++)
3968 const struct ctables_category *c = &cats->cats[i];
3972 ctables_add_occurrence (var, &(const union value) { .f = c->number },
3980 assert (var_is_numeric (var));
3981 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3982 vl = val_labs_next (val_labs, vl))
3983 if (vl->value.f >= c->range[0] && vl->value.f <= c->range[1])
3984 ctables_add_occurrence (var, &vl->value, occurrences);
3988 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3989 vl = val_labs_next (val_labs, vl))
3990 if (var_is_value_missing (var, &vl->value))
3991 ctables_add_occurrence (var, &vl->value, occurrences);
3995 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3996 vl = val_labs_next (val_labs, vl))
3997 ctables_add_occurrence (var, &vl->value, occurrences);
4000 case CCT_POSTCOMPUTE:
4010 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
4011 vl = val_labs_next (val_labs, vl))
4012 if (c->include_missing || !var_is_value_missing (var, &vl->value))
4013 ctables_add_occurrence (var, &vl->value, occurrences);
4020 ctables_section_recurse_add_empty_categories (
4021 struct ctables_section *s,
4022 const struct ctables_category *cats[PIVOT_N_AXES][10], struct ccase *c,
4023 enum pivot_axis_type a, size_t a_idx)
4025 if (a >= PIVOT_N_AXES)
4026 ctables_cell_insert__ (s, c, cats);
4027 else if (!s->nests[a] || a_idx >= s->nests[a]->n)
4028 ctables_section_recurse_add_empty_categories (s, cats, c, a + 1, 0);
4031 const struct variable *var = s->nests[a]->vars[a_idx];
4032 const struct ctables_categories *categories = s->table->categories[
4033 var_get_dict_index (var)];
4034 int width = var_get_width (var);
4035 const struct hmap *occurrences = &s->occurrences[a][a_idx];
4036 const struct ctables_occurrence *o;
4037 HMAP_FOR_EACH (o, struct ctables_occurrence, node, occurrences)
4039 union value *value = case_data_rw (c, var);
4040 value_destroy (value, width);
4041 value_clone (value, &o->value, width);
4042 cats[a][a_idx] = ctables_categories_match (categories, value, var);
4043 assert (cats[a][a_idx] != NULL);
4044 ctables_section_recurse_add_empty_categories (s, cats, c, a, a_idx + 1);
4047 for (size_t i = 0; i < categories->n_cats; i++)
4049 const struct ctables_category *cat = &categories->cats[i];
4050 if (cat->type == CCT_POSTCOMPUTE)
4052 cats[a][a_idx] = cat;
4053 ctables_section_recurse_add_empty_categories (s, cats, c, a, a_idx + 1);
4060 ctables_section_add_empty_categories (struct ctables_section *s)
4062 bool show_empty = false;
4063 for (size_t a = 0; a < PIVOT_N_AXES; a++)
4065 for (size_t k = 0; k < s->nests[a]->n; k++)
4066 if (k != s->nests[a]->scale_idx)
4068 const struct variable *var = s->nests[a]->vars[k];
4069 const struct ctables_categories *cats = s->table->categories[
4070 var_get_dict_index (var)];
4071 if (cats->show_empty)
4074 ctables_add_category_occurrences (var, &s->occurrences[a][k], cats);
4080 const struct ctables_category *cats[PIVOT_N_AXES][10]; /* XXX */
4081 struct ccase *c = case_create (dict_get_proto (s->table->ctables->dict));
4082 ctables_section_recurse_add_empty_categories (s, cats, c, 0, 0);
4087 ctables_execute (struct dataset *ds, struct ctables *ct)
4089 for (size_t i = 0; i < ct->n_tables; i++)
4091 struct ctables_table *t = ct->tables[i];
4092 t->sections = xnmalloc (MAX (1, t->stacks[PIVOT_AXIS_ROW].n) *
4093 MAX (1, t->stacks[PIVOT_AXIS_COLUMN].n) *
4094 MAX (1, t->stacks[PIVOT_AXIS_LAYER].n),
4095 sizeof *t->sections);
4096 size_t ix[PIVOT_N_AXES];
4097 ctables_table_add_section (t, 0, ix);
4100 struct casereader *input = proc_open (ds);
4101 bool warn_on_invalid = true;
4102 for (struct ccase *c = casereader_read (input); c;
4103 case_unref (c), c = casereader_read (input))
4105 double d_weight = dict_get_case_weight (dataset_dict (ds), c,
4107 double e_weight = (ct->e_weight
4108 ? var_force_valid_weight (ct->e_weight,
4109 case_num (c, ct->e_weight),
4113 for (size_t i = 0; i < ct->n_tables; i++)
4115 struct ctables_table *t = ct->tables[i];
4117 for (size_t j = 0; j < t->n_sections; j++)
4118 ctables_cell_insert (&t->sections[j], c, d_weight, e_weight);
4120 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
4121 if (t->label_axis[a] != a)
4122 ctables_insert_clabels_values (t, c, a);
4125 casereader_destroy (input);
4127 for (size_t i = 0; i < ct->n_tables; i++)
4129 struct ctables_table *t = ct->tables[i];
4131 if (t->clabels_example)
4132 ctables_sort_clabels_values (t);
4134 for (size_t j = 0; j < t->n_sections; j++)
4135 ctables_section_add_empty_categories (&t->sections[j]);
4137 ctables_table_output (ct, ct->tables[i]);
4139 return proc_commit (ds);
4144 typedef struct ctables_pcexpr *parse_recursively_func (struct lexer *);
4147 ctables_pcexpr_destroy (struct ctables_pcexpr *e)
4153 case CTPO_CAT_STRING:
4163 for (size_t i = 0; i < 2; i++)
4164 ctables_pcexpr_destroy (e->subs[i]);
4168 case CTPO_CAT_NUMBER:
4169 case CTPO_CAT_RANGE:
4170 case CTPO_CAT_MISSING:
4171 case CTPO_CAT_OTHERNM:
4172 case CTPO_CAT_SUBTOTAL:
4173 case CTPO_CAT_TOTAL:
4177 msg_location_destroy (e->location);
4182 static struct ctables_pcexpr *
4183 ctables_pcexpr_allocate_binary (enum ctables_postcompute_op op,
4184 struct ctables_pcexpr *sub0,
4185 struct ctables_pcexpr *sub1)
4187 struct ctables_pcexpr *e = xmalloc (sizeof *e);
4188 *e = (struct ctables_pcexpr) {
4190 .subs = { sub0, sub1 },
4191 .location = msg_location_merged (sub0->location, sub1->location),
4196 /* How to parse an operator. */
4199 enum token_type token;
4200 enum ctables_postcompute_op op;
4203 static const struct operator *
4204 match_operator (struct lexer *lexer, const struct operator ops[], size_t n_ops)
4206 for (const struct operator *op = ops; op < ops + n_ops; op++)
4207 if (lex_token (lexer) == op->token)
4209 if (op->token != T_NEG_NUM)
4218 static struct ctables_pcexpr *
4219 parse_binary_operators__ (struct lexer *lexer,
4220 const struct operator ops[], size_t n_ops,
4221 parse_recursively_func *parse_next_level,
4222 const char *chain_warning,
4223 struct ctables_pcexpr *lhs)
4225 for (int op_count = 0; ; op_count++)
4227 const struct operator *op = match_operator (lexer, ops, n_ops);
4230 if (op_count > 1 && chain_warning)
4231 msg_at (SW, lhs->location, "%s", chain_warning);
4236 struct ctables_pcexpr *rhs = parse_next_level (lexer);
4239 ctables_pcexpr_destroy (lhs);
4243 lhs = ctables_pcexpr_allocate_binary (op->op, lhs, rhs);
4247 static struct ctables_pcexpr *
4248 parse_binary_operators (struct lexer *lexer,
4249 const struct operator ops[], size_t n_ops,
4250 parse_recursively_func *parse_next_level,
4251 const char *chain_warning)
4253 struct ctables_pcexpr *lhs = parse_next_level (lexer);
4257 return parse_binary_operators__ (lexer, ops, n_ops, parse_next_level,
4258 chain_warning, lhs);
4261 static struct ctables_pcexpr *parse_add (struct lexer *);
4263 static struct ctables_pcexpr
4264 ctpo_cat_range (double low, double high)
4266 return (struct ctables_pcexpr) {
4267 .op = CTPO_CAT_RANGE,
4268 .range = { low, high },
4272 static struct ctables_pcexpr *
4273 parse_primary (struct lexer *lexer)
4275 int start_ofs = lex_ofs (lexer);
4276 struct ctables_pcexpr e;
4277 if (lex_is_number (lexer))
4279 e = (struct ctables_pcexpr) { .op = CTPO_CONSTANT,
4280 .number = lex_number (lexer) };
4283 else if (lex_match_id (lexer, "MISSING"))
4284 e = (struct ctables_pcexpr) { .op = CTPO_CAT_MISSING };
4285 else if (lex_match_id (lexer, "OTHERNM"))
4286 e = (struct ctables_pcexpr) { .op = CTPO_CAT_OTHERNM };
4287 else if (lex_match_id (lexer, "TOTAL"))
4288 e = (struct ctables_pcexpr) { .op = CTPO_CAT_TOTAL };
4289 else if (lex_match_id (lexer, "SUBTOTAL"))
4291 size_t subtotal_index = 0;
4292 if (lex_match (lexer, T_LBRACK))
4294 if (!lex_force_int_range (lexer, "SUBTOTAL", 1, LONG_MAX))
4296 subtotal_index = lex_integer (lexer);
4298 if (!lex_force_match (lexer, T_RBRACK))
4301 e = (struct ctables_pcexpr) { .op = CTPO_CAT_SUBTOTAL,
4302 .subtotal_index = subtotal_index };
4304 else if (lex_match (lexer, T_LBRACK))
4306 if (lex_match_id (lexer, "LO"))
4308 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
4310 e = ctpo_cat_range (-DBL_MAX, lex_number (lexer));
4313 else if (lex_is_number (lexer))
4315 double number = lex_number (lexer);
4317 if (lex_match_id (lexer, "THRU"))
4319 if (lex_match_id (lexer, "HI"))
4320 e = ctpo_cat_range (number, DBL_MAX);
4323 if (!lex_force_num (lexer))
4325 e = ctpo_cat_range (number, lex_number (lexer));
4330 e = (struct ctables_pcexpr) { .op = CTPO_CAT_NUMBER,
4333 else if (lex_is_string (lexer))
4335 e = (struct ctables_pcexpr) {
4336 .op = CTPO_CAT_STRING,
4337 .string = ss_xstrdup (lex_tokss (lexer)),
4343 lex_error (lexer, NULL);
4347 if (!lex_force_match (lexer, T_RBRACK))
4349 if (e.op == CTPO_CAT_STRING)
4354 else if (lex_match (lexer, T_LPAREN))
4356 struct ctables_pcexpr *ep = parse_add (lexer);
4359 if (!lex_force_match (lexer, T_RPAREN))
4361 ctables_pcexpr_destroy (ep);
4368 lex_error (lexer, NULL);
4372 e.location = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1);
4373 return xmemdup (&e, sizeof e);
4376 static struct ctables_pcexpr *
4377 ctables_pcexpr_allocate_neg (struct ctables_pcexpr *sub,
4378 struct lexer *lexer, int start_ofs)
4380 struct ctables_pcexpr *e = xmalloc (sizeof *e);
4381 *e = (struct ctables_pcexpr) {
4384 .location = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
4389 static struct ctables_pcexpr *
4390 parse_exp (struct lexer *lexer)
4392 static const struct operator op = { T_EXP, CTPO_POW };
4394 const char *chain_warning =
4395 _("The exponentiation operator (`**') is left-associative: "
4396 "`a**b**c' equals `(a**b)**c', not `a**(b**c)'. "
4397 "To disable this warning, insert parentheses.");
4399 if (lex_token (lexer) != T_NEG_NUM || lex_next_token (lexer, 1) != T_EXP)
4400 return parse_binary_operators (lexer, &op, 1,
4401 parse_primary, chain_warning);
4403 /* Special case for situations like "-5**6", which must be parsed as
4406 int start_ofs = lex_ofs (lexer);
4407 struct ctables_pcexpr *lhs = xmalloc (sizeof *lhs);
4408 *lhs = (struct ctables_pcexpr) {
4409 .op = CTPO_CONSTANT,
4410 .number = -lex_tokval (lexer),
4411 .location = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer)),
4415 struct ctables_pcexpr *node = parse_binary_operators__ (
4416 lexer, &op, 1, parse_primary, chain_warning, lhs);
4420 return ctables_pcexpr_allocate_neg (node, lexer, start_ofs);
4423 /* Parses the unary minus level. */
4424 static struct ctables_pcexpr *
4425 parse_neg (struct lexer *lexer)
4427 int start_ofs = lex_ofs (lexer);
4428 if (!lex_match (lexer, T_DASH))
4429 return parse_exp (lexer);
4431 struct ctables_pcexpr *inner = parse_neg (lexer);
4435 return ctables_pcexpr_allocate_neg (inner, lexer, start_ofs);
4438 /* Parses the multiplication and division level. */
4439 static struct ctables_pcexpr *
4440 parse_mul (struct lexer *lexer)
4442 static const struct operator ops[] =
4444 { T_ASTERISK, CTPO_MUL },
4445 { T_SLASH, CTPO_DIV },
4448 return parse_binary_operators (lexer, ops, sizeof ops / sizeof *ops,
4452 /* Parses the addition and subtraction level. */
4453 static struct ctables_pcexpr *
4454 parse_add (struct lexer *lexer)
4456 static const struct operator ops[] =
4458 { T_PLUS, CTPO_ADD },
4459 { T_DASH, CTPO_SUB },
4460 { T_NEG_NUM, CTPO_ADD },
4463 return parse_binary_operators (lexer, ops, sizeof ops / sizeof *ops,
4467 static struct ctables_postcompute *
4468 ctables_find_postcompute (struct ctables *ct, const char *name)
4470 struct ctables_postcompute *pc;
4471 HMAP_FOR_EACH_WITH_HASH (pc, struct ctables_postcompute, hmap_node,
4472 utf8_hash_case_string (name, 0), &ct->postcomputes)
4473 if (!utf8_strcasecmp (pc->name, name))
4479 ctables_parse_pcompute (struct lexer *lexer, struct ctables *ct)
4481 int pcompute_start = lex_ofs (lexer) - 1;
4483 if (!lex_force_match (lexer, T_AND) || !lex_force_id (lexer))
4486 char *name = ss_xstrdup (lex_tokss (lexer));
4489 if (!lex_force_match (lexer, T_EQUALS)
4490 || !lex_force_match_id (lexer, "EXPR")
4491 || !lex_force_match (lexer, T_LPAREN))
4497 int expr_start = lex_ofs (lexer);
4498 struct ctables_pcexpr *expr = parse_add (lexer);
4499 int expr_end = lex_ofs (lexer) - 1;
4500 if (!expr || !lex_force_match (lexer, T_RPAREN))
4505 int pcompute_end = lex_ofs (lexer) - 1;
4507 struct msg_location *location = lex_ofs_location (lexer, pcompute_start,
4510 struct ctables_postcompute *pc = ctables_find_postcompute (ct, name);
4513 msg_at (SW, location, _("New definition of &%s will override the "
4514 "previous definition."),
4516 msg_at (SN, pc->location, _("This is the previous definition."));
4518 ctables_pcexpr_destroy (pc->expr);
4519 msg_location_destroy (pc->location);
4524 pc = xmalloc (sizeof *pc);
4525 *pc = (struct ctables_postcompute) { .name = name };
4526 hmap_insert (&ct->postcomputes, &pc->hmap_node,
4527 utf8_hash_case_string (pc->name, 0));
4530 pc->location = location;
4532 pc->label = lex_ofs_representation (lexer, expr_start, expr_end);
4537 ctables_parse_pproperties_format (struct lexer *lexer,
4538 struct ctables_summary_spec_set *sss)
4540 *sss = (struct ctables_summary_spec_set) { .n = 0 };
4542 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH
4543 && !(lex_token (lexer) == T_ID
4544 && (lex_id_match (ss_cstr ("LABEL"), lex_tokss (lexer))
4545 || lex_id_match (ss_cstr ("HIDESOURCECATS"),
4546 lex_tokss (lexer)))))
4548 /* Parse function. */
4549 enum ctables_summary_function function;
4550 if (!parse_ctables_summary_function (lexer, &function))
4553 /* Parse percentile. */
4554 double percentile = 0;
4555 if (function == CTSF_PTILE)
4557 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
4559 percentile = lex_number (lexer);
4564 struct fmt_spec format;
4565 if (!parse_format_specifier (lexer, &format)
4566 || !fmt_check_output (&format)
4567 || !fmt_check_type_compat (&format, VAL_NUMERIC))
4570 if (sss->n >= sss->allocated)
4571 sss->specs = x2nrealloc (sss->specs, &sss->allocated,
4572 sizeof *sss->specs);
4573 sss->specs[sss->n++] = (struct ctables_summary_spec) {
4574 .function = function,
4575 .percentile = percentile,
4582 ctables_summary_spec_set_uninit (sss);
4587 ctables_parse_pproperties (struct lexer *lexer, struct ctables *ct)
4589 struct ctables_postcompute **pcs = NULL;
4591 size_t allocated_pcs = 0;
4593 while (lex_match (lexer, T_AND))
4595 if (!lex_force_id (lexer))
4597 struct ctables_postcompute *pc
4598 = ctables_find_postcompute (ct, lex_tokcstr (lexer));
4601 msg (SE, _("Unknown computed category &%s."), lex_tokcstr (lexer));
4606 if (n_pcs >= allocated_pcs)
4607 pcs = x2nrealloc (pcs, &allocated_pcs, sizeof *pcs);
4611 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
4613 if (lex_match_id (lexer, "LABEL"))
4615 lex_match (lexer, T_EQUALS);
4616 if (!lex_force_string (lexer))
4619 for (size_t i = 0; i < n_pcs; i++)
4621 free (pcs[i]->label);
4622 pcs[i]->label = ss_xstrdup (lex_tokss (lexer));
4627 else if (lex_match_id (lexer, "FORMAT"))
4629 lex_match (lexer, T_EQUALS);
4631 struct ctables_summary_spec_set sss;
4632 if (!ctables_parse_pproperties_format (lexer, &sss))
4635 for (size_t i = 0; i < n_pcs; i++)
4638 ctables_summary_spec_set_uninit (pcs[i]->specs);
4640 pcs[i]->specs = xmalloc (sizeof *pcs[i]->specs);
4641 ctables_summary_spec_set_clone (pcs[i]->specs, &sss);
4643 ctables_summary_spec_set_uninit (&sss);
4645 else if (lex_match_id (lexer, "HIDESOURCECATS"))
4647 lex_match (lexer, T_EQUALS);
4648 bool hide_source_cats;
4649 if (!parse_bool (lexer, &hide_source_cats))
4651 for (size_t i = 0; i < n_pcs; i++)
4652 pcs[i]->hide_source_cats = hide_source_cats;
4656 lex_error_expecting (lexer, "LABEL", "FORMAT", "HIDESOURCECATS");
4669 cmd_ctables (struct lexer *lexer, struct dataset *ds)
4671 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
4672 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
4673 enum settings_value_show tvars = settings_get_show_variables ();
4674 for (size_t i = 0; i < n_vars; i++)
4675 vlabels[i] = (enum ctables_vlabel) tvars;
4677 struct ctables *ct = xmalloc (sizeof *ct);
4678 *ct = (struct ctables) {
4679 .dict = dataset_dict (ds),
4680 .look = pivot_table_look_unshare (pivot_table_look_ref (
4681 pivot_table_look_get_default ())),
4683 .postcomputes = HMAP_INITIALIZER (ct->postcomputes),
4685 ct->look->omit_empty = false;
4687 if (!lex_force_match (lexer, T_SLASH))
4690 while (!lex_match_id (lexer, "TABLE"))
4692 if (lex_match_id (lexer, "FORMAT"))
4694 double widths[2] = { SYSMIS, SYSMIS };
4695 double units_per_inch = 72.0;
4697 while (lex_token (lexer) != T_SLASH)
4699 if (lex_match_id (lexer, "MINCOLWIDTH"))
4701 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
4704 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
4706 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
4709 else if (lex_match_id (lexer, "UNITS"))
4711 lex_match (lexer, T_EQUALS);
4712 if (lex_match_id (lexer, "POINTS"))
4713 units_per_inch = 72.0;
4714 else if (lex_match_id (lexer, "INCHES"))
4715 units_per_inch = 1.0;
4716 else if (lex_match_id (lexer, "CM"))
4717 units_per_inch = 2.54;
4720 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
4724 else if (lex_match_id (lexer, "EMPTY"))
4729 lex_match (lexer, T_EQUALS);
4730 if (lex_match_id (lexer, "ZERO"))
4732 /* Nothing to do. */
4734 else if (lex_match_id (lexer, "BLANK"))
4735 ct->zero = xstrdup ("");
4736 else if (lex_force_string (lexer))
4738 ct->zero = ss_xstrdup (lex_tokss (lexer));
4744 else if (lex_match_id (lexer, "MISSING"))
4746 lex_match (lexer, T_EQUALS);
4747 if (!lex_force_string (lexer))
4751 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
4752 ? ss_xstrdup (lex_tokss (lexer))
4758 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
4759 "UNITS", "EMPTY", "MISSING");
4764 if (widths[0] != SYSMIS && widths[1] != SYSMIS
4765 && widths[0] > widths[1])
4767 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
4771 for (size_t i = 0; i < 2; i++)
4772 if (widths[i] != SYSMIS)
4774 int *wr = ct->look->width_ranges[TABLE_HORZ];
4775 wr[i] = widths[i] / units_per_inch * 96.0;
4780 else if (lex_match_id (lexer, "VLABELS"))
4782 if (!lex_force_match_id (lexer, "VARIABLES"))
4784 lex_match (lexer, T_EQUALS);
4786 struct variable **vars;
4788 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
4792 if (!lex_force_match_id (lexer, "DISPLAY"))
4797 lex_match (lexer, T_EQUALS);
4799 enum ctables_vlabel vlabel;
4800 if (lex_match_id (lexer, "DEFAULT"))
4801 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
4802 else if (lex_match_id (lexer, "NAME"))
4804 else if (lex_match_id (lexer, "LABEL"))
4805 vlabel = CTVL_LABEL;
4806 else if (lex_match_id (lexer, "BOTH"))
4808 else if (lex_match_id (lexer, "NONE"))
4812 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
4818 for (size_t i = 0; i < n_vars; i++)
4819 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
4822 else if (lex_match_id (lexer, "MRSETS"))
4824 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
4826 lex_match (lexer, T_EQUALS);
4827 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
4830 else if (lex_match_id (lexer, "SMISSING"))
4832 if (lex_match_id (lexer, "VARIABLE"))
4833 ct->smissing_listwise = false;
4834 else if (lex_match_id (lexer, "LISTWISE"))
4835 ct->smissing_listwise = true;
4838 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
4842 else if (lex_match_id (lexer, "PCOMPUTE"))
4844 if (!ctables_parse_pcompute (lexer, ct))
4847 else if (lex_match_id (lexer, "PPROPERTIES"))
4849 if (!ctables_parse_pproperties (lexer, ct))
4852 else if (lex_match_id (lexer, "WEIGHT"))
4854 if (!lex_force_match_id (lexer, "VARIABLE"))
4856 lex_match (lexer, T_EQUALS);
4857 ct->e_weight = parse_variable (lexer, dataset_dict (ds));
4861 else if (lex_match_id (lexer, " HIDESMALLCOUNTS"))
4863 if (lex_match_id (lexer, "COUNT"))
4865 lex_match (lexer, T_EQUALS);
4866 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT",
4869 ct->hide_threshold = lex_integer (lexer);
4872 else if (ct->hide_threshold == 0)
4873 ct->hide_threshold = 5;
4877 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
4878 "SMISSING", "PCOMPUTE", "PPROPERTIES",
4879 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
4883 if (!lex_force_match (lexer, T_SLASH))
4887 size_t allocated_tables = 0;
4890 if (ct->n_tables >= allocated_tables)
4891 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
4892 sizeof *ct->tables);
4894 struct ctables_category *cat = xmalloc (sizeof *cat);
4895 *cat = (struct ctables_category) {
4897 .include_missing = false,
4898 .sort_ascending = true,
4901 struct ctables_categories *c = xmalloc (sizeof *c);
4902 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
4903 *c = (struct ctables_categories) {
4910 struct ctables_categories **categories = xnmalloc (n_vars,
4911 sizeof *categories);
4912 for (size_t i = 0; i < n_vars; i++)
4915 struct ctables_table *t = xmalloc (sizeof *t);
4916 *t = (struct ctables_table) {
4918 .slabels_axis = PIVOT_AXIS_COLUMN,
4919 .slabels_visible = true,
4920 .clabels_values_map = HMAP_INITIALIZER (t->clabels_values_map),
4922 [PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW,
4923 [PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN,
4924 [PIVOT_AXIS_LAYER] = PIVOT_AXIS_LAYER,
4926 .clabels_from_axis = PIVOT_AXIS_LAYER,
4927 .categories = categories,
4928 .n_categories = n_vars,
4931 ct->tables[ct->n_tables++] = t;
4933 lex_match (lexer, T_EQUALS);
4934 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
4936 if (lex_match (lexer, T_BY))
4938 if (!ctables_axis_parse (lexer, dataset_dict (ds),
4939 ct, t, PIVOT_AXIS_COLUMN))
4942 if (lex_match (lexer, T_BY))
4944 if (!ctables_axis_parse (lexer, dataset_dict (ds),
4945 ct, t, PIVOT_AXIS_LAYER))
4950 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
4951 && !t->axes[PIVOT_AXIS_LAYER])
4953 lex_error (lexer, _("At least one variable must be specified."));
4957 const struct ctables_axis *scales[PIVOT_N_AXES];
4958 size_t n_scales = 0;
4959 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
4961 scales[a] = find_scale (t->axes[a]);
4967 msg (SE, _("Scale variables may appear only on one axis."));
4968 if (scales[PIVOT_AXIS_ROW])
4969 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
4970 _("This scale variable appears on the rows axis."));
4971 if (scales[PIVOT_AXIS_COLUMN])
4972 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
4973 _("This scale variable appears on the columns axis."));
4974 if (scales[PIVOT_AXIS_LAYER])
4975 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
4976 _("This scale variable appears on the layer axis."));
4980 const struct ctables_axis *summaries[PIVOT_N_AXES];
4981 size_t n_summaries = 0;
4982 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
4984 summaries[a] = (scales[a]
4986 : find_categorical_summary_spec (t->axes[a]));
4990 if (n_summaries > 1)
4992 msg (SE, _("Summaries may appear only on one axis."));
4993 if (summaries[PIVOT_AXIS_ROW])
4994 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
4995 _("This variable on the rows axis has a summary."));
4996 if (summaries[PIVOT_AXIS_COLUMN])
4997 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
4998 _("This variable on the columns axis has a summary."));
4999 if (summaries[PIVOT_AXIS_LAYER])
5000 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
5001 _("This variable on the layers axis has a summary."));
5004 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
5005 if (n_summaries ? summaries[a] : t->axes[a])
5007 t->summary_axis = a;
5011 if (lex_token (lexer) == T_ENDCMD)
5013 if (!ctables_prepare_table (t))
5017 if (!lex_force_match (lexer, T_SLASH))
5020 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
5022 if (lex_match_id (lexer, "SLABELS"))
5024 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
5026 if (lex_match_id (lexer, "POSITION"))
5028 lex_match (lexer, T_EQUALS);
5029 if (lex_match_id (lexer, "COLUMN"))
5030 t->slabels_axis = PIVOT_AXIS_COLUMN;
5031 else if (lex_match_id (lexer, "ROW"))
5032 t->slabels_axis = PIVOT_AXIS_ROW;
5033 else if (lex_match_id (lexer, "LAYER"))
5034 t->slabels_axis = PIVOT_AXIS_LAYER;
5037 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
5041 else if (lex_match_id (lexer, "VISIBLE"))
5043 lex_match (lexer, T_EQUALS);
5044 if (!parse_bool (lexer, &t->slabels_visible))
5049 lex_error_expecting (lexer, "POSITION", "VISIBLE");
5054 else if (lex_match_id (lexer, "CLABELS"))
5056 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
5058 if (lex_match_id (lexer, "AUTO"))
5060 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW;
5061 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN;
5063 else if (lex_match_id (lexer, "ROWLABELS"))
5065 lex_match (lexer, T_EQUALS);
5066 if (lex_match_id (lexer, "OPPOSITE"))
5067 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_COLUMN;
5068 else if (lex_match_id (lexer, "LAYER"))
5069 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_LAYER;
5072 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
5076 else if (lex_match_id (lexer, "COLLABELS"))
5078 lex_match (lexer, T_EQUALS);
5079 if (lex_match_id (lexer, "OPPOSITE"))
5080 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_ROW;
5081 else if (lex_match_id (lexer, "LAYER"))
5082 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_LAYER;
5085 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
5091 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
5097 else if (lex_match_id (lexer, "CRITERIA"))
5099 if (!lex_force_match_id (lexer, "CILEVEL"))
5101 lex_match (lexer, T_EQUALS);
5103 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
5105 t->cilevel = lex_number (lexer);
5108 else if (lex_match_id (lexer, "CATEGORIES"))
5110 if (!ctables_table_parse_categories (lexer, dataset_dict (ds),
5114 else if (lex_match_id (lexer, "TITLES"))
5119 if (lex_match_id (lexer, "CAPTION"))
5120 textp = &t->caption;
5121 else if (lex_match_id (lexer, "CORNER"))
5123 else if (lex_match_id (lexer, "TITLE"))
5127 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
5130 lex_match (lexer, T_EQUALS);
5132 struct string s = DS_EMPTY_INITIALIZER;
5133 while (lex_is_string (lexer))
5135 if (!ds_is_empty (&s))
5136 ds_put_byte (&s, ' ');
5137 ds_put_substring (&s, lex_tokss (lexer));
5141 *textp = ds_steal_cstr (&s);
5143 while (lex_token (lexer) != T_SLASH
5144 && lex_token (lexer) != T_ENDCMD);
5146 else if (lex_match_id (lexer, "SIGTEST"))
5150 t->chisq = xmalloc (sizeof *t->chisq);
5151 *t->chisq = (struct ctables_chisq) {
5153 .include_mrsets = true,
5154 .all_visible = true,
5160 if (lex_match_id (lexer, "TYPE"))
5162 lex_match (lexer, T_EQUALS);
5163 if (!lex_force_match_id (lexer, "CHISQUARE"))
5166 else if (lex_match_id (lexer, "ALPHA"))
5168 lex_match (lexer, T_EQUALS);
5169 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
5171 t->chisq->alpha = lex_number (lexer);
5174 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
5176 lex_match (lexer, T_EQUALS);
5177 if (parse_bool (lexer, &t->chisq->include_mrsets))
5180 else if (lex_match_id (lexer, "CATEGORIES"))
5182 lex_match (lexer, T_EQUALS);
5183 if (lex_match_id (lexer, "ALLVISIBLE"))
5184 t->chisq->all_visible = true;
5185 else if (lex_match_id (lexer, "SUBTOTALS"))
5186 t->chisq->all_visible = false;
5189 lex_error_expecting (lexer,
5190 "ALLVISIBLE", "SUBTOTALS");
5196 lex_error_expecting (lexer, "TYPE", "ALPHA",
5197 "INCLUDEMRSETS", "CATEGORIES");
5201 while (lex_token (lexer) != T_SLASH
5202 && lex_token (lexer) != T_ENDCMD);
5204 else if (lex_match_id (lexer, "COMPARETEST"))
5208 t->pairwise = xmalloc (sizeof *t->pairwise);
5209 *t->pairwise = (struct ctables_pairwise) {
5211 .alpha = { .05, .05 },
5212 .adjust = BONFERRONI,
5213 .include_mrsets = true,
5214 .meansvariance_allcats = true,
5215 .all_visible = true,
5224 if (lex_match_id (lexer, "TYPE"))
5226 lex_match (lexer, T_EQUALS);
5227 if (lex_match_id (lexer, "PROP"))
5228 t->pairwise->type = PROP;
5229 else if (lex_match_id (lexer, "MEAN"))
5230 t->pairwise->type = MEAN;
5233 lex_error_expecting (lexer, "PROP", "MEAN");
5237 else if (lex_match_id (lexer, "ALPHA"))
5239 lex_match (lexer, T_EQUALS);
5241 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
5243 double a0 = lex_number (lexer);
5246 lex_match (lexer, T_COMMA);
5247 if (lex_is_number (lexer))
5249 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
5251 double a1 = lex_number (lexer);
5254 t->pairwise->alpha[0] = MIN (a0, a1);
5255 t->pairwise->alpha[1] = MAX (a0, a1);
5258 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
5260 else if (lex_match_id (lexer, "ADJUST"))
5262 lex_match (lexer, T_EQUALS);
5263 if (lex_match_id (lexer, "BONFERRONI"))
5264 t->pairwise->adjust = BONFERRONI;
5265 else if (lex_match_id (lexer, "BH"))
5266 t->pairwise->adjust = BH;
5267 else if (lex_match_id (lexer, "NONE"))
5268 t->pairwise->adjust = 0;
5271 lex_error_expecting (lexer, "BONFERRONI", "BH",
5276 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
5278 lex_match (lexer, T_EQUALS);
5279 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
5282 else if (lex_match_id (lexer, "MEANSVARIANCE"))
5284 lex_match (lexer, T_EQUALS);
5285 if (lex_match_id (lexer, "ALLCATS"))
5286 t->pairwise->meansvariance_allcats = true;
5287 else if (lex_match_id (lexer, "TESTEDCATS"))
5288 t->pairwise->meansvariance_allcats = false;
5291 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
5295 else if (lex_match_id (lexer, "CATEGORIES"))
5297 lex_match (lexer, T_EQUALS);
5298 if (lex_match_id (lexer, "ALLVISIBLE"))
5299 t->pairwise->all_visible = true;
5300 else if (lex_match_id (lexer, "SUBTOTALS"))
5301 t->pairwise->all_visible = false;
5304 lex_error_expecting (lexer, "ALLVISIBLE",
5309 else if (lex_match_id (lexer, "MERGE"))
5311 lex_match (lexer, T_EQUALS);
5312 if (!parse_bool (lexer, &t->pairwise->merge))
5315 else if (lex_match_id (lexer, "STYLE"))
5317 lex_match (lexer, T_EQUALS);
5318 if (lex_match_id (lexer, "APA"))
5319 t->pairwise->apa_style = true;
5320 else if (lex_match_id (lexer, "SIMPLE"))
5321 t->pairwise->apa_style = false;
5324 lex_error_expecting (lexer, "APA", "SIMPLE");
5328 else if (lex_match_id (lexer, "SHOWSIG"))
5330 lex_match (lexer, T_EQUALS);
5331 if (!parse_bool (lexer, &t->pairwise->show_sig))
5336 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
5337 "INCLUDEMRSETS", "MEANSVARIANCE",
5338 "CATEGORIES", "MERGE", "STYLE",
5343 while (lex_token (lexer) != T_SLASH
5344 && lex_token (lexer) != T_ENDCMD);
5348 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
5349 "CRITERIA", "CATEGORIES", "TITLES",
5350 "SIGTEST", "COMPARETEST");
5354 if (!lex_match (lexer, T_SLASH))
5358 if (t->label_axis[PIVOT_AXIS_ROW] != PIVOT_AXIS_ROW
5359 && t->label_axis[PIVOT_AXIS_COLUMN] != PIVOT_AXIS_COLUMN)
5361 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
5365 if (!ctables_prepare_table (t))
5368 while (lex_token (lexer) != T_ENDCMD);
5370 bool ok = ctables_execute (ds, ct);
5371 ctables_destroy (ct);
5372 return ok ? CMD_SUCCESS : CMD_FAILURE;
5375 ctables_destroy (ct);