66542469e321c630c87b2396c9d6ab0f9a84c3e2
[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 *);
31
32
33 /* Clearing and destroying dictionaries. */
34 void dict_clear (struct dictionary *);
35 void dict_destroy (struct dictionary *);
36
37 /* Common ways to access variables. */
38 struct variable *dict_lookup_var (const struct dictionary *, const char *);
39 struct variable *dict_lookup_var_assert (const struct dictionary *,
40                                          const char *);
41 struct variable *dict_get_var (const struct dictionary *, size_t position);
42 size_t dict_get_var_cnt (const struct dictionary *);
43
44 /* Other access to variables. */
45 bool dict_contains_var (const struct dictionary *, const struct variable *);
46 void dict_get_vars (const struct dictionary *,
47                     const struct variable ***vars, size_t *cnt,
48                     enum dict_class exclude);
49 void dict_get_vars_mutable (const struct dictionary *,
50                             struct variable ***vars, size_t *cnt,
51                             enum dict_class exclude);
52
53 /* Creating variables. */
54 struct variable *dict_create_var (struct dictionary *, const char *,
55                                   int width);
56 struct variable *dict_create_var_assert (struct dictionary *, const char *,
57                                          int width);
58 struct variable *dict_clone_var (struct dictionary *, const struct variable *);
59 struct variable *dict_clone_var_assert (struct dictionary *,
60                                         const struct variable *);
61 struct variable *dict_clone_var_as (struct dictionary *,
62                                     const struct variable *, const char *);
63 struct variable *dict_clone_var_as_assert (struct dictionary *,
64                                            const struct variable *,
65                                            const char *);
66
67 struct variable *dict_clone_var_in_place_assert (struct dictionary *,
68                                                  const struct variable *);
69
70 /* Deleting variables. */
71 void dict_delete_var (struct dictionary *, struct variable *);
72 void dict_delete_vars (struct dictionary *,
73                        struct variable *const *, size_t count);
74 void dict_delete_consecutive_vars (struct dictionary *,
75                                    size_t idx, size_t count);
76 void dict_delete_scratch_vars (struct dictionary *);
77
78 /* Changing the order of variables. */
79 void dict_reorder_var (struct dictionary *, struct variable *,
80                        size_t new_index);
81 void dict_reorder_vars (struct dictionary *,
82                         struct variable *const *, size_t count);
83
84 /* Variable names. */
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 /* Weight variable. */
93 double dict_get_case_weight (const struct dictionary *,
94                              const struct ccase *, bool *);
95 struct variable *dict_get_weight (const struct dictionary *);
96 void dict_set_weight (struct dictionary *, struct variable *);
97
98 /* Filter variable. */
99 struct variable *dict_get_filter (const struct dictionary *);
100 void dict_set_filter (struct dictionary *, struct variable *);
101
102 /* Case limit (N OF CASES). */
103 casenumber dict_get_case_limit (const struct dictionary *);
104 void dict_set_case_limit (struct dictionary *, casenumber);
105
106 /* Size of cases for this dictionary. */
107 const struct caseproto *dict_get_proto (const struct dictionary *);
108 int dict_get_next_value_idx (const struct dictionary *);
109 size_t dict_get_case_size (const struct dictionary *);
110
111 /* Making this dictionary's cases smaller (if some variables were
112    deleted). */
113 size_t dict_count_values (const struct dictionary *,
114                           unsigned int exclude_classes);
115 void dict_compact_values (struct dictionary *);
116 struct caseproto *dict_get_compacted_proto (const struct dictionary *,
117                                             unsigned int exclude_classes);
118
119 /* SPLIT FILE variables. */
120 const struct variable *const *dict_get_split_vars (const struct dictionary *);
121 size_t dict_get_split_cnt (const struct dictionary *);
122 void dict_set_split_vars (struct dictionary *,
123                           struct variable *const *, size_t cnt);
124
125 /* File label. */
126 const char *dict_get_label (const struct dictionary *);
127 void dict_set_label (struct dictionary *, const char *);
128
129 /* Documents. */
130 #define DOC_LINE_LENGTH 80 /* Fixed length of document lines. */
131
132 const struct string_array *dict_get_documents (const struct dictionary *);
133 void dict_set_documents (struct dictionary *, const struct string_array *);
134 void dict_set_documents_string (struct dictionary *, const char *);
135 void dict_clear_documents (struct dictionary *);
136
137 bool dict_add_document_line (struct dictionary *, const char *,
138                              bool issue_warning);
139 size_t dict_get_document_line_cnt (const struct dictionary *);
140 const char *dict_get_document_line (const struct dictionary *, size_t);
141
142 /* Vectors. */
143 bool dict_create_vector (struct dictionary *, const char *name,
144                          struct variable **, size_t cnt);
145 void dict_create_vector_assert (struct dictionary *, const char *name,
146                                 struct variable **, size_t cnt);
147 const struct vector *dict_get_vector (const struct dictionary *, size_t idx);
148 size_t dict_get_vector_cnt (const struct dictionary *);
149 const struct vector *dict_lookup_vector (const struct dictionary *,
150                                          const char *name);
151 void dict_clear_vectors (struct dictionary *);
152
153 /* Multiple response sets. */
154 const struct mrset *dict_get_mrset (const struct dictionary *, size_t idx);
155 size_t dict_get_n_mrsets (const struct dictionary *);
156 const struct mrset *dict_lookup_mrset (const struct dictionary *,
157                                        const char *name);
158
159 bool dict_add_mrset (struct dictionary *, struct mrset *);
160 bool dict_delete_mrset (struct dictionary *, const char *name);
161 void dict_clear_mrsets (struct dictionary *);
162
163 /* Attributes. */
164 struct attrset *dict_get_attributes (const struct dictionary *);
165 void dict_set_attributes (struct dictionary *, const struct attrset *);
166 bool dict_has_attributes (const struct dictionary *);
167
168 /* Data encoding. */
169 const char *dict_get_encoding (const struct dictionary *d);
170
171 bool dict_id_is_valid (const struct dictionary *, const char *id,
172                        bool issue_error);
173
174 /* Internal variables. */
175 struct variable *dict_create_internal_var (int case_idx, int width);
176 void dict_destroy_internal_var (struct variable *);
177
178 /* Functions to be called upon dictionary changes. */
179 struct dict_callbacks
180  {
181   void (*var_added) (struct dictionary *, int, void *);
182   void (*var_deleted) (struct dictionary *, const struct variable *,
183                        int dict_index, int case_index, void *);
184   void (*var_changed) (struct dictionary *, int, unsigned int, const struct variable *, void *);
185   void (*weight_changed) (struct dictionary *, int, void *);
186   void (*filter_changed) (struct dictionary *, int, void *);
187   void (*split_changed) (struct dictionary *, void *);
188  };
189
190 void dict_set_callbacks (struct dictionary *, const struct dict_callbacks *,
191                          void *);
192 void dict_copy_callbacks (struct dictionary *, const struct dictionary *);
193
194 void dict_set_change_callback (struct dictionary *d,
195                                void (*changed) (struct dictionary *, void*),
196                                void *data);
197
198
199 /* Debug use only. */
200 void dict_dump (const struct dictionary *);
201
202 #endif /* data/dictionary.h */