1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/casereader.h"
22 #include "data/casewriter.h"
23 #include "data/dataset.h"
24 #include "data/dictionary.h"
25 #include "data/mrset.h"
26 #include "data/subcase.h"
27 #include "data/value-labels.h"
28 #include "language/command.h"
29 #include "language/lexer/format-parser.h"
30 #include "language/lexer/lexer.h"
31 #include "language/lexer/variable-parser.h"
32 #include "libpspp/array.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/hash-functions.h"
35 #include "libpspp/hmap.h"
36 #include "libpspp/message.h"
37 #include "libpspp/string-array.h"
38 #include "math/mode.h"
39 #include "math/moments.h"
40 #include "math/percentiles.h"
41 #include "math/sort.h"
42 #include "output/pivot-table.h"
44 #include "gl/minmax.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) (msgid)
53 CTVL_NONE = SETTINGS_VALUE_SHOW_DEFAULT,
54 CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
55 CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
56 CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
60 - unweighted summaries (U*)
61 - lower confidence limits (*.LCL)
62 - upper confidence limits (*.UCL)
63 - standard error (*.SE)
66 /* All variables. */ \
67 S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL) \
68 S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
69 S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
70 S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
71 S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
72 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
73 S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
74 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
75 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
76 S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
77 S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
78 S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
79 S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
80 S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
81 S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
82 S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
83 S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
84 S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
85 S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
86 S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
87 S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
88 S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
89 S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
91 /* Scale variables, totals, and subtotals. */ \
92 S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE) \
93 S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE) \
94 S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE) \
95 S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE) \
96 S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE) \
97 S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE) \
98 S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE) \
99 S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE) \
100 S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
101 S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
102 S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE) \
103 S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE) \
104 S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
105 S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE) \
106 S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
107 S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
108 S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
109 S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
110 S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
111 S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
112 S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
113 S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
114 S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
116 /* Multiple response sets. */ \
117 S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
118 S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
119 S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
120 S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
121 S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
122 S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
123 S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
124 S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125 S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
126 S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
127 S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
128 S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
129 S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
130 S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
131 S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132 S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
133 S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
134 S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
135 S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
136 S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
137 S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
138 S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
140 enum ctables_summary_function
142 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
148 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
149 N_CTSF_FUNCTIONS = SUMMARIES
153 enum ctables_domain_type
155 /* Within a section, where stacked variables divide one section from
157 CTDT_TABLE, /* All layers of a whole section. */
158 CTDT_LAYER, /* One layer within a section. */
159 CTDT_LAYERROW, /* Row in one layer within a section. */
160 CTDT_LAYERCOL, /* Column in one layer within a section. */
162 /* Within a subtable, where a subtable pairs an innermost row variable with
163 an innermost column variable within a single layer. */
164 CTDT_SUBTABLE, /* Whole subtable. */
165 CTDT_ROW, /* Row within a subtable. */
166 CTDT_COL, /* Column within a subtable. */
170 struct ctables_domain
172 struct hmap_node node;
174 const struct ctables_cell *example;
176 double d_valid; /* Dictionary weight. */
178 double e_valid; /* Effective weight */
182 enum ctables_summary_variant
191 /* In struct ctables's 'cells' hmap. Indexed by all the values in all the
192 axes (except the scalar variable, if any). */
193 struct hmap_node node;
195 /* The domains that contain this cell. */
196 bool contributes_to_domains;
197 struct ctables_domain *domains[N_CTDTS];
200 enum ctables_summary_variant sv;
204 struct ctables_cell_value
206 const struct ctables_category *category;
214 union ctables_summary *summaries;
219 struct pivot_table_look *look;
221 /* If this is NULL, zeros are displayed using the normal print format.
222 Otherwise, this string is displayed. */
225 /* If this is NULL, missing values are displayed using the normal print
226 format. Otherwise, this string is displayed. */
229 /* Indexed by variable dictionary index. */
230 enum ctables_vlabel *vlabels;
232 bool mrsets_count_duplicates; /* MRSETS. */
233 bool smissing_listwise; /* SMISSING. */
234 struct variable *e_weight; /* WEIGHT. */
235 int hide_threshold; /* HIDESMALLCOUNTS. */
237 struct ctables_table **tables;
241 struct ctables_postcompute
243 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
244 const char *name; /* Name, without leading &. */
246 struct ctables_postcompute_expr *expr;
249 bool hide_source_cats;
252 struct ctables_postcompute_expr
254 enum ctables_postcompute_op
262 /* XXX SUBTOTAL and HSUBTOTAL */
275 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
280 XXX what about string ranges? */
283 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
284 struct ctables_postcompute_expr *subs[2];
288 struct ctables_summary_spec_set
290 struct ctables_summary_spec *specs;
294 struct variable *var;
297 static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
298 const struct ctables_summary_spec_set *);
299 static void ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *);
301 /* A nested sequence of variables, e.g. a > b > c. */
304 struct variable **vars;
307 size_t *domains[N_CTDTS];
308 size_t n_domains[N_CTDTS];
310 struct ctables_summary_spec_set specs[N_CSVS];
313 /* A stack of nestings, e.g. nest1 + nest2 + ... + nestN. */
316 struct ctables_nest *nests;
322 struct hmap_node node;
327 struct ctables_section
329 struct ctables_table *table;
330 struct ctables_nest *nests[PIVOT_N_AXES];
331 struct hmap cells; /* Contains "struct ctable_cell"s. */
332 struct hmap domains[N_CTDTS]; /* Contains "struct ctable_domain"s. */
337 struct ctables_axis *axes[PIVOT_N_AXES];
338 struct ctables_stack stacks[PIVOT_N_AXES];
339 struct ctables_section *sections;
341 enum pivot_axis_type summary_axis;
342 struct ctables_summary_spec_set summary_specs;
344 const struct variable *clabels_example;
345 struct hmap clabels_values_map;
346 struct ctables_value **clabels_values;
347 size_t n_clabels_values;
349 enum pivot_axis_type slabels_axis;
350 bool slabels_visible;
352 /* The innermost category labels for axis 'a' appear on axis label_axis[a].
354 Most commonly, label_axis[a] == a, and in particular we always have
355 label_axis{PIVOT_AXIS_LAYER] == PIVOT_AXIS_LAYER.
357 If ROWLABELS or COLLABELS is specified, then one of
358 label_axis[PIVOT_AXIS_ROW] or label_axis[PIVOT_AXIS_COLUMN] can be the
359 opposite axis or PIVOT_AXIS_LAYER. Only one of them will differ.
361 enum pivot_axis_type label_axis[PIVOT_N_AXES];
362 enum pivot_axis_type clabels_from_axis;
364 /* Indexed by variable dictionary index. */
365 struct ctables_categories **categories;
374 struct ctables_chisq *chisq;
375 struct ctables_pairwise *pairwise;
383 struct variable *var;
384 const struct mrset *mrset;
388 static const struct fmt_spec *
389 ctables_var_get_print_format (const struct ctables_var *var)
391 return (var->is_mrset
392 ? var_get_print_format (var->mrset->vars[0])
393 : var_get_print_format (var->var));
397 ctables_var_name (const struct ctables_var *var)
399 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
402 struct ctables_categories
405 struct ctables_category *cats;
410 struct ctables_category
412 enum ctables_category_type
430 struct ctables_category *subtotal;
434 double number; /* CCT_NUMBER. */
435 char *string; /* CCT_STRING. */
436 double range[2]; /* CCT_RANGE. */
437 char *total_label; /* CCT_SUBTOTAL, CCT_HSUBTOTAL, CCT_TOTAL. */
439 /* CCT_VALUE, CCT_LABEL, CCT_FUNCTION. */
442 bool include_missing;
446 enum ctables_summary_function sort_function;
447 struct variable *sort_var;
454 ctables_category_uninit (struct ctables_category *cat)
474 free (cat->total_label);
485 ctables_category_equal (const struct ctables_category *a,
486 const struct ctables_category *b)
488 if (a->type != b->type)
494 return a->number == b->number;
497 return strcmp (a->string, b->string);
500 return a->range[0] == b->range[0] && a->range[1] == b->range[1];
509 return !strcmp (a->total_label, b->total_label);
514 return (a->include_missing == b->include_missing
515 && a->sort_ascending == b->sort_ascending
516 && a->sort_function == b->sort_function
517 && a->sort_var == b->sort_var
518 && a->percentile == b->percentile);
525 ctables_categories_unref (struct ctables_categories *c)
530 assert (c->n_refs > 0);
534 for (size_t i = 0; i < c->n_cats; i++)
535 ctables_category_uninit (&c->cats[i]);
541 ctables_categories_equal (const struct ctables_categories *a,
542 const struct ctables_categories *b)
544 if (a->n_cats != b->n_cats || a->show_empty != b->show_empty)
547 for (size_t i = 0; i < a->n_cats; i++)
548 if (!ctables_category_equal (&a->cats[i], &b->cats[i]))
554 /* Chi-square test (SIGTEST). */
562 /* Pairwise comparison test (COMPARETEST). */
563 struct ctables_pairwise
565 enum { PROP, MEAN } type;
568 bool meansvariance_allcats;
570 enum { BONFERRONI = 1, BH } adjust;
594 struct ctables_var var;
596 struct ctables_summary_spec_set specs[N_CSVS];
600 struct ctables_axis *subs[2];
603 struct msg_location *loc;
606 static void ctables_axis_destroy (struct ctables_axis *);
615 enum ctables_function_availability
617 CTFA_ALL, /* Any variables. */
618 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
619 CTFA_MRSETS, /* Only multiple-response sets */
622 struct ctables_summary_spec
624 enum ctables_summary_function function;
625 double percentile; /* CTSF_PTILE only. */
627 struct fmt_spec format; /* XXX extra CTABLES formats */
632 ctables_summary_spec_clone (struct ctables_summary_spec *dst,
633 const struct ctables_summary_spec *src)
636 dst->label = xstrdup (src->label);
640 ctables_summary_spec_uninit (struct ctables_summary_spec *s)
647 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
648 const struct ctables_summary_spec_set *src)
650 struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
651 for (size_t i = 0; i < src->n; i++)
652 ctables_summary_spec_clone (&specs[i], &src->specs[i]);
654 *dst = (struct ctables_summary_spec_set) {
663 ctables_summary_spec_set_uninit (struct ctables_summary_spec_set *set)
665 for (size_t i = 0; i < set->n; i++)
666 ctables_summary_spec_uninit (&set->specs[i]);
671 parse_col_width (struct lexer *lexer, const char *name, double *width)
673 lex_match (lexer, T_EQUALS);
674 if (lex_match_id (lexer, "DEFAULT"))
676 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
678 *width = lex_number (lexer);
688 parse_bool (struct lexer *lexer, bool *b)
690 if (lex_match_id (lexer, "NO"))
692 else if (lex_match_id (lexer, "YES"))
696 lex_error_expecting (lexer, "YES", "NO");
702 static enum ctables_function_availability
703 ctables_function_availability (enum ctables_summary_function f)
705 static enum ctables_function_availability availability[] = {
706 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
711 return availability[f];
715 parse_ctables_summary_function (struct lexer *lexer,
716 enum ctables_summary_function *f)
720 enum ctables_summary_function function;
721 struct substring name;
723 static struct pair names[] = {
724 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
725 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
728 /* The .COUNT suffix may be omitted. */
729 S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
730 S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
731 S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
732 S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
733 S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
734 S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
735 S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
739 if (!lex_force_id (lexer))
742 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
743 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
745 *f = names[i].function;
750 lex_error (lexer, _("Expecting summary function name."));
755 ctables_axis_destroy (struct ctables_axis *axis)
763 for (size_t i = 0; i < N_CSVS; i++)
764 ctables_summary_spec_set_uninit (&axis->specs[i]);
769 ctables_axis_destroy (axis->subs[0]);
770 ctables_axis_destroy (axis->subs[1]);
773 msg_location_destroy (axis->loc);
777 static struct ctables_axis *
778 ctables_axis_new_nonterminal (enum ctables_axis_op op,
779 struct ctables_axis *sub0,
780 struct ctables_axis *sub1,
781 struct lexer *lexer, int start_ofs)
783 struct ctables_axis *axis = xmalloc (sizeof *axis);
784 *axis = (struct ctables_axis) {
786 .subs = { sub0, sub1 },
787 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
792 struct ctables_axis_parse_ctx
795 struct dictionary *dict;
797 struct ctables_table *t;
800 static struct fmt_spec
801 ctables_summary_default_format (enum ctables_summary_function function,
802 const struct ctables_var *var)
804 static const enum ctables_format default_formats[] = {
805 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
809 switch (default_formats[function])
812 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
815 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
818 return *ctables_var_get_print_format (var);
826 ctables_summary_default_label (enum ctables_summary_function function,
829 static const char *default_labels[] = {
830 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
835 return (function == CTSF_PTILE
836 ? xasprintf (_("Percentile %.2f"), percentile)
837 : xstrdup (gettext (default_labels[function])));
841 ctables_summary_function_name (enum ctables_summary_function function)
843 static const char *names[] = {
844 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
848 return names[function];
852 add_summary_spec (struct ctables_axis *axis,
853 enum ctables_summary_function function, double percentile,
854 const char *label, const struct fmt_spec *format,
855 const struct msg_location *loc, enum ctables_summary_variant sv)
857 if (axis->op == CTAO_VAR)
859 const char *function_name = ctables_summary_function_name (function);
860 const char *var_name = ctables_var_name (&axis->var);
861 switch (ctables_function_availability (function))
864 if (!axis->var.is_mrset)
866 msg_at (SE, loc, _("Summary function %s applies only to multiple "
867 "response sets."), function_name);
868 msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
878 _("Summary function %s applies only to scale variables."),
880 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
890 struct ctables_summary_spec_set *set = &axis->specs[sv];
891 if (set->n >= set->allocated)
892 set->specs = x2nrealloc (set->specs, &set->allocated,
895 struct ctables_summary_spec *dst = &set->specs[set->n++];
896 *dst = (struct ctables_summary_spec) {
897 .function = function,
898 .percentile = percentile,
899 .label = xstrdup (label),
900 .format = (format ? *format
901 : ctables_summary_default_format (function, &axis->var)),
907 for (size_t i = 0; i < 2; i++)
908 if (!add_summary_spec (axis->subs[i], function, percentile, label,
915 static struct ctables_axis *ctables_axis_parse_stack (
916 struct ctables_axis_parse_ctx *);
919 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
920 struct ctables_var *var)
922 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
924 *var = (struct ctables_var) {
926 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
930 lex_error (lexer, _("'%s' does not name a multiple-response set "
931 "in the active file dictionary."),
932 lex_tokcstr (lexer));
940 *var = (struct ctables_var) {
942 .var = parse_variable (lexer, dict),
944 return var->var != NULL;
948 static struct ctables_axis *
949 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
951 if (lex_match (ctx->lexer, T_LPAREN))
953 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
954 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
956 ctables_axis_destroy (sub);
962 if (!lex_force_id (ctx->lexer))
965 int start_ofs = lex_ofs (ctx->lexer);
966 struct ctables_var var;
967 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
970 struct ctables_axis *axis = xmalloc (sizeof *axis);
971 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
973 /* XXX should figure out default measures by reading data */
974 axis->scale = (var.is_mrset ? false
975 : lex_match_phrase (ctx->lexer, "[S]") ? true
976 : lex_match_phrase (ctx->lexer, "[C]") ? false
977 : var_get_measure (var.var) == MEASURE_SCALE);
978 axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
979 lex_ofs (ctx->lexer) - 1);
984 has_digit (const char *s)
986 return s[strcspn (s, "0123456789")] != '\0';
989 static struct ctables_axis *
990 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
992 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
993 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
996 enum ctables_summary_variant sv = CSV_CELL;
999 int start_ofs = lex_ofs (ctx->lexer);
1001 /* Parse function. */
1002 enum ctables_summary_function function;
1003 if (!parse_ctables_summary_function (ctx->lexer, &function))
1006 /* Parse percentile. */
1007 double percentile = 0;
1008 if (function == CTSF_PTILE)
1010 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
1012 percentile = lex_number (ctx->lexer);
1013 lex_get (ctx->lexer);
1018 if (lex_is_string (ctx->lexer))
1020 label = ss_xstrdup (lex_tokss (ctx->lexer));
1021 lex_get (ctx->lexer);
1024 label = ctables_summary_default_label (function, percentile);
1027 struct fmt_spec format;
1028 const struct fmt_spec *formatp;
1029 if (lex_token (ctx->lexer) == T_ID
1030 && has_digit (lex_tokcstr (ctx->lexer)))
1032 if (!parse_format_specifier (ctx->lexer, &format)
1033 || !fmt_check_output (&format)
1034 || !fmt_check_type_compat (&format, VAL_NUMERIC))
1044 struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
1045 lex_ofs (ctx->lexer) - 1);
1046 add_summary_spec (sub, function, percentile, label, formatp, loc, sv);
1048 msg_location_destroy (loc);
1050 lex_match (ctx->lexer, T_COMMA);
1051 if (sv == CSV_CELL && lex_match_id (ctx->lexer, "TOTALS"))
1053 if (!lex_force_match (ctx->lexer, T_LBRACK))
1057 else if (lex_match (ctx->lexer, T_RBRACK))
1059 if (sv == CSV_TOTAL && !lex_force_match (ctx->lexer, T_RBRACK))
1066 ctables_axis_destroy (sub);
1070 static const struct ctables_axis *
1071 find_scale (const struct ctables_axis *axis)
1075 else if (axis->op == CTAO_VAR)
1079 assert (!axis->var.is_mrset);
1087 for (size_t i = 0; i < 2; i++)
1089 const struct ctables_axis *scale = find_scale (axis->subs[i]);
1097 static const struct ctables_axis *
1098 find_categorical_summary_spec (const struct ctables_axis *axis)
1102 else if (axis->op == CTAO_VAR)
1103 return !axis->scale && axis->specs[CSV_CELL].n ? axis : NULL;
1106 for (size_t i = 0; i < 2; i++)
1108 const struct ctables_axis *sum
1109 = find_categorical_summary_spec (axis->subs[i]);
1117 static struct ctables_axis *
1118 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
1120 int start_ofs = lex_ofs (ctx->lexer);
1121 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
1125 while (lex_match (ctx->lexer, T_GT))
1127 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
1131 struct ctables_axis *nest = ctables_axis_new_nonterminal (
1132 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
1134 const struct ctables_axis *outer_scale = find_scale (lhs);
1135 const struct ctables_axis *inner_scale = find_scale (rhs);
1136 if (outer_scale && inner_scale)
1138 msg_at (SE, nest->loc, _("Cannot nest scale variables."));
1139 msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
1140 msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
1141 ctables_axis_destroy (nest);
1145 const struct ctables_axis *outer_sum = find_categorical_summary_spec (lhs);
1148 msg_at (SE, nest->loc,
1149 _("Summaries may only be requested for categorical variables "
1150 "at the innermost nesting level."));
1151 msg_at (SN, outer_sum->loc,
1152 _("This outer categorical variable has a summary."));
1153 ctables_axis_destroy (nest);
1163 static struct ctables_axis *
1164 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
1166 int start_ofs = lex_ofs (ctx->lexer);
1167 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
1171 while (lex_match (ctx->lexer, T_PLUS))
1173 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
1177 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
1178 ctx->lexer, start_ofs);
1185 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
1186 struct ctables *ct, struct ctables_table *t,
1187 enum pivot_axis_type a)
1189 if (lex_token (lexer) == T_BY
1190 || lex_token (lexer) == T_SLASH
1191 || lex_token (lexer) == T_ENDCMD)
1194 struct ctables_axis_parse_ctx ctx = {
1200 t->axes[a] = ctables_axis_parse_stack (&ctx);
1201 return t->axes[a] != NULL;
1205 ctables_chisq_destroy (struct ctables_chisq *chisq)
1211 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
1217 ctables_table_destroy (struct ctables_table *t)
1222 for (size_t i = 0; i < t->n_categories; i++)
1223 ctables_categories_unref (t->categories[i]);
1224 free (t->categories);
1226 ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
1227 ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
1228 ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
1232 ctables_chisq_destroy (t->chisq);
1233 ctables_pairwise_destroy (t->pairwise);
1238 ctables_destroy (struct ctables *ct)
1243 pivot_table_look_unref (ct->look);
1247 for (size_t i = 0; i < ct->n_tables; i++)
1248 ctables_table_destroy (ct->tables[i]);
1253 static struct ctables_category
1254 cct_range (double low, double high)
1256 return (struct ctables_category) {
1258 .range = { low, high }
1263 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1264 struct ctables_table *t)
1266 if (!lex_match_id (lexer, "VARIABLES"))
1268 lex_match (lexer, T_EQUALS);
1270 struct variable **vars;
1272 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1275 struct ctables_categories *c = xmalloc (sizeof *c);
1276 *c = (struct ctables_categories) { .n_refs = n_vars };
1277 for (size_t i = 0; i < n_vars; i++)
1279 struct ctables_categories **cp
1280 = &t->categories[var_get_dict_index (vars[i])];
1281 ctables_categories_unref (*cp);
1286 size_t allocated_cats = 0;
1287 if (lex_match (lexer, T_LBRACK))
1291 if (c->n_cats >= allocated_cats)
1292 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1294 struct ctables_category *cat = &c->cats[c->n_cats];
1295 if (lex_match_id (lexer, "OTHERNM"))
1296 cat->type = CCT_OTHERNM;
1297 else if (lex_match_id (lexer, "MISSING"))
1298 cat->type = CCT_MISSING;
1299 else if (lex_match_id (lexer, "SUBTOTAL"))
1300 *cat = (struct ctables_category)
1301 { .type = CCT_SUBTOTAL, .total_label = NULL };
1302 else if (lex_match_id (lexer, "HSUBTOTAL"))
1303 *cat = (struct ctables_category)
1304 { .type = CCT_HSUBTOTAL, .total_label = NULL };
1305 else if (lex_match_id (lexer, "LO"))
1307 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1309 *cat = cct_range (-DBL_MAX, lex_number (lexer));
1312 else if (lex_is_number (lexer))
1314 double number = lex_number (lexer);
1316 if (lex_match_id (lexer, "THRU"))
1318 cat->type = CCT_RANGE;
1319 cat->range[0] = number;
1320 if (lex_match_id (lexer, "HI"))
1321 *cat = cct_range (number, DBL_MAX);
1324 if (!lex_force_num (lexer))
1326 *cat = cct_range (number, lex_number (lexer));
1331 *cat = (struct ctables_category) {
1336 else if (lex_is_string (lexer))
1338 *cat = (struct ctables_category) {
1340 .string = ss_xstrdup (lex_tokss (lexer)),
1346 lex_error (lexer, NULL);
1350 if (cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
1352 if (lex_match (lexer, T_EQUALS))
1354 if (!lex_force_string (lexer))
1357 cat->total_label = ss_xstrdup (lex_tokss (lexer));
1361 cat->total_label = xstrdup (_("Subtotal"));
1365 lex_match (lexer, T_COMMA);
1367 while (!lex_match (lexer, T_RBRACK));
1370 struct ctables_category cat = {
1372 .include_missing = false,
1373 .sort_ascending = true,
1375 bool show_totals = false;
1376 char *total_label = NULL;
1377 bool totals_before = false;
1378 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1380 if (!c->n_cats && lex_match_id (lexer, "ORDER"))
1382 lex_match (lexer, T_EQUALS);
1383 if (lex_match_id (lexer, "A"))
1384 cat.sort_ascending = true;
1385 else if (lex_match_id (lexer, "D"))
1386 cat.sort_ascending = false;
1389 lex_error_expecting (lexer, "A", "D");
1393 else if (!c->n_cats && lex_match_id (lexer, "KEY"))
1395 lex_match (lexer, T_EQUALS);
1396 if (lex_match_id (lexer, "VALUE"))
1397 cat.type = CCT_VALUE;
1398 else if (lex_match_id (lexer, "LABEL"))
1399 cat.type = CCT_LABEL;
1402 cat.type = CCT_FUNCTION;
1403 if (!parse_ctables_summary_function (lexer, &cat.sort_function))
1406 if (lex_match (lexer, T_LPAREN))
1408 cat.sort_var = parse_variable (lexer, dict);
1412 if (cat.sort_function == CTSF_PTILE)
1414 lex_match (lexer, T_COMMA);
1415 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1417 cat.percentile = lex_number (lexer);
1421 if (!lex_force_match (lexer, T_RPAREN))
1424 else if (ctables_function_availability (cat.sort_function)
1427 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1432 else if (!c->n_cats && lex_match_id (lexer, "MISSING"))
1434 lex_match (lexer, T_EQUALS);
1435 if (lex_match_id (lexer, "INCLUDE"))
1436 cat.include_missing = true;
1437 else if (lex_match_id (lexer, "EXCLUDE"))
1438 cat.include_missing = false;
1441 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1445 else if (lex_match_id (lexer, "TOTAL"))
1447 lex_match (lexer, T_EQUALS);
1448 if (!parse_bool (lexer, &show_totals))
1451 else if (lex_match_id (lexer, "LABEL"))
1453 lex_match (lexer, T_EQUALS);
1454 if (!lex_force_string (lexer))
1457 total_label = ss_xstrdup (lex_tokss (lexer));
1460 else if (lex_match_id (lexer, "POSITION"))
1462 lex_match (lexer, T_EQUALS);
1463 if (lex_match_id (lexer, "BEFORE"))
1464 totals_before = true;
1465 else if (lex_match_id (lexer, "AFTER"))
1466 totals_before = false;
1469 lex_error_expecting (lexer, "BEFORE", "AFTER");
1473 else if (lex_match_id (lexer, "EMPTY"))
1475 lex_match (lexer, T_EQUALS);
1476 if (lex_match_id (lexer, "INCLUDE"))
1477 c->show_empty = true;
1478 else if (lex_match_id (lexer, "EXCLUDE"))
1479 c->show_empty = false;
1482 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1489 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1490 "TOTAL", "LABEL", "POSITION", "EMPTY");
1492 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1499 if (c->n_cats >= allocated_cats)
1500 c->cats = x2nrealloc (c->cats, &allocated_cats,
1502 c->cats[c->n_cats++] = cat;
1507 if (c->n_cats >= allocated_cats)
1508 c->cats = x2nrealloc (c->cats, &allocated_cats, sizeof *c->cats);
1510 struct ctables_category *totals;
1513 insert_element (c->cats, c->n_cats, sizeof *c->cats, 0);
1514 totals = &c->cats[0];
1517 totals = &c->cats[c->n_cats];
1520 *totals = (struct ctables_category) {
1522 .total_label = total_label ? total_label : xstrdup (_("Total")),
1526 struct ctables_category *subtotal = NULL;
1527 for (size_t i = totals_before ? 0 : c->n_cats;
1528 totals_before ? i < c->n_cats : i-- > 0;
1529 totals_before ? i++ : 0)
1531 struct ctables_category *cat = &c->cats[i];
1539 cat->subtotal = subtotal;
1559 ctables_nest_uninit (struct ctables_nest *nest)
1566 ctables_stack_uninit (struct ctables_stack *stack)
1570 for (size_t i = 0; i < stack->n; i++)
1571 ctables_nest_uninit (&stack->nests[i]);
1572 free (stack->nests);
1576 static struct ctables_stack
1577 nest_fts (struct ctables_stack s0, struct ctables_stack s1)
1584 struct ctables_stack stack = { .nests = xnmalloc (s0.n, s1.n * sizeof *stack.nests) };
1585 for (size_t i = 0; i < s0.n; i++)
1586 for (size_t j = 0; j < s1.n; j++)
1588 const struct ctables_nest *a = &s0.nests[i];
1589 const struct ctables_nest *b = &s1.nests[j];
1591 size_t allocate = a->n + b->n;
1592 struct variable **vars = xnmalloc (allocate, sizeof *vars);
1593 enum pivot_axis_type *axes = xnmalloc (allocate, sizeof *axes);
1595 for (size_t k = 0; k < a->n; k++)
1596 vars[n++] = a->vars[k];
1597 for (size_t k = 0; k < b->n; k++)
1598 vars[n++] = b->vars[k];
1599 assert (n == allocate);
1601 const struct ctables_nest *summary_src;
1602 if (!a->specs[CSV_CELL].var)
1604 else if (!b->specs[CSV_CELL].var)
1609 struct ctables_nest *new = &stack.nests[stack.n++];
1610 *new = (struct ctables_nest) {
1612 .scale_idx = (a->scale_idx != SIZE_MAX ? a->scale_idx
1613 : b->scale_idx != SIZE_MAX ? a->n + b->scale_idx
1617 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1618 ctables_summary_spec_set_clone (&new->specs[sv], &summary_src->specs[sv]);
1620 ctables_stack_uninit (&s0);
1621 ctables_stack_uninit (&s1);
1625 static struct ctables_stack
1626 stack_fts (struct ctables_stack s0, struct ctables_stack s1)
1628 struct ctables_stack stack = { .nests = xnmalloc (s0.n + s1.n, sizeof *stack.nests) };
1629 for (size_t i = 0; i < s0.n; i++)
1630 stack.nests[stack.n++] = s0.nests[i];
1631 for (size_t i = 0; i < s1.n; i++)
1632 stack.nests[stack.n++] = s1.nests[i];
1633 assert (stack.n == s0.n + s1.n);
1639 static struct ctables_stack
1640 enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
1643 return (struct ctables_stack) { .n = 0 };
1648 assert (!a->var.is_mrset);
1650 struct variable **vars = xmalloc (sizeof *vars);
1653 struct ctables_nest *nest = xmalloc (sizeof *nest);
1654 *nest = (struct ctables_nest) {
1657 .scale_idx = a->scale ? 0 : SIZE_MAX,
1659 if (a->specs[CSV_CELL].n || a->scale)
1660 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
1662 ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
1663 nest->specs[sv].var = a->var.var;
1665 return (struct ctables_stack) { .nests = nest, .n = 1 };
1668 return stack_fts (enumerate_fts (axis_type, a->subs[0]),
1669 enumerate_fts (axis_type, a->subs[1]));
1672 return nest_fts (enumerate_fts (axis_type, a->subs[0]),
1673 enumerate_fts (axis_type, a->subs[1]));
1679 union ctables_summary
1681 /* COUNT, VALIDN, TOTALN. */
1688 /* MINIMUM, MAXIMUM, RANGE. */
1695 /* MEAN, SEMEAN, STDDEV, SUM, VARIANCE, *.SUM. */
1696 struct moments1 *moments;
1698 /* MEDIAN, MODE, PTILE. */
1701 struct casewriter *writer;
1706 /* XXX multiple response */
1710 ctables_summary_init (union ctables_summary *s,
1711 const struct ctables_summary_spec *ss)
1713 switch (ss->function)
1717 case CTSF_ROWPCT_COUNT:
1718 case CTSF_COLPCT_COUNT:
1719 case CTSF_TABLEPCT_COUNT:
1720 case CTSF_SUBTABLEPCT_COUNT:
1721 case CTSF_LAYERPCT_COUNT:
1722 case CTSF_LAYERROWPCT_COUNT:
1723 case CTSF_LAYERCOLPCT_COUNT:
1724 case CTSF_ROWPCT_VALIDN:
1725 case CTSF_COLPCT_VALIDN:
1726 case CTSF_TABLEPCT_VALIDN:
1727 case CTSF_SUBTABLEPCT_VALIDN:
1728 case CTSF_LAYERPCT_VALIDN:
1729 case CTSF_LAYERROWPCT_VALIDN:
1730 case CTSF_LAYERCOLPCT_VALIDN:
1731 case CTSF_ROWPCT_TOTALN:
1732 case CTSF_COLPCT_TOTALN:
1733 case CTSF_TABLEPCT_TOTALN:
1734 case CTSF_SUBTABLEPCT_TOTALN:
1735 case CTSF_LAYERPCT_TOTALN:
1736 case CTSF_LAYERROWPCT_TOTALN:
1737 case CTSF_LAYERCOLPCT_TOTALN:
1743 s->missing = s->valid = 0;
1749 s->min = s->max = SYSMIS;
1757 case CTSF_ROWPCT_SUM:
1758 case CTSF_COLPCT_SUM:
1759 case CTSF_TABLEPCT_SUM:
1760 case CTSF_SUBTABLEPCT_SUM:
1761 case CTSF_LAYERPCT_SUM:
1762 case CTSF_LAYERROWPCT_SUM:
1763 case CTSF_LAYERCOLPCT_SUM:
1764 s->moments = moments1_create (MOMENT_VARIANCE);
1771 struct caseproto *proto = caseproto_create ();
1772 proto = caseproto_add_width (proto, 0);
1773 proto = caseproto_add_width (proto, 0);
1775 struct subcase ordering;
1776 subcase_init (&ordering, 0, 0, SC_ASCEND);
1777 s->writer = sort_create_writer (&ordering, proto);
1778 subcase_uninit (&ordering);
1779 caseproto_unref (proto);
1786 case CTSF_RESPONSES:
1787 case CTSF_ROWPCT_RESPONSES:
1788 case CTSF_COLPCT_RESPONSES:
1789 case CTSF_TABLEPCT_RESPONSES:
1790 case CTSF_SUBTABLEPCT_RESPONSES:
1791 case CTSF_LAYERPCT_RESPONSES:
1792 case CTSF_LAYERROWPCT_RESPONSES:
1793 case CTSF_LAYERCOLPCT_RESPONSES:
1794 case CTSF_ROWPCT_RESPONSES_COUNT:
1795 case CTSF_COLPCT_RESPONSES_COUNT:
1796 case CTSF_TABLEPCT_RESPONSES_COUNT:
1797 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1798 case CTSF_LAYERPCT_RESPONSES_COUNT:
1799 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1800 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1801 case CTSF_ROWPCT_COUNT_RESPONSES:
1802 case CTSF_COLPCT_COUNT_RESPONSES:
1803 case CTSF_TABLEPCT_COUNT_RESPONSES:
1804 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1805 case CTSF_LAYERPCT_COUNT_RESPONSES:
1806 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1807 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1813 ctables_summary_uninit (union ctables_summary *s,
1814 const struct ctables_summary_spec *ss)
1816 switch (ss->function)
1820 case CTSF_ROWPCT_COUNT:
1821 case CTSF_COLPCT_COUNT:
1822 case CTSF_TABLEPCT_COUNT:
1823 case CTSF_SUBTABLEPCT_COUNT:
1824 case CTSF_LAYERPCT_COUNT:
1825 case CTSF_LAYERROWPCT_COUNT:
1826 case CTSF_LAYERCOLPCT_COUNT:
1827 case CTSF_ROWPCT_VALIDN:
1828 case CTSF_COLPCT_VALIDN:
1829 case CTSF_TABLEPCT_VALIDN:
1830 case CTSF_SUBTABLEPCT_VALIDN:
1831 case CTSF_LAYERPCT_VALIDN:
1832 case CTSF_LAYERROWPCT_VALIDN:
1833 case CTSF_LAYERCOLPCT_VALIDN:
1834 case CTSF_ROWPCT_TOTALN:
1835 case CTSF_COLPCT_TOTALN:
1836 case CTSF_TABLEPCT_TOTALN:
1837 case CTSF_SUBTABLEPCT_TOTALN:
1838 case CTSF_LAYERPCT_TOTALN:
1839 case CTSF_LAYERROWPCT_TOTALN:
1840 case CTSF_LAYERCOLPCT_TOTALN:
1858 case CTSF_ROWPCT_SUM:
1859 case CTSF_COLPCT_SUM:
1860 case CTSF_TABLEPCT_SUM:
1861 case CTSF_SUBTABLEPCT_SUM:
1862 case CTSF_LAYERPCT_SUM:
1863 case CTSF_LAYERROWPCT_SUM:
1864 case CTSF_LAYERCOLPCT_SUM:
1865 moments1_destroy (s->moments);
1871 casewriter_destroy (s->writer);
1874 case CTSF_RESPONSES:
1875 case CTSF_ROWPCT_RESPONSES:
1876 case CTSF_COLPCT_RESPONSES:
1877 case CTSF_TABLEPCT_RESPONSES:
1878 case CTSF_SUBTABLEPCT_RESPONSES:
1879 case CTSF_LAYERPCT_RESPONSES:
1880 case CTSF_LAYERROWPCT_RESPONSES:
1881 case CTSF_LAYERCOLPCT_RESPONSES:
1882 case CTSF_ROWPCT_RESPONSES_COUNT:
1883 case CTSF_COLPCT_RESPONSES_COUNT:
1884 case CTSF_TABLEPCT_RESPONSES_COUNT:
1885 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
1886 case CTSF_LAYERPCT_RESPONSES_COUNT:
1887 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
1888 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
1889 case CTSF_ROWPCT_COUNT_RESPONSES:
1890 case CTSF_COLPCT_COUNT_RESPONSES:
1891 case CTSF_TABLEPCT_COUNT_RESPONSES:
1892 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
1893 case CTSF_LAYERPCT_COUNT_RESPONSES:
1894 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
1895 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
1901 ctables_summary_add (union ctables_summary *s,
1902 const struct ctables_summary_spec *ss,
1903 const struct variable *var, const union value *value,
1904 double d_weight, double e_weight)
1906 switch (ss->function)
1911 if (var_is_value_missing (var, value))
1912 s->missing += d_weight;
1914 s->valid += d_weight;
1918 case CTSF_ROWPCT_COUNT:
1919 case CTSF_COLPCT_COUNT:
1920 case CTSF_TABLEPCT_COUNT:
1921 case CTSF_SUBTABLEPCT_COUNT:
1922 case CTSF_LAYERPCT_COUNT:
1923 case CTSF_LAYERROWPCT_COUNT:
1924 case CTSF_LAYERCOLPCT_COUNT:
1925 case CTSF_ROWPCT_VALIDN:
1926 case CTSF_COLPCT_VALIDN:
1927 case CTSF_TABLEPCT_VALIDN:
1928 case CTSF_SUBTABLEPCT_VALIDN:
1929 case CTSF_LAYERPCT_VALIDN:
1930 case CTSF_LAYERROWPCT_VALIDN:
1931 case CTSF_LAYERCOLPCT_VALIDN:
1932 case CTSF_ROWPCT_TOTALN:
1933 case CTSF_COLPCT_TOTALN:
1934 case CTSF_TABLEPCT_TOTALN:
1935 case CTSF_SUBTABLEPCT_TOTALN:
1936 case CTSF_LAYERPCT_TOTALN:
1937 case CTSF_LAYERROWPCT_TOTALN:
1938 case CTSF_LAYERCOLPCT_TOTALN:
1942 if (var_is_value_missing (var, value))
1943 s->missing += e_weight;
1945 s->valid += e_weight;
1951 if (!var_is_value_missing (var, value))
1953 assert (!var_is_alpha (var)); /* XXX? */
1954 if (s->min == SYSMIS || value->f < s->min)
1956 if (s->max == SYSMIS || value->f > s->max)
1966 case CTSF_ROWPCT_SUM:
1967 case CTSF_COLPCT_SUM:
1968 case CTSF_TABLEPCT_SUM:
1969 case CTSF_SUBTABLEPCT_SUM:
1970 case CTSF_LAYERPCT_SUM:
1971 case CTSF_LAYERROWPCT_SUM:
1972 case CTSF_LAYERCOLPCT_SUM:
1973 if (!var_is_value_missing (var, value))
1974 moments1_add (s->moments, value->f, e_weight);
1980 if (var_is_value_missing (var, value))
1982 s->ovalid += e_weight;
1984 struct ccase *c = case_create (casewriter_get_proto (s->writer));
1985 *case_num_rw_idx (c, 0) = value->f;
1986 *case_num_rw_idx (c, 1) = e_weight;
1987 casewriter_write (s->writer, c);
1991 case CTSF_RESPONSES:
1992 case CTSF_ROWPCT_RESPONSES:
1993 case CTSF_COLPCT_RESPONSES:
1994 case CTSF_TABLEPCT_RESPONSES:
1995 case CTSF_SUBTABLEPCT_RESPONSES:
1996 case CTSF_LAYERPCT_RESPONSES:
1997 case CTSF_LAYERROWPCT_RESPONSES:
1998 case CTSF_LAYERCOLPCT_RESPONSES:
1999 case CTSF_ROWPCT_RESPONSES_COUNT:
2000 case CTSF_COLPCT_RESPONSES_COUNT:
2001 case CTSF_TABLEPCT_RESPONSES_COUNT:
2002 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2003 case CTSF_LAYERPCT_RESPONSES_COUNT:
2004 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2005 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2006 case CTSF_ROWPCT_COUNT_RESPONSES:
2007 case CTSF_COLPCT_COUNT_RESPONSES:
2008 case CTSF_TABLEPCT_COUNT_RESPONSES:
2009 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2010 case CTSF_LAYERPCT_COUNT_RESPONSES:
2011 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2012 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2017 static enum ctables_domain_type
2018 ctables_function_domain (enum ctables_summary_function function)
2040 case CTSF_RESPONSES:
2043 case CTSF_COLPCT_COUNT:
2044 case CTSF_COLPCT_COUNT_RESPONSES:
2045 case CTSF_COLPCT_RESPONSES:
2046 case CTSF_COLPCT_RESPONSES_COUNT:
2047 case CTSF_COLPCT_SUM:
2048 case CTSF_COLPCT_TOTALN:
2049 case CTSF_COLPCT_VALIDN:
2052 case CTSF_LAYERCOLPCT_COUNT:
2053 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2054 case CTSF_LAYERCOLPCT_RESPONSES:
2055 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2056 case CTSF_LAYERCOLPCT_SUM:
2057 case CTSF_LAYERCOLPCT_TOTALN:
2058 case CTSF_LAYERCOLPCT_VALIDN:
2059 return CTDT_LAYERCOL;
2061 case CTSF_LAYERPCT_COUNT:
2062 case CTSF_LAYERPCT_COUNT_RESPONSES:
2063 case CTSF_LAYERPCT_RESPONSES:
2064 case CTSF_LAYERPCT_RESPONSES_COUNT:
2065 case CTSF_LAYERPCT_SUM:
2066 case CTSF_LAYERPCT_TOTALN:
2067 case CTSF_LAYERPCT_VALIDN:
2070 case CTSF_LAYERROWPCT_COUNT:
2071 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2072 case CTSF_LAYERROWPCT_RESPONSES:
2073 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2074 case CTSF_LAYERROWPCT_SUM:
2075 case CTSF_LAYERROWPCT_TOTALN:
2076 case CTSF_LAYERROWPCT_VALIDN:
2077 return CTDT_LAYERROW;
2079 case CTSF_ROWPCT_COUNT:
2080 case CTSF_ROWPCT_COUNT_RESPONSES:
2081 case CTSF_ROWPCT_RESPONSES:
2082 case CTSF_ROWPCT_RESPONSES_COUNT:
2083 case CTSF_ROWPCT_SUM:
2084 case CTSF_ROWPCT_TOTALN:
2085 case CTSF_ROWPCT_VALIDN:
2088 case CTSF_SUBTABLEPCT_COUNT:
2089 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2090 case CTSF_SUBTABLEPCT_RESPONSES:
2091 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2092 case CTSF_SUBTABLEPCT_SUM:
2093 case CTSF_SUBTABLEPCT_TOTALN:
2094 case CTSF_SUBTABLEPCT_VALIDN:
2095 return CTDT_SUBTABLE;
2097 case CTSF_TABLEPCT_COUNT:
2098 case CTSF_TABLEPCT_COUNT_RESPONSES:
2099 case CTSF_TABLEPCT_RESPONSES:
2100 case CTSF_TABLEPCT_RESPONSES_COUNT:
2101 case CTSF_TABLEPCT_SUM:
2102 case CTSF_TABLEPCT_TOTALN:
2103 case CTSF_TABLEPCT_VALIDN:
2111 ctables_summary_value (const struct ctables_cell *cell,
2112 union ctables_summary *s,
2113 const struct ctables_summary_spec *ss)
2115 switch (ss->function)
2121 case CTSF_ROWPCT_COUNT:
2122 case CTSF_COLPCT_COUNT:
2123 case CTSF_TABLEPCT_COUNT:
2124 case CTSF_SUBTABLEPCT_COUNT:
2125 case CTSF_LAYERPCT_COUNT:
2126 case CTSF_LAYERROWPCT_COUNT:
2127 case CTSF_LAYERCOLPCT_COUNT:
2129 enum ctables_domain_type d = ctables_function_domain (ss->function);
2130 return (cell->domains[d]->e_valid
2131 ? s->valid / cell->domains[d]->e_valid * 100
2135 case CTSF_ROWPCT_VALIDN:
2136 case CTSF_COLPCT_VALIDN:
2137 case CTSF_TABLEPCT_VALIDN:
2138 case CTSF_SUBTABLEPCT_VALIDN:
2139 case CTSF_LAYERPCT_VALIDN:
2140 case CTSF_LAYERROWPCT_VALIDN:
2141 case CTSF_LAYERCOLPCT_VALIDN:
2142 case CTSF_ROWPCT_TOTALN:
2143 case CTSF_COLPCT_TOTALN:
2144 case CTSF_TABLEPCT_TOTALN:
2145 case CTSF_SUBTABLEPCT_TOTALN:
2146 case CTSF_LAYERPCT_TOTALN:
2147 case CTSF_LAYERROWPCT_TOTALN:
2148 case CTSF_LAYERCOLPCT_TOTALN:
2156 return s->valid + s->missing;
2169 return s->max != SYSMIS && s->min != SYSMIS ? s->max - s->min : SYSMIS;
2174 moments1_calculate (s->moments, NULL, &mean, NULL, NULL, NULL);
2180 double weight, variance;
2181 moments1_calculate (s->moments, &weight, NULL, &variance, NULL, NULL);
2182 return calc_semean (variance, weight);
2188 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2189 return variance != SYSMIS ? sqrt (variance) : SYSMIS;
2194 double weight, mean;
2195 moments1_calculate (s->moments, &weight, &mean, NULL, NULL, NULL);
2196 return weight != SYSMIS && mean != SYSMIS ? weight * mean : SYSMIS;
2202 moments1_calculate (s->moments, NULL, NULL, &variance, NULL, NULL);
2206 case CTSF_ROWPCT_SUM:
2207 case CTSF_COLPCT_SUM:
2208 case CTSF_TABLEPCT_SUM:
2209 case CTSF_SUBTABLEPCT_SUM:
2210 case CTSF_LAYERPCT_SUM:
2211 case CTSF_LAYERROWPCT_SUM:
2212 case CTSF_LAYERCOLPCT_SUM:
2219 struct casereader *reader = casewriter_make_reader (s->writer);
2222 struct percentile *ptile = percentile_create (
2223 ss->function == CTSF_PTILE ? ss->percentile : 0.5, s->ovalid);
2224 struct order_stats *os = &ptile->parent;
2225 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2226 s->ovalue = percentile_calculate (ptile, PC_HAVERAGE);
2227 statistic_destroy (&ptile->parent.parent);
2234 struct casereader *reader = casewriter_make_reader (s->writer);
2237 struct mode *mode = mode_create ();
2238 struct order_stats *os = &mode->parent;
2239 order_stats_accumulate_idx (&os, 1, reader, 1, 0);
2240 s->ovalue = mode->mode;
2241 statistic_destroy (&mode->parent.parent);
2245 case CTSF_RESPONSES:
2246 case CTSF_ROWPCT_RESPONSES:
2247 case CTSF_COLPCT_RESPONSES:
2248 case CTSF_TABLEPCT_RESPONSES:
2249 case CTSF_SUBTABLEPCT_RESPONSES:
2250 case CTSF_LAYERPCT_RESPONSES:
2251 case CTSF_LAYERROWPCT_RESPONSES:
2252 case CTSF_LAYERCOLPCT_RESPONSES:
2253 case CTSF_ROWPCT_RESPONSES_COUNT:
2254 case CTSF_COLPCT_RESPONSES_COUNT:
2255 case CTSF_TABLEPCT_RESPONSES_COUNT:
2256 case CTSF_SUBTABLEPCT_RESPONSES_COUNT:
2257 case CTSF_LAYERPCT_RESPONSES_COUNT:
2258 case CTSF_LAYERROWPCT_RESPONSES_COUNT:
2259 case CTSF_LAYERCOLPCT_RESPONSES_COUNT:
2260 case CTSF_ROWPCT_COUNT_RESPONSES:
2261 case CTSF_COLPCT_COUNT_RESPONSES:
2262 case CTSF_TABLEPCT_COUNT_RESPONSES:
2263 case CTSF_SUBTABLEPCT_COUNT_RESPONSES:
2264 case CTSF_LAYERPCT_COUNT_RESPONSES:
2265 case CTSF_LAYERROWPCT_COUNT_RESPONSES:
2266 case CTSF_LAYERCOLPCT_COUNT_RESPONSES:
2273 struct ctables_cell_sort_aux
2275 const struct ctables_nest *nest;
2276 enum pivot_axis_type a;
2280 ctables_cell_compare_3way (const void *a_, const void *b_, const void *aux_)
2282 const struct ctables_cell_sort_aux *aux = aux_;
2283 struct ctables_cell *const *ap = a_;
2284 struct ctables_cell *const *bp = b_;
2285 const struct ctables_cell *a = *ap;
2286 const struct ctables_cell *b = *bp;
2288 const struct ctables_nest *nest = aux->nest;
2289 for (size_t i = 0; i < nest->n; i++)
2290 if (i != nest->scale_idx)
2292 const struct variable *var = nest->vars[i];
2293 const struct ctables_cell_value *a_cv = &a->axes[aux->a].cvs[i];
2294 const struct ctables_cell_value *b_cv = &b->axes[aux->a].cvs[i];
2295 if (a_cv->category != b_cv->category)
2296 return a_cv->category > b_cv->category ? 1 : -1;
2298 const union value *a_val = &a_cv->value;
2299 const union value *b_val = &b_cv->value;
2300 switch (a_cv->category->type)
2307 /* Must be equal. */
2314 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2322 int cmp = value_compare_3way (a_val, b_val, var_get_width (var));
2324 return a_cv->category->sort_ascending ? cmp : -cmp;
2330 const char *a_label = var_lookup_value_label (var, a_val);
2331 const char *b_label = var_lookup_value_label (var, b_val);
2333 ? (b_label ? strcmp (a_label, b_label) : 1)
2334 : (b_label ? -1 : value_compare_3way (
2335 a_val, b_val, var_get_width (var))));
2337 return a_cv->category->sort_ascending ? cmp : -cmp;
2351 For each ctables_table:
2352 For each combination of row vars:
2353 For each combination of column vars:
2354 For each combination of layer vars:
2356 Make a table of row values:
2357 Sort entries by row values
2358 Assign a 0-based index to each actual value
2359 Construct a dimension
2360 Make a table of column values
2361 Make a table of layer values
2363 Fill the table entry using the indexes from before.
2366 static struct ctables_domain *
2367 ctables_domain_insert (struct ctables_section *s, struct ctables_cell *cell,
2368 enum ctables_domain_type domain)
2371 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2373 const struct ctables_nest *nest = s->nests[a];
2374 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2376 size_t v_idx = nest->domains[domain][i];
2377 hash = value_hash (&cell->axes[a].cvs[v_idx].value,
2378 var_get_width (nest->vars[v_idx]), hash);
2382 struct ctables_domain *d;
2383 HMAP_FOR_EACH_WITH_HASH (d, struct ctables_domain, node, hash, &s->domains[domain])
2385 const struct ctables_cell *df = d->example;
2386 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2388 const struct ctables_nest *nest = s->nests[a];
2389 for (size_t i = 0; i < nest->n_domains[domain]; i++)
2391 size_t v_idx = nest->domains[domain][i];
2392 if (!value_equal (&df->axes[a].cvs[v_idx].value,
2393 &cell->axes[a].cvs[v_idx].value,
2394 var_get_width (nest->vars[v_idx])))
2403 d = xmalloc (sizeof *d);
2404 *d = (struct ctables_domain) { .example = cell };
2405 hmap_insert (&s->domains[domain], &d->node, hash);
2409 static const struct ctables_category *
2410 ctables_categories_match (const struct ctables_categories *c,
2411 const union value *v, const struct variable *var)
2413 const struct ctables_category *othernm = NULL;
2414 for (size_t i = c->n_cats; i-- > 0; )
2416 const struct ctables_category *cat = &c->cats[i];
2420 if (cat->number == v->f)
2428 if ((cat->range[0] == -DBL_MAX || v->f >= cat->range[0])
2429 && (cat->range[1] == DBL_MAX || v->f <= cat->range[1]))
2434 if (var_is_value_missing (var, v))
2451 return (cat->include_missing || !var_is_value_missing (var, v) ? cat
2456 return var_is_value_missing (var, v) ? NULL : othernm;
2459 static const struct ctables_category *
2460 ctables_categories_total (const struct ctables_categories *c)
2462 const struct ctables_category *first = &c->cats[0];
2463 const struct ctables_category *last = &c->cats[c->n_cats - 1];
2464 return (first->type == CCT_TOTAL ? first
2465 : last->type == CCT_TOTAL ? last
2469 static struct ctables_cell *
2470 ctables_cell_insert__ (struct ctables_section *s, const struct ccase *c,
2471 const struct ctables_category *cats[PIVOT_N_AXES][10])
2473 const struct ctables_nest *ss = s->nests[s->table->summary_axis];
2476 enum ctables_summary_variant sv = CSV_CELL;
2477 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2479 const struct ctables_nest *nest = s->nests[a];
2480 for (size_t i = 0; i < nest->n; i++)
2481 if (i != nest->scale_idx)
2483 hash = hash_pointer (cats[a][i], hash);
2484 if (cats[a][i]->type != CCT_TOTAL
2485 && cats[a][i]->type != CCT_SUBTOTAL
2486 && cats[a][i]->type != CCT_HSUBTOTAL)
2487 hash = value_hash (case_data (c, nest->vars[i]),
2488 var_get_width (nest->vars[i]), hash);
2494 struct ctables_cell *cell;
2495 HMAP_FOR_EACH_WITH_HASH (cell, struct ctables_cell, node, hash, &s->cells)
2497 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2499 const struct ctables_nest *nest = s->nests[a];
2500 for (size_t i = 0; i < nest->n; i++)
2501 if (i != nest->scale_idx
2502 && (cats[a][i] != cell->axes[a].cvs[i].category
2503 || (cats[a][i]->type != CCT_TOTAL
2504 && cats[a][i]->type != CCT_SUBTOTAL
2505 && cats[a][i]->type != CCT_HSUBTOTAL
2506 && !value_equal (case_data (c, nest->vars[i]),
2507 &cell->axes[a].cvs[i].value,
2508 var_get_width (nest->vars[i])))))
2517 cell = xmalloc (sizeof *cell);
2520 cell->contributes_to_domains = true;
2521 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2523 const struct ctables_nest *nest = s->nests[a];
2524 cell->axes[a].cvs = (nest->n
2525 ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
2527 for (size_t i = 0; i < nest->n; i++)
2529 const struct ctables_category *cat = cats[a][i];
2531 if (i != nest->scale_idx)
2533 const struct ctables_category *subtotal = cat->subtotal;
2534 if (subtotal && subtotal->type == CCT_HSUBTOTAL)
2537 if (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL)
2538 cell->contributes_to_domains = false;
2541 cell->axes[a].cvs[i].category = cat;
2542 value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
2543 var_get_width (nest->vars[i]));
2547 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2548 cell->summaries = xmalloc (specs->n * sizeof *cell->summaries);
2549 for (size_t i = 0; i < specs->n; i++)
2550 ctables_summary_init (&cell->summaries[i], &specs->specs[i]);
2551 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2552 cell->domains[dt] = ctables_domain_insert (s, cell, dt);
2553 hmap_insert (&s->cells, &cell->node, hash);
2558 ctables_cell_add__ (struct ctables_section *s, const struct ccase *c,
2559 const struct ctables_category *cats[PIVOT_N_AXES][10],
2560 double d_weight, double e_weight)
2562 struct ctables_cell *cell = ctables_cell_insert__ (s, c, cats);
2563 const struct ctables_nest *ss = s->nests[s->table->summary_axis];
2565 const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
2566 for (size_t i = 0; i < specs->n; i++)
2567 ctables_summary_add (&cell->summaries[i], &specs->specs[i], specs->var,
2568 case_data (c, specs->var), d_weight, e_weight);
2569 if (cell->contributes_to_domains)
2571 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
2573 cell->domains[dt]->d_valid += d_weight;
2574 cell->domains[dt]->e_valid += e_weight;
2580 recurse_totals (struct ctables_section *s, const struct ccase *c,
2581 const struct ctables_category *cats[PIVOT_N_AXES][10],
2582 double d_weight, double e_weight,
2583 enum pivot_axis_type start_axis, size_t start_nest)
2585 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2587 const struct ctables_nest *nest = s->nests[a];
2588 for (size_t i = start_nest; i < nest->n; i++)
2590 if (i == nest->scale_idx)
2593 const struct variable *var = nest->vars[i];
2595 const struct ctables_category *total = ctables_categories_total (
2596 s->table->categories[var_get_dict_index (var)]);
2599 const struct ctables_category *save = cats[a][i];
2601 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2602 recurse_totals (s, c, cats, d_weight, e_weight, a, i + 1);
2611 recurse_subtotals (struct ctables_section *s, const struct ccase *c,
2612 const struct ctables_category *cats[PIVOT_N_AXES][10],
2613 double d_weight, double e_weight,
2614 enum pivot_axis_type start_axis, size_t start_nest)
2616 for (enum pivot_axis_type a = start_axis; a < PIVOT_N_AXES; a++)
2618 const struct ctables_nest *nest = s->nests[a];
2619 for (size_t i = start_nest; i < nest->n; i++)
2621 if (i == nest->scale_idx)
2624 const struct ctables_category *save = cats[a][i];
2627 cats[a][i] = save->subtotal;
2628 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2629 recurse_subtotals (s, c, cats, d_weight, e_weight, a, i + 1);
2638 ctables_cell_insert (struct ctables_section *s,
2639 const struct ccase *c,
2640 double d_weight, double e_weight)
2642 const struct ctables_category *cats[PIVOT_N_AXES][10];
2643 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2645 const struct ctables_nest *nest = s->nests[a];
2646 for (size_t i = 0; i < nest->n; i++)
2648 if (i == nest->scale_idx)
2651 const struct variable *var = nest->vars[i];
2652 const union value *value = case_data (c, var);
2654 if (var_is_numeric (var) && value->f == SYSMIS)
2657 cats[a][i] = ctables_categories_match (
2658 s->table->categories[var_get_dict_index (var)], value, var);
2664 ctables_cell_add__ (s, c, cats, d_weight, e_weight);
2666 recurse_totals (s, c, cats, d_weight, e_weight, 0, 0);
2667 recurse_subtotals (s, c, cats, d_weight, e_weight, 0, 0);
2672 const struct ctables_summary_spec_set *set;
2677 merge_item_compare_3way (const struct merge_item *a, const struct merge_item *b)
2679 const struct ctables_summary_spec *as = &a->set->specs[a->ofs];
2680 const struct ctables_summary_spec *bs = &b->set->specs[b->ofs];
2681 if (as->function != bs->function)
2682 return as->function > bs->function ? 1 : -1;
2683 else if (as->percentile != bs->percentile)
2684 return as->percentile < bs->percentile ? 1 : -1;
2685 return strcmp (as->label, bs->label);
2688 static struct pivot_value *
2689 ctables_category_create_label (const struct ctables_category *cat,
2690 const struct variable *var,
2691 const union value *value)
2693 return (cat->type == CCT_TOTAL || cat->type == CCT_SUBTOTAL || cat->type == CCT_HSUBTOTAL
2694 ? pivot_value_new_user_text (cat->total_label, SIZE_MAX)
2695 : pivot_value_new_var_value (var, value));
2698 static struct ctables_value *
2699 ctables_value_find__ (struct ctables_table *t, const union value *value,
2700 int width, unsigned int hash)
2702 struct ctables_value *clv;
2703 HMAP_FOR_EACH_WITH_HASH (clv, struct ctables_value, node,
2704 hash, &t->clabels_values_map)
2705 if (value_equal (value, &clv->value, width))
2710 static struct ctables_value *
2711 ctables_value_find (struct ctables_table *t,
2712 const union value *value, int width)
2714 return ctables_value_find__ (t, value, width,
2715 value_hash (value, width, 0));
2719 ctables_table_add_section (struct ctables_table *t, enum pivot_axis_type a,
2720 size_t ix[PIVOT_N_AXES])
2722 if (a < PIVOT_N_AXES)
2724 size_t limit = MAX (t->stacks[a].n, 1);
2725 for (ix[a] = 0; ix[a] < limit; ix[a]++)
2726 ctables_table_add_section (t, a + 1, ix);
2730 struct ctables_section *s = &t->sections[t->n_sections++];
2731 *s = (struct ctables_section) {
2733 .cells = HMAP_INITIALIZER (s->cells),
2735 for (a = 0; a < PIVOT_N_AXES; a++)
2736 s->nests[a] = t->stacks[a].n ? &t->stacks[a].nests[ix[a]] : NULL;
2737 for (size_t i = 0; i < N_CTDTS; i++)
2738 hmap_init (&s->domains[i]);
2743 ctables_table_output (struct ctables *ct, struct ctables_table *t)
2745 struct pivot_table *pt = pivot_table_create__ (
2747 ? pivot_value_new_user_text (t->title, SIZE_MAX)
2748 : pivot_value_new_text (N_("Custom Tables"))),
2751 pivot_table_set_caption (
2752 pt, pivot_value_new_user_text (t->caption, SIZE_MAX));
2754 pivot_table_set_caption (
2755 pt, pivot_value_new_user_text (t->corner, SIZE_MAX));
2757 bool summary_dimension = (t->summary_axis != t->slabels_axis
2758 || (!t->slabels_visible
2759 && t->summary_specs.n > 1));
2760 if (summary_dimension)
2762 struct pivot_dimension *d = pivot_dimension_create (
2763 pt, t->slabels_axis, N_("Statistics"));
2764 const struct ctables_summary_spec_set *specs = &t->summary_specs;
2765 if (!t->slabels_visible)
2766 d->hide_all_labels = true;
2767 for (size_t i = 0; i < specs->n; i++)
2768 pivot_category_create_leaf (
2769 d->root, pivot_value_new_text (specs->specs[i].label));
2772 bool categories_dimension = t->clabels_example != NULL;
2773 if (categories_dimension)
2775 struct pivot_dimension *d = pivot_dimension_create (
2776 pt, t->label_axis[t->clabels_from_axis],
2777 t->clabels_from_axis == PIVOT_AXIS_ROW
2778 ? N_("Row Categories")
2779 : N_("Column Categories"));
2780 const struct variable *var = t->clabels_example;
2781 const struct ctables_categories *c = t->categories[var_get_dict_index (var)];
2782 for (size_t i = 0; i < t->n_clabels_values; i++)
2784 const struct ctables_value *value = t->clabels_values[i];
2785 const struct ctables_category *cat = ctables_categories_match (c, &value->value, var);
2786 assert (cat != NULL);
2787 pivot_category_create_leaf (d->root, ctables_category_create_label (
2788 cat, t->clabels_example, &value->value));
2792 pivot_table_set_look (pt, ct->look);
2793 struct pivot_dimension *d[PIVOT_N_AXES];
2794 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
2796 static const char *names[] = {
2797 [PIVOT_AXIS_ROW] = N_("Rows"),
2798 [PIVOT_AXIS_COLUMN] = N_("Columns"),
2799 [PIVOT_AXIS_LAYER] = N_("Layers"),
2801 d[a] = (t->axes[a] || a == t->summary_axis
2802 ? pivot_dimension_create (pt, a, names[a])
2807 assert (t->axes[a]);
2809 for (size_t i = 0; i < t->stacks[a].n; i++)
2811 struct ctables_nest *nest = &t->stacks[a].nests[i];
2812 struct ctables_section **sections = xnmalloc (t->n_sections,
2814 size_t n_sections = 0;
2816 size_t n_total_cells = 0;
2817 size_t max_depth = 0;
2818 for (size_t j = 0; j < t->n_sections; j++)
2819 if (t->sections[j].nests[a] == nest)
2821 struct ctables_section *s = &t->sections[j];
2822 sections[n_sections++] = s;
2823 n_total_cells += s->cells.count;
2825 size_t depth = s->nests[a]->n;
2826 max_depth = MAX (depth, max_depth);
2829 struct ctables_cell **sorted = xnmalloc (n_total_cells,
2831 size_t n_sorted = 0;
2833 for (size_t j = 0; j < n_sections; j++)
2835 struct ctables_section *s = sections[j];
2837 struct ctables_cell *cell;
2838 HMAP_FOR_EACH (cell, struct ctables_cell, node, &s->cells)
2840 sorted[n_sorted++] = cell;
2841 assert (n_sorted <= n_total_cells);
2844 struct ctables_cell_sort_aux aux = { .nest = nest, .a = a };
2845 sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
2847 struct ctables_level
2849 enum ctables_level_type
2851 CTL_VAR, /* Variable label for nest->vars[var_idx]. */
2852 CTL_CATEGORY, /* Category for nest->vars[var_idx]. */
2853 CTL_SUMMARY, /* Summary functions. */
2859 struct ctables_level *levels = xnmalloc (1 + 2 * max_depth, sizeof *levels);
2860 size_t n_levels = 0;
2861 for (size_t k = 0; k < nest->n; k++)
2863 enum ctables_vlabel vlabel = ct->vlabels[var_get_dict_index (nest->vars[k])];
2864 if (vlabel != CTVL_NONE)
2866 levels[n_levels++] = (struct ctables_level) {
2872 if (nest->scale_idx != k
2873 && (k != nest->n - 1 || t->label_axis[a] == a))
2875 levels[n_levels++] = (struct ctables_level) {
2876 .type = CTL_CATEGORY,
2882 if (!summary_dimension && a == t->slabels_axis)
2884 levels[n_levels++] = (struct ctables_level) {
2885 .type = CTL_SUMMARY,
2886 .var_idx = SIZE_MAX,
2890 /* Pivot categories:
2892 - variable label for nest->vars[0], if vlabel != CTVL_NONE
2893 - category for nest->vars[0], if nest->scale_idx != 0
2894 - variable label for nest->vars[1], if vlabel != CTVL_NONE
2895 - category for nest->vars[1], if nest->scale_idx != 1
2897 - variable label for nest->vars[n - 1], if vlabel != CTVL_NONE
2898 - category for nest->vars[n - 1], if t->label_axis[a] == a && nest->scale_idx != n - 1.
2899 - summary function, if 'a == t->slabels_axis && a ==
2902 Additional dimensions:
2904 - If 'a == t->slabels_axis && a != t->summary_axis', add a summary
2906 - If 't->label_axis[b] == a' for some 'b != a', add a category
2911 struct pivot_category **groups = xnmalloc (1 + 2 * max_depth, sizeof *groups);
2913 for (size_t j = 0; j < n_sorted; j++)
2915 struct ctables_cell *cell = sorted[j];
2916 struct ctables_cell *prev = j > 0 ? sorted[j - 1] : NULL;
2918 size_t n_common = 0;
2921 for (; n_common < n_levels; n_common++)
2923 const struct ctables_level *level = &levels[n_common];
2924 if (level->type == CTL_CATEGORY)
2926 size_t var_idx = level->var_idx;
2927 const struct ctables_category *c = cell->axes[a].cvs[var_idx].category;
2928 if (prev->axes[a].cvs[var_idx].category != c)
2930 else if (c->type != CCT_SUBTOTAL
2931 && c->type != CCT_HSUBTOTAL
2932 && c->type != CCT_TOTAL
2933 && !value_equal (&prev->axes[a].cvs[var_idx].value,
2934 &cell->axes[a].cvs[var_idx].value,
2935 var_get_type (nest->vars[var_idx])))
2941 for (size_t k = n_common; k < n_levels; k++)
2943 const struct ctables_level *level = &levels[k];
2944 struct pivot_category *parent = k ? groups[k - 1] : d[a]->root;
2945 if (level->type == CTL_SUMMARY)
2947 assert (k == n_levels - 1);
2949 const struct ctables_summary_spec_set *specs = &t->summary_specs;
2950 for (size_t m = 0; m < specs->n; m++)
2952 int leaf = pivot_category_create_leaf (
2953 parent, pivot_value_new_text (specs->specs[m].label));
2960 const struct variable *var = nest->vars[level->var_idx];
2961 struct pivot_value *label;
2962 if (level->type == CTL_VAR)
2963 label = pivot_value_new_variable (var);
2964 else if (level->type == CTL_CATEGORY)
2966 const struct ctables_cell_value *cv = &cell->axes[a].cvs[level->var_idx];
2967 label = ctables_category_create_label (cv->category,
2973 if (k == n_levels - 1)
2974 prev_leaf = pivot_category_create_leaf (parent, label);
2976 groups[k] = pivot_category_create_group__ (parent, label);
2980 cell->axes[a].leaf = prev_leaf;
2987 for (size_t i = 0; i < t->n_sections; i++)
2989 struct ctables_section *s = &t->sections[i];
2991 struct ctables_cell *cell;
2992 HMAP_FOR_EACH (cell, struct ctables_cell, node, &s->cells)
2997 const struct ctables_nest *specs_nest = s->nests[t->summary_axis];
2998 const struct ctables_summary_spec_set *specs = &specs_nest->specs[cell->sv];
2999 for (size_t j = 0; j < specs->n; j++)
3002 size_t n_dindexes = 0;
3004 if (summary_dimension)
3005 dindexes[n_dindexes++] = specs->specs[j].axis_idx;
3007 if (categories_dimension)
3009 const struct ctables_nest *clabels_nest = s->nests[t->clabels_from_axis];
3010 const struct variable *var = clabels_nest->vars[clabels_nest->n - 1];
3011 const union value *value = &cell->axes[t->clabels_from_axis].cvs[clabels_nest->n - 1].value;
3012 const struct ctables_value *ctv = ctables_value_find (t, value, var_get_width (var));
3013 assert (ctv != NULL);
3014 dindexes[n_dindexes++] = ctv->leaf;
3017 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3020 int leaf = cell->axes[a].leaf;
3021 if (a == t->summary_axis && !summary_dimension)
3023 dindexes[n_dindexes++] = leaf;
3026 double d = ctables_summary_value (cell, &cell->summaries[j], &specs->specs[j]);
3027 struct pivot_value *value = pivot_value_new_number (d);
3028 value->numeric.format = specs->specs[j].format;
3029 pivot_table_put (pt, dindexes, n_dindexes, value);
3034 pivot_table_submit (pt);
3038 ctables_check_label_position (struct ctables_table *t, enum pivot_axis_type a)
3040 enum pivot_axis_type label_pos = t->label_axis[a];
3044 t->clabels_from_axis = a;
3046 const char *subcommand_name = a == PIVOT_AXIS_ROW ? "ROWLABELS" : "COLLABELS";
3047 const char *pos_name = label_pos == PIVOT_AXIS_LAYER ? "LAYER" : "OPPOSITE";
3049 const struct ctables_stack *stack = &t->stacks[a];
3053 const struct ctables_nest *n0 = &stack->nests[0];
3055 const struct variable *v0 = n0->vars[n0->n - 1];
3056 struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)];
3057 t->clabels_example = v0;
3059 for (size_t i = 0; i < c0->n_cats; i++)
3060 if (c0->cats[i].type == CCT_FUNCTION)
3062 msg (SE, _("%s=%s is not allowed with sorting based "
3063 "on a summary function."),
3064 subcommand_name, pos_name);
3067 if (n0->n - 1 == n0->scale_idx)
3069 msg (SE, _("%s=%s requires the variables to be moved to be categorical, "
3070 "but %s is a scale variable."),
3071 subcommand_name, pos_name, var_get_name (v0));
3075 for (size_t i = 1; i < stack->n; i++)
3077 const struct ctables_nest *ni = &stack->nests[i];
3079 const struct variable *vi = ni->vars[ni->n - 1];
3080 struct ctables_categories *ci = t->categories[var_get_dict_index (vi)];
3082 if (ni->n - 1 == ni->scale_idx)
3084 msg (SE, _("%s=%s requires the variables to be moved to be "
3085 "categorical, but %s is a scale variable."),
3086 subcommand_name, pos_name, var_get_name (vi));
3089 if (var_get_width (v0) != var_get_width (vi))
3091 msg (SE, _("%s=%s requires the variables to be "
3092 "moved to have the same width, but %s has "
3093 "width %d and %s has width %d."),
3094 subcommand_name, pos_name,
3095 var_get_name (v0), var_get_width (v0),
3096 var_get_name (vi), var_get_width (vi));
3099 if (!val_labs_equal (var_get_value_labels (v0),
3100 var_get_value_labels (vi)))
3102 msg (SE, _("%s=%s requires the variables to be "
3103 "moved to have the same value labels, but %s "
3104 "and %s have different value labels."),
3105 subcommand_name, pos_name,
3106 var_get_name (v0), var_get_name (vi));
3109 if (!ctables_categories_equal (c0, ci))
3111 msg (SE, _("%s=%s requires the variables to be "
3112 "moved to have the same category "
3113 "specifications, but %s and %s have different "
3114 "category specifications."),
3115 subcommand_name, pos_name,
3116 var_get_name (v0), var_get_name (vi));
3125 ctables_prepare_table (struct ctables_table *t)
3127 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3130 t->stacks[a] = enumerate_fts (a, t->axes[a]);
3132 for (size_t j = 0; j < t->stacks[a].n; j++)
3134 struct ctables_nest *nest = &t->stacks[a].nests[j];
3135 for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
3137 nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
3138 nest->n_domains[dt] = 0;
3140 for (size_t k = 0; k < nest->n; k++)
3142 if (k == nest->scale_idx)
3151 if (a != PIVOT_AXIS_LAYER)
3158 if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
3159 : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
3160 : a == PIVOT_AXIS_ROW)
3162 if (k == nest->n - 1
3163 || (nest->scale_idx == nest->n - 1
3164 && k == nest->n - 2))
3170 if (a == PIVOT_AXIS_COLUMN)
3175 if (a == PIVOT_AXIS_ROW)
3180 nest->domains[dt][nest->n_domains[dt]++] = k;
3187 struct ctables_nest *nest = xmalloc (sizeof *nest);
3188 *nest = (struct ctables_nest) { .n = 0 };
3189 t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
3192 struct ctables_stack *stack = &t->stacks[t->summary_axis];
3193 for (size_t i = 0; i < stack->n; i++)
3195 struct ctables_nest *nest = &stack->nests[i];
3196 if (!nest->specs[CSV_CELL].n)
3198 struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
3199 specs->specs = xmalloc (sizeof *specs->specs);
3202 enum ctables_summary_function function
3203 = specs->var ? CTSF_MEAN : CTSF_COUNT;
3204 struct ctables_var var = { .is_mrset = false, .var = specs->var };
3206 *specs->specs = (struct ctables_summary_spec) {
3207 .function = function,
3208 .format = ctables_summary_default_format (function, &var),
3209 .label = ctables_summary_default_label (function, 0),
3212 specs->var = nest->vars[0];
3214 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3215 &nest->specs[CSV_CELL]);
3217 else if (!nest->specs[CSV_TOTAL].n)
3218 ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
3219 &nest->specs[CSV_CELL]);
3222 struct ctables_summary_spec_set *merged = &t->summary_specs;
3223 struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
3225 for (size_t j = 0; j < stack->n; j++)
3227 const struct ctables_nest *nest = &stack->nests[j];
3229 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3230 items[n_left++] = (struct merge_item) { .set = &nest->specs[sv] };
3235 struct merge_item min = items[0];
3236 for (size_t j = 1; j < n_left; j++)
3237 if (merge_item_compare_3way (&items[j], &min) < 0)
3240 if (merged->n >= merged->allocated)
3241 merged->specs = x2nrealloc (merged->specs, &merged->allocated,
3242 sizeof *merged->specs);
3243 merged->specs[merged->n++] = min.set->specs[min.ofs];
3245 for (size_t j = 0; j < n_left; )
3247 if (merge_item_compare_3way (&items[j], &min) == 0)
3249 struct merge_item *item = &items[j];
3250 item->set->specs[item->ofs].axis_idx = merged->n - 1;
3251 if (++item->ofs >= item->set->n)
3253 items[j] = items[--n_left];
3262 for (size_t j = 0; j < merged->n; j++)
3263 printf ("%s\n", ctables_summary_function_name (merged->specs[j].function));
3265 for (size_t j = 0; j < stack->n; j++)
3267 const struct ctables_nest *nest = &stack->nests[j];
3268 for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
3270 const struct ctables_summary_spec_set *specs = &nest->specs[sv];
3271 for (size_t k = 0; k < specs->n; k++)
3272 printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
3273 specs->specs[k].axis_idx);
3279 return (ctables_check_label_position (t, PIVOT_AXIS_ROW)
3280 && ctables_check_label_position (t, PIVOT_AXIS_COLUMN));
3284 ctables_insert_clabels_values (struct ctables_table *t, const struct ccase *c,
3285 enum pivot_axis_type a)
3287 struct ctables_stack *stack = &t->stacks[a];
3288 for (size_t i = 0; i < stack->n; i++)
3290 const struct ctables_nest *nest = &stack->nests[i];
3291 const struct variable *var = nest->vars[nest->n - 1];
3292 int width = var_get_width (var);
3293 const union value *value = case_data (c, var);
3295 if (var_is_numeric (var) && value->f == SYSMIS)
3298 if (!ctables_categories_match (t->categories [var_get_dict_index (var)],
3302 unsigned int hash = value_hash (value, width, 0);
3304 struct ctables_value *clv = ctables_value_find__ (t, value, width, hash);
3307 clv = xmalloc (sizeof *clv);
3308 value_clone (&clv->value, value, width);
3309 hmap_insert (&t->clabels_values_map, &clv->node, hash);
3315 compare_clabels_values_3way (const void *a_, const void *b_, const void *width_)
3317 const struct ctables_value *const *ap = a_;
3318 const struct ctables_value *const *bp = b_;
3319 const struct ctables_value *a = *ap;
3320 const struct ctables_value *b = *bp;
3321 const int *width = width_;
3322 return value_compare_3way (&a->value, &b->value, *width);
3326 ctables_sort_clabels_values (struct ctables_table *t)
3328 int width = var_get_width (t->clabels_example);
3330 size_t n = hmap_count (&t->clabels_values_map);
3331 t->clabels_values = xnmalloc (n, sizeof *t->clabels_values);
3333 struct ctables_value *clv;
3335 HMAP_FOR_EACH (clv, struct ctables_value, node, &t->clabels_values_map)
3336 t->clabels_values[i++] = clv;
3337 t->n_clabels_values = n;
3340 sort (t->clabels_values, n, sizeof *t->clabels_values,
3341 compare_clabels_values_3way, &width);
3343 for (size_t i = 0; i < n; i++)
3344 t->clabels_values[i]->leaf = i;
3348 ctables_execute (struct dataset *ds, struct ctables *ct)
3350 for (size_t i = 0; i < ct->n_tables; i++)
3352 struct ctables_table *t = ct->tables[i];
3353 t->sections = xnmalloc (MAX (1, t->stacks[PIVOT_AXIS_ROW].n) *
3354 MAX (1, t->stacks[PIVOT_AXIS_COLUMN].n) *
3355 MAX (1, t->stacks[PIVOT_AXIS_LAYER].n),
3356 sizeof *t->sections);
3357 size_t ix[PIVOT_N_AXES];
3358 ctables_table_add_section (t, 0, ix);
3361 struct casereader *input = proc_open (ds);
3362 bool warn_on_invalid = true;
3363 for (struct ccase *c = casereader_read (input); c;
3364 case_unref (c), c = casereader_read (input))
3366 double d_weight = dict_get_case_weight (dataset_dict (ds), c,
3368 double e_weight = (ct->e_weight
3369 ? var_force_valid_weight (ct->e_weight,
3370 case_num (c, ct->e_weight),
3374 for (size_t i = 0; i < ct->n_tables; i++)
3376 struct ctables_table *t = ct->tables[i];
3378 for (size_t j = 0; j < t->n_sections; j++)
3379 ctables_cell_insert (&t->sections[j], c, d_weight, e_weight);
3381 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3382 if (t->label_axis[a] != a)
3383 ctables_insert_clabels_values (t, c, a);
3386 casereader_destroy (input);
3388 for (size_t i = 0; i < ct->n_tables; i++)
3390 struct ctables_table *t = ct->tables[i];
3392 if (t->clabels_example)
3393 ctables_sort_clabels_values (t);
3395 ctables_table_output (ct, ct->tables[i]);
3397 return proc_commit (ds);
3401 cmd_ctables (struct lexer *lexer, struct dataset *ds)
3403 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3404 enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
3405 enum settings_value_show tvars = settings_get_show_variables ();
3406 for (size_t i = 0; i < n_vars; i++)
3407 vlabels[i] = (enum ctables_vlabel) tvars;
3409 struct ctables *ct = xmalloc (sizeof *ct);
3410 *ct = (struct ctables) {
3411 .look = pivot_table_look_unshare (pivot_table_look_ref (
3412 pivot_table_look_get_default ())),
3414 .hide_threshold = 5,
3416 ct->look->omit_empty = false;
3418 if (!lex_force_match (lexer, T_SLASH))
3421 while (!lex_match_id (lexer, "TABLE"))
3423 if (lex_match_id (lexer, "FORMAT"))
3425 double widths[2] = { SYSMIS, SYSMIS };
3426 double units_per_inch = 72.0;
3428 while (lex_token (lexer) != T_SLASH)
3430 if (lex_match_id (lexer, "MINCOLWIDTH"))
3432 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
3435 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
3437 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
3440 else if (lex_match_id (lexer, "UNITS"))
3442 lex_match (lexer, T_EQUALS);
3443 if (lex_match_id (lexer, "POINTS"))
3444 units_per_inch = 72.0;
3445 else if (lex_match_id (lexer, "INCHES"))
3446 units_per_inch = 1.0;
3447 else if (lex_match_id (lexer, "CM"))
3448 units_per_inch = 2.54;
3451 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
3455 else if (lex_match_id (lexer, "EMPTY"))
3460 lex_match (lexer, T_EQUALS);
3461 if (lex_match_id (lexer, "ZERO"))
3463 /* Nothing to do. */
3465 else if (lex_match_id (lexer, "BLANK"))
3466 ct->zero = xstrdup ("");
3467 else if (lex_force_string (lexer))
3469 ct->zero = ss_xstrdup (lex_tokss (lexer));
3475 else if (lex_match_id (lexer, "MISSING"))
3477 lex_match (lexer, T_EQUALS);
3478 if (!lex_force_string (lexer))
3482 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
3483 ? ss_xstrdup (lex_tokss (lexer))
3489 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
3490 "UNITS", "EMPTY", "MISSING");
3495 if (widths[0] != SYSMIS && widths[1] != SYSMIS
3496 && widths[0] > widths[1])
3498 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
3502 for (size_t i = 0; i < 2; i++)
3503 if (widths[i] != SYSMIS)
3505 int *wr = ct->look->width_ranges[TABLE_HORZ];
3506 wr[i] = widths[i] / units_per_inch * 96.0;
3511 else if (lex_match_id (lexer, "VLABELS"))
3513 if (!lex_force_match_id (lexer, "VARIABLES"))
3515 lex_match (lexer, T_EQUALS);
3517 struct variable **vars;
3519 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
3523 if (!lex_force_match_id (lexer, "DISPLAY"))
3528 lex_match (lexer, T_EQUALS);
3530 enum ctables_vlabel vlabel;
3531 if (lex_match_id (lexer, "DEFAULT"))
3532 vlabel = (enum ctables_vlabel) settings_get_show_variables ();
3533 else if (lex_match_id (lexer, "NAME"))
3535 else if (lex_match_id (lexer, "LABEL"))
3536 vlabel = CTVL_LABEL;
3537 else if (lex_match_id (lexer, "BOTH"))
3539 else if (lex_match_id (lexer, "NONE"))
3543 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
3549 for (size_t i = 0; i < n_vars; i++)
3550 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
3553 else if (lex_match_id (lexer, "MRSETS"))
3555 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
3557 lex_match (lexer, T_EQUALS);
3558 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
3561 else if (lex_match_id (lexer, "SMISSING"))
3563 if (lex_match_id (lexer, "VARIABLE"))
3564 ct->smissing_listwise = false;
3565 else if (lex_match_id (lexer, "LISTWISE"))
3566 ct->smissing_listwise = true;
3569 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
3574 else if (lex_match_id (lexer, "WEIGHT"))
3576 if (!lex_force_match_id (lexer, "VARIABLE"))
3578 lex_match (lexer, T_EQUALS);
3579 ct->e_weight = parse_variable (lexer, dataset_dict (ds));
3583 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
3585 if (!lex_force_match_id (lexer, "COUNT"))
3587 lex_match (lexer, T_EQUALS);
3588 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
3590 ct->hide_threshold = lex_integer (lexer);
3595 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
3596 "SMISSING", "PCOMPUTE", "PPROPERTIES",
3597 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
3601 if (!lex_force_match (lexer, T_SLASH))
3605 size_t allocated_tables = 0;
3608 if (ct->n_tables >= allocated_tables)
3609 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
3610 sizeof *ct->tables);
3612 struct ctables_category *cat = xmalloc (sizeof *cat);
3613 *cat = (struct ctables_category) {
3615 .include_missing = false,
3616 .sort_ascending = true,
3619 struct ctables_categories *c = xmalloc (sizeof *c);
3620 size_t n_vars = dict_get_n_vars (dataset_dict (ds));
3621 *c = (struct ctables_categories) {
3627 struct ctables_categories **categories = xnmalloc (n_vars,
3628 sizeof *categories);
3629 for (size_t i = 0; i < n_vars; i++)
3632 struct ctables_table *t = xmalloc (sizeof *t);
3633 *t = (struct ctables_table) {
3634 .slabels_axis = PIVOT_AXIS_COLUMN,
3635 .slabels_visible = true,
3636 .clabels_values_map = HMAP_INITIALIZER (t->clabels_values_map),
3638 [PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW,
3639 [PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN,
3640 [PIVOT_AXIS_LAYER] = PIVOT_AXIS_LAYER,
3642 .clabels_from_axis = PIVOT_AXIS_LAYER,
3643 .categories = categories,
3644 .n_categories = n_vars,
3647 ct->tables[ct->n_tables++] = t;
3649 lex_match (lexer, T_EQUALS);
3650 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
3652 if (lex_match (lexer, T_BY))
3654 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3655 ct, t, PIVOT_AXIS_COLUMN))
3658 if (lex_match (lexer, T_BY))
3660 if (!ctables_axis_parse (lexer, dataset_dict (ds),
3661 ct, t, PIVOT_AXIS_LAYER))
3666 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
3667 && !t->axes[PIVOT_AXIS_LAYER])
3669 lex_error (lexer, _("At least one variable must be specified."));
3673 const struct ctables_axis *scales[PIVOT_N_AXES];
3674 size_t n_scales = 0;
3675 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3677 scales[a] = find_scale (t->axes[a]);
3683 msg (SE, _("Scale variables may appear only on one axis."));
3684 if (scales[PIVOT_AXIS_ROW])
3685 msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
3686 _("This scale variable appears on the rows axis."));
3687 if (scales[PIVOT_AXIS_COLUMN])
3688 msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
3689 _("This scale variable appears on the columns axis."));
3690 if (scales[PIVOT_AXIS_LAYER])
3691 msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
3692 _("This scale variable appears on the layer axis."));
3696 const struct ctables_axis *summaries[PIVOT_N_AXES];
3697 size_t n_summaries = 0;
3698 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3700 summaries[a] = (scales[a]
3702 : find_categorical_summary_spec (t->axes[a]));
3706 if (n_summaries > 1)
3708 msg (SE, _("Summaries may appear only on one axis."));
3709 if (summaries[PIVOT_AXIS_ROW])
3710 msg_at (SN, summaries[PIVOT_AXIS_ROW]->loc,
3711 _("This variable on the rows axis has a summary."));
3712 if (summaries[PIVOT_AXIS_COLUMN])
3713 msg_at (SN, summaries[PIVOT_AXIS_COLUMN]->loc,
3714 _("This variable on the columns axis has a summary."));
3715 if (summaries[PIVOT_AXIS_LAYER])
3716 msg_at (SN, summaries[PIVOT_AXIS_LAYER]->loc,
3717 _("This variable on the layers axis has a summary."));
3720 for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
3721 if (n_summaries ? summaries[a] : t->axes[a])
3723 t->summary_axis = a;
3727 if (lex_token (lexer) == T_ENDCMD)
3729 if (!ctables_prepare_table (t))
3733 if (!lex_force_match (lexer, T_SLASH))
3736 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
3738 if (lex_match_id (lexer, "SLABELS"))
3740 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3742 if (lex_match_id (lexer, "POSITION"))
3744 lex_match (lexer, T_EQUALS);
3745 if (lex_match_id (lexer, "COLUMN"))
3746 t->slabels_axis = PIVOT_AXIS_COLUMN;
3747 else if (lex_match_id (lexer, "ROW"))
3748 t->slabels_axis = PIVOT_AXIS_ROW;
3749 else if (lex_match_id (lexer, "LAYER"))
3750 t->slabels_axis = PIVOT_AXIS_LAYER;
3753 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
3757 else if (lex_match_id (lexer, "VISIBLE"))
3759 lex_match (lexer, T_EQUALS);
3760 if (!parse_bool (lexer, &t->slabels_visible))
3765 lex_error_expecting (lexer, "POSITION", "VISIBLE");
3770 else if (lex_match_id (lexer, "CLABELS"))
3772 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
3774 if (lex_match_id (lexer, "AUTO"))
3776 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW;
3777 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN;
3779 else if (lex_match_id (lexer, "ROWLABELS"))
3781 lex_match (lexer, T_EQUALS);
3782 if (lex_match_id (lexer, "OPPOSITE"))
3783 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_COLUMN;
3784 else if (lex_match_id (lexer, "LAYER"))
3785 t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_LAYER;
3788 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3792 else if (lex_match_id (lexer, "COLLABELS"))
3794 lex_match (lexer, T_EQUALS);
3795 if (lex_match_id (lexer, "OPPOSITE"))
3796 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_ROW;
3797 else if (lex_match_id (lexer, "LAYER"))
3798 t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_LAYER;
3801 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
3807 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
3813 else if (lex_match_id (lexer, "CRITERIA"))
3815 if (!lex_force_match_id (lexer, "CILEVEL"))
3817 lex_match (lexer, T_EQUALS);
3819 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
3821 t->cilevel = lex_number (lexer);
3824 else if (lex_match_id (lexer, "CATEGORIES"))
3826 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
3829 else if (lex_match_id (lexer, "TITLES"))
3834 if (lex_match_id (lexer, "CAPTION"))
3835 textp = &t->caption;
3836 else if (lex_match_id (lexer, "CORNER"))
3838 else if (lex_match_id (lexer, "TITLE"))
3842 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
3845 lex_match (lexer, T_EQUALS);
3847 struct string s = DS_EMPTY_INITIALIZER;
3848 while (lex_is_string (lexer))
3850 if (!ds_is_empty (&s))
3851 ds_put_byte (&s, ' ');
3852 ds_put_substring (&s, lex_tokss (lexer));
3856 *textp = ds_steal_cstr (&s);
3858 while (lex_token (lexer) != T_SLASH
3859 && lex_token (lexer) != T_ENDCMD);
3861 else if (lex_match_id (lexer, "SIGTEST"))
3865 t->chisq = xmalloc (sizeof *t->chisq);
3866 *t->chisq = (struct ctables_chisq) {
3868 .include_mrsets = true,
3869 .all_visible = true,
3875 if (lex_match_id (lexer, "TYPE"))
3877 lex_match (lexer, T_EQUALS);
3878 if (!lex_force_match_id (lexer, "CHISQUARE"))
3881 else if (lex_match_id (lexer, "ALPHA"))
3883 lex_match (lexer, T_EQUALS);
3884 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
3886 t->chisq->alpha = lex_number (lexer);
3889 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3891 lex_match (lexer, T_EQUALS);
3892 if (parse_bool (lexer, &t->chisq->include_mrsets))
3895 else if (lex_match_id (lexer, "CATEGORIES"))
3897 lex_match (lexer, T_EQUALS);
3898 if (lex_match_id (lexer, "ALLVISIBLE"))
3899 t->chisq->all_visible = true;
3900 else if (lex_match_id (lexer, "SUBTOTALS"))
3901 t->chisq->all_visible = false;
3904 lex_error_expecting (lexer,
3905 "ALLVISIBLE", "SUBTOTALS");
3911 lex_error_expecting (lexer, "TYPE", "ALPHA",
3912 "INCLUDEMRSETS", "CATEGORIES");
3916 while (lex_token (lexer) != T_SLASH
3917 && lex_token (lexer) != T_ENDCMD);
3919 else if (lex_match_id (lexer, "COMPARETEST"))
3923 t->pairwise = xmalloc (sizeof *t->pairwise);
3924 *t->pairwise = (struct ctables_pairwise) {
3926 .alpha = { .05, .05 },
3927 .adjust = BONFERRONI,
3928 .include_mrsets = true,
3929 .meansvariance_allcats = true,
3930 .all_visible = true,
3939 if (lex_match_id (lexer, "TYPE"))
3941 lex_match (lexer, T_EQUALS);
3942 if (lex_match_id (lexer, "PROP"))
3943 t->pairwise->type = PROP;
3944 else if (lex_match_id (lexer, "MEAN"))
3945 t->pairwise->type = MEAN;
3948 lex_error_expecting (lexer, "PROP", "MEAN");
3952 else if (lex_match_id (lexer, "ALPHA"))
3954 lex_match (lexer, T_EQUALS);
3956 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3958 double a0 = lex_number (lexer);
3961 lex_match (lexer, T_COMMA);
3962 if (lex_is_number (lexer))
3964 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
3966 double a1 = lex_number (lexer);
3969 t->pairwise->alpha[0] = MIN (a0, a1);
3970 t->pairwise->alpha[1] = MAX (a0, a1);
3973 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
3975 else if (lex_match_id (lexer, "ADJUST"))
3977 lex_match (lexer, T_EQUALS);
3978 if (lex_match_id (lexer, "BONFERRONI"))
3979 t->pairwise->adjust = BONFERRONI;
3980 else if (lex_match_id (lexer, "BH"))
3981 t->pairwise->adjust = BH;
3982 else if (lex_match_id (lexer, "NONE"))
3983 t->pairwise->adjust = 0;
3986 lex_error_expecting (lexer, "BONFERRONI", "BH",
3991 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
3993 lex_match (lexer, T_EQUALS);
3994 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
3997 else if (lex_match_id (lexer, "MEANSVARIANCE"))
3999 lex_match (lexer, T_EQUALS);
4000 if (lex_match_id (lexer, "ALLCATS"))
4001 t->pairwise->meansvariance_allcats = true;
4002 else if (lex_match_id (lexer, "TESTEDCATS"))
4003 t->pairwise->meansvariance_allcats = false;
4006 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
4010 else if (lex_match_id (lexer, "CATEGORIES"))
4012 lex_match (lexer, T_EQUALS);
4013 if (lex_match_id (lexer, "ALLVISIBLE"))
4014 t->pairwise->all_visible = true;
4015 else if (lex_match_id (lexer, "SUBTOTALS"))
4016 t->pairwise->all_visible = false;
4019 lex_error_expecting (lexer, "ALLVISIBLE",
4024 else if (lex_match_id (lexer, "MERGE"))
4026 lex_match (lexer, T_EQUALS);
4027 if (!parse_bool (lexer, &t->pairwise->merge))
4030 else if (lex_match_id (lexer, "STYLE"))
4032 lex_match (lexer, T_EQUALS);
4033 if (lex_match_id (lexer, "APA"))
4034 t->pairwise->apa_style = true;
4035 else if (lex_match_id (lexer, "SIMPLE"))
4036 t->pairwise->apa_style = false;
4039 lex_error_expecting (lexer, "APA", "SIMPLE");
4043 else if (lex_match_id (lexer, "SHOWSIG"))
4045 lex_match (lexer, T_EQUALS);
4046 if (!parse_bool (lexer, &t->pairwise->show_sig))
4051 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
4052 "INCLUDEMRSETS", "MEANSVARIANCE",
4053 "CATEGORIES", "MERGE", "STYLE",
4058 while (lex_token (lexer) != T_SLASH
4059 && lex_token (lexer) != T_ENDCMD);
4063 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
4064 "CRITERIA", "CATEGORIES", "TITLES",
4065 "SIGTEST", "COMPARETEST");
4069 if (!lex_match (lexer, T_SLASH))
4073 if (t->label_axis[PIVOT_AXIS_ROW] != PIVOT_AXIS_ROW
4074 && t->label_axis[PIVOT_AXIS_COLUMN] != PIVOT_AXIS_COLUMN)
4076 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
4080 if (!ctables_prepare_table (t))
4083 while (lex_token (lexer) != T_ENDCMD);
4085 bool ok = ctables_execute (ds, ct);
4086 ctables_destroy (ct);
4087 return ok ? CMD_SUCCESS : CMD_FAILURE;
4090 ctables_destroy (ct);