0b3c6cd273aae6e8a27357bf0a1a3104963cb0bb
[pspp] / src / data / dictionary.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2007, 2009, 2010 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 (void);
30 struct dictionary *dict_clone (const struct dictionary *);
31
32
33 /* Clearing and destroying dictionaries. */
34 void dict_clear (struct dictionary *);
35 void dict_clear_aux (struct dictionary *);
36 void dict_destroy (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_var_cnt (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 *cnt,
49                     enum dict_class exclude);
50 void dict_get_vars_mutable (const struct dictionary *,
51                             struct variable ***vars, size_t *cnt,
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 void dict_rename_var (struct dictionary *, struct variable *, const char *);
84 bool dict_rename_vars (struct dictionary *,
85                        struct variable **, char **new_names,
86                        size_t count, char **err_name);
87 bool dict_make_unique_var_name (const struct dictionary *, const char *hint,
88                                 unsigned long int *num_start,
89                                 char name[]);
90
91 /* Weight variable. */
92 double dict_get_case_weight (const struct dictionary *,
93                              const struct ccase *, bool *);
94 struct variable *dict_get_weight (const struct dictionary *);
95 void dict_set_weight (struct dictionary *, struct variable *);
96
97 /* Filter variable. */
98 struct variable *dict_get_filter (const struct dictionary *);
99 void dict_set_filter (struct dictionary *, struct variable *);
100
101 /* Case limit (N OF CASES). */
102 casenumber dict_get_case_limit (const struct dictionary *);
103 void dict_set_case_limit (struct dictionary *, casenumber);
104
105 /* Size of cases for this dictionary. */
106 const struct caseproto *dict_get_proto (const struct dictionary *);
107 int dict_get_next_value_idx (const struct dictionary *);
108 size_t dict_get_case_size (const struct dictionary *);
109
110 /* Making this dictionary's cases smaller (if some variables were
111    deleted). */
112 size_t dict_count_values (const struct dictionary *,
113                           unsigned int exclude_classes);
114 void dict_compact_values (struct dictionary *);
115 struct caseproto *dict_get_compacted_proto (const struct dictionary *,
116                                             unsigned int exclude_classes);
117
118 /* SPLIT FILE variables. */
119 const struct variable *const *dict_get_split_vars (const struct dictionary *);
120 size_t dict_get_split_cnt (const struct dictionary *);
121 void dict_set_split_vars (struct dictionary *,
122                           struct variable *const *, size_t cnt);
123 void dict_unset_split_var (struct dictionary *, struct variable *);
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 char *dict_get_documents (const struct dictionary *);
133 void dict_set_documents (struct dictionary *, const char *);
134 void dict_clear_documents (struct dictionary *);
135
136 void dict_add_document_line (struct dictionary *, const char *);
137 size_t dict_get_document_line_cnt (const struct dictionary *);
138 void dict_get_document_line (const struct dictionary *,
139                              size_t, struct string *);
140
141 /* Vectors. */
142 bool dict_create_vector (struct dictionary *, const char *name,
143                          struct variable **, size_t cnt);
144 void dict_create_vector_assert (struct dictionary *, const char *name,
145                                 struct variable **, size_t cnt);
146 const struct vector *dict_get_vector (const struct dictionary *, size_t idx);
147 size_t dict_get_vector_cnt (const struct dictionary *);
148 const struct vector *dict_lookup_vector (const struct dictionary *,
149                                          const char *name);
150 void dict_clear_vectors (struct dictionary *);
151
152 /* Attributes. */
153 struct attrset *dict_get_attributes (const struct dictionary *);
154 void dict_set_attributes (struct dictionary *, const struct attrset *);
155 bool dict_has_attributes (const struct dictionary *);
156
157 /* Data encoding. */
158 void dict_set_encoding (struct dictionary *d, const char *enc);
159 const char *dict_get_encoding (const struct dictionary *d);
160
161 /* Internal variables. */
162 struct variable *dict_create_internal_var (int case_idx, int width);
163 void dict_destroy_internal_var (struct variable *);
164
165 /* Functions to be called upon dictionary changes. */
166 struct dict_callbacks
167  {
168   void (*var_added) (struct dictionary *, int, void *);
169   void (*var_deleted) (struct dictionary *, int, int, int, void *);
170   void (*var_changed) (struct dictionary *, int, void *);
171   void (*var_resized) (struct dictionary *, int, int, void *);
172   void (*weight_changed) (struct dictionary *, int, void *);
173   void (*filter_changed) (struct dictionary *, int, void *);
174   void (*split_changed) (struct dictionary *, void *);
175   void (*var_display_width_changed) (struct dictionary *, int, void *);
176  };
177
178 void dict_set_callbacks (struct dictionary *, const struct dict_callbacks *,
179                          void *);
180 void dict_copy_callbacks (struct dictionary *, const struct dictionary *);
181
182 void dict_set_change_callback (struct dictionary *d,
183                                void (*changed) (struct dictionary *, void*),
184                                void *data);
185
186
187 /* Debug use only. */
188 void dict_dump (const struct dictionary *);
189
190 #endif /* data/dictionary.h */