94adcca73eaeb63799d983df794dafd404c6df5a
[pspp] / src / output / pivot-table.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2017-2018 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #ifndef OUTPUT_PIVOT_TABLE_H
18 #define OUTPUT_PIVOT_TABLE_H 1
19
20 #include <stdint.h>
21 #include <time.h>
22 #include "data/format.h"
23 #include "data/settings.h"
24 #include "libpspp/compiler.h"
25 #include "libpspp/hmap.h"
26 #include "output/table.h"
27
28 struct pivot_value;
29 struct variable;
30 union value;
31
32 /* Pivot tables.
33
34    Pivot tables are PSPP's primary form of output.  They are analogous to the
35    pivot tables you might be familiar with from spreadsheets and databases.
36    See https://en.wikipedia.org/wiki/Pivot_table for a brief introduction to
37    the overall concept of a pivot table.
38
39    In PSPP, the most important internal pieces of a pivot table are:
40
41    - Title.  Every pivot table has a title that is displayed above it.  It also
42      has an optional caption (displayed below it) and corner text (displayed in
43      the upper left corner).
44
45    - Dimensions.  A dimension consists of zero or more categories.  A category
46      has a label, such as "df" or "Asymp. Sig." or 123 or a variable name.  The
47      categories are the leaves of a tree whose non-leaf nodes form groups of
48      categories.  The tree always has a root group whose label is the name of
49      the dimension.
50
51    - Axes.  A table has three axes: column, row, and layer.  Each dimension is
52      assigned to an axis, and each axis has zero or more dimensions.  When an
53      axis has more than one dimension, they are ordered from innermost to
54      outermost.
55
56    - Data.  A table's data consists of zero or more cells.  Each cell maps from
57      a category for each dimension to a value, which is commonly a number but
58      could also be a variable name or an arbitrary text string.
59
60    Creating a pivot table usually consists of the following steps:
61
62    1. Create the table with pivot_table_create(), passing in the title.
63       It's commonly useful to set up a few options at this point:
64
65       - If empty rows or columns should not be displayed, set ->omit_empty to
66         true.
67
68       - Set the format to use for "count" values with
69         pivot_table_set_weight_var() or pivot_table_set_weight_format().
70
71    2. Create each dimension with pivot_dimension_create() and populate it with
72       categories and, possibly, with groups that contain the categories.  This
73       call also assigns the dimension to an axis.
74
75       In simple cases, only a call to pivot_dimension_create() is needed.
76       Other functions such as pivot_category_create_group() can be used for
77       hierarchies of categories.
78
79       Sometimes it's easier to create categories in tandem with inserting data,
80       for example by adding a category for a variable just before inserting the
81       first cell for that variable.  In that case, creating categories and
82       inserting data can be interleaved.
83
84    3. Insert data.  For each cell, supply the category indexes, which are
85       assigned starting from 0 in the order in which the categories were
86       created in step 2, and the value to go in the cell.  If the table has a
87       small, fixed number of dimensions, functions like, e.g.
88       pivot_table_put3() for 3 dimensions, can be used.  The general function
89       pivot_table_put() works for other cases.
90
91    4. Output the table for user consumption.  Use pivot_table_submit(). */
92 \f
93 /* Pivot table display styling. */
94
95 /* Areas of a pivot table for styling purposes. */
96 enum pivot_area
97   {
98     PIVOT_AREA_TITLE,
99     PIVOT_AREA_CAPTION,
100     PIVOT_AREA_FOOTER,          /* Footnotes. */
101     PIVOT_AREA_CORNER,          /* Top-left corner. */
102     PIVOT_AREA_COLUMN_LABELS,
103     PIVOT_AREA_ROW_LABELS,
104     PIVOT_AREA_DATA,
105     PIVOT_AREA_LAYERS,          /* Layer indication. */
106     PIVOT_N_AREAS
107   };
108
109 const char *pivot_area_to_string (enum pivot_area);
110
111 /* Table borders for styling purposes. */
112 enum pivot_border
113   {
114     PIVOT_BORDER_TITLE,
115
116     /* Outer frame. */
117     PIVOT_BORDER_OUTER_LEFT,
118     PIVOT_BORDER_OUTER_TOP,
119     PIVOT_BORDER_OUTER_RIGHT,
120     PIVOT_BORDER_OUTER_BOTTOM,
121
122     /* Inner frame. */
123     PIVOT_BORDER_INNER_LEFT,
124     PIVOT_BORDER_INNER_TOP,
125     PIVOT_BORDER_INNER_RIGHT,
126     PIVOT_BORDER_INNER_BOTTOM,
127
128     /* Data area. */
129     PIVOT_BORDER_DATA_LEFT,
130     PIVOT_BORDER_DATA_TOP,
131
132     /* Dimensions. */
133     PIVOT_BORDER_DIM_ROW_HORZ,
134     PIVOT_BORDER_DIM_ROW_VERT,
135     PIVOT_BORDER_DIM_COL_HORZ,
136     PIVOT_BORDER_DIM_COL_VERT,
137
138     /* Categories. */
139     PIVOT_BORDER_CAT_ROW_HORZ,
140     PIVOT_BORDER_CAT_ROW_VERT,
141     PIVOT_BORDER_CAT_COL_HORZ,
142     PIVOT_BORDER_CAT_COL_VERT,
143
144     PIVOT_N_BORDERS
145   };
146
147 const char *pivot_border_to_string (enum pivot_border);
148
149 /* Sizing for rows or columns of a rendered table.  The comments below talk
150    about columns and their widths but they apply equally to rows and their
151    heights. */
152 struct pivot_table_sizing
153   {
154     /* Specific column widths, in 1/96" units. */
155     int *widths;
156     size_t n_widths;
157
158     /* Specific page breaks: 0-based columns after which a page break must
159        occur, e.g. a value of 1 requests a break after the second column. */
160     size_t *breaks;
161     size_t n_breaks;
162
163     /* Keeps: columns to keep together on a page if possible. */
164     struct pivot_keep *keeps;
165     size_t n_keeps;
166   };
167
168 void pivot_table_sizing_uninit (struct pivot_table_sizing *);
169
170 /* A set of columns to keep together on a page if possible, e.g. ofs=1, n=10
171    requests keeping together the 2nd through 11th columns. */
172 struct pivot_keep
173   {
174     size_t ofs;                 /* 0-based first column. */
175     size_t n;                   /* Number of columns. */
176   };
177 \f
178 /* Axes. */
179
180 enum pivot_axis_type
181   {
182     PIVOT_AXIS_LAYER,
183     PIVOT_AXIS_ROW,
184     PIVOT_AXIS_COLUMN,
185
186     PIVOT_N_AXES
187   };
188
189 const char *pivot_axis_type_to_string (enum pivot_axis_type);
190
191 /* An axis within a pivot table. */
192 struct pivot_axis
193   {
194     /* dimensions[0] is the innermost dimension,
195        dimensions[1] is the next outer dimension,
196        ...
197        dimensions[n_dimensions - 1] is the outermost dimension. */
198     struct pivot_dimension **dimensions;
199     size_t n_dimensions;
200
201     /* The number of rows or columns along the axis,
202        that is, the product of dimension[*]->n_leaves.
203        It is 0 if any dimension has 0 leaves. */
204     size_t extent;
205
206     /* Sum of dimensions[*]->label_depth. */
207     size_t label_depth;
208   };
209
210 /* Successively assigns to INDEXES (which should be a "size_t *") each of the
211    combinations of the categories in AXIS's dimensions, in lexicographic order
212    with the innermost dimension iterating most quickly.
213
214    The value assigned to INDEXES is dynamically allocated.  If the client
215    breaks out of the loop prematurely, it needs to free it with free(). */
216 #define PIVOT_AXIS_FOR_EACH(INDEXES, AXIS)                              \
217   for ((INDEXES) = NULL;                                                \
218        ((INDEXES) = pivot_axis_iterator_next (INDEXES, AXIS)) != NULL;)
219 size_t *pivot_axis_iterator_next (size_t *indexes, const struct pivot_axis *);
220 \f
221 /* Dimensions.
222
223    A pivot_dimension identifies the categories associated with a single
224    dimension within a multidimensional pivot table.
225
226    A dimension contains a collection of categories, which are the leaves in a
227    tree of groups.
228
229    (A dimension or a group can contain zero categories, but this is unusual.
230    If a dimension contains no categories, then its table cannot contain any
231    data.)
232 */
233 struct pivot_dimension
234   {
235     /* table->axes[axis_type]->dimensions[level] == dimension. */
236     struct pivot_table *table;
237     enum pivot_axis_type axis_type;
238     size_t level;               /* 0 for innermost dimension within axis. */
239
240     /* table->dimensions[top_index] == dimension. */
241     size_t top_index;
242
243     /* Hierarchy of categories within the dimension.  The groups and categories
244        are sorted in the order that should be used for display.  This might be
245        different from the original order produced for output if the user
246        adjusted it.
247
248        The root must always be a group, although it is allowed to have no
249        subcategories. */
250     struct pivot_category *root;
251
252     /* All of the leaves reachable via the root.
253
254        The indexing for presentation_leaves is presentation order, thus
255        presentation_leaves[i]->presentation_index == i.  This order is the same
256        as would be produced by an in-order traversal of the groups.  It is the
257        order into which the user reordered or sorted the categories.
258
259        The indexing for data_leaves is that used for idx[] in struct
260        pivot_cell, thus data_leaves[i]->data_index == i.  This might differ
261        from what an in-order traversal of 'root' would yield, if the user
262        reordered categories. */
263     struct pivot_category **data_leaves;
264     struct pivot_category **presentation_leaves;
265     size_t n_leaves, allocated_leaves;
266
267     /* Display. */
268     bool hide_all_labels;
269
270     /* Number of rows or columns needed to express the labels. */
271     int label_depth;
272   };
273
274 struct pivot_dimension *pivot_dimension_create (
275   struct pivot_table *, enum pivot_axis_type, const char *name, ...)
276   SENTINEL (0);
277 #define pivot_dimension_create(...) \
278   pivot_dimension_create(__VA_ARGS__, NULL_SENTINEL)
279 struct pivot_dimension *pivot_dimension_create__ (struct pivot_table *,
280                                                   enum pivot_axis_type,
281                                                   struct pivot_value *name);
282
283 void pivot_dimension_destroy (struct pivot_dimension *);
284
285 void pivot_dimension_dump (const struct pivot_dimension *, int indentation);
286 \f
287 /* A pivot_category is a leaf (a category) or a group:
288
289    - For a leaf, neither index is SIZE_MAX.
290
291    - For a group, both indexes are SIZE_MAX.
292
293    Do not use 'subs' or 'n_subs' to determine whether a category is a group,
294    because a group may (pathologically) have no leaves. */
295 struct pivot_category
296   {
297     struct pivot_value *name;
298     struct pivot_category *parent;
299     struct pivot_dimension *dimension;
300     size_t label_depth, extra_depth;
301
302     /* Groups only.
303
304        If show_label is true, then the group itself has a row (or a column)
305        giving the group's name.  Otherwise, the group's own name is not
306        displayed. */
307     struct pivot_category **subs; /* Child categories or groups. */
308     size_t n_subs, allocated_subs;
309     bool show_label;            /* Display a label for the group itself? */
310     bool show_label_in_corner;
311
312     /* Leaf only. */
313     struct fmt_spec format;
314     size_t group_index;        /* In ->parent->subs[]. */
315     size_t data_index;         /* In ->dimension->data_leaves[]. */
316     size_t presentation_index; /* In ->dimension->presentation_leaves[]. */
317   };
318
319 static inline bool
320 pivot_category_is_group (const struct pivot_category *category)
321 {
322   return category->data_index == SIZE_MAX;
323 }
324
325 static inline bool
326 pivot_category_is_leaf (const struct pivot_category *category)
327 {
328   return !pivot_category_is_group (category);
329 }
330
331 /* Creating leaf categories. */
332 int pivot_category_create_leaves (struct pivot_category *parent, ...)
333   SENTINEL (0);
334 #define pivot_category_create_leaves(...) \
335   pivot_category_create_leaves(__VA_ARGS__, NULL_SENTINEL)
336
337 int pivot_category_create_leaf (
338   struct pivot_category *parent, struct pivot_value *name);
339 int pivot_category_create_leaf_rc (
340   struct pivot_category *parent, struct pivot_value *name, const char *rc);
341
342 /* Creating category groups. */
343 struct pivot_category *pivot_category_create_group (
344   struct pivot_category *parent, const char *name, ...) SENTINEL (0);
345 #define pivot_category_create_group(...) \
346   pivot_category_create_group(__VA_ARGS__, NULL_SENTINEL)
347 struct pivot_category *pivot_category_create_group__ (
348   struct pivot_category *parent, struct pivot_value *name);
349
350 void pivot_category_destroy (struct pivot_category *);
351
352 /* Pivot result classes.
353
354    These are used to mark leaf categories as having particular types of data,
355    to set their numeric formats.  The formats that actually get used for these
356    classes are in the result_classes[] global array in pivot-table.c, except
357    that PIVOT_RC_OTHER comes from settings_get_format() and PIVOT_RC_COUNT
358    should come from the weight variable in the dataset's dictionary. */
359 #define PIVOT_RC_OTHER ("RC_OTHER")
360 #define PIVOT_RC_INTEGER ("RC_INTEGER")
361 #define PIVOT_RC_CORRELATION ("RC_CORRELATIONS")
362 #define PIVOT_RC_SIGNIFICANCE ("RC_SIGNIFICANCE")
363 #define PIVOT_RC_PERCENT ("RC_PERCENT")
364 #define PIVOT_RC_RESIDUAL ("RC_RESIDUAL")
365 #define PIVOT_RC_COUNT ("RC_COUNT")
366
367 bool pivot_result_class_change (const char *, const struct fmt_spec *);
368 \f
369 /* Styling for a pivot table.
370
371    The division between this and the style information in struct pivot_table
372    seems fairly arbitrary.  The ultimate reason for the division is simply
373    because that's how SPSS documentation and file formats do it. */
374 struct pivot_table_look
375   {
376     /* Reference count.  A pivot_table_look may be shared between multiple
377        owners, indicated by a reference count greater than 1.  When this is the
378        case, the pivot_table must not be modified. */
379     int ref_cnt;
380
381     char *name;                 /* May be null. */
382
383     /* General properties. */
384     bool omit_empty;
385     bool row_labels_in_corner;
386     int width_ranges[TABLE_N_AXES][2];      /* In 1/96" units. */
387
388     /* Footnote display settings. */
389     bool show_numeric_markers;
390     bool footnote_marker_superscripts;
391
392     /* Styles. */
393     struct table_area_style areas[PIVOT_N_AREAS];
394     struct table_border_style borders[PIVOT_N_BORDERS];
395
396     /* Print settings. */
397     bool print_all_layers;
398     bool paginate_layers;
399     bool shrink_to_fit[TABLE_N_AXES];
400     bool top_continuation, bottom_continuation;
401     char *continuation;
402     size_t n_orphan_lines;
403   };
404
405 const struct pivot_table_look *pivot_table_look_builtin_default (void);
406 struct pivot_table_look *pivot_table_look_new_builtin_default (void);
407 struct pivot_table_look *pivot_table_look_ref (
408   const struct pivot_table_look *);
409 void pivot_table_look_unref (struct pivot_table_look *);
410 struct pivot_table_look *pivot_table_look_unshare (struct pivot_table_look *);
411 \f
412 /* A pivot table.  See the top of this file for more information. */
413 struct pivot_table
414   {
415     /* Reference count.  A pivot_table may be shared between multiple owners,
416        indicated by a reference count greater than 1.  When this is the case,
417        the pivot_table must not be modified. */
418     int ref_cnt;
419
420     /* Styling. */
421     struct pivot_table_look *look;
422
423     /* Display settings. */
424     bool rotate_inner_column_labels;
425     bool rotate_outer_row_labels;
426     bool show_grid_lines;
427     bool show_caption;
428     size_t *current_layer; /* axis[PIVOT_AXIS_LAYER].n_dimensions elements. */
429     enum settings_value_show show_values;
430     enum settings_value_show show_variables;
431     struct fmt_spec weight_format;
432
433     /* Column and row sizing and page breaks.
434        sizing[TABLE_HORZ] is for columns, sizing[TABLE_VERT] is for rows. */
435     struct pivot_table_sizing sizing[TABLE_N_AXES];
436
437     /* Format settings. */
438     int epoch;
439     char decimal;               /* Usually ',' or '.'. */
440     char grouping;              /* Usually '.' or ','. */
441     char *ccs[5];               /* Custom currency. */
442     double small;
443
444     /* Command information. */
445     char *command_local;        /* May be NULL. */
446     char *command_c;            /* May be NULL. */
447     char *language;             /* May be NULL. */
448     char *locale;               /* May be NULL. */
449
450     /* Source information. */
451     char *dataset;              /* May be NULL. */
452     char *datafile;             /* May be NULL. */
453     time_t date;                /* May be 0 if unknown. */
454
455     /* Footnotes. */
456     struct pivot_footnote **footnotes;
457     size_t n_footnotes, allocated_footnotes;
458
459     /* Titles. */
460     struct pivot_value *title;
461     struct pivot_value *subtype;  /* Same as spv_item's subtype. */
462     struct pivot_value *corner_text;
463     struct pivot_value *caption;
464     char *notes;
465
466     /* Dimensions. */
467     struct pivot_dimension **dimensions;
468     size_t n_dimensions;
469
470     /* Allocation of dimensions to rows, columns, and layers. */
471     struct pivot_axis axes[PIVOT_N_AXES];
472
473     struct hmap cells;          /* Contains "struct pivot_cell"s. */
474   };
475
476 /* Creating and destroy pivot tables. */
477 struct pivot_table *pivot_table_create (const char *title);
478 struct pivot_table *pivot_table_create__ (struct pivot_value *title,
479                                           const char *subtype);
480 struct pivot_table *pivot_table_create_for_text (struct pivot_value *title,
481                                                  struct pivot_value *content);
482
483 struct pivot_table *pivot_table_ref (const struct pivot_table *);
484 void pivot_table_unref (struct pivot_table *);
485 bool pivot_table_is_shared (const struct pivot_table *);
486
487 /* Styling. */
488 const struct pivot_table_look *pivot_table_get_look (
489   const struct pivot_table *);
490 void pivot_table_set_look (struct pivot_table *,
491                            const struct pivot_table_look *);
492
493 /* Format of PIVOT_RC_COUNT cells. */
494 void pivot_table_set_weight_var (struct pivot_table *,
495                                  const struct variable *);
496 void pivot_table_set_weight_format (struct pivot_table *,
497                                     const struct fmt_spec *);
498
499 /* Query. */
500 bool pivot_table_is_empty (const struct pivot_table *);
501
502 /* Output. */
503 void pivot_table_submit (struct pivot_table *);
504
505 /* Data cells. */
506 void pivot_table_put (struct pivot_table *, const size_t *dindexes, size_t n,
507                       struct pivot_value *);
508 void pivot_table_put1 (struct pivot_table *, size_t idx1,
509                        struct pivot_value *);
510 void pivot_table_put2 (struct pivot_table *, size_t idx1, size_t idx2,
511                        struct pivot_value *);
512 void pivot_table_put3 (struct pivot_table *, size_t idx1, size_t idx2,
513                        size_t idx3, struct pivot_value *);
514 void pivot_table_put4 (struct pivot_table *, size_t idx1, size_t idx2,
515                        size_t idx3, size_t idx4, struct pivot_value *);
516
517 const struct pivot_value *pivot_table_get (const struct pivot_table *,
518                                            const size_t *dindexes);
519
520 struct pivot_value *pivot_table_get_rw (struct pivot_table *,
521                                         const size_t *dindexes);
522
523 /* Footnotes.
524
525    Use pivot_table_create_footnote() to create a footnote.
526    Use pivot_value_add_footnote() to add a reference to a footnote. */
527 struct pivot_footnote
528   {
529     size_t idx;
530     struct pivot_value *content;
531     struct pivot_value *marker;
532     bool show;
533   };
534
535 struct pivot_footnote *pivot_table_create_footnote (
536   struct pivot_table *, struct pivot_value *content);
537 struct pivot_footnote *pivot_table_create_footnote__ (
538   struct pivot_table *, size_t idx,
539   struct pivot_value *marker, struct pivot_value *content);
540
541 void pivot_footnote_destroy (struct pivot_footnote *);
542
543 /* Internals. */
544 void pivot_table_convert_indexes_ptod (const struct pivot_table *,
545                                        const size_t *pindexes[PIVOT_N_AXES],
546                                        size_t *dindexes);
547 size_t *pivot_table_enumerate_axis (const struct pivot_table *,
548                                     enum pivot_axis_type,
549                                     const size_t *layer_indexes,
550                                     bool omit_empty, size_t *n);
551 #define PIVOT_ENUMERATION_FOR_EACH(INDEXES, ENUMERATION, AXIS)  \
552   for ((INDEXES) = (ENUMERATION); *(INDEXES) != SIZE_MAX;       \
553        (INDEXES) += MAX (1, (AXIS)->n_dimensions))
554
555 void pivot_table_assign_label_depth (struct pivot_table *);
556
557 void pivot_table_dump (const struct pivot_table *, int indentation);
558 \f
559 /* pivot_value. */
560
561 enum pivot_value_type
562   {
563     PIVOT_VALUE_NUMERIC,          /* A value of a numeric variable. */
564     PIVOT_VALUE_STRING,           /* A value of a string variable. */
565     PIVOT_VALUE_VARIABLE,         /* Name of a variable. */
566     PIVOT_VALUE_TEXT,             /* Text. */
567     PIVOT_VALUE_TEMPLATE,         /* Templated text. */
568   };
569
570 /* A pivot_value is the content of a single pivot table cell.  A pivot_value is
571    also a pivot table's title, caption, footnote marker and contents, and so
572    on.
573
574    A given pivot_value is one of:
575
576    1. A number resulting from a calculation (PIVOT_VALUE_NUMERIC).  Use
577       pivot_value_new_number() to create such a pivot_value.
578
579       A numeric pivot_value has an associated display format (usually an F or
580       PCT format).  This format can be set directly on the pivot_value, but
581       that is not usually the easiest way.  Instead, it is usually true that
582       all of the values in a single category should have the same format
583       (e.g. all "Significance" values might use format F40.3), so PSPP makes
584       it easy to set the default format for a category while creating the
585       category.  See pivot_dimension_create() for more details.
586
587       For numbers that should be displayed as integers,
588       pivot_value_new_integer() can occasionally be a useful special case.
589
590    2. A numeric or string value obtained from data (PIVOT_VALUE_NUMERIC or
591       PIVOT_VALUE_STRING).  If such a value corresponds to a variable, then the
592       variable's name can be attached to the pivot_value.  If the value has a
593       value label, then that can also be attached.  When a label is present,
594       the user can control whether to show the value or the label or both.
595
596       Use pivot_value_new_var_value() to create pivot_values of these kinds.
597
598    3. A variable name (PIVOT_VALUE_VARIABLE).  The variable label, if any, can
599       be attached too, and again the user can control whether to show the value
600       or the label or both.
601
602    4. A text string (PIVOT_VALUE_TEXT).  The value stores the string in English
603       and translated into the output language (localized).  Use
604       pivot_value_new_text() or pivot_value_new_text_format() for those cases.
605       In some cases, only an English or a localized version is available for
606       one reason or another, although this is regrettable; in those cases, use
607       pivot_value_new_user_text() or pivot_value_new_user_text_nocopy().
608
609    (There is also a PIVOT_VALUE_TEMPLATE but PSPP does not yet create these
610    itself.)
611
612
613    Footnotes
614    =========
615
616    A pivot_value may reference any number of footnotes.  Use
617    pivot_value_add_footnote() to add a footnote reference.  The footnotes being
618    referenced must first be created with pivot_table_create_footnote().
619
620
621    Styling
622    =======
623
624    A pivot_value can have specific font and cell styles.  Only the user should
625    add these.
626 */
627 struct pivot_value
628   {
629     struct font_style *font_style;
630     struct cell_style *cell_style;
631
632     char **subscripts;
633     size_t n_subscripts;
634
635     char *superscript;
636
637     const struct pivot_footnote **footnotes;
638     size_t n_footnotes;
639
640     enum pivot_value_type type;
641     union
642       {
643         /* PIVOT_VALUE_NUMERIC. */
644         struct
645           {
646             double x;                 /* The numeric value. */
647             struct fmt_spec format;   /* Format to display 'x'. */
648             char *var_name;           /* May be NULL. */
649             char *value_label;        /* May be NULL. */
650             enum settings_value_show show; /* Show value or label or both? */
651           }
652         numeric;
653
654         /* PIVOT_VALUE_STRING. */
655         struct
656           {
657             char *s;                  /* The string value. */
658             bool hex;                 /* Display in hex? */
659             char *var_name;           /* May be NULL. */
660             char *value_label;        /* May be NULL. */
661             enum settings_value_show show; /* Show value or label or both? */
662           }
663         string;
664
665         /* PIVOT_VALUE_VARIABLE. */
666         struct
667           {
668             char *var_name;
669             char *var_label;          /* May be NULL. */
670             enum settings_value_show show; /* Show name or label or both? */
671           }
672         variable;
673
674         /* PIVOT_VALUE_TEXT. */
675         struct
676           {
677             char *local;              /* Localized. */
678             char *c;                  /* English. */
679             char *id;                 /* Identifier. */
680             bool user_provided;
681           }
682         text;
683
684         /* PIVOT_VALUE_TEMPLATE. */
685         struct
686           {
687             char *local;              /* Localized. */
688             char *id;                 /* Identifier. */
689             struct pivot_argument *args;
690             size_t n_args;
691           }
692         template;
693       };
694   };
695
696 /* Numbers resulting from calculations. */
697 struct pivot_value *pivot_value_new_number (double);
698 struct pivot_value *pivot_value_new_integer (double);
699
700 /* Values from data. */
701 struct pivot_value *pivot_value_new_var_value (
702   const struct variable *, const union value *);
703 struct pivot_value *pivot_value_new_value (const union value *, int width,
704                                            const struct fmt_spec *,
705                                            const char *encoding);
706
707 /* Values from variable names. */
708 struct pivot_value *pivot_value_new_variable (const struct variable *);
709
710 /* Values from text strings. */
711 struct pivot_value *pivot_value_new_text (const char *);
712 struct pivot_value *pivot_value_new_text_format (const char *, ...)
713 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__>= 4) || __GNUC__ > 4)
714   __attribute__((format(gnu_printf, 1, 2)));
715 #else
716   __attribute__((format(__printf__, 1, 2)));
717 #endif
718
719 struct pivot_value *pivot_value_new_user_text (const char *, size_t length);
720 struct pivot_value *pivot_value_new_user_text_nocopy (char *);
721
722 /* Footnotes. */
723 void pivot_value_add_footnote (struct pivot_value *, const struct pivot_footnote *);
724
725 /* Numeric formats. */
726 void pivot_value_set_rc (const struct pivot_table *, struct pivot_value *,
727                          const char *rc);
728
729 /* Converting a pivot_value to a string for display. */
730 char *pivot_value_to_string (const struct pivot_value *,
731                              enum settings_value_show show_values,
732                              enum settings_value_show show_variables);
733 void pivot_value_format (const struct pivot_value *,
734                          enum settings_value_show show_values,
735                          enum settings_value_show show_variables,
736                          struct string *);
737 bool pivot_value_format_body (const struct pivot_value *,
738                               enum settings_value_show show_values,
739                               enum settings_value_show show_variables,
740                               struct string *);
741
742 void pivot_value_destroy (struct pivot_value *);
743
744 /* Styling. */
745 void pivot_value_get_style (struct pivot_value *,
746                             const struct font_style *base_font_style,
747                             const struct cell_style *base_cell_style,
748                             struct table_area_style *);
749 void pivot_value_set_style (struct pivot_value *,
750                             const struct table_area_style *);
751
752 /* Template arguments. */
753 struct pivot_argument
754   {
755     size_t n;
756     struct pivot_value **values;
757   };
758
759 void pivot_argument_uninit (struct pivot_argument *);
760 \f
761 /* One piece of data within a pivot table. */
762 struct pivot_cell
763   {
764     struct hmap_node hmap_node; /* In struct pivot_table's 'cells' hmap. */
765     struct pivot_value *value;
766     unsigned int idx[];         /* One index per table dimension. */
767   };
768
769 #endif /* output/pivot-table.h */