1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2019 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/>. */
19 #include "data/case.h"
20 #include "data/casegrouper.h"
21 #include "data/casereader.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/format.h"
25 #include "data/variable.h"
27 #include "libpspp/hmap.h"
28 #include "libpspp/bt.h"
29 #include "libpspp/hash-functions.h"
30 #include "libpspp/misc.h"
31 #include "libpspp/pool.h"
33 #include "language/command.h"
35 #include "count-one-bits.h"
36 #include "count-leading-zeros.h"
38 #include "output/pivot-table.h"
44 #define _(msgid) gettext (msgid)
45 #define N_(msgid) (msgid)
48 /* A "cell" in this procedure represents a distinct value of the
49 procedure's categorical variables, and a set of summary statistics
50 of all cases which whose categorical variables have that set of
51 values. For example, the dataset
60 has three cells in layer 0 and two cells in layer 1 in addition
61 to a "grand summary" cell to which all (non-missing) cases
64 The cells form a n-ary tree structure with the "grand summary"
69 struct hmap_node hmap_node; /* Element in hash table. */
70 struct bt_node bt_node; /* Element in binary tree */
73 struct cell_container *children;
75 /* The statistics to be calculated for the cell. */
76 struct statistic **stat;
78 /* The parent of this cell, or NULL if this is the root cell. */
79 const struct cell *parent_cell;
81 /* A bit-field variable which indicates which control variables
82 are allocated a fixed value (for this cell), and which are
85 A one indicates a fixed value. A zero indicates a wildcard.
86 Wildcard values are used to calculate totals and sub-totals.
88 unsigned int not_wild;
93 /* The variables corresponding to the above values. */
94 const struct variable **vars;
97 /* A structure used to find the union of all values used
98 within a layer, and to sort those values. */
101 struct hmap_node hmap_node; /* Element in hash table. */
102 struct bt_node bt_node; /* Element in binary tree */
104 /* A unique, consecutive, zero based index identifying this
108 /* The top level value of this instance. */
110 const struct variable *var;
115 destroy_workspace (const struct mtable *mt, struct workspace *ws)
117 for (int l = 0; l < mt->n_layers; ++l)
119 struct cell_container *instances = ws->instances + l;
120 struct instance *inst;
121 struct instance *next;
122 HMAP_FOR_EACH_SAFE (inst, next, struct instance, hmap_node,
125 int width = var_get_width (inst->var);
126 value_destroy (&inst->value, width);
129 hmap_destroy (&instances->map);
131 free (ws->control_idx);
132 free (ws->instances);
137 destroy_cell (const struct means *means,
138 const struct mtable *mt, struct cell *cell)
141 for (int i = 0; i < mt->n_layers; ++i)
143 if (0 == ((cell->not_wild >> i) & 0x1))
146 const struct layer *layer = mt->layers[i];
147 for (int cmb = 0; cmb < mt->n_combinations; ++cmb)
149 struct workspace *ws = mt->ws + cmb;
150 const struct variable *var
151 = layer->factor_vars[ws->control_idx[i]];
153 int width = var_get_width (var);
154 value_destroy (&cell->values[idx++], width);
157 for (int i = 0; i < cell->n_children; ++i)
159 struct cell_container *container = cell->children + i;
160 hmap_destroy (&container->map);
163 for (int v = 0; v < mt->n_dep_vars; ++v)
165 for (int s = 0; s < means->n_statistics; ++s)
167 stat_destroy *des = cell_spec[means->statistics[s]].sf;
168 des (cell->stat[s + v * means->n_statistics]);
173 free (cell->children);
180 /* Walk the tree in postorder starting from CELL and destroy all the
183 means_destroy_cells (const struct means *means, struct cell *cell,
184 const struct mtable *table)
186 for (int i = 0; i < cell->n_children; ++i)
188 struct cell_container *container = cell->children + i;
189 struct cell *sub_cell;
191 HMAP_FOR_EACH_SAFE (sub_cell, next, struct cell, hmap_node,
194 means_destroy_cells (means, sub_cell, table);
198 destroy_cell (means, table, cell);
204 dump_cell (const struct cell *cell, const struct mtable *mt, int level)
206 for (int l = 0; l < level; ++l)
208 printf ("%p: ", cell);
209 for (int i = 0; i < mt->n_layers; ++i)
211 putchar (((cell->not_wild >> i) & 0x1) ? 'w' : '.');
215 for (int i = 0; i < mt->n_layers; ++i)
217 if ((cell->not_wild >> i) & 0x1)
219 printf ("%s: ", var_get_name (cell->vars[x]));
220 printf ("%g ", cell->values[x++].f);
225 stat_get *sg = cell_spec[MEANS_N].sd;
226 printf ("--- S1: %g", sg (cell->stat[0]));
228 printf ("--- N Children: %d", cell->n_children);
229 // printf ("--- Level: %d", level);
230 printf ("--- Parent: %p", cell->parent_cell);
235 dump_indeces (const size_t *indexes, int n)
237 for (int i = 0 ; i < n; ++i)
239 printf ("%ld; ", indexes[i]);
244 /* Dump the tree in pre-order. */
246 dump_tree (const struct cell *cell, const struct mtable *table,
247 int level, const struct cell *parent)
249 assert (cell->parent_cell == parent);
250 dump_cell (cell, table, level);
252 for (int i = 0; i < cell->n_children; ++i)
254 struct cell_container *container = cell->children + i;
255 struct cell *sub_cell;
256 BT_FOR_EACH (sub_cell, struct cell, bt_node, &container->bt)
258 dump_tree (sub_cell, table, level + 1, cell);
265 /* Generate a hash based on the values of the N variables in
266 the array VARS which are taken from the case C. */
268 generate_hash (const struct mtable *mt,
269 const struct ccase *c,
270 unsigned int not_wild,
271 const struct workspace *ws)
273 unsigned int hash = 0;
274 for (int i = 0; i < mt->n_layers; ++i)
276 if (0 == ((not_wild >> i) & 0x1))
279 const struct layer *layer = mt->layers[i];
280 const struct variable *var = layer->factor_vars[ws->control_idx[i]];
281 const union value *vv = case_data (c, var);
282 int width = var_get_width (var);
283 hash = hash_int (i, hash);
284 hash = value_hash (vv, width, hash);
290 /* Create a cell based on the N variables in the array VARS,
291 which are indeces into the case C.
292 The caller is responsible for destroying this cell when
295 generate_cell (const struct means *means,
296 const struct mtable *mt,
297 const struct ccase *c,
298 unsigned int not_wild,
299 const struct cell *pcell,
300 const struct workspace *ws)
302 int n_vars = count_one_bits (not_wild);
303 struct cell *cell = XZALLOC (struct cell);
304 cell->values = xcalloc (n_vars, sizeof *cell->values);
305 cell->vars = xcalloc (n_vars, sizeof *cell->vars);
306 cell->not_wild = not_wild;
308 cell->parent_cell = pcell;
309 cell->n_children = mt->n_layers -
310 (sizeof (cell->not_wild) * CHAR_BIT) +
311 count_leading_zeros (cell->not_wild);
314 for (int i = 0; i < mt->n_layers; ++i)
316 if (0 == ((not_wild >> i) & 0x1))
319 const struct layer *layer = mt->layers[i];
320 const struct variable *var = layer->factor_vars[ws->control_idx[i]];
321 const union value *vv = case_data (c, var);
322 int width = var_get_width (var);
323 cell->vars[idx] = var;
324 value_clone (&cell->values[idx++], vv, width);
326 assert (idx == n_vars);
328 cell->children = xcalloc (cell->n_children, sizeof *cell->children);
329 for (int i = 0; i < cell->n_children; ++i)
331 struct cell_container *container = cell->children + i;
332 hmap_init (&container->map);
335 cell->stat = xcalloc (means->n_statistics * mt->n_dep_vars, sizeof *cell->stat);
336 for (int v = 0; v < mt->n_dep_vars; ++v)
338 for (int stat = 0; stat < means->n_statistics; ++stat)
340 stat_create *sc = cell_spec[means->statistics[stat]].sc;
342 cell->stat[stat + v * means->n_statistics] = sc (means->pool);
349 /* If a cell based on the N variables in the array VARS,
350 which are indeces into the case C and whose hash is HASH,
351 exists in HMAP, then return that cell.
352 Otherwise, return NULL. */
354 lookup_cell (const struct mtable *mt,
355 struct hmap *hmap, unsigned int hash,
356 const struct ccase *c,
357 unsigned int not_wild,
358 const struct workspace *ws)
360 struct cell *cell = NULL;
361 HMAP_FOR_EACH_WITH_HASH (cell, struct cell, hmap_node, hash, hmap)
365 if (cell->not_wild != not_wild)
367 for (int i = 0; i < mt->n_layers; ++i)
369 if (0 == ((cell->not_wild >> i) & 0x1))
372 const struct layer *layer = mt->layers[i];
373 const struct variable *var = layer->factor_vars[ws->control_idx[i]];
374 const union value *vv = case_data (c, var);
375 int width = var_get_width (var);
376 assert (var == cell->vars[idx]);
377 if (!value_equal (vv, &cell->values[idx++], width))
390 /* A comparison function used to sort cells in a binary tree.
391 Only the innermost value needs to be compared, because no
392 two cells with similar outer values will appear in the same
395 cell_compare_3way (const struct bt_node *a,
396 const struct bt_node *b,
397 const void *aux UNUSED)
399 const struct cell *fa = BT_DATA (a, struct cell, bt_node);
400 const struct cell *fb = BT_DATA (b, struct cell, bt_node);
402 assert (fa->not_wild == fb->not_wild);
403 int vidx = count_one_bits (fa->not_wild) - 1;
404 assert (fa->vars[vidx] == fb->vars[vidx]);
406 return value_compare_3way (&fa->values[vidx],
408 var_get_width (fa->vars[vidx]));
411 /* A comparison function used to sort cells in a binary tree. */
413 compare_instance_3way (const struct bt_node *a,
414 const struct bt_node *b,
415 const void *aux UNUSED)
417 const struct instance *fa = BT_DATA (a, struct instance, bt_node);
418 const struct instance *fb = BT_DATA (b, struct instance, bt_node);
420 assert (fa->var == fb->var);
422 return value_compare_3way (&fa->value,
424 var_get_width (fa->var));
428 static void arrange_cells (struct workspace *ws,
429 struct cell *cell, const struct mtable *table);
432 /* Iterate CONTAINER's map inserting a copy of its elements into
433 CONTAINER's binary tree. Also, for each layer in TABLE, create
434 an instance container, containing the union of all elements in
437 arrange_cell (struct workspace *ws, struct cell_container *container,
438 const struct mtable *mt)
440 struct bt *bt = &container->bt;
441 struct hmap *map = &container->map;
442 bt_init (bt, cell_compare_3way, NULL);
445 HMAP_FOR_EACH (cell, struct cell, hmap_node, map)
447 bt_insert (bt, &cell->bt_node);
450 for (int i = 0; i < mt->n_layers; ++i)
452 if (0 == ((cell->not_wild >> i) & 0x1))
455 struct cell_container *instances = ws->instances + i;
456 const struct variable *var = cell->vars[idx];
457 int width = var_get_width (var);
459 = value_hash (&cell->values[idx], width, 0);
461 struct instance *inst = NULL;
462 struct instance *next = NULL;
463 HMAP_FOR_EACH_WITH_HASH_SAFE (inst, next, struct instance,
465 hash, &instances->map)
467 assert (cell->vars[idx] == var);
468 if (value_equal (&inst->value,
478 inst = xzalloc (sizeof *inst);
481 value_clone (&inst->value, &cell->values[idx],
483 hmap_insert (&instances->map, &inst->hmap_node, hash);
489 arrange_cells (ws, cell, mt);
493 /* Arrange the children and then all the subtotals. */
495 arrange_cells (struct workspace *ws, struct cell *cell,
496 const struct mtable *table)
498 for (int i = 0; i < cell->n_children; ++i)
500 struct cell_container *container = cell->children + i;
501 arrange_cell (ws, container, table);
508 /* If the top level value in CELL, has an instance in the L_IDX'th layer,
509 then return that instance. Otherwise return NULL. */
510 static const struct instance *
511 lookup_instance (const struct mtable *mt, const struct workspace *ws,
512 int l_idx, const struct cell *cell)
514 const struct layer *layer = mt->layers[l_idx];
515 int n_vals = count_one_bits (cell->not_wild);
516 const struct variable *var = layer->factor_vars[ws->control_idx[l_idx]];
517 const union value *val = cell->values + n_vals - 1;
518 int width = var_get_width (var);
519 unsigned int hash = value_hash (val, width, 0);
520 const struct cell_container *instances = ws->instances + l_idx;
521 struct instance *inst = NULL;
522 struct instance *next;
523 HMAP_FOR_EACH_WITH_HASH_SAFE (inst, next,
524 struct instance, hmap_node,
525 hash, &instances->map)
527 if (value_equal (val, &inst->value, width))
533 /* Enter the values into PT. */
535 populate_table (const struct means *means, const struct mtable *mt,
536 const struct workspace *ws,
537 const struct cell *cell,
538 struct pivot_table *pt)
540 size_t *indexes = XCALLOC (pt->n_dimensions, size_t);
541 for (int v = 0; v < mt->n_dep_vars; ++v)
543 for (int s = 0; s < means->n_statistics; ++s)
546 if (mt->n_dep_vars > 1)
549 int stat = means->statistics[s];
550 stat_get *sg = cell_spec[stat].sd;
552 const struct cell *pc = cell;
553 for (; i < pt->n_dimensions; ++i)
555 int l_idx = pt->n_dimensions - i - 1;
556 const struct cell_container *instances = ws->instances + l_idx;
557 if (0 == (cell->not_wild >> l_idx & 0x1U))
559 indexes [i] = hmap_count (&instances->map);
564 const struct instance *inst
565 = lookup_instance (mt, ws, l_idx, pc);
567 indexes [i] = inst->index;
568 pc = pc->parent_cell;
573 int idx = s + v * means->n_statistics;
574 struct pivot_value *pv
575 = pivot_value_new_number (sg (cell->stat[idx]));
576 if (NULL == cell_spec[stat].rc)
578 const struct variable *dv = mt->dep_vars[v];
579 pv->numeric.format = * var_get_print_format (dv);
581 pivot_table_put (pt, indexes, pt->n_dimensions, pv);
586 for (int i = 0; i < cell->n_children; ++i)
588 struct cell_container *container = cell->children + i;
589 struct cell *child = NULL;
590 BT_FOR_EACH (child, struct cell, bt_node, &container->bt)
592 populate_table (means, mt, ws, child, pt);
598 create_table_structure (const struct mtable *mt, struct pivot_table *pt,
599 const struct workspace *ws)
601 int * lindexes = ws->control_idx;
602 /* The inner layers are situated rightmost in the table.
603 So this iteration is in reverse order. */
604 for (int l = mt->n_layers -1; l >=0 ; --l)
606 const struct layer *layer = mt->layers[l];
607 const struct cell_container *instances = ws->instances + l;
608 const struct variable *var = layer->factor_vars[lindexes[l]];
609 struct pivot_dimension *dim_layer
610 = pivot_dimension_create (pt, PIVOT_AXIS_ROW,
611 var_to_string (var));
612 dim_layer->root->show_label = true;
614 /* Place the values of the control variables as table headings. */
616 struct instance *inst = NULL;
617 BT_FOR_EACH (inst, struct instance, bt_node, &instances->bt)
619 struct substring space = SS_LITERAL_INITIALIZER ("\t ");
621 ds_init_empty (&str);
622 var_append_value_name (var,
626 ds_ltrim (&str, space);
628 pivot_category_create_leaf (dim_layer->root,
629 pivot_value_new_text (ds_cstr (&str)));
635 pivot_category_create_leaf (dim_layer->root,
636 pivot_value_new_text ("Total"));
640 /* Initialise C_DES with a string describing the control variable
641 relating to MT, LINDEXES. */
643 layers_to_string (const struct mtable *mt, const int *lindexes,
644 struct string *c_des)
646 for (int l = 0; l < mt->n_layers; ++l)
648 const struct layer *layer = mt->layers[l];
649 const struct variable *ctrl_var = layer->factor_vars[lindexes[l]];
651 ds_put_cstr (c_des, " * ");
652 ds_put_cstr (c_des, var_get_name (ctrl_var));
657 populate_case_processing_summary (struct pivot_category *pc,
658 const struct mtable *mt,
664 for (l = 0; l < mt->n_layers; ++l)
666 const struct layer *layer = mt->layers[l];
667 const struct variable *ctrl_var = layer->factor_vars[lindexes[l]];
669 ds_put_cstr (&ds, " * ");
670 ds_put_cstr (&ds, var_get_name (ctrl_var));
672 for (int dv = 0; dv < mt->n_dep_vars; ++dv)
675 ds_init_empty (&dss);
676 ds_put_cstr (&dss, var_get_name (mt->dep_vars[dv]));
677 if (mt->n_layers > 0)
679 ds_put_cstr (&dss, " * ");
680 ds_put_substring (&dss, ds.ss);
682 pivot_category_create_leaf (pc,
683 pivot_value_new_text (ds_cstr (&dss)));
690 /* Create the "Case Processing Summary" table. */
692 means_case_processing_summary (const struct mtable *mt)
694 struct pivot_table *pt = pivot_table_create (N_("Case Processing Summary"));
696 struct pivot_dimension *dim_cases =
697 pivot_dimension_create (pt, PIVOT_AXIS_COLUMN, N_("Cases"));
698 dim_cases->root->show_label = true;
700 struct pivot_category *cats[3];
701 cats[0] = pivot_category_create_group (dim_cases->root,
702 N_("Included"), NULL);
703 cats[1] = pivot_category_create_group (dim_cases->root,
704 N_("Excluded"), NULL);
705 cats[2] = pivot_category_create_group (dim_cases->root,
707 for (int i = 0; i < 3; ++i)
709 pivot_category_create_leaf_rc (cats[i],
710 pivot_value_new_text (N_("N")),
712 pivot_category_create_leaf_rc (cats[i],
713 pivot_value_new_text (N_("Percent")),
717 struct pivot_dimension *rows =
718 pivot_dimension_create (pt, PIVOT_AXIS_ROW, N_("Variables"));
720 for (int cmb = 0; cmb < mt->n_combinations; ++cmb)
722 const struct workspace *ws = mt->ws + cmb;
723 populate_case_processing_summary (rows->root, mt, ws->control_idx);
724 for (int dv = 0; dv < mt->n_dep_vars; ++dv)
726 int idx = cmb * mt->n_dep_vars + dv;
727 const struct summary *summ = mt->summ + idx;
728 double n_included = summ->n_total - summ->n_missing;
729 pivot_table_put2 (pt, 5, idx,
730 pivot_value_new_number (100.0 * summ->n_total / summ->n_total));
731 pivot_table_put2 (pt, 4, idx,
732 pivot_value_new_number (summ->n_total));
734 pivot_table_put2 (pt, 3, idx,
735 pivot_value_new_number (100.0 * summ->n_missing / summ->n_total));
736 pivot_table_put2 (pt, 2, idx,
737 pivot_value_new_number (summ->n_missing));
739 pivot_table_put2 (pt, 1, idx,
740 pivot_value_new_number (100.0 * n_included / summ->n_total));
741 pivot_table_put2 (pt, 0, idx,
742 pivot_value_new_number (n_included));
746 pivot_table_submit (pt);
750 means_shipout_single (const struct mtable *mt, const struct means *means,
751 const struct workspace *ws)
753 struct pivot_table *pt = pivot_table_create (N_("Report"));
755 struct pivot_dimension *dim_cells =
756 pivot_dimension_create (pt, PIVOT_AXIS_COLUMN, N_("Statistics"));
758 /* Set the statistics headings, eg "Mean", "Std. Dev" etc. */
759 for (int i = 0; i < means->n_statistics; ++i)
761 const struct cell_spec *cs = cell_spec + means->statistics[i];
762 pivot_category_create_leaf_rc
764 pivot_value_new_text (gettext (cs->title)), cs->rc);
767 create_table_structure (mt, pt, ws);
768 populate_table (means, mt, ws, ws->root_cell, pt);
769 pivot_table_submit (pt);
774 means_shipout_multivar (const struct mtable *mt, const struct means *means,
775 const struct workspace *ws)
778 ds_init_empty (&dss);
779 for (int dv = 0; dv < mt->n_dep_vars; ++dv)
782 ds_put_cstr (&dss, " * ");
783 ds_put_cstr (&dss, var_get_name (mt->dep_vars[dv]));
786 for (int l = 0; l < mt->n_layers; ++l)
788 ds_put_cstr (&dss, " * ");
789 const struct layer *layer = mt->layers[l];
790 const struct variable *var = layer->factor_vars[ws->control_idx[l]];
791 ds_put_cstr (&dss, var_get_name (var));
794 struct pivot_table *pt = pivot_table_create (ds_cstr (&dss));
797 struct pivot_dimension *dim_cells =
798 pivot_dimension_create (pt, PIVOT_AXIS_COLUMN, N_("Variables"));
800 for (int i = 0; i < mt->n_dep_vars; ++i)
802 pivot_category_create_leaf
804 pivot_value_new_variable (mt->dep_vars[i]));
807 struct pivot_dimension *dim_stats
808 = pivot_dimension_create (pt, PIVOT_AXIS_ROW,
810 dim_stats->root->show_label = false;
812 for (int i = 0; i < means->n_statistics; ++i)
814 const struct cell_spec *cs = cell_spec + means->statistics[i];
815 pivot_category_create_leaf_rc
817 pivot_value_new_text (gettext (cs->title)), cs->rc);
820 create_table_structure (mt, pt, ws);
821 populate_table (means, mt, ws, ws->root_cell, pt);
822 pivot_table_submit (pt);
826 means_shipout (const struct mtable *mt, const struct means *means)
828 for (int cmb = 0; cmb < mt->n_combinations; ++cmb)
830 const struct workspace *ws = mt->ws + cmb;
831 if (ws->root_cell == NULL)
834 ds_init_empty (&des);
835 layers_to_string (mt, ws->control_idx, &des);
836 msg (MW, _("The table \"%s\" has no non-empty control variables."
837 " No result for this table will be displayed."),
842 if (mt->n_dep_vars > 1)
843 means_shipout_multivar (mt, means, ws);
845 means_shipout_single (mt, means, ws);
853 control_var_missing (const struct means *means,
854 const struct mtable *mt,
855 unsigned int not_wild UNUSED,
856 const struct ccase *c,
857 const struct workspace *ws)
860 for (int l = 0; l < mt->n_layers; ++l)
862 /* if (0 == ((not_wild >> l) & 0x1)) */
867 const struct layer *layer = mt->layers[l];
868 const struct variable *var = layer->factor_vars[ws->control_idx[l]];
869 const union value *vv = case_data (c, var);
871 miss = (var_is_value_missing (var, vv) & means->ctrl_exclude) != 0;
879 /* Lookup the set of control variables described by MT, C and NOT_WILD,
880 in the hash table MAP. If there is no such entry, then create a
881 cell with these paremeters and add is to MAP.
882 If the generated cell has childen, repeat for all the children.
883 Returns the root cell.
886 service_cell_map (const struct means *means, const struct mtable *mt,
887 const struct ccase *c,
888 unsigned int not_wild,
890 const struct cell *pcell,
892 const struct workspace *ws)
894 struct cell *cell = NULL;
897 if (!control_var_missing (means, mt, not_wild, c, ws))
899 /* Lookup this set of values in the cell's hash table. */
900 unsigned int hash = generate_hash (mt, c, not_wild, ws);
901 cell = lookup_cell (mt, map, hash, c, not_wild, ws);
903 /* If it has not been seen before, then create a new
904 subcell, with this set of values, and insert it
908 cell = generate_cell (means, mt, c, not_wild, pcell, ws);
909 hmap_insert (map, &cell->hmap_node, hash);
915 /* This condition should only happen in the root node case. */
916 cell = ws->root_cell;
918 !control_var_missing (means, mt, not_wild, c, ws))
919 cell = generate_cell (means, mt, c, not_wild, pcell, ws);
924 /* Here is where the business really happens! After
925 testing for missing values, the cell's statistics
927 if (!control_var_missing (means, mt, not_wild, c, ws))
929 for (int v = 0; v < mt->n_dep_vars; ++v)
931 const struct variable *dep_var = mt->dep_vars[v];
932 const union value *vv = case_data (c, dep_var);
933 if (var_is_value_missing (dep_var, vv) & means->dep_exclude)
936 for (int stat = 0; stat < means->n_statistics; ++stat)
938 const double weight = dict_get_case_weight (means->dict, c,
940 stat_update *su = cell_spec[means->statistics[stat]].su;
941 su (cell->stat[stat + v * means->n_statistics], weight,
942 case_num (c, dep_var));
947 /* Recurse into all the children (if there are any). */
948 for (int i = 0; i < cell->n_children; ++i)
950 struct cell_container *cc = cell->children + i;
951 service_cell_map (means, mt, c,
952 not_wild | (0x1U << (i + level)),
953 &cc->map, cell, level + i + 1, ws);
960 /* Do all the necessary preparation and pre-calculation that
961 needs to be done before iterating the data. */
963 prepare_means (struct means *cmd)
965 for (int t = 0; t < cmd->n_tables; ++t)
967 struct mtable *mt = cmd->table + t;
969 for (int i = 0; i < mt->n_combinations; ++i)
971 struct workspace *ws = mt->ws + i;
972 ws->root_cell = NULL;
973 ws->control_idx = xcalloc (mt->n_layers, sizeof *ws->control_idx);
974 ws->instances = xcalloc (mt->n_layers, sizeof *ws->instances);
976 for (int l = mt->n_layers - 1; l >= 0; --l)
978 struct cell_container *instances = ws->instances + l;
979 const struct layer *layer = mt->layers[l];
980 ws->control_idx[l] = cmb % layer->n_factor_vars;
981 cmb /= layer->n_factor_vars;
982 hmap_init (&instances->map);
989 /* Do all the necessary calculations that occur AFTER iterating
992 post_means (struct means *cmd)
994 for (int t = 0; t < cmd->n_tables; ++t)
996 struct mtable *mt = cmd->table + t;
997 for (int cmb = 0; cmb < mt->n_combinations; ++cmb)
999 struct workspace *ws = mt->ws + cmb;
1000 if (ws->root_cell == NULL)
1002 arrange_cells (ws, ws->root_cell, mt);
1003 /* The root cell should have no parent. */
1004 assert (ws->root_cell->parent_cell == 0);
1006 for (int l = 0; l < mt->n_layers; ++l)
1008 struct cell_container *instances = ws->instances + l;
1009 bt_init (&instances->bt, compare_instance_3way, NULL);
1011 /* Iterate the instance hash table, and insert each instance
1012 into the binary tree BT. */
1013 struct instance *inst;
1014 HMAP_FOR_EACH (inst, struct instance, hmap_node,
1017 bt_insert (&instances->bt, &inst->bt_node);
1020 /* Iterate the binary tree (in order) and assign the index
1021 member accordingly. */
1023 BT_FOR_EACH (inst, struct instance, bt_node, &instances->bt)
1025 inst->index = index++;
1033 /* Update the summary information (the missings and the totals). */
1035 update_summaries (const struct means *means, struct mtable *mt,
1036 const struct ccase *c, double weight)
1038 for (int dv = 0; dv < mt->n_dep_vars; ++dv)
1040 for (int cmb = 0; cmb < mt->n_combinations; ++cmb)
1042 struct workspace *ws = mt->ws + cmb;
1043 struct summary *summ = mt->summ
1044 + cmb * mt->n_dep_vars + dv;
1046 summ->n_total += weight;
1047 const struct variable *var = mt->dep_vars[dv];
1048 const union value *vv = case_data (c, var);
1049 /* First check if the dependent variable is missing. */
1050 if (var_is_value_missing (var, vv) & means->dep_exclude)
1051 summ->n_missing += weight;
1052 /* If the dep var is not missing, then check each
1053 control variable. */
1055 for (int l = 0; l < mt->n_layers; ++l)
1057 const struct layer *layer = mt->layers [l];
1058 const struct variable *var
1059 = layer->factor_vars[ws->control_idx[l]];
1060 const union value *vv = case_data (c, var);
1061 if (var_is_value_missing (var, vv) & means->ctrl_exclude)
1063 summ->n_missing += weight;
1073 run_means (struct means *cmd, struct casereader *input,
1074 const struct dataset *ds UNUSED)
1076 struct ccase *c = NULL;
1077 struct casereader *reader;
1079 prepare_means (cmd);
1081 for (reader = input;
1082 (c = casereader_read (reader)) != NULL; case_unref (c))
1085 = dict_get_case_weight (cmd->dict, c, NULL);
1086 for (int t = 0; t < cmd->n_tables; ++t)
1088 struct mtable *mt = cmd->table + t;
1089 update_summaries (cmd, mt, c, weight);
1091 for (int cmb = 0; cmb < mt->n_combinations; ++cmb)
1093 struct workspace *ws = mt->ws + cmb;
1095 ws->root_cell = service_cell_map (cmd, mt, c,
1096 0U, NULL, NULL, 0, ws);
1100 casereader_destroy (reader);
1108 cmd_means (struct lexer *lexer, struct dataset *ds)
1111 means.pool = pool_create ();
1113 means.ctrl_exclude = MV_ANY;
1114 means.dep_exclude = MV_ANY;
1118 means.dict = dataset_dict (ds);
1120 means.n_statistics = 3;
1121 means.statistics = pool_calloc (means.pool, 3, sizeof *means.statistics);
1122 means.statistics[0] = MEANS_MEAN;
1123 means.statistics[1] = MEANS_N;
1124 means.statistics[2] = MEANS_STDDEV;
1126 if (! means_parse (lexer, &means))
1129 /* Calculate some constant data for each table. */
1130 for (int t = 0; t < means.n_tables; ++t)
1132 struct mtable *mt = means.table + t;
1133 mt->n_combinations = 1;
1134 for (int l = 0; l < mt->n_layers; ++l)
1135 mt->n_combinations *= mt->layers[l]->n_factor_vars;
1139 struct casegrouper *grouper;
1140 struct casereader *group;
1143 grouper = casegrouper_create_splits (proc_open (ds), means.dict);
1144 while (casegrouper_get_next_group (grouper, &group))
1146 /* Allocate the workspaces. */
1147 for (int t = 0; t < means.n_tables; ++t)
1149 struct mtable *mt = means.table + t;
1150 mt->summ = xcalloc (mt->n_combinations * mt->n_dep_vars,
1151 sizeof (*mt->summ));
1152 mt->ws = xcalloc (mt->n_combinations, sizeof (*mt->ws));
1154 run_means (&means, group, ds);
1155 for (int t = 0; t < means.n_tables; ++t)
1157 const struct mtable *mt = means.table + t;
1159 means_case_processing_summary (mt);
1160 means_shipout (mt, &means);
1162 for (int i = 0; i < mt->n_combinations; ++i)
1164 struct workspace *ws = mt->ws + i;
1165 if (ws->root_cell == NULL)
1168 means_destroy_cells (&means, ws->root_cell, mt);
1172 /* Destroy the workspaces. */
1173 for (int t = 0; t < means.n_tables; ++t)
1175 struct mtable *mt = means.table + t;
1177 for (int i = 0; i < mt->n_combinations; ++i)
1179 struct workspace *ws = mt->ws + i;
1180 destroy_workspace (mt, ws);
1185 ok = casegrouper_destroy (grouper);
1186 ok = proc_commit (ds) && ok;
1189 pool_destroy (means.pool);
1194 pool_destroy (means.pool);