Automatically infer variables' measurement level from format and data.
[pspp] / src / data / variable.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013,
3    2014, 2020 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #ifndef DATA_VARIABLE_H
19 #define DATA_VARIABLE_H 1
20
21 #include <stddef.h>
22 #include <stdbool.h>
23 #include "data/dict-class.h"
24 #include "data/missing-values.h"
25 #include "data/val-type.h"
26 #include "data/settings.h"
27
28 /* Bitfields to identify traits of a variable */
29
30 #define VAR_TRAIT_NAME             0x0001
31 #define VAR_TRAIT_WIDTH            0x0002
32 #define VAR_TRAIT_ROLE             0x0004
33 #define VAR_TRAIT_LABEL            0x0008
34 #define VAR_TRAIT_VALUE_LABELS     0x0010
35 #define VAR_TRAIT_MISSING_VALUES   0x0020
36 #define VAR_TRAIT_ALIGNMENT        0x0040
37 #define VAR_TRAIT_MEASURE          0x0080
38 #define VAR_TRAIT_DISPLAY_WIDTH    0x0100
39 #define VAR_TRAIT_LEAVE            0x0200
40 #define VAR_TRAIT_POSITION         0x0400
41 #define VAR_TRAIT_ATTRIBUTES       0x0800
42 #define VAR_TRAIT_PRINT_FORMAT     0x1000
43 #define VAR_TRAIT_WRITE_FORMAT     0x2000
44
45
46 union value;
47
48 /* Variables.
49    These functions should rarely be called directly: use
50    dict_create_var, dict_clone_var, or dict_delete_var
51    instead. */
52 struct variable *var_create (const char *name, int width);
53 struct variable *var_clone (const struct variable *);
54 struct variable * var_ref (struct variable *) WARN_UNUSED_RESULT;
55 void var_unref (struct variable *);
56
57 /* Variable names. */
58 const char *var_get_name (const struct variable *);
59 void var_set_name (struct variable *, const char *);
60 enum dict_class var_get_dict_class (const struct variable *);
61
62 int compare_vars_by_name (const void *, const void *, const void *);
63 unsigned hash_var_by_name (const void *, const void *);
64
65 int compare_var_ptrs_by_name (const void *, const void *, const void *);
66 unsigned hash_var_ptr_by_name (const void *, const void *);
67
68 int compare_var_ptrs_by_dict_index (const void *, const void *, const void *);
69
70 struct fmt_spec;
71
72 /* Types and widths of values associated with a variable. */
73 enum val_type var_get_type (const struct variable *);
74 int var_get_width (const struct variable *);
75 void var_set_width (struct variable *, int width);
76 void var_set_width_and_formats (struct variable *v, int new_width,
77                                 const struct fmt_spec *print, const struct fmt_spec *write);
78
79 bool var_is_numeric (const struct variable *);
80 bool var_is_alpha (const struct variable *);
81
82 /* Variables' missing values. */
83 const struct missing_values *var_get_missing_values (const struct variable *);
84 void var_set_missing_values (struct variable *, const struct missing_values *);
85 void var_clear_missing_values (struct variable *);
86 bool var_has_missing_values (const struct variable *);
87
88 enum mv_class var_is_value_missing (const struct variable *, const union value *);
89 enum mv_class var_is_num_missing (const struct variable *, double);
90 enum mv_class var_is_str_missing (const struct variable *, const uint8_t[]);
91
92 /* Value labels. */
93 const char *var_lookup_value_label (const struct variable *,
94                                     const union value *);
95 struct string;
96 void var_append_value_name (const struct variable *, const union value *,
97                             struct string *);
98 void var_append_value_name__ (const struct variable *, const union value *,
99                               enum settings_value_show, struct string *);
100
101 bool var_has_value_labels (const struct variable *);
102 const struct val_labs *var_get_value_labels (const struct variable *);
103 void var_set_value_labels (struct variable *, const struct val_labs *);
104
105 bool var_add_value_label (struct variable *,
106                           const union value *, const char *);
107 void var_replace_value_label (struct variable *,
108                               const union value *, const char *);
109 void var_clear_value_labels (struct variable *);
110
111 /* Print and write formats. */
112 const struct fmt_spec *var_get_print_format (const struct variable *);
113 void var_set_print_format (struct variable *, const struct fmt_spec *);
114 const struct fmt_spec *var_get_write_format (const struct variable *);
115 void var_set_write_format (struct variable *, const struct fmt_spec *);
116 void var_set_both_formats (struct variable *, const struct fmt_spec *);
117
118 struct fmt_spec var_default_formats (int width);
119
120 /* Variable labels. */
121 const char *var_to_string (const struct variable *);
122 const char *var_get_label (const struct variable *);
123 void var_set_label (struct variable *, const char *label);
124 void var_clear_label (struct variable *);
125 bool var_has_label (const struct variable *);
126
127 /* How data is measured. */
128 enum measure
129   {
130     MEASURE_UNKNOWN = 0,
131     MEASURE_NOMINAL = 1,
132     MEASURE_ORDINAL = 2,
133     MEASURE_SCALE = 3,
134     n_MEASURES
135   };
136
137 bool measure_is_valid (enum measure);
138 const char *measure_to_string (enum measure);
139 const char *measure_to_syntax (enum measure);
140
141 enum measure var_get_measure (const struct variable *);
142 void var_set_measure (struct variable *, enum measure);
143
144 enum measure var_default_measure_for_type (enum val_type);
145 enum measure var_default_measure_for_format (enum fmt_type);
146
147 /* Intended usage of a variable, for populating dialogs. */
148 enum var_role
149   {
150     ROLE_INPUT,
151     ROLE_TARGET,
152     ROLE_BOTH,
153     ROLE_NONE,
154     ROLE_PARTITION,
155     ROLE_SPLIT
156   };
157
158 bool var_role_is_valid (enum var_role);
159 const char *var_role_to_string (enum var_role);
160 const char *var_role_to_syntax (enum var_role);
161
162 enum var_role var_get_role (const struct variable *);
163 void var_set_role (struct variable *, enum var_role);
164
165 /* GUI display width. */
166 int var_get_display_width (const struct variable *);
167 void var_set_display_width (struct variable *, int display_width);
168
169 int var_default_display_width (int width);
170
171 /* Alignment of data for display. */
172 enum alignment
173   {
174     ALIGN_LEFT = 0,
175     ALIGN_RIGHT = 1,
176     ALIGN_CENTRE = 2
177   };
178
179 bool alignment_is_valid (enum alignment);
180 const char *alignment_to_string (enum alignment);
181 const char *alignment_to_syntax (enum alignment);
182
183 enum alignment var_get_alignment (const struct variable *);
184 void var_set_alignment (struct variable *, enum alignment);
185
186 enum alignment var_default_alignment (enum val_type);
187
188 /* Whether variables' values should be preserved from case to
189    case. */
190 bool var_get_leave (const struct variable *);
191 void var_set_leave (struct variable *, bool leave);
192 bool var_must_leave (const struct variable *);
193
194 /* Short names. */
195 size_t var_get_n_short_names (const struct variable *);
196 const char *var_get_short_name (const struct variable *, size_t idx);
197 void var_set_short_name (struct variable *, size_t, const char *);
198 void var_clear_short_names (struct variable *);
199
200 /* Relationship with dictionary. */
201 size_t var_get_dict_index (const struct variable *);
202 size_t var_get_case_index (const struct variable *);
203
204 /* Custom attributes. */
205 struct attrset *var_get_attributes (const struct variable *);
206 void var_set_attributes (struct variable *, const struct attrset *);
207 bool var_has_attributes (const struct variable *);
208
209 /* Encoding. */
210 const char *var_get_encoding (const struct variable *);
211
212 /* Function types. */
213 typedef bool var_predicate_func (const struct variable *);
214
215 double var_force_valid_weight (const struct variable *wv, double w,
216                                bool *warn_on_invalid);
217
218 #endif /* data/variable.h */