sack works
[pspp] / src / data / dictionary.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2007, 2009, 2010, 2011, 2012, 2013 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 DATA_DICTIONARY_H
18 #define DATA_DICTIONARY_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include "data/case.h"
23 #include "data/dict-class.h"
24
25 struct string;
26 struct ccase;
27
28 /* Creating dictionaries. */
29 struct dictionary *dict_create (const char *encoding);
30 struct dictionary *dict_clone (const struct dictionary *) WARN_UNUSED_RESULT;
31 struct dictionary *dict_ref (struct dictionary *s) WARN_UNUSED_RESULT;
32
33
34 /* Clearing and destroying dictionaries. */
35 void dict_clear (struct dictionary *);
36 void dict_unref (struct dictionary *);
37
38 /* Common ways to access variables. */
39 struct variable *dict_lookup_var (const struct dictionary *, const char *);
40 struct variable *dict_lookup_var_assert (const struct dictionary *,
41                                          const char *);
42 struct variable *dict_get_var (const struct dictionary *, size_t position);
43 size_t dict_get_n_vars (const struct dictionary *);
44
45 /* Other access to variables. */
46 bool dict_contains_var (const struct dictionary *, const struct variable *);
47 void dict_get_vars (const struct dictionary *,
48                     const struct variable ***vars, size_t *n_vars,
49                     enum dict_class exclude);
50 void dict_get_vars_mutable (const struct dictionary *,
51                             struct variable ***vars, size_t *n_vars,
52                             enum dict_class exclude);
53
54 /* Creating variables. */
55 struct variable *dict_create_var (struct dictionary *, const char *,
56                                   int width);
57 struct variable *dict_create_var_assert (struct dictionary *, const char *,
58                                          int width);
59 struct variable *dict_clone_var (struct dictionary *, const struct variable *);
60 struct variable *dict_clone_var_assert (struct dictionary *,
61                                         const struct variable *);
62 struct variable *dict_clone_var_as (struct dictionary *,
63                                     const struct variable *, const char *);
64 struct variable *dict_clone_var_as_assert (struct dictionary *,
65                                            const struct variable *,
66                                            const char *);
67
68 /* Deleting variables. */
69 void dict_delete_var (struct dictionary *, struct variable *);
70 void dict_delete_vars (struct dictionary *,
71                        struct variable *const *, size_t count);
72 void dict_delete_consecutive_vars (struct dictionary *,
73                                    size_t idx, size_t count);
74 void dict_delete_scratch_vars (struct dictionary *);
75
76 /* Changing the order of variables. */
77 void dict_reorder_var (struct dictionary *, struct variable *,
78                        size_t new_index);
79 void dict_reorder_vars (struct dictionary *,
80                         struct variable *const *, size_t count);
81
82 /* Variable names. */
83 bool dict_try_rename_var (struct dictionary *,
84                           struct variable *, const char *);
85 void dict_rename_var (struct dictionary *, struct variable *, const char *);
86 bool dict_rename_vars (struct dictionary *,
87                        struct variable **, char **new_names,
88                        size_t count, char **err_name);
89 char *dict_make_unique_var_name (const struct dictionary *, const char *hint,
90                                  unsigned long int *num_start);
91
92 bool dict_get_names_must_be_ids (const struct dictionary *);
93 void dict_set_names_must_be_ids (struct dictionary *, bool);
94
95 /* Weight variable. */
96 double dict_get_case_weight (const struct dictionary *,
97                              const struct ccase *, bool *);
98 double dict_get_rounded_case_weight (const struct dictionary *,
99                                      const struct ccase *, bool *);
100 struct variable *dict_get_weight (const struct dictionary *);
101 void dict_set_weight (struct dictionary *, struct variable *);
102 struct fmt_spec dict_get_weight_format (const struct dictionary *);
103
104 /* Filter variable. */
105 struct variable *dict_get_filter (const struct dictionary *);
106 void dict_set_filter (struct dictionary *, struct variable *);
107
108 /* Case limit (N OF CASES). */
109 casenumber dict_get_case_limit (const struct dictionary *);
110 void dict_set_case_limit (struct dictionary *, casenumber);
111
112 /* Size of cases for this dictionary. */
113 const struct caseproto *dict_get_proto (const struct dictionary *);
114
115 /* Making this dictionary's cases smaller (if some variables were
116    deleted). */
117 size_t dict_count_values (const struct dictionary *,
118                           unsigned int exclude_classes);
119 void dict_compact_values (struct dictionary *);
120
121 /* SPLIT FILE variables.
122
123    SPLIT_NONE is used if and only if there are no split file variables. */
124 enum split_type
125   {
126     SPLIT_NONE,                 /* No split file variables. */
127     SPLIT_SEPARATE,             /* Produce separate output for each split. */
128     SPLIT_LAYERED,              /* Output splits in same table.  */
129   };
130 #define MAX_SPLITS 8
131 const struct variable *const *dict_get_split_vars (const struct dictionary *);
132 size_t dict_get_n_splits (const struct dictionary *);
133 enum split_type dict_get_split_type (const struct dictionary *);
134 void dict_set_split_vars (struct dictionary *,
135                           struct variable *const *, size_t n,
136                           enum split_type);
137 void dict_clear_split_vars (struct dictionary *);
138
139 /* File label. */
140 const char *dict_get_label (const struct dictionary *);
141 void dict_set_label (struct dictionary *, const char *);
142
143 /* Documents. */
144 #define DOC_LINE_LENGTH 80 /* Fixed length of document lines. */
145
146 const struct string_array *dict_get_documents (const struct dictionary *);
147 void dict_set_documents (struct dictionary *, const struct string_array *);
148 void dict_set_documents_string (struct dictionary *, const char *);
149 void dict_clear_documents (struct dictionary *);
150
151 bool dict_add_document_line (struct dictionary *, const char *,
152                              bool issue_warning);
153 size_t dict_get_document_n_lines (const struct dictionary *);
154 const char *dict_get_document_line (const struct dictionary *, size_t);
155
156 /* Vectors. */
157 bool dict_create_vector (struct dictionary *, const char *name,
158                          struct variable **, size_t n);
159 void dict_create_vector_assert (struct dictionary *, const char *name,
160                                 struct variable **, size_t n);
161 const struct vector *dict_get_vector (const struct dictionary *, size_t idx);
162 size_t dict_get_n_vectors (const struct dictionary *);
163 const struct vector *dict_lookup_vector (const struct dictionary *,
164                                          const char *name);
165 void dict_clear_vectors (struct dictionary *);
166
167 /* Multiple response sets. */
168 const struct mrset *dict_get_mrset (const struct dictionary *, size_t idx);
169 size_t dict_get_n_mrsets (const struct dictionary *);
170 const struct mrset *dict_lookup_mrset (const struct dictionary *,
171                                        const char *name);
172
173 bool dict_add_mrset (struct dictionary *, struct mrset *);
174 bool dict_delete_mrset (struct dictionary *, const char *name);
175 void dict_clear_mrsets (struct dictionary *);
176
177 /* Variable sets. */
178 const struct varset *dict_get_varset (const struct dictionary *, size_t idx);
179 size_t dict_get_n_varsets (const struct dictionary *);
180 const struct varset *dict_lookup_varset (const struct dictionary *,
181                                          const char *name);
182
183 bool dict_add_varset (struct dictionary *, struct varset *);
184 bool dict_delete_varset (struct dictionary *, const char *name);
185 void dict_clear_varsets (struct dictionary *);
186
187 /* Attributes. */
188 struct attrset *dict_get_attributes (const struct dictionary *);
189 void dict_set_attributes (struct dictionary *, const struct attrset *);
190 bool dict_has_attributes (const struct dictionary *);
191
192 /* Data encoding. */
193 const char *dict_get_encoding (const struct dictionary *d);
194
195 char *dict_id_is_valid__ (const struct dictionary *, const char *id)
196   WARN_UNUSED_RESULT;
197 bool dict_id_is_valid (const struct dictionary *, const char *id);
198
199 /* Functions to be called upon dictionary changes. */
200 struct dict_callbacks
201  {
202   void (*var_added) (struct dictionary *, int, void *);
203   void (*vars_deleted) (struct dictionary *, int dict_index, unsigned int n, void *);
204   void (*var_moved) (struct dictionary *, int new_dict_index, int old_dict_index, void *);
205   void (*var_changed) (struct dictionary *, int, unsigned int, const struct variable *, void *);
206   void (*weight_changed) (struct dictionary *, int, void *);
207   void (*filter_changed) (struct dictionary *, int, void *);
208   void (*split_changed) (struct dictionary *, void *);
209  };
210
211 void dict_set_callbacks (struct dictionary *, const struct dict_callbacks *,
212                          void *);
213 void dict_copy_callbacks (struct dictionary *, const struct dictionary *);
214
215 void dict_set_change_callback (struct dictionary *d,
216                                void (*changed) (struct dictionary *, void*),
217                                void *data);
218
219
220 /* Debug use only. */
221 void dict_dump (const struct dictionary *);
222
223 #endif /* data/dictionary.h */