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