1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/casereader.h"
22 #include "data/casewriter.h"
23 #include "data/dataset.h"
24 #include "data/dictionary.h"
25 #include "data/mrset.h"
26 #include "data/subcase.h"
27 #include "data/value-labels.h"
28 #include "language/command.h"
29 #include "language/lexer/format-parser.h"
30 #include "language/lexer/lexer.h"
31 #include "language/lexer/variable-parser.h"
32 #include "libpspp/array.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/hash-functions.h"
35 #include "libpspp/hmap.h"
36 #include "libpspp/message.h"
37 #include "libpspp/string-array.h"
38 #include "math/mode.h"
39 #include "math/moments.h"
40 #include "math/percentiles.h"
41 #include "math/sort.h"
42 #include "output/pivot-table.h"
44 #include "gl/minmax.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) (msgid)
53 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
54 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
55 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
56 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
60 - unweighted summaries (U*)
61 - lower confidence limits (*.LCL)
62 - upper confidence limits (*.UCL)
63 - standard error (*.SE)
66 /* All variables. */ \
67 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
68 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
69 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
85 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
86 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
87 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
88 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
89 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
91 /* Scale variables, totals, and subtotals. */ \
92 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
97 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
98 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
100 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
103 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
104 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
105 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
106 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
107 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
108 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
109 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
110 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
111 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
112 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
113 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
114 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
116 /* Multiple response sets. */ \
117 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
118 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
134 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
135 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
136 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
137 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
138 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
140 enum ctables_summary_function
142 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
148 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
149 N_CTSF_FUNCTIONS = SUMMARIES
153 enum ctables_domain_type
155 /* Within a section, where stacked variables divide one section from
157 CTDT_TABLE, /* All layers of a whole section. */
158 CTDT_LAYER, /* One layer within a section. */
159 CTDT_LAYERROW, /* Row in one layer within a section. */
160 CTDT_LAYERCOL, /* Column in one layer within a section. */
162 /* Within a subtable, where a subtable pairs an innermost row variable with
163 an innermost column variable within a single layer. */
164 CTDT_SUBTABLE, /* Whole subtable. */
165 CTDT_ROW, /* Row within a subtable. */
166 CTDT_COL, /* Column within a subtable. */
170 struct ctables_domain
172 struct hmap_node node;
174 const struct ctables_cell *example;
176 double d_valid; /* Dictionary weight. */
178 double e_valid; /* Effective weight */
182 enum ctables_summary_variant
191 /* In struct ctables_section's 'cells' hmap. Indexed by all the values in
192 all the axes (except the scalar variable, if any). */
193 struct hmap_node node;
195 /* The domains that contain this cell. */
196 bool contributes_to_domains;
197 struct ctables_domain *domains[N_CTDTS];
200 enum ctables_summary_variant sv;
202 struct ctables_cell_axis
204 struct ctables_cell_value
206 const struct ctables_category *category;
214 union ctables_summary *summaries;
219 const struct dictionary *dict;
220 struct pivot_table_look *look;
222 /* If this is NULL, zeros are displayed using the normal print format.
223 Otherwise, this string is displayed. */
226 /* If this is NULL, missing values are displayed using the normal print
227 format. Otherwise, this string is displayed. */
230 /* Indexed by variable dictionary index. */
231 enum ctables_vlabel *vlabels;
233 bool mrsets_count_duplicates; /* MRSETS. */
234 bool smissing_listwise; /* SMISSING. */
235 struct variable *e_weight; /* WEIGHT. */
236 int hide_threshold; /* HIDESMALLCOUNTS. */
238 struct ctables_table **tables;
242 struct ctables_postcompute
244 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
245 const char *name; /* Name, without leading &. */
247 struct ctables_postcompute_expr *expr;
250 bool hide_source_cats;
253 struct ctables_postcompute_expr
255 enum ctables_postcompute_op
263 /* XXX SUBTOTAL and HSUBTOTAL */
276 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
281 XXX what about string ranges? */
284 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
285 struct ctables_postcompute_expr *subs[2];
289 struct ctables_summary_spec_set
291 struct ctables_summary_spec *specs;
295 struct variable *var;
298 static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
299 const struct ctables_summary_spec_set *);
300 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
302 /* A nested sequence of variables, e.g. a > b > c. */
305 struct variable **vars;
308 size_t *domains[N_CTDTS];
309 size_t n_domains[N_CTDTS];
311 struct ctables_summary_spec_set specs[N_CSVS];
314 /* A stack of nestings, e.g. nest1 + nest2 + ... + nestN. */
317 struct ctables_nest *nests;
323 struct hmap_node node;
328 struct ctables_section_value
330 struct hmap_node node;
334 struct ctables_section
336 struct ctables_table *table;
337 struct ctables_nest *nests[PIVOT_N_AXES];
338 struct hmap *occurrences[PIVOT_N_AXES];
339 struct hmap cells; /* Contains "struct ctable_cell"s. */
340 struct hmap domains[N_CTDTS]; /* Contains "struct ctable_domain"s. */
345 struct ctables *ctables;
346 struct ctables_axis *axes[PIVOT_N_AXES];
347 struct ctables_stack stacks[PIVOT_N_AXES];
348 struct ctables_section *sections;
350 enum pivot_axis_type summary_axis;
351 struct ctables_summary_spec_set summary_specs;
353 const struct variable *clabels_example;
354 struct hmap clabels_values_map;
355 struct ctables_value **clabels_values;
356 size_t n_clabels_values;
358 enum pivot_axis_type slabels_axis;
359 bool slabels_visible;
361 /* The innermost category labels for axis 'a' appear on axis label_axis[a].
363 Most commonly, label_axis[a] == a, and in particular we always have
364 label_axis{PIVOT_AXIS_LAYER] == PIVOT_AXIS_LAYER.
366 If ROWLABELS or COLLABELS is specified, then one of
367 label_axis[PIVOT_AXIS_ROW] or label_axis[PIVOT_AXIS_COLUMN] can be the
368 opposite axis or PIVOT_AXIS_LAYER. Only one of them will differ.
370 enum pivot_axis_type label_axis[PIVOT_N_AXES];
371 enum pivot_axis_type clabels_from_axis;
373 /* Indexed by variable dictionary index. */
374 struct ctables_categories **categories;
383 struct ctables_chisq *chisq;
384 struct ctables_pairwise *pairwise;
392 struct variable *var;
393 const struct mrset *mrset;
397 static const struct fmt_spec *
398 ctables_var_get_print_format (const struct ctables_var *var)
400 return (var->is_mrset
401 ? var_get_print_format (var->mrset->vars[0])
402 : var_get_print_format (var->var));
406 ctables_var_name (const struct ctables_var *var)
408 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
411 struct ctables_categories
414 struct ctables_category *cats;
419 struct ctables_category
421 enum ctables_category_type
423 /* Explicit category lists. */
430 /* Totals and subtotals. */
435 /* Implicit category lists. */
442 struct ctables_category *subtotal;
446 double number; /* CCT_NUMBER. */
447 char *string; /* CCT_STRING. */
448 double range[2]; /* CCT_RANGE. */
449 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
451 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
454 bool include_missing;
458 enum ctables_summary_function sort_function;
459 struct variable *sort_var;
466 ctables_category_uninit (struct ctables_category *cat)
486 free (cat->total_label);
497 ctables_category_equal (const struct ctables_category *a,
498 const struct ctables_category *b)
500 if (a->type != b->type)
506 return a->number == b->number;
509 return strcmp (a->string, b->string);
512 return a->range[0] == b->range[0] && a->range[1] == b->range[1];
521 return !strcmp (a->total_label, b->total_label);
526 return (a->include_missing == b->include_missing
527 && a->sort_ascending == b->sort_ascending
528 && a->sort_function == b->sort_function
529 && a->sort_var == b->sort_var
530 && a->percentile == b->percentile);
537 ctables_categories_unref (struct ctables_categories *c)
542 assert (c->n_refs > 0);
546 for (size_t i = 0; i < c->n_cats; i++)
547 ctables_category_uninit (&c->cats[i]);
553 ctables_categories_equal (const struct ctables_categories *a,
554 const struct ctables_categories *b)
556 if (a->n_cats != b->n_cats || a->show_empty != b->show_empty)
559 for (size_t i = 0; i < a->n_cats; i++)
560 if (!ctables_category_equal (&a->cats[i], &b->cats[i]))
566 /* Chi-square test (SIGTEST). */
574 /* Pairwise comparison test (COMPARETEST). */
575 struct ctables_pairwise
577 enum { PROP, MEAN } type;
580 bool meansvariance_allcats;
582 enum { BONFERRONI = 1, BH } adjust;
606 struct ctables_var var;
608 struct ctables_summary_spec_set specs[N_CSVS];
612 struct ctables_axis *subs[2];
615 struct msg_location *loc;
618 static void ctables_axis_destroy (struct ctables_axis *);
627 enum ctables_function_availability
629 CTFA_ALL, /* Any variables. */
630 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
631 CTFA_MRSETS, /* Only multiple-response sets */
634 struct ctables_summary_spec
636 enum ctables_summary_function function;
637 double percentile; /* CTSF_PTILE only. */
639 struct fmt_spec format; /* XXX extra CTABLES formats */
644 ctables_summary_spec_clone (struct ctables_summary_spec *dst,
645 const struct ctables_summary_spec *src)
648 dst->label = xstrdup (src->label);
652 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
659 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
660 const struct ctables_summary_spec_set *src)
662 struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
663 for (size_t i = 0; i < src->n; i++)
664 ctables_summary_spec_clone (&specs[i], &src->specs[i]);
666 *dst = (struct ctables_summary_spec_set) {
675 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
677 for (size_t i = 0; i < set->n; i++)
678 ctables_summary_spec_uninit (&set->specs[i]);
683 parse_col_width (struct lexer *lexer, const char *name, double *width)
685 lex_match (lexer, T_EQUALS);
686 if (lex_match_id (lexer, "DEFAULT"))
688 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
690 *width = lex_number (lexer);
700 parse_bool (struct lexer *lexer, bool *b)
702 if (lex_match_id (lexer, "NO"))
704 else if (lex_match_id (lexer, "YES"))
708 lex_error_expecting (lexer, "YES", "NO");
714 static enum ctables_function_availability
715 ctables_function_availability (enum ctables_summary_function f)
717 static enum ctables_function_availability availability[] = {
718 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
723 return availability[f];
727 parse_ctables_summary_function (struct lexer *lexer,
728 enum ctables_summary_function *f)
732 enum ctables_summary_function function;
733 struct substring name;
735 static struct pair names[] = {
736 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
737 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
740 /* The .COUNT suffix may be omitted. */
741 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
742 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
743 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
744 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
745 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
746 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
747 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
751 if (!lex_force_id (lexer))
754 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
755 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
757 *f = names[i].function;
762 lex_error (lexer, _("Expecting summary function name."));
767 ctables_axis_destroy (struct ctables_axis *axis)
775 for (size_t i = 0; i < N_CSVS; i++)
776 ctables_summary_spec_set_uninit (&axis->specs[i]);
781 ctables_axis_destroy (axis->subs[0]);
782 ctables_axis_destroy (axis->subs[1]);
785 msg_location_destroy (axis->loc);
789 static struct ctables_axis *
790 ctables_axis_new_nonterminal (enum ctables_axis_op op,
791 struct ctables_axis *sub0,
792 struct ctables_axis *sub1,
793 struct lexer *lexer, int start_ofs)
795 struct ctables_axis *axis = xmalloc (sizeof *axis);
796 *axis = (struct ctables_axis) {
798 .subs = { sub0, sub1 },
799 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
804 struct ctables_axis_parse_ctx
807 struct dictionary *dict;
809 struct ctables_table *t;
812 static struct fmt_spec
813 ctables_summary_default_format (enum ctables_summary_function function,
814 const struct ctables_var *var)
816 static const enum ctables_format default_formats[] = {
817 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
821 switch (default_formats[function])
824 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
827 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
830 return *ctables_var_get_print_format (var);
838 ctables_summary_default_label (enum ctables_summary_function function,
841 static const char *default_labels[] = {
842 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
847 return (function == CTSF_PTILE
848 ? xasprintf (_("Percentile %.2f"), percentile)
849 : xstrdup (gettext (default_labels[function])));
853 ctables_summary_function_name (enum ctables_summary_function function)
855 static const char *names[] = {
856 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
860 return names[function];
864 add_summary_spec (struct ctables_axis *axis,
865 enum ctables_summary_function function, double percentile,
866 const char *label, const struct fmt_spec *format,
867 const struct msg_location *loc, enum ctables_summary_variant sv)
869 if (axis->op == CTAO_VAR)
871 const char *function_name = ctables_summary_function_name (function);
872 const char *var_name = ctables_var_name (&axis->var);
873 switch (ctables_function_availability (function))
876 if (!axis->var.is_mrset)
878 msg_at (SE, loc, _("Summary function %s applies only to multiple "
879 "response sets."), function_name);
880 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
890 _("Summary function %s applies only to scale variables."),
892 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
902 struct ctables_summary_spec_set *set = &axis->specs[sv];
903 if (set->n >= set->allocated)
904 set->specs = x2nrealloc (set->specs, &set->allocated,
907 struct ctables_summary_spec *dst = &set->specs[set->n++];
908 *dst = (struct ctables_summary_spec) {
909 .function = function,
910 .percentile = percentile,
911 .label = xstrdup (label),
912 .format = (format ? *format
913 : ctables_summary_default_format (function, &axis->var)),
919 for (size_t i = 0; i < 2; i++)
920 if (!add_summary_spec (axis->subs[i], function, percentile, label,
927 static struct ctables_axis *ctables_axis_parse_stack (
928 struct ctables_axis_parse_ctx *);
931 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
932 struct ctables_var *var)
934 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
936 *var = (struct ctables_var) {
938 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
942 lex_error (lexer, _("'%s' does not name a multiple-response set "
943 "in the active file dictionary."),
944 lex_tokcstr (lexer));
952 *var = (struct ctables_var) {
954 .var = parse_variable (lexer, dict),
956 return var->var != NULL;
960 static struct ctables_axis *
961 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
963 if (lex_match (ctx->lexer, T_LPAREN))
965 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
966 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
968 ctables_axis_destroy (sub);
974 if (!lex_force_id (ctx->lexer))
977 int start_ofs = lex_ofs (ctx->lexer);
978 struct ctables_var var;
979 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
982 struct ctables_axis *axis = xmalloc (sizeof *axis);
983 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
985 /* XXX should figure out default measures by reading data */
986 axis->scale = (var.is_mrset ? false
987 : lex_match_phrase (ctx->lexer, "[S]") ? true
988 : lex_match_phrase (ctx->lexer, "[C]") ? false
989 : var_get_measure (var.var) == MEASURE_SCALE);
990 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
991 lex_ofs (ctx->lexer) - 1);
996 has_digit (const char *s)
998 return s[strcspn (s, "0123456789")] != '\0';
1001 static struct ctables_axis *
1002 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
1004 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
1005 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
1008 enum ctables_summary_variant sv = CSV_CELL;
1011 int start_ofs = lex_ofs (ctx->lexer);
1013 /* Parse function. */
1014 enum ctables_summary_function function;
1015 if (!parse_ctables_summary_function (ctx->lexer, &function))
1018 /* Parse percentile. */
1019 double percentile = 0;
1020 if (function == CTSF_PTILE)
1022 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
1024 percentile = lex_number (ctx->lexer);
1025 lex_get (ctx->lexer);
1030 if (lex_is_string (ctx->lexer))
1032 label = ss_xstrdup (lex_tokss (ctx->lexer));
1033 lex_get (ctx->lexer);
1036 label = ctables_summary_default_label (function, percentile);
1039 struct fmt_spec format;
1040 const struct fmt_spec *formatp;
1041 if (lex_token (ctx->lexer) == T_ID
1042 && has_digit (lex_tokcstr (ctx->lexer)))
1044 if (!parse_format_specifier (ctx->lexer, &format)
1045 || !fmt_check_output (&format)
1046 || !fmt_check_type_compat (&format, VAL_NUMERIC))
1056 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
1057 lex_ofs (ctx->lexer) - 1);
1058 add_summary_spec (sub, function, percentile, label, formatp, loc, sv);
1060 msg_location_destroy (loc);
1062 lex_match (ctx->lexer, T_COMMA);
1063 if (sv == CSV_CELL && lex_match_id (ctx->lexer, "TOTALS"))
1065 if (!lex_force_match (ctx->lexer, T_LBRACK))
1069 else if (lex_match (ctx->lexer, T_RBRACK))
1071 if (sv == CSV_TOTAL && !lex_force_match (ctx->lexer, T_RBRACK))
1078 ctables_axis_destroy (sub);
1082 static const struct ctables_axis *
1083 find_scale (const struct ctables_axis *axis)
1087 else if (axis->op == CTAO_VAR)
1091 assert (!axis->var.is_mrset);
1099 for (size_t i = 0; i < 2; i++)
1101 const struct ctables_axis *scale = find_scale (axis->subs[i]);
1109 static const struct ctables_axis *
1110 find_categorical_summary_spec (const struct ctables_axis *axis)
1114 else if (axis->op == CTAO_VAR)
1115 return !axis->scale && axis->specs[CSV_CELL].n ? axis : NULL;
1118 for (size_t i = 0; i < 2; i++)
1120 const struct ctables_axis *sum
1121 = find_categorical_summary_spec (axis->subs[i]);
1129 static struct ctables_axis *
1130 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1132 int start_ofs = lex_ofs (ctx->lexer);
1133 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1137 while (lex_match (ctx->lexer, T_GT))
1139 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1143 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1144 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1146 const struct ctables_axis *outer_scale = find_scale (lhs);
1147 const struct ctables_axis *inner_scale = find_scale (rhs);
1148 if (outer_scale && inner_scale)
1150 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1151 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1152 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1153 ctables_axis_destroy (nest);
1157 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1160 msg_at (SE, nest->loc,
1161 _("Summaries may only be requested for categorical variables "
1162 "at the innermost nesting level."));
1163 msg_at (SN, outer_sum->loc,
1164 _("This outer categorical variable has a summary."));
1165 ctables_axis_destroy (nest);
1175 static struct ctables_axis *
1176 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1178 int start_ofs = lex_ofs (ctx->lexer);
1179 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1183 while (lex_match (ctx->lexer, T_PLUS))
1185 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1189 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1190 ctx->lexer, start_ofs);
1197 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1198 struct ctables *ct, struct ctables_table *t,
1199 enum pivot_axis_type a)
1201 if (lex_token (lexer) == T_BY
1202 || lex_token (lexer) == T_SLASH
1203 || lex_token (lexer) == T_ENDCMD)
1206 struct ctables_axis_parse_ctx ctx = {
1212 t->axes[a] = ctables_axis_parse_stack (&ctx);
1213 return t->axes[a] != NULL;
1217 ctables_chisq_destroy (struct ctables_chisq *chisq)
1223 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1229 ctables_table_destroy (struct ctables_table *t)
1234 for (size_t i = 0; i < t->n_categories; i++)
1235 ctables_categories_unref (t->categories[i]);
1236 free (t->categories);
1238 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1239 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1240 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1244 ctables_chisq_destroy (t->chisq);
1245 ctables_pairwise_destroy (t->pairwise);
1250 ctables_destroy (struct ctables *ct)
1255 pivot_table_look_unref (ct->look);
1259 for (size_t i = 0; i < ct->n_tables; i++)
1260 ctables_table_destroy (ct->tables[i]);
1265 static struct ctables_category
1266 cct_range (double low, double high)
1268 return (struct ctables_category) {
1270 .range = { low, high }
1275 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1276 struct ctables_table *t)
1278 if (!lex_match_id (lexer, "VARIABLES"))
1280 lex_match (lexer, T_EQUALS);
1282 struct variable **vars;
1284 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1287 struct ctables_categories *c = xmalloc (sizeof *c);
1288 *c = (struct ctables_categories) { .n_refs = n_vars };
1289 for (size_t i = 0; i < n_vars; i++)
1291 struct ctables_categories **cp
1292 = &t->categories[var_get_dict_index (vars[i])];
1293 ctables_categories_unref (*cp);
1298 size_t allocated_cats = 0;
1299 if (lex_match (lexer, T_LBRACK))
1303 if (c->n_cats >= allocated_cats)
1304 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1306 struct ctables_category *cat = &c->cats[c->n_cats];
1307 if (lex_match_id (lexer, "OTHERNM"))
1308 cat->type = CCT_OTHERNM;
1309 else if (lex_match_id (lexer, "MISSING"))
1310 cat->type = CCT_MISSING;
1311 else if (lex_match_id (lexer, "SUBTOTAL"))
1312 *cat = (struct ctables_category)
1313 { .type = CCT_SUBTOTAL, .total_label = NULL };
1314 else if (lex_match_id (lexer, "HSUBTOTAL"))
1315 *cat = (struct ctables_category)
1316 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1317 else if (lex_match_id (lexer, "LO"))
1319 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1321 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1324 else if (lex_is_number (lexer))
1326 double number = lex_number (lexer);
1328 if (lex_match_id (lexer, "THRU"))
1330 cat->type = CCT_RANGE;
1331 cat->range[0] = number;
1332 if (lex_match_id (lexer, "HI"))
1333 *cat = cct_range (number, DBL_MAX);
1336 if (!lex_force_num (lexer))
1338 *cat = cct_range (number, lex_number (lexer));
1343 *cat = (struct ctables_category) {
1348 else if (lex_is_string (lexer))
1350 *cat = (struct ctables_category) {
1352 .string = ss_xstrdup (lex_tokss (lexer)),
1358 lex_error (lexer, NULL);
1362 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1364 if (lex_match (lexer, T_EQUALS))
1366 if (!lex_force_string (lexer))
1369 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1373 cat->total_label = xstrdup (_("Subtotal"));
1377 lex_match (lexer, T_COMMA);
1379 while (!lex_match (lexer, T_RBRACK));
1382 struct ctables_category cat = {
1384 .include_missing = false,
1385 .sort_ascending = true,
1387 bool show_totals = false;
1388 char *total_label = NULL;
1389 bool totals_before = false;
1390 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1392 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1394 lex_match (lexer, T_EQUALS);
1395 if (lex_match_id (lexer, "A"))
1396 cat.sort_ascending = true;
1397 else if (lex_match_id (lexer, "D"))
1398 cat.sort_ascending = false;
1401 lex_error_expecting (lexer, "A", "D");
1405 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1407 lex_match (lexer, T_EQUALS);
1408 if (lex_match_id (lexer, "VALUE"))
1409 cat.type = CCT_VALUE;
1410 else if (lex_match_id (lexer, "LABEL"))
1411 cat.type = CCT_LABEL;
1414 cat.type = CCT_FUNCTION;
1415 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1418 if (lex_match (lexer, T_LPAREN))
1420 cat.sort_var = parse_variable (lexer, dict);
1424 if (cat.sort_function == CTSF_PTILE)
1426 lex_match (lexer, T_COMMA);
1427 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1429 cat.percentile = lex_number (lexer);
1433 if (!lex_force_match (lexer, T_RPAREN))
1436 else if (ctables_function_availability (cat.sort_function)
1439 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1444 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1446 lex_match (lexer, T_EQUALS);
1447 if (lex_match_id (lexer, "INCLUDE"))
1448 cat.include_missing = true;
1449 else if (lex_match_id (lexer, "EXCLUDE"))
1450 cat.include_missing = false;
1453 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1457 else if (lex_match_id (lexer, "TOTAL"))
1459 lex_match (lexer, T_EQUALS);
1460 if (!parse_bool (lexer, &show_totals))
1463 else if (lex_match_id (lexer, "LABEL"))
1465 lex_match (lexer, T_EQUALS);
1466 if (!lex_force_string (lexer))
1469 total_label = ss_xstrdup (lex_tokss (lexer));
1472 else if (lex_match_id (lexer, "POSITION"))
1474 lex_match (lexer, T_EQUALS);
1475 if (lex_match_id (lexer, "BEFORE"))
1476 totals_before = true;
1477 else if (lex_match_id (lexer, "AFTER"))
1478 totals_before = false;
1481 lex_error_expecting (lexer, "BEFORE", "AFTER");
1485 else if (lex_match_id (lexer, "EMPTY"))
1487 lex_match (lexer, T_EQUALS);
1488 if (lex_match_id (lexer, "INCLUDE"))
1489 c->show_empty = true;
1490 else if (lex_match_id (lexer, "EXCLUDE"))
1491 c->show_empty = false;
1494 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1501 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1502 "TOTAL", "LABEL", "POSITION", "EMPTY");
1504 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1511 if (c->n_cats >= allocated_cats)
1512 c->cats = x2nrealloc (c->cats, &allocated_cats,
1514 c->cats[c->n_cats++] = cat;
1519 if (c->n_cats >= allocated_cats)
1520 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1522 struct ctables_category *totals;
1525 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1526 totals = &c->cats[0];
1529 totals = &c->cats[c->n_cats];
1532 *totals = (struct ctables_category) {
1534 .total_label = total_label ? total_label : xstrdup (_("Total")),
1538 struct ctables_category *subtotal = NULL;
1539 for (size_t i = totals_before ? 0 : c->n_cats;
1540 totals_before ? i < c->n_cats : i-- > 0;
1541 totals_before ? i++ : 0)
1543 struct ctables_category *cat = &c->cats[i];
1551 cat->subtotal = subtotal;
1571 ctables_nest_uninit (struct ctables_nest *nest)
1578 ctables_stack_uninit (struct ctables_stack *stack)
1582 for (size_t i = 0; i < stack->n; i++)
1583 ctables_nest_uninit (&stack->nests[i]);
1584 free (stack->nests);
1588 static struct ctables_stack
1589 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1596 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1597 for (size_t i = 0; i < s0.n; i++)
1598 for (size_t j = 0; j < s1.n; j++)
1600 const struct ctables_nest *a = &s0.nests[i];
1601 const struct ctables_nest *b = &s1.nests[j];
1603 size_t allocate = a->n + b->n;
1604 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1605 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1607 for (size_t k = 0; k < a->n; k++)
1608 vars[n++] = a->vars[k];
1609 for (size_t k = 0; k < b->n; k++)
1610 vars[n++] = b->vars[k];
1611 assert (n == allocate);
1613 const struct ctables_nest *summary_src;
1614 if (!a->specs[CSV_CELL].var)
1616 else if (!b->specs[CSV_CELL].var)
1621 struct ctables_nest *new = &stack.nests[stack.n++];
1622 *new = (struct ctables_nest) {
1624 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1625 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1629 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1630 ctables_summary_spec_set_clone (&new->specs[sv], &summary_src->specs[sv]);
1632 ctables_stack_uninit (&s0);
1633 ctables_stack_uninit (&s1);
1637 static struct ctables_stack
1638 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1640 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1641 for (size_t i = 0; i < s0.n; i++)
1642 stack.nests[stack.n++] = s0.nests[i];
1643 for (size_t i = 0; i < s1.n; i++)
1644 stack.nests[stack.n++] = s1.nests[i];
1645 assert (stack.n == s0.n + s1.n);
1651 static struct ctables_stack
1652 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1655 return (struct ctables_stack) { .n = 0 };
1660 assert (!a->var.is_mrset);
1662 struct variable **vars = xmalloc (sizeof *vars);
1665 struct ctables_nest *nest = xmalloc (sizeof *nest);
1666 *nest = (struct ctables_nest) {
1669 .scale_idx = a->scale ? 0 : SIZE_MAX,
1671 if (a->specs[CSV_CELL].n || a->scale)
1672 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1674 ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
1675 nest->specs[sv].var = a->var.var;
1677 return (struct ctables_stack) { .nests = nest, .n = 1 };
1680 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1681 enumerate_fts (axis_type, a->subs[1]));
1684 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1685 enumerate_fts (axis_type, a->subs[1]));
1691 union ctables_summary
1693 /* COUNT, VALIDN, TOTALN. */
1700 /* MINIMUM, MAXIMUM, RANGE. */
1707 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1708 struct moments1 *moments;
1710 /* MEDIAN, MODE, PTILE. */
1713 struct casewriter *writer;
1718 /* XXX multiple response */
1722 ctables_summary_init (union ctables_summary *s,
1723 const struct ctables_summary_spec *ss)
1725 switch (ss->function)
1729 case CTSF_ROWPCT_COUNT:
1730 case CTSF_COLPCT_COUNT:
1731 case CTSF_TABLEPCT_COUNT:
1732 case CTSF_SUBTABLEPCT_COUNT:
1733 case CTSF_LAYERPCT_COUNT:
1734 case CTSF_LAYERROWPCT_COUNT:
1735 case CTSF_LAYERCOLPCT_COUNT:
1736 case CTSF_ROWPCT_VALIDN:
1737 case CTSF_COLPCT_VALIDN:
1738 case CTSF_TABLEPCT_VALIDN:
1739 case CTSF_SUBTABLEPCT_VALIDN:
1740 case CTSF_LAYERPCT_VALIDN:
1741 case CTSF_LAYERROWPCT_VALIDN:
1742 case CTSF_LAYERCOLPCT_VALIDN:
1743 case CTSF_ROWPCT_TOTALN:
1744 case CTSF_COLPCT_TOTALN:
1745 case CTSF_TABLEPCT_TOTALN:
1746 case CTSF_SUBTABLEPCT_TOTALN:
1747 case CTSF_LAYERPCT_TOTALN:
1748 case CTSF_LAYERROWPCT_TOTALN:
1749 case CTSF_LAYERCOLPCT_TOTALN:
1755 s->missing = s->valid = 0;
1761 s->min = s->max = SYSMIS;
1769 case CTSF_ROWPCT_SUM:
1770 case CTSF_COLPCT_SUM:
1771 case CTSF_TABLEPCT_SUM:
1772 case CTSF_SUBTABLEPCT_SUM:
1773 case CTSF_LAYERPCT_SUM:
1774 case CTSF_LAYERROWPCT_SUM:
1775 case CTSF_LAYERCOLPCT_SUM:
1776 s->moments = moments1_create (MOMENT_VARIANCE);
1783 struct caseproto *proto = caseproto_create ();
1784 proto = caseproto_add_width (proto, 0);
1785 proto = caseproto_add_width (proto, 0);
1787 struct subcase ordering;
1788 subcase_init (&ordering, 0, 0, SC_ASCEND);
1789 s->writer = sort_create_writer (&ordering, proto);
1790 subcase_uninit (&ordering);
1791 caseproto_unref (proto);
1798 case CTSF_RESPONSES:
1799 case CTSF_ROWPCT_RESPONSES:
1800 case CTSF_COLPCT_RESPONSES:
1801 case CTSF_TABLEPCT_RESPONSES:
1802 case CTSF_SUBTABLEPCT_RESPONSES:
1803 case CTSF_LAYERPCT_RESPONSES:
1804 case CTSF_LAYERROWPCT_RESPONSES:
1805 case CTSF_LAYERCOLPCT_RESPONSES:
1806 case CTSF_ROWPCT_RESPONSES_COUNT:
1807 case CTSF_COLPCT_RESPONSES_COUNT:
1808 case CTSF_TABLEPCT_RESPONSES_COUNT:
1809 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1810 case CTSF_LAYERPCT_RESPONSES_COUNT:
1811 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1812 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1813 case CTSF_ROWPCT_COUNT_RESPONSES:
1814 case CTSF_COLPCT_COUNT_RESPONSES:
1815 case CTSF_TABLEPCT_COUNT_RESPONSES:
1816 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1817 case CTSF_LAYERPCT_COUNT_RESPONSES:
1818 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1819 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1825 ctables_summary_uninit (union ctables_summary *s,
1826 const struct ctables_summary_spec *ss)
1828 switch (ss->function)
1832 case CTSF_ROWPCT_COUNT:
1833 case CTSF_COLPCT_COUNT:
1834 case CTSF_TABLEPCT_COUNT:
1835 case CTSF_SUBTABLEPCT_COUNT:
1836 case CTSF_LAYERPCT_COUNT:
1837 case CTSF_LAYERROWPCT_COUNT:
1838 case CTSF_LAYERCOLPCT_COUNT:
1839 case CTSF_ROWPCT_VALIDN:
1840 case CTSF_COLPCT_VALIDN:
1841 case CTSF_TABLEPCT_VALIDN:
1842 case CTSF_SUBTABLEPCT_VALIDN:
1843 case CTSF_LAYERPCT_VALIDN:
1844 case CTSF_LAYERROWPCT_VALIDN:
1845 case CTSF_LAYERCOLPCT_VALIDN:
1846 case CTSF_ROWPCT_TOTALN:
1847 case CTSF_COLPCT_TOTALN:
1848 case CTSF_TABLEPCT_TOTALN:
1849 case CTSF_SUBTABLEPCT_TOTALN:
1850 case CTSF_LAYERPCT_TOTALN:
1851 case CTSF_LAYERROWPCT_TOTALN:
1852 case CTSF_LAYERCOLPCT_TOTALN:
1870 case CTSF_ROWPCT_SUM:
1871 case CTSF_COLPCT_SUM:
1872 case CTSF_TABLEPCT_SUM:
1873 case CTSF_SUBTABLEPCT_SUM:
1874 case CTSF_LAYERPCT_SUM:
1875 case CTSF_LAYERROWPCT_SUM:
1876 case CTSF_LAYERCOLPCT_SUM:
1877 moments1_destroy (s->moments);
1883 casewriter_destroy (s->writer);
1886 case CTSF_RESPONSES:
1887 case CTSF_ROWPCT_RESPONSES:
1888 case CTSF_COLPCT_RESPONSES:
1889 case CTSF_TABLEPCT_RESPONSES:
1890 case CTSF_SUBTABLEPCT_RESPONSES:
1891 case CTSF_LAYERPCT_RESPONSES:
1892 case CTSF_LAYERROWPCT_RESPONSES:
1893 case CTSF_LAYERCOLPCT_RESPONSES:
1894 case CTSF_ROWPCT_RESPONSES_COUNT:
1895 case CTSF_COLPCT_RESPONSES_COUNT:
1896 case CTSF_TABLEPCT_RESPONSES_COUNT:
1897 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1898 case CTSF_LAYERPCT_RESPONSES_COUNT:
1899 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1900 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1901 case CTSF_ROWPCT_COUNT_RESPONSES:
1902 case CTSF_COLPCT_COUNT_RESPONSES:
1903 case CTSF_TABLEPCT_COUNT_RESPONSES:
1904 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1905 case CTSF_LAYERPCT_COUNT_RESPONSES:
1906 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1907 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1913 ctables_summary_add (union ctables_summary *s,
1914 const struct ctables_summary_spec *ss,
1915 const struct variable *var, const union value *value,
1916 double d_weight, double e_weight)
1918 switch (ss->function)
1923 if (var_is_value_missing (var, value))
1924 s->missing += d_weight;
1926 s->valid += d_weight;
1930 case CTSF_ROWPCT_COUNT:
1931 case CTSF_COLPCT_COUNT:
1932 case CTSF_TABLEPCT_COUNT:
1933 case CTSF_SUBTABLEPCT_COUNT:
1934 case CTSF_LAYERPCT_COUNT:
1935 case CTSF_LAYERROWPCT_COUNT:
1936 case CTSF_LAYERCOLPCT_COUNT:
1937 case CTSF_ROWPCT_VALIDN:
1938 case CTSF_COLPCT_VALIDN:
1939 case CTSF_TABLEPCT_VALIDN:
1940 case CTSF_SUBTABLEPCT_VALIDN:
1941 case CTSF_LAYERPCT_VALIDN:
1942 case CTSF_LAYERROWPCT_VALIDN:
1943 case CTSF_LAYERCOLPCT_VALIDN:
1944 case CTSF_ROWPCT_TOTALN:
1945 case CTSF_COLPCT_TOTALN:
1946 case CTSF_TABLEPCT_TOTALN:
1947 case CTSF_SUBTABLEPCT_TOTALN:
1948 case CTSF_LAYERPCT_TOTALN:
1949 case CTSF_LAYERROWPCT_TOTALN:
1950 case CTSF_LAYERCOLPCT_TOTALN:
1954 if (var_is_value_missing (var, value))
1955 s->missing += e_weight;
1957 s->valid += e_weight;
1963 if (!var_is_value_missing (var, value))
1965 assert (!var_is_alpha (var)); /* XXX? */
1966 if (s->min == SYSMIS || value->f < s->min)
1968 if (s->max == SYSMIS || value->f > s->max)
1978 case CTSF_ROWPCT_SUM:
1979 case CTSF_COLPCT_SUM:
1980 case CTSF_TABLEPCT_SUM:
1981 case CTSF_SUBTABLEPCT_SUM:
1982 case CTSF_LAYERPCT_SUM:
1983 case CTSF_LAYERROWPCT_SUM:
1984 case CTSF_LAYERCOLPCT_SUM:
1985 if (!var_is_value_missing (var, value))
1986 moments1_add (s->moments, value->f, e_weight);
1992 if (var_is_value_missing (var, value))
1994 s->ovalid += e_weight;
1996 struct ccase *c = case_create (casewriter_get_proto (s->writer));
1997 *case_num_rw_idx (c, 0) = value->f;
1998 *case_num_rw_idx (c, 1) = e_weight;
1999 casewriter_write (s->writer, c);
2003 case CTSF_RESPONSES:
2004 case CTSF_ROWPCT_RESPONSES:
2005 case CTSF_COLPCT_RESPONSES:
2006 case CTSF_TABLEPCT_RESPONSES:
2007 case CTSF_SUBTABLEPCT_RESPONSES:
2008 case CTSF_LAYERPCT_RESPONSES:
2009 case CTSF_LAYERROWPCT_RESPONSES:
2010 case CTSF_LAYERCOLPCT_RESPONSES:
2011 case CTSF_ROWPCT_RESPONSES_COUNT:
2012 case CTSF_COLPCT_RESPONSES_COUNT:
2013 case CTSF_TABLEPCT_RESPONSES_COUNT:
2014 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2015 case CTSF_LAYERPCT_RESPONSES_COUNT:
2016 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2017 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2018 case CTSF_ROWPCT_COUNT_RESPONSES:
2019 case CTSF_COLPCT_COUNT_RESPONSES:
2020 case CTSF_TABLEPCT_COUNT_RESPONSES:
2021 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2022 case CTSF_LAYERPCT_COUNT_RESPONSES:
2023 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2024 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2029 static enum ctables_domain_type
2030 ctables_function_domain (enum ctables_summary_function function)
2052 case CTSF_RESPONSES:
2055 case CTSF_COLPCT_COUNT:
2056 case CTSF_COLPCT_COUNT_RESPONSES:
2057 case CTSF_COLPCT_RESPONSES:
2058 case CTSF_COLPCT_RESPONSES_COUNT:
2059 case CTSF_COLPCT_SUM:
2060 case CTSF_COLPCT_TOTALN:
2061 case CTSF_COLPCT_VALIDN:
2064 case CTSF_LAYERCOLPCT_COUNT:
2065 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2066 case CTSF_LAYERCOLPCT_RESPONSES:
2067 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2068 case CTSF_LAYERCOLPCT_SUM:
2069 case CTSF_LAYERCOLPCT_TOTALN:
2070 case CTSF_LAYERCOLPCT_VALIDN:
2071 return CTDT_LAYERCOL;
2073 case CTSF_LAYERPCT_COUNT:
2074 case CTSF_LAYERPCT_COUNT_RESPONSES:
2075 case CTSF_LAYERPCT_RESPONSES:
2076 case CTSF_LAYERPCT_RESPONSES_COUNT:
2077 case CTSF_LAYERPCT_SUM:
2078 case CTSF_LAYERPCT_TOTALN:
2079 case CTSF_LAYERPCT_VALIDN:
2082 case CTSF_LAYERROWPCT_COUNT:
2083 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2084 case CTSF_LAYERROWPCT_RESPONSES:
2085 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2086 case CTSF_LAYERROWPCT_SUM:
2087 case CTSF_LAYERROWPCT_TOTALN:
2088 case CTSF_LAYERROWPCT_VALIDN:
2089 return CTDT_LAYERROW;
2091 case CTSF_ROWPCT_COUNT:
2092 case CTSF_ROWPCT_COUNT_RESPONSES:
2093 case CTSF_ROWPCT_RESPONSES:
2094 case CTSF_ROWPCT_RESPONSES_COUNT:
2095 case CTSF_ROWPCT_SUM:
2096 case CTSF_ROWPCT_TOTALN:
2097 case CTSF_ROWPCT_VALIDN:
2100 case CTSF_SUBTABLEPCT_COUNT:
2101 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2102 case CTSF_SUBTABLEPCT_RESPONSES:
2103 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2104 case CTSF_SUBTABLEPCT_SUM:
2105 case CTSF_SUBTABLEPCT_TOTALN:
2106 case CTSF_SUBTABLEPCT_VALIDN:
2107 return CTDT_SUBTABLE;
2109 case CTSF_TABLEPCT_COUNT:
2110 case CTSF_TABLEPCT_COUNT_RESPONSES:
2111 case CTSF_TABLEPCT_RESPONSES:
2112 case CTSF_TABLEPCT_RESPONSES_COUNT:
2113 case CTSF_TABLEPCT_SUM:
2114 case CTSF_TABLEPCT_TOTALN:
2115 case CTSF_TABLEPCT_VALIDN:
2123 ctables_summary_value (const struct ctables_cell *cell,
2124 union ctables_summary *s,
2125 const struct ctables_summary_spec *ss)
2127 switch (ss->function)
2133 case CTSF_ROWPCT_COUNT:
2134 case CTSF_COLPCT_COUNT:
2135 case CTSF_TABLEPCT_COUNT:
2136 case CTSF_SUBTABLEPCT_COUNT:
2137 case CTSF_LAYERPCT_COUNT:
2138 case CTSF_LAYERROWPCT_COUNT:
2139 case CTSF_LAYERCOLPCT_COUNT:
2141 enum ctables_domain_type d = ctables_function_domain (ss->function);
2142 return (cell->domains[d]->e_valid
2143 ? s->valid / cell->domains[d]->e_valid * 100
2147 case CTSF_ROWPCT_VALIDN:
2148 case CTSF_COLPCT_VALIDN:
2149 case CTSF_TABLEPCT_VALIDN:
2150 case CTSF_SUBTABLEPCT_VALIDN:
2151 case CTSF_LAYERPCT_VALIDN:
2152 case CTSF_LAYERROWPCT_VALIDN:
2153 case CTSF_LAYERCOLPCT_VALIDN:
2154 case CTSF_ROWPCT_TOTALN:
2155 case CTSF_COLPCT_TOTALN:
2156 case CTSF_TABLEPCT_TOTALN:
2157 case CTSF_SUBTABLEPCT_TOTALN:
2158 case CTSF_LAYERPCT_TOTALN:
2159 case CTSF_LAYERROWPCT_TOTALN:
2160 case CTSF_LAYERCOLPCT_TOTALN:
2168 return s->valid + s->missing;
2181 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
2186 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
2192 double weight, variance;
2193 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
2194 return calc_semean (variance, weight);
2200 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2201 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
2206 double weight, mean;
2207 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
2208 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
2214 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2218 case CTSF_ROWPCT_SUM:
2219 case CTSF_COLPCT_SUM:
2220 case CTSF_TABLEPCT_SUM:
2221 case CTSF_SUBTABLEPCT_SUM:
2222 case CTSF_LAYERPCT_SUM:
2223 case CTSF_LAYERROWPCT_SUM:
2224 case CTSF_LAYERCOLPCT_SUM:
2231 struct casereader *reader = casewriter_make_reader (s->writer);
2234 struct percentile *ptile = percentile_create (
2235 ss->function == CTSF_PTILE ? ss->percentile : 0.5, s->ovalid);
2236 struct order_stats *os = &ptile->parent;
2237 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2238 s->ovalue = percentile_calculate (ptile, PC_HAVERAGE);
2239 statistic_destroy (&ptile->parent.parent);
2246 struct casereader *reader = casewriter_make_reader (s->writer);
2249 struct mode *mode = mode_create ();
2250 struct order_stats *os = &mode->parent;
2251 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2252 s->ovalue = mode->mode;
2253 statistic_destroy (&mode->parent.parent);
2257 case CTSF_RESPONSES:
2258 case CTSF_ROWPCT_RESPONSES:
2259 case CTSF_COLPCT_RESPONSES:
2260 case CTSF_TABLEPCT_RESPONSES:
2261 case CTSF_SUBTABLEPCT_RESPONSES:
2262 case CTSF_LAYERPCT_RESPONSES:
2263 case CTSF_LAYERROWPCT_RESPONSES:
2264 case CTSF_LAYERCOLPCT_RESPONSES:
2265 case CTSF_ROWPCT_RESPONSES_COUNT:
2266 case CTSF_COLPCT_RESPONSES_COUNT:
2267 case CTSF_TABLEPCT_RESPONSES_COUNT:
2268 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2269 case CTSF_LAYERPCT_RESPONSES_COUNT:
2270 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2271 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2272 case CTSF_ROWPCT_COUNT_RESPONSES:
2273 case CTSF_COLPCT_COUNT_RESPONSES:
2274 case CTSF_TABLEPCT_COUNT_RESPONSES:
2275 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2276 case CTSF_LAYERPCT_COUNT_RESPONSES:
2277 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2278 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2285 struct ctables_cell_sort_aux
2287 const struct ctables_nest *nest;
2288 enum pivot_axis_type a;
2292 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2294 const struct ctables_cell_sort_aux *aux = aux_;
2295 struct ctables_cell *const *ap = a_;
2296 struct ctables_cell *const *bp = b_;
2297 const struct ctables_cell *a = *ap;
2298 const struct ctables_cell *b = *bp;
2300 const struct ctables_nest *nest = aux->nest;
2301 for (size_t i = 0; i < nest->n; i++)
2302 if (i != nest->scale_idx)
2304 const struct variable *var = nest->vars[i];
2305 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2306 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2307 if (a_cv->category != b_cv->category)
2308 return a_cv->category > b_cv->category ? 1 : -1;
2310 const union value *a_val = &a_cv->value;
2311 const union value *b_val = &b_cv->value;
2312 switch (a_cv->category->type)
2319 /* Must be equal. */
2326 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2334 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2336 return a_cv->category->sort_ascending ? cmp : -cmp;
2342 const char *a_label = var_lookup_value_label (var, a_val);
2343 const char *b_label = var_lookup_value_label (var, b_val);
2345 ? (b_label ? strcmp (a_label, b_label) : 1)
2346 : (b_label ? -1 : value_compare_3way (
2347 a_val, b_val, var_get_width (var))));
2349 return a_cv->category->sort_ascending ? cmp : -cmp;
2363 For each ctables_table:
2364 For each combination of row vars:
2365 For each combination of column vars:
2366 For each combination of layer vars:
2368 Make a table of row values:
2369 Sort entries by row values
2370 Assign a 0-based index to each actual value
2371 Construct a dimension
2372 Make a table of column values
2373 Make a table of layer values
2375 Fill the table entry using the indexes from before.
2378 static struct ctables_domain *
2379 ctables_domain_insert (struct ctables_section *s, struct ctables_cell *cell,
2380 enum ctables_domain_type domain)
2383 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2385 const struct ctables_nest *nest = s->nests[a];
2386 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2388 size_t v_idx = nest->domains[domain][i];
2389 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2390 var_get_width (nest->vars[v_idx]), hash);
2394 struct ctables_domain *d;
2395 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &s->domains[domain])
2397 const struct ctables_cell *df = d->example;
2398 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2400 const struct ctables_nest *nest = s->nests[a];
2401 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2403 size_t v_idx = nest->domains[domain][i];
2404 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2405 &cell->axes[a].cvs[v_idx].value,
2406 var_get_width (nest->vars[v_idx])))
2415 d = xmalloc (sizeof *d);
2416 *d = (struct ctables_domain) { .example = cell };
2417 hmap_insert (&s->domains[domain], &d->node, hash);
2421 static const struct ctables_category *
2422 ctables_categories_match (const struct ctables_categories *c,
2423 const union value *v, const struct variable *var)
2425 const struct ctables_category *othernm = NULL;
2426 for (size_t i = c->n_cats; i-- > 0; )
2428 const struct ctables_category *cat = &c->cats[i];
2432 if (cat->number == v->f)
2440 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2441 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2446 if (var_is_value_missing (var, v))
2463 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2468 return var_is_value_missing (var, v) ? NULL : othernm;
2471 static const struct ctables_category *
2472 ctables_categories_total (const struct ctables_categories *c)
2474 const struct ctables_category *first = &c->cats[0];
2475 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2476 return (first->type == CCT_TOTAL ? first
2477 : last->type == CCT_TOTAL ? last
2481 static struct ctables_cell *
2482 ctables_cell_insert__ (struct ctables_section *s, const struct ccase *c,
2483 const struct ctables_category *cats[PIVOT_N_AXES][10])
2485 const struct ctables_nest *ss = s->nests[s->table->summary_axis];
2488 enum ctables_summary_variant sv = CSV_CELL;
2489 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2491 const struct ctables_nest *nest = s->nests[a];
2492 for (size_t i = 0; i < nest->n; i++)
2493 if (i != nest->scale_idx)
2495 hash = hash_pointer (cats[a][i], hash);
2496 if (cats[a][i]->type != CCT_TOTAL
2497 && cats[a][i]->type != CCT_SUBTOTAL
2498 && cats[a][i]->type != CCT_HSUBTOTAL)
2499 hash = value_hash (case_data (c, nest->vars[i]),
2500 var_get_width (nest->vars[i]), hash);
2506 struct ctables_cell *cell;
2507 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &s->cells)
2509 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2511 const struct ctables_nest *nest = s->nests[a];
2512 for (size_t i = 0; i < nest->n; i++)
2513 if (i != nest->scale_idx
2514 && (cats[a][i] != cell->axes[a].cvs[i].category
2515 || (cats[a][i]->type != CCT_TOTAL
2516 && cats[a][i]->type != CCT_SUBTOTAL
2517 && cats[a][i]->type != CCT_HSUBTOTAL
2518 && !value_equal (case_data (c, nest->vars[i]),
2519 &cell->axes[a].cvs[i].value,
2520 var_get_width (nest->vars[i])))))
2529 cell = xmalloc (sizeof *cell);
2532 cell->contributes_to_domains = true;
2533 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2535 const struct ctables_nest *nest = s->nests[a];
2536 cell->axes[a].cvs = (nest->n
2537 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2539 for (size_t i = 0; i < nest->n; i++)
2541 const struct ctables_category *cat = cats[a][i];
2543 if (i != nest->scale_idx)
2545 const struct ctables_category *subtotal = cat->subtotal;
2546 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2549 if (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
2550 cell->contributes_to_domains = false;
2553 cell->axes[a].cvs[i].category = cat;
2554 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2555 var_get_width (nest->vars[i]));
2559 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2560 cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
2561 for (size_t i = 0; i < specs->n; i++)
2562 ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
2563 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2564 cell->domains[dt] = ctables_domain_insert (s, cell, dt);
2565 hmap_insert (&s->cells, &cell->node, hash);
2570 ctables_cell_add__ (struct ctables_section *s, const struct ccase *c,
2571 const struct ctables_category *cats[PIVOT_N_AXES][10],
2572 double d_weight, double e_weight)
2574 struct ctables_cell *cell = ctables_cell_insert__ (s, c, cats);
2575 const struct ctables_nest *ss = s->nests[s->table->summary_axis];
2577 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2578 for (size_t i = 0; i < specs->n; i++)
2579 ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
2580 case_data (c, specs->var), d_weight, e_weight);
2581 if (cell->contributes_to_domains)
2583 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2585 cell->domains[dt]->d_valid += d_weight;
2586 cell->domains[dt]->e_valid += e_weight;
2592 recurse_totals (struct ctables_section *s, const struct ccase *c,
2593 const struct ctables_category *cats[PIVOT_N_AXES][10],
2594 double d_weight, double e_weight,
2595 enum pivot_axis_type start_axis, size_t start_nest)
2597 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2599 const struct ctables_nest *nest = s->nests[a];
2600 for (size_t i = start_nest; i < nest->n; i++)
2602 if (i == nest->scale_idx)
2605 const struct variable *var = nest->vars[i];
2607 const struct ctables_category *total = ctables_categories_total (
2608 s->table->categories[var_get_dict_index (var)]);
2611 const struct ctables_category *save = cats[a][i];
2613 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2614 recurse_totals (s, c, cats, d_weight, e_weight, a, i + 1);
2623 recurse_subtotals (struct ctables_section *s, const struct ccase *c,
2624 const struct ctables_category *cats[PIVOT_N_AXES][10],
2625 double d_weight, double e_weight,
2626 enum pivot_axis_type start_axis, size_t start_nest)
2628 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2630 const struct ctables_nest *nest = s->nests[a];
2631 for (size_t i = start_nest; i < nest->n; i++)
2633 if (i == nest->scale_idx)
2636 const struct ctables_category *save = cats[a][i];
2639 cats[a][i] = save->subtotal;
2640 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2641 recurse_subtotals (s, c, cats, d_weight, e_weight, a, i + 1);
2650 ctables_add_occurrence (const struct variable *var,
2651 const union value *value,
2652 struct hmap *occurrences)
2654 int width = var_get_width (var);
2655 unsigned int hash = value_hash (value, width, 0);
2657 struct ctables_section_value *sv;
2658 HMAP_FOR_EACH_WITH_HASH (sv, struct ctables_section_value, node, hash,
2660 if (value_equal (value, &sv->value, width))
2663 sv = xmalloc (sizeof *sv);
2664 value_clone (&sv->value, value, width);
2665 hmap_insert (occurrences, &sv->node, hash);
2669 ctables_cell_insert (struct ctables_section *s,
2670 const struct ccase *c,
2671 double d_weight, double e_weight)
2673 const struct ctables_category *cats[PIVOT_N_AXES][10]; /* XXX */
2674 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2676 const struct ctables_nest *nest = s->nests[a];
2677 for (size_t i = 0; i < nest->n; i++)
2679 if (i == nest->scale_idx)
2682 const struct variable *var = nest->vars[i];
2683 const union value *value = case_data (c, var);
2685 if (var_is_numeric (var) && value->f == SYSMIS)
2688 cats[a][i] = ctables_categories_match (
2689 s->table->categories[var_get_dict_index (var)], value, var);
2695 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2697 const struct ctables_nest *nest = s->nests[a];
2698 for (size_t i = 0; i < nest->n; i++)
2699 if (i != nest->scale_idx)
2701 const struct variable *var = nest->vars[i];
2702 const union value *value = case_data (c, var);
2703 ctables_add_occurrence (var, value, &s->occurrences[a][i]);
2707 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2709 recurse_totals (s, c, cats, d_weight, e_weight, 0, 0);
2710 recurse_subtotals (s, c, cats, d_weight, e_weight, 0, 0);
2715 const struct ctables_summary_spec_set *set;
2720 merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
2722 const struct ctables_summary_spec *as = &a->set->specs[a->ofs];
2723 const struct ctables_summary_spec *bs = &b->set->specs[b->ofs];
2724 if (as->function != bs->function)
2725 return as->function > bs->function ? 1 : -1;
2726 else if (as->percentile != bs->percentile)
2727 return as->percentile < bs->percentile ? 1 : -1;
2728 return strcmp (as->label, bs->label);
2731 static struct pivot_value *
2732 ctables_category_create_label (const struct ctables_category *cat,
2733 const struct variable *var,
2734 const union value *value)
2736 return (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL
2737 ? pivot_value_new_user_text (cat->total_label, SIZE_MAX)
2738 : pivot_value_new_var_value (var, value));
2741 static struct ctables_value *
2742 ctables_value_find__ (struct ctables_table *t, const union value *value,
2743 int width, unsigned int hash)
2745 struct ctables_value *clv;
2746 HMAP_FOR_EACH_WITH_HASH (clv, struct ctables_value, node,
2747 hash, &t->clabels_values_map)
2748 if (value_equal (value, &clv->value, width))
2753 static struct ctables_value *
2754 ctables_value_find (struct ctables_table *t,
2755 const union value *value, int width)
2757 return ctables_value_find__ (t, value, width,
2758 value_hash (value, width, 0));
2762 ctables_table_add_section (struct ctables_table *t, enum pivot_axis_type a,
2763 size_t ix[PIVOT_N_AXES])
2765 if (a < PIVOT_N_AXES)
2767 size_t limit = MAX (t->stacks[a].n, 1);
2768 for (ix[a] = 0; ix[a] < limit; ix[a]++)
2769 ctables_table_add_section (t, a + 1, ix);
2773 struct ctables_section *s = &t->sections[t->n_sections++];
2774 *s = (struct ctables_section) {
2776 .cells = HMAP_INITIALIZER (s->cells),
2778 for (a = 0; a < PIVOT_N_AXES; a++)
2781 struct ctables_nest *nest = &t->stacks[a].nests[ix[a]];
2783 s->occurrences[a] = xnmalloc (nest->n, sizeof *s->occurrences[a]);
2784 for (size_t i = 0; i < nest->n; i++)
2785 hmap_init (&s->occurrences[a][i]);
2787 for (size_t i = 0; i < N_CTDTS; i++)
2788 hmap_init (&s->domains[i]);
2793 ctables_table_output (struct ctables *ct, struct ctables_table *t)
2795 struct pivot_table *pt = pivot_table_create__ (
2797 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2798 : pivot_value_new_text (N_("Custom Tables"))),
2801 pivot_table_set_caption (
2802 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2804 pivot_table_set_caption (
2805 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2807 bool summary_dimension = (t->summary_axis != t->slabels_axis
2808 || (!t->slabels_visible
2809 && t->summary_specs.n > 1));
2810 if (summary_dimension)
2812 struct pivot_dimension *d = pivot_dimension_create (
2813 pt, t->slabels_axis, N_("Statistics"));
2814 const struct ctables_summary_spec_set *specs = &t->summary_specs;
2815 if (!t->slabels_visible)
2816 d->hide_all_labels = true;
2817 for (size_t i = 0; i < specs->n; i++)
2818 pivot_category_create_leaf (
2819 d->root, pivot_value_new_text (specs->specs[i].label));
2822 bool categories_dimension = t->clabels_example != NULL;
2823 if (categories_dimension)
2825 struct pivot_dimension *d = pivot_dimension_create (
2826 pt, t->label_axis[t->clabels_from_axis],
2827 t->clabels_from_axis == PIVOT_AXIS_ROW
2828 ? N_("Row Categories")
2829 : N_("Column Categories"));
2830 const struct variable *var = t->clabels_example;
2831 const struct ctables_categories *c = t->categories[var_get_dict_index (var)];
2832 for (size_t i = 0; i < t->n_clabels_values; i++)
2834 const struct ctables_value *value = t->clabels_values[i];
2835 const struct ctables_category *cat = ctables_categories_match (c, &value->value, var);
2836 assert (cat != NULL);
2837 pivot_category_create_leaf (d->root, ctables_category_create_label (
2838 cat, t->clabels_example, &value->value));
2842 pivot_table_set_look (pt, ct->look);
2843 struct pivot_dimension *d[PIVOT_N_AXES];
2844 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2846 static const char *names[] = {
2847 [PIVOT_AXIS_ROW] = N_("Rows"),
2848 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2849 [PIVOT_AXIS_LAYER] = N_("Layers"),
2851 d[a] = (t->axes[a] || a == t->summary_axis
2852 ? pivot_dimension_create (pt, a, names[a])
2857 assert (t->axes[a]);
2859 for (size_t i = 0; i < t->stacks[a].n; i++)
2861 struct ctables_nest *nest = &t->stacks[a].nests[i];
2862 struct ctables_section **sections = xnmalloc (t->n_sections,
2864 size_t n_sections = 0;
2866 size_t n_total_cells = 0;
2867 size_t max_depth = 0;
2868 for (size_t j = 0; j < t->n_sections; j++)
2869 if (t->sections[j].nests[a] == nest)
2871 struct ctables_section *s = &t->sections[j];
2872 sections[n_sections++] = s;
2873 n_total_cells += s->cells.count;
2875 size_t depth = s->nests[a]->n;
2876 max_depth = MAX (depth, max_depth);
2879 struct ctables_cell **sorted = xnmalloc (n_total_cells,
2881 size_t n_sorted = 0;
2883 for (size_t j = 0; j < n_sections; j++)
2885 struct ctables_section *s = sections[j];
2887 struct ctables_cell *cell;
2888 HMAP_FOR_EACH (cell, struct ctables_cell, node, &s->cells)
2890 sorted[n_sorted++] = cell;
2891 assert (n_sorted <= n_total_cells);
2894 struct ctables_cell_sort_aux aux = { .nest = nest, .a = a };
2895 sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
2897 struct ctables_level
2899 enum ctables_level_type
2901 CTL_VAR, /* Variable label for nest->vars[var_idx]. */
2902 CTL_CATEGORY, /* Category for nest->vars[var_idx]. */
2903 CTL_SUMMARY, /* Summary functions. */
2909 struct ctables_level *levels = xnmalloc (1 + 2 * max_depth, sizeof *levels);
2910 size_t n_levels = 0;
2911 for (size_t k = 0; k < nest->n; k++)
2913 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k])];
2914 if (vlabel != CTVL_NONE)
2916 levels[n_levels++] = (struct ctables_level) {
2922 if (nest->scale_idx != k
2923 && (k != nest->n - 1 || t->label_axis[a] == a))
2925 levels[n_levels++] = (struct ctables_level) {
2926 .type = CTL_CATEGORY,
2932 if (!summary_dimension && a == t->slabels_axis)
2934 levels[n_levels++] = (struct ctables_level) {
2935 .type = CTL_SUMMARY,
2936 .var_idx = SIZE_MAX,
2940 /* Pivot categories:
2942 - variable label for nest->vars[0], if vlabel != CTVL_NONE
2943 - category for nest->vars[0], if nest->scale_idx != 0
2944 - variable label for nest->vars[1], if vlabel != CTVL_NONE
2945 - category for nest->vars[1], if nest->scale_idx != 1
2947 - variable label for nest->vars[n - 1], if vlabel != CTVL_NONE
2948 - category for nest->vars[n - 1], if t->label_axis[a] == a && nest->scale_idx != n - 1.
2949 - summary function, if 'a == t->slabels_axis && a ==
2952 Additional dimensions:
2954 - If 'a == t->slabels_axis && a != t->summary_axis', add a summary
2956 - If 't->label_axis[b] == a' for some 'b != a', add a category
2961 struct pivot_category **groups = xnmalloc (1 + 2 * max_depth, sizeof *groups);
2963 for (size_t j = 0; j < n_sorted; j++)
2965 struct ctables_cell *cell = sorted[j];
2966 struct ctables_cell *prev = j > 0 ? sorted[j - 1] : NULL;
2968 size_t n_common = 0;
2971 for (; n_common < n_levels; n_common++)
2973 const struct ctables_level *level = &levels[n_common];
2974 if (level->type == CTL_CATEGORY)
2976 size_t var_idx = level->var_idx;
2977 const struct ctables_category *c = cell->axes[a].cvs[var_idx].category;
2978 if (prev->axes[a].cvs[var_idx].category != c)
2980 else if (c->type != CCT_SUBTOTAL
2981 && c->type != CCT_HSUBTOTAL
2982 && c->type != CCT_TOTAL
2983 && !value_equal (&prev->axes[a].cvs[var_idx].value,
2984 &cell->axes[a].cvs[var_idx].value,
2985 var_get_type (nest->vars[var_idx])))
2991 for (size_t k = n_common; k < n_levels; k++)
2993 const struct ctables_level *level = &levels[k];
2994 struct pivot_category *parent = k ? groups[k - 1] : d[a]->root;
2995 if (level->type == CTL_SUMMARY)
2997 assert (k == n_levels - 1);
2999 const struct ctables_summary_spec_set *specs = &t->summary_specs;
3000 for (size_t m = 0; m < specs->n; m++)
3002 int leaf = pivot_category_create_leaf (
3003 parent, pivot_value_new_text (specs->specs[m].label));
3010 const struct variable *var = nest->vars[level->var_idx];
3011 struct pivot_value *label;
3012 if (level->type == CTL_VAR)
3013 label = pivot_value_new_variable (var);
3014 else if (level->type == CTL_CATEGORY)
3016 const struct ctables_cell_value *cv = &cell->axes[a].cvs[level->var_idx];
3017 label = ctables_category_create_label (cv->category,
3023 if (k == n_levels - 1)
3024 prev_leaf = pivot_category_create_leaf (parent, label);
3026 groups[k] = pivot_category_create_group__ (parent, label);
3030 cell->axes[a].leaf = prev_leaf;
3037 for (size_t i = 0; i < t->n_sections; i++)
3039 struct ctables_section *s = &t->sections[i];
3041 struct ctables_cell *cell;
3042 HMAP_FOR_EACH (cell, struct ctables_cell, node, &s->cells)
3047 const struct ctables_nest *specs_nest = s->nests[t->summary_axis];
3048 const struct ctables_summary_spec_set *specs = &specs_nest->specs[cell->sv];
3049 for (size_t j = 0; j < specs->n; j++)
3052 size_t n_dindexes = 0;
3054 if (summary_dimension)
3055 dindexes[n_dindexes++] = specs->specs[j].axis_idx;
3057 if (categories_dimension)
3059 const struct ctables_nest *clabels_nest = s->nests[t->clabels_from_axis];
3060 const struct variable *var = clabels_nest->vars[clabels_nest->n - 1];
3061 const union value *value = &cell->axes[t->clabels_from_axis].cvs[clabels_nest->n - 1].value;
3062 const struct ctables_value *ctv = ctables_value_find (t, value, var_get_width (var));
3063 assert (ctv != NULL);
3064 dindexes[n_dindexes++] = ctv->leaf;
3067 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3070 int leaf = cell->axes[a].leaf;
3071 if (a == t->summary_axis && !summary_dimension)
3073 dindexes[n_dindexes++] = leaf;
3076 double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
3077 struct pivot_value *value = pivot_value_new_number (d);
3078 value->numeric.format = specs->specs[j].format;
3079 pivot_table_put (pt, dindexes, n_dindexes, value);
3084 pivot_table_submit (pt);
3088 ctables_check_label_position (struct ctables_table *t, enum pivot_axis_type a)
3090 enum pivot_axis_type label_pos = t->label_axis[a];
3094 t->clabels_from_axis = a;
3096 const char *subcommand_name = a == PIVOT_AXIS_ROW ? "ROWLABELS" : "COLLABELS";
3097 const char *pos_name = label_pos == PIVOT_AXIS_LAYER ? "LAYER" : "OPPOSITE";
3099 const struct ctables_stack *stack = &t->stacks[a];
3103 const struct ctables_nest *n0 = &stack->nests[0];
3105 const struct variable *v0 = n0->vars[n0->n - 1];
3106 struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
3107 t->clabels_example = v0;
3109 for (size_t i = 0; i < c0->n_cats; i++)
3110 if (c0->cats[i].type == CCT_FUNCTION)
3112 msg (SE, _("%s=%s is not allowed with sorting based "
3113 "on a summary function."),
3114 subcommand_name, pos_name);
3117 if (n0->n - 1 == n0->scale_idx)
3119 msg (SE, _("%s=%s requires the variables to be moved to be categorical, "
3120 "but %s is a scale variable."),
3121 subcommand_name, pos_name, var_get_name (v0));
3125 for (size_t i = 1; i < stack->n; i++)
3127 const struct ctables_nest *ni = &stack->nests[i];
3129 const struct variable *vi = ni->vars[ni->n - 1];
3130 struct ctables_categories *ci = t->categories[var_get_dict_index (vi)];
3132 if (ni->n - 1 == ni->scale_idx)
3134 msg (SE, _("%s=%s requires the variables to be moved to be "
3135 "categorical, but %s is a scale variable."),
3136 subcommand_name, pos_name, var_get_name (vi));
3139 if (var_get_width (v0) != var_get_width (vi))
3141 msg (SE, _("%s=%s requires the variables to be "
3142 "moved to have the same width, but %s has "
3143 "width %d and %s has width %d."),
3144 subcommand_name, pos_name,
3145 var_get_name (v0), var_get_width (v0),
3146 var_get_name (vi), var_get_width (vi));
3149 if (!val_labs_equal (var_get_value_labels (v0),
3150 var_get_value_labels (vi)))
3152 msg (SE, _("%s=%s requires the variables to be "
3153 "moved to have the same value labels, but %s "
3154 "and %s have different value labels."),
3155 subcommand_name, pos_name,
3156 var_get_name (v0), var_get_name (vi));
3159 if (!ctables_categories_equal (c0, ci))
3161 msg (SE, _("%s=%s requires the variables to be "
3162 "moved to have the same category "
3163 "specifications, but %s and %s have different "
3164 "category specifications."),
3165 subcommand_name, pos_name,
3166 var_get_name (v0), var_get_name (vi));
3175 ctables_prepare_table (struct ctables_table *t)
3177 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3180 t->stacks[a] = enumerate_fts (a, t->axes[a]);
3182 for (size_t j = 0; j < t->stacks[a].n; j++)
3184 struct ctables_nest *nest = &t->stacks[a].nests[j];
3185 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
3187 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
3188 nest->n_domains[dt] = 0;
3190 for (size_t k = 0; k < nest->n; k++)
3192 if (k == nest->scale_idx)
3201 if (a != PIVOT_AXIS_LAYER)
3208 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
3209 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
3210 : a == PIVOT_AXIS_ROW)
3212 if (k == nest->n - 1
3213 || (nest->scale_idx == nest->n - 1
3214 && k == nest->n - 2))
3220 if (a == PIVOT_AXIS_COLUMN)
3225 if (a == PIVOT_AXIS_ROW)
3230 nest->domains[dt][nest->n_domains[dt]++] = k;
3237 struct ctables_nest *nest = xmalloc (sizeof *nest);
3238 *nest = (struct ctables_nest) { .n = 0 };
3239 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
3242 struct ctables_stack *stack = &t->stacks[t->summary_axis];
3243 for (size_t i = 0; i < stack->n; i++)
3245 struct ctables_nest *nest = &stack->nests[i];
3246 if (!nest->specs[CSV_CELL].n)
3248 struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
3249 specs->specs = xmalloc (sizeof *specs->specs);
3252 enum ctables_summary_function function
3253 = specs->var ? CTSF_MEAN : CTSF_COUNT;
3254 struct ctables_var var = { .is_mrset = false, .var = specs->var };
3256 *specs->specs = (struct ctables_summary_spec) {
3257 .function = function,
3258 .format = ctables_summary_default_format (function, &var),
3259 .label = ctables_summary_default_label (function, 0),
3262 specs->var = nest->vars[0];
3264 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3265 &nest->specs[CSV_CELL]);
3267 else if (!nest->specs[CSV_TOTAL].n)
3268 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3269 &nest->specs[CSV_CELL]);
3272 struct ctables_summary_spec_set *merged = &t->summary_specs;
3273 struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
3275 for (size_t j = 0; j < stack->n; j++)
3277 const struct ctables_nest *nest = &stack->nests[j];
3279 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3280 items[n_left++] = (struct merge_item) { .set = &nest->specs[sv] };
3285 struct merge_item min = items[0];
3286 for (size_t j = 1; j < n_left; j++)
3287 if (merge_item_compare_3way (&items[j], &min) < 0)
3290 if (merged->n >= merged->allocated)
3291 merged->specs = x2nrealloc (merged->specs, &merged->allocated,
3292 sizeof *merged->specs);
3293 merged->specs[merged->n++] = min.set->specs[min.ofs];
3295 for (size_t j = 0; j < n_left; )
3297 if (merge_item_compare_3way (&items[j], &min) == 0)
3299 struct merge_item *item = &items[j];
3300 item->set->specs[item->ofs].axis_idx = merged->n - 1;
3301 if (++item->ofs >= item->set->n)
3303 items[j] = items[--n_left];
3312 for (size_t j = 0; j < merged->n; j++)
3313 printf ("%s\n", ctables_summary_function_name (merged->specs[j].function));
3315 for (size_t j = 0; j < stack->n; j++)
3317 const struct ctables_nest *nest = &stack->nests[j];
3318 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3320 const struct ctables_summary_spec_set *specs = &nest->specs[sv];
3321 for (size_t k = 0; k < specs->n; k++)
3322 printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
3323 specs->specs[k].axis_idx);
3329 return (ctables_check_label_position (t, PIVOT_AXIS_ROW)
3330 && ctables_check_label_position (t, PIVOT_AXIS_COLUMN));
3334 ctables_insert_clabels_values (struct ctables_table *t, const struct ccase *c,
3335 enum pivot_axis_type a)
3337 struct ctables_stack *stack = &t->stacks[a];
3338 for (size_t i = 0; i < stack->n; i++)
3340 const struct ctables_nest *nest = &stack->nests[i];
3341 const struct variable *var = nest->vars[nest->n - 1];
3342 int width = var_get_width (var);
3343 const union value *value = case_data (c, var);
3345 if (var_is_numeric (var) && value->f == SYSMIS)
3348 if (!ctables_categories_match (t->categories [var_get_dict_index (var)],
3352 unsigned int hash = value_hash (value, width, 0);
3354 struct ctables_value *clv = ctables_value_find__ (t, value, width, hash);
3357 clv = xmalloc (sizeof *clv);
3358 value_clone (&clv->value, value, width);
3359 hmap_insert (&t->clabels_values_map, &clv->node, hash);
3365 compare_clabels_values_3way (const void *a_, const void *b_, const void *width_)
3367 const struct ctables_value *const *ap = a_;
3368 const struct ctables_value *const *bp = b_;
3369 const struct ctables_value *a = *ap;
3370 const struct ctables_value *b = *bp;
3371 const int *width = width_;
3372 return value_compare_3way (&a->value, &b->value, *width);
3376 ctables_sort_clabels_values (struct ctables_table *t)
3378 int width = var_get_width (t->clabels_example);
3380 size_t n = hmap_count (&t->clabels_values_map);
3381 t->clabels_values = xnmalloc (n, sizeof *t->clabels_values);
3383 struct ctables_value *clv;
3385 HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
3386 t->clabels_values[i++] = clv;
3387 t->n_clabels_values = n;
3390 sort (t->clabels_values, n, sizeof *t->clabels_values,
3391 compare_clabels_values_3way, &width);
3393 for (size_t i = 0; i < n; i++)
3394 t->clabels_values[i]->leaf = i;
3398 ctables_add_category_occurrences (const struct variable *var,
3399 struct hmap *occurrences,
3400 const struct ctables_categories *cats)
3402 const struct val_labs *val_labs = var_get_value_labels (var);
3404 for (size_t i = 0; i < cats->n_cats; i++)
3406 const struct ctables_category *c = &cats->cats[i];
3410 ctables_add_occurrence (var, &(const union value) { .f = c->number },
3418 assert (var_is_numeric (var));
3419 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3420 vl = val_labs_next (val_labs, vl))
3421 if (vl->value.f >= c->range[0] && vl->value.f <= c->range[1])
3422 ctables_add_occurrence (var, &vl->value, occurrences);
3426 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3427 vl = val_labs_next (val_labs, vl))
3428 if (var_is_value_missing (var, &vl->value))
3429 ctables_add_occurrence (var, &vl->value, occurrences);
3433 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3434 vl = val_labs_next (val_labs, vl))
3435 ctables_add_occurrence (var, &vl->value, occurrences);
3446 for (const struct val_lab *vl = val_labs_first (val_labs); vl;
3447 vl = val_labs_next (val_labs, vl))
3448 if (c->include_missing || !var_is_value_missing (var, &vl->value))
3449 ctables_add_occurrence (var, &vl->value, occurrences);
3456 ctables_section_recurse_add_empty_categories (
3457 struct ctables_section *s,
3458 const struct ctables_category *cats[PIVOT_N_AXES][10], struct ccase *c,
3459 enum pivot_axis_type a, size_t a_idx)
3461 if (a >= PIVOT_N_AXES)
3465 else if (!s->nests[a] || idx >= s->nests[a]->n)
3473 for (size_t i = 0; i < s->nests[a]->n; i++)
3479 ctables_section_add_empty_categories (struct ctables_section *s)
3481 bool show_empty = false;
3482 for (size_t a = 0; a < PIVOT_N_AXES; a++)
3484 for (size_t k = 0; k < s->nests[a]->n; k++)
3485 if (k != s->nests[a]->scale_idx)
3487 const struct variable *var = s->nests[a]->vars[k];
3488 const struct ctables_categories *cats = s->table->categories[
3489 var_get_dict_index (var)];
3490 if (cats->show_empty)
3493 ctables_add_category_occurrences (var, &s->occurrences[a][k], cats);
3499 const struct ctables_category *cats[PIVOT_N_AXES][10]; /* XXX */
3500 struct ccase *c = case_create (dict_get_proto (s->table->ctables->dict));
3501 ctables_section_recurse_add_empty_categories (s, c, cats, a, 0);
3505 ctables_execute (struct dataset *ds, struct ctables *ct)
3507 for (size_t i = 0; i < ct->n_tables; i++)
3509 struct ctables_table *t = ct->tables[i];
3510 t->sections = xnmalloc (MAX (1, t->stacks[PIVOT_AXIS_ROW].n) *
3511 MAX (1, t->stacks[PIVOT_AXIS_COLUMN].n) *
3512 MAX (1, t->stacks[PIVOT_AXIS_LAYER].n),
3513 sizeof *t->sections);
3514 size_t ix[PIVOT_N_AXES];
3515 ctables_table_add_section (t, 0, ix);
3518 struct casereader *input = proc_open (ds);
3519 bool warn_on_invalid = true;
3520 for (struct ccase *c = casereader_read (input); c;
3521 case_unref (c), c = casereader_read (input))
3523 double d_weight = dict_get_case_weight (dataset_dict (ds), c,
3525 double e_weight = (ct->e_weight
3526 ? var_force_valid_weight (ct->e_weight,
3527 case_num (c, ct->e_weight),
3531 for (size_t i = 0; i < ct->n_tables; i++)
3533 struct ctables_table *t = ct->tables[i];
3535 for (size_t j = 0; j < t->n_sections; j++)
3536 ctables_cell_insert (&t->sections[j], c, d_weight, e_weight);
3538 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3539 if (t->label_axis[a] != a)
3540 ctables_insert_clabels_values (t, c, a);
3543 casereader_destroy (input);
3545 for (size_t i = 0; i < ct->n_tables; i++)
3547 struct ctables_table *t = ct->tables[i];
3549 if (t->clabels_example)
3550 ctables_sort_clabels_values (t);
3552 for (size_t j = 0; j < t->n_sections; j++)
3553 ctables_section_add_empty_categories (&t->sections[j]);
3555 ctables_table_output (ct, ct->tables[i]);
3557 return proc_commit (ds);
3561 cmd_ctables (struct lexer *lexer, struct dataset *ds)
3563 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3564 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
3565 enum settings_value_show tvars = settings_get_show_variables ();
3566 for (size_t i = 0; i < n_vars; i++)
3567 vlabels[i] = (enum ctables_vlabel) tvars;
3569 struct ctables *ct = xmalloc (sizeof *ct);
3570 *ct = (struct ctables) {
3571 .dict = dataset_dict (ds),
3572 .look = pivot_table_look_unshare (pivot_table_look_ref (
3573 pivot_table_look_get_default ())),
3575 .hide_threshold = 5,
3577 ct->look->omit_empty = false;
3579 if (!lex_force_match (lexer, T_SLASH))
3582 while (!lex_match_id (lexer, "TABLE"))
3584 if (lex_match_id (lexer, "FORMAT"))
3586 double widths[2] = { SYSMIS, SYSMIS };
3587 double units_per_inch = 72.0;
3589 while (lex_token (lexer) != T_SLASH)
3591 if (lex_match_id (lexer, "MINCOLWIDTH"))
3593 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
3596 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
3598 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
3601 else if (lex_match_id (lexer, "UNITS"))
3603 lex_match (lexer, T_EQUALS);
3604 if (lex_match_id (lexer, "POINTS"))
3605 units_per_inch = 72.0;
3606 else if (lex_match_id (lexer, "INCHES"))
3607 units_per_inch = 1.0;
3608 else if (lex_match_id (lexer, "CM"))
3609 units_per_inch = 2.54;
3612 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
3616 else if (lex_match_id (lexer, "EMPTY"))
3621 lex_match (lexer, T_EQUALS);
3622 if (lex_match_id (lexer, "ZERO"))
3624 /* Nothing to do. */
3626 else if (lex_match_id (lexer, "BLANK"))
3627 ct->zero = xstrdup ("");
3628 else if (lex_force_string (lexer))
3630 ct->zero = ss_xstrdup (lex_tokss (lexer));
3636 else if (lex_match_id (lexer, "MISSING"))
3638 lex_match (lexer, T_EQUALS);
3639 if (!lex_force_string (lexer))
3643 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
3644 ? ss_xstrdup (lex_tokss (lexer))
3650 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
3651 "UNITS", "EMPTY", "MISSING");
3656 if (widths[0] != SYSMIS && widths[1] != SYSMIS
3657 && widths[0] > widths[1])
3659 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
3663 for (size_t i = 0; i < 2; i++)
3664 if (widths[i] != SYSMIS)
3666 int *wr = ct->look->width_ranges[TABLE_HORZ];
3667 wr[i] = widths[i] / units_per_inch * 96.0;
3672 else if (lex_match_id (lexer, "VLABELS"))
3674 if (!lex_force_match_id (lexer, "VARIABLES"))
3676 lex_match (lexer, T_EQUALS);
3678 struct variable **vars;
3680 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
3684 if (!lex_force_match_id (lexer, "DISPLAY"))
3689 lex_match (lexer, T_EQUALS);
3691 enum ctables_vlabel vlabel;
3692 if (lex_match_id (lexer, "DEFAULT"))
3693 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
3694 else if (lex_match_id (lexer, "NAME"))
3696 else if (lex_match_id (lexer, "LABEL"))
3697 vlabel = CTVL_LABEL;
3698 else if (lex_match_id (lexer, "BOTH"))
3700 else if (lex_match_id (lexer, "NONE"))
3704 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
3710 for (size_t i = 0; i < n_vars; i++)
3711 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
3714 else if (lex_match_id (lexer, "MRSETS"))
3716 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
3718 lex_match (lexer, T_EQUALS);
3719 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
3722 else if (lex_match_id (lexer, "SMISSING"))
3724 if (lex_match_id (lexer, "VARIABLE"))
3725 ct->smissing_listwise = false;
3726 else if (lex_match_id (lexer, "LISTWISE"))
3727 ct->smissing_listwise = true;
3730 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
3735 else if (lex_match_id (lexer, "WEIGHT"))
3737 if (!lex_force_match_id (lexer, "VARIABLE"))
3739 lex_match (lexer, T_EQUALS);
3740 ct->e_weight = parse_variable (lexer, dataset_dict (ds));
3744 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
3746 if (!lex_force_match_id (lexer, "COUNT"))
3748 lex_match (lexer, T_EQUALS);
3749 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
3751 ct->hide_threshold = lex_integer (lexer);
3756 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
3757 "SMISSING", "PCOMPUTE", "PPROPERTIES",
3758 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
3762 if (!lex_force_match (lexer, T_SLASH))
3766 size_t allocated_tables = 0;
3769 if (ct->n_tables >= allocated_tables)
3770 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
3771 sizeof *ct->tables);
3773 struct ctables_category *cat = xmalloc (sizeof *cat);
3774 *cat = (struct ctables_category) {
3776 .include_missing = false,
3777 .sort_ascending = true,
3780 struct ctables_categories *c = xmalloc (sizeof *c);
3781 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3782 *c = (struct ctables_categories) {
3788 struct ctables_categories **categories = xnmalloc (n_vars,
3789 sizeof *categories);
3790 for (size_t i = 0; i < n_vars; i++)
3793 struct ctables_table *t = xmalloc (sizeof *t);
3794 *t = (struct ctables_table) {
3796 .slabels_axis = PIVOT_AXIS_COLUMN,
3797 .slabels_visible = true,
3798 .clabels_values_map = HMAP_INITIALIZER (t->clabels_values_map),
3800 [PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW,
3801 [PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN,
3802 [PIVOT_AXIS_LAYER] = PIVOT_AXIS_LAYER,
3804 .clabels_from_axis = PIVOT_AXIS_LAYER,
3805 .categories = categories,
3806 .n_categories = n_vars,
3809 ct->tables[ct->n_tables++] = t;
3811 lex_match (lexer, T_EQUALS);
3812 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
3814 if (lex_match (lexer, T_BY))
3816 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3817 ct, t, PIVOT_AXIS_COLUMN))
3820 if (lex_match (lexer, T_BY))
3822 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3823 ct, t, PIVOT_AXIS_LAYER))
3828 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
3829 && !t->axes[PIVOT_AXIS_LAYER])
3831 lex_error (lexer, _("At least one variable must be specified."));
3835 const struct ctables_axis *scales[PIVOT_N_AXES];
3836 size_t n_scales = 0;
3837 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3839 scales[a] = find_scale (t->axes[a]);
3845 msg (SE, _("Scale variables may appear only on one axis."));
3846 if (scales[PIVOT_AXIS_ROW])
3847 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
3848 _("This scale variable appears on the rows axis."));
3849 if (scales[PIVOT_AXIS_COLUMN])
3850 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
3851 _("This scale variable appears on the columns axis."));
3852 if (scales[PIVOT_AXIS_LAYER])
3853 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
3854 _("This scale variable appears on the layer axis."));
3858 const struct ctables_axis *summaries[PIVOT_N_AXES];
3859 size_t n_summaries = 0;
3860 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3862 summaries[a] = (scales[a]
3864 : find_categorical_summary_spec (t->axes[a]));
3868 if (n_summaries > 1)
3870 msg (SE, _("Summaries may appear only on one axis."));
3871 if (summaries[PIVOT_AXIS_ROW])
3872 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3873 _("This variable on the rows axis has a summary."));
3874 if (summaries[PIVOT_AXIS_COLUMN])
3875 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3876 _("This variable on the columns axis has a summary."));
3877 if (summaries[PIVOT_AXIS_LAYER])
3878 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3879 _("This variable on the layers axis has a summary."));
3882 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3883 if (n_summaries ? summaries[a] : t->axes[a])
3885 t->summary_axis = a;
3889 if (lex_token (lexer) == T_ENDCMD)
3891 if (!ctables_prepare_table (t))
3895 if (!lex_force_match (lexer, T_SLASH))
3898 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3900 if (lex_match_id (lexer, "SLABELS"))
3902 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3904 if (lex_match_id (lexer, "POSITION"))
3906 lex_match (lexer, T_EQUALS);
3907 if (lex_match_id (lexer, "COLUMN"))
3908 t->slabels_axis = PIVOT_AXIS_COLUMN;
3909 else if (lex_match_id (lexer, "ROW"))
3910 t->slabels_axis = PIVOT_AXIS_ROW;
3911 else if (lex_match_id (lexer, "LAYER"))
3912 t->slabels_axis = PIVOT_AXIS_LAYER;
3915 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3919 else if (lex_match_id (lexer, "VISIBLE"))
3921 lex_match (lexer, T_EQUALS);
3922 if (!parse_bool (lexer, &t->slabels_visible))
3927 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3932 else if (lex_match_id (lexer, "CLABELS"))
3934 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3936 if (lex_match_id (lexer, "AUTO"))
3938 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW;
3939 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN;
3941 else if (lex_match_id (lexer, "ROWLABELS"))
3943 lex_match (lexer, T_EQUALS);
3944 if (lex_match_id (lexer, "OPPOSITE"))
3945 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_COLUMN;
3946 else if (lex_match_id (lexer, "LAYER"))
3947 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_LAYER;
3950 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3954 else if (lex_match_id (lexer, "COLLABELS"))
3956 lex_match (lexer, T_EQUALS);
3957 if (lex_match_id (lexer, "OPPOSITE"))
3958 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_ROW;
3959 else if (lex_match_id (lexer, "LAYER"))
3960 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_LAYER;
3963 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3969 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3975 else if (lex_match_id (lexer, "CRITERIA"))
3977 if (!lex_force_match_id (lexer, "CILEVEL"))
3979 lex_match (lexer, T_EQUALS);
3981 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3983 t->cilevel = lex_number (lexer);
3986 else if (lex_match_id (lexer, "CATEGORIES"))
3988 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3991 else if (lex_match_id (lexer, "TITLES"))
3996 if (lex_match_id (lexer, "CAPTION"))
3997 textp = &t->caption;
3998 else if (lex_match_id (lexer, "CORNER"))
4000 else if (lex_match_id (lexer, "TITLE"))
4004 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
4007 lex_match (lexer, T_EQUALS);
4009 struct string s = DS_EMPTY_INITIALIZER;
4010 while (lex_is_string (lexer))
4012 if (!ds_is_empty (&s))
4013 ds_put_byte (&s, ' ');
4014 ds_put_substring (&s, lex_tokss (lexer));
4018 *textp = ds_steal_cstr (&s);
4020 while (lex_token (lexer) != T_SLASH
4021 && lex_token (lexer) != T_ENDCMD);
4023 else if (lex_match_id (lexer, "SIGTEST"))
4027 t->chisq = xmalloc (sizeof *t->chisq);
4028 *t->chisq = (struct ctables_chisq) {
4030 .include_mrsets = true,
4031 .all_visible = true,
4037 if (lex_match_id (lexer, "TYPE"))
4039 lex_match (lexer, T_EQUALS);
4040 if (!lex_force_match_id (lexer, "CHISQUARE"))
4043 else if (lex_match_id (lexer, "ALPHA"))
4045 lex_match (lexer, T_EQUALS);
4046 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
4048 t->chisq->alpha = lex_number (lexer);
4051 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
4053 lex_match (lexer, T_EQUALS);
4054 if (parse_bool (lexer, &t->chisq->include_mrsets))
4057 else if (lex_match_id (lexer, "CATEGORIES"))
4059 lex_match (lexer, T_EQUALS);
4060 if (lex_match_id (lexer, "ALLVISIBLE"))
4061 t->chisq->all_visible = true;
4062 else if (lex_match_id (lexer, "SUBTOTALS"))
4063 t->chisq->all_visible = false;
4066 lex_error_expecting (lexer,
4067 "ALLVISIBLE", "SUBTOTALS");
4073 lex_error_expecting (lexer, "TYPE", "ALPHA",
4074 "INCLUDEMRSETS", "CATEGORIES");
4078 while (lex_token (lexer) != T_SLASH
4079 && lex_token (lexer) != T_ENDCMD);
4081 else if (lex_match_id (lexer, "COMPARETEST"))
4085 t->pairwise = xmalloc (sizeof *t->pairwise);
4086 *t->pairwise = (struct ctables_pairwise) {
4088 .alpha = { .05, .05 },
4089 .adjust = BONFERRONI,
4090 .include_mrsets = true,
4091 .meansvariance_allcats = true,
4092 .all_visible = true,
4101 if (lex_match_id (lexer, "TYPE"))
4103 lex_match (lexer, T_EQUALS);
4104 if (lex_match_id (lexer, "PROP"))
4105 t->pairwise->type = PROP;
4106 else if (lex_match_id (lexer, "MEAN"))
4107 t->pairwise->type = MEAN;
4110 lex_error_expecting (lexer, "PROP", "MEAN");
4114 else if (lex_match_id (lexer, "ALPHA"))
4116 lex_match (lexer, T_EQUALS);
4118 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
4120 double a0 = lex_number (lexer);
4123 lex_match (lexer, T_COMMA);
4124 if (lex_is_number (lexer))
4126 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
4128 double a1 = lex_number (lexer);
4131 t->pairwise->alpha[0] = MIN (a0, a1);
4132 t->pairwise->alpha[1] = MAX (a0, a1);
4135 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
4137 else if (lex_match_id (lexer, "ADJUST"))
4139 lex_match (lexer, T_EQUALS);
4140 if (lex_match_id (lexer, "BONFERRONI"))
4141 t->pairwise->adjust = BONFERRONI;
4142 else if (lex_match_id (lexer, "BH"))
4143 t->pairwise->adjust = BH;
4144 else if (lex_match_id (lexer, "NONE"))
4145 t->pairwise->adjust = 0;
4148 lex_error_expecting (lexer, "BONFERRONI", "BH",
4153 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
4155 lex_match (lexer, T_EQUALS);
4156 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
4159 else if (lex_match_id (lexer, "MEANSVARIANCE"))
4161 lex_match (lexer, T_EQUALS);
4162 if (lex_match_id (lexer, "ALLCATS"))
4163 t->pairwise->meansvariance_allcats = true;
4164 else if (lex_match_id (lexer, "TESTEDCATS"))
4165 t->pairwise->meansvariance_allcats = false;
4168 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
4172 else if (lex_match_id (lexer, "CATEGORIES"))
4174 lex_match (lexer, T_EQUALS);
4175 if (lex_match_id (lexer, "ALLVISIBLE"))
4176 t->pairwise->all_visible = true;
4177 else if (lex_match_id (lexer, "SUBTOTALS"))
4178 t->pairwise->all_visible = false;
4181 lex_error_expecting (lexer, "ALLVISIBLE",
4186 else if (lex_match_id (lexer, "MERGE"))
4188 lex_match (lexer, T_EQUALS);
4189 if (!parse_bool (lexer, &t->pairwise->merge))
4192 else if (lex_match_id (lexer, "STYLE"))
4194 lex_match (lexer, T_EQUALS);
4195 if (lex_match_id (lexer, "APA"))
4196 t->pairwise->apa_style = true;
4197 else if (lex_match_id (lexer, "SIMPLE"))
4198 t->pairwise->apa_style = false;
4201 lex_error_expecting (lexer, "APA", "SIMPLE");
4205 else if (lex_match_id (lexer, "SHOWSIG"))
4207 lex_match (lexer, T_EQUALS);
4208 if (!parse_bool (lexer, &t->pairwise->show_sig))
4213 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
4214 "INCLUDEMRSETS", "MEANSVARIANCE",
4215 "CATEGORIES", "MERGE", "STYLE",
4220 while (lex_token (lexer) != T_SLASH
4221 && lex_token (lexer) != T_ENDCMD);
4225 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
4226 "CRITERIA", "CATEGORIES", "TITLES",
4227 "SIGTEST", "COMPARETEST");
4231 if (!lex_match (lexer, T_SLASH))
4235 if (t->label_axis[PIVOT_AXIS_ROW] != PIVOT_AXIS_ROW
4236 && t->label_axis[PIVOT_AXIS_COLUMN] != PIVOT_AXIS_COLUMN)
4238 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
4242 if (!ctables_prepare_table (t))
4245 while (lex_token (lexer) != T_ENDCMD);
4247 bool ok = ctables_execute (ds, ct);
4248 ctables_destroy (ct);
4249 return ok ? CMD_SUCCESS : CMD_FAILURE;
4252 ctables_destroy (ct);