1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "data/dictionary.h"
26 #include "data/attributes.h"
27 #include "data/case.h"
28 #include "data/identifier.h"
29 #include "data/mrset.h"
30 #include "data/settings.h"
31 #include "data/value-labels.h"
32 #include "data/vardict.h"
33 #include "data/variable.h"
34 #include "data/vector.h"
35 #include "libpspp/array.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/compiler.h"
38 #include "libpspp/hash-functions.h"
39 #include "libpspp/hmap.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/message.h"
42 #include "libpspp/misc.h"
43 #include "libpspp/pool.h"
44 #include "libpspp/str.h"
45 #include "libpspp/string-array.h"
47 #include "gl/intprops.h"
48 #include "gl/minmax.h"
49 #include "gl/xalloc.h"
50 #include "gl/xmemdup0.h"
53 #define _(msgid) gettext (msgid)
58 struct vardict_info *var; /* Variables. */
59 size_t var_cnt, var_cap; /* Number of variables, capacity. */
60 struct caseproto *proto; /* Prototype for dictionary cases
62 struct hmap name_map; /* Variable index by name. */
63 int next_value_idx; /* Index of next `union value' to allocate. */
64 const struct variable **split; /* SPLIT FILE vars. */
65 size_t split_cnt; /* SPLIT FILE count. */
66 struct variable *weight; /* WEIGHT variable. */
67 struct variable *filter; /* FILTER variable. */
68 casenumber case_limit; /* Current case limit (N command). */
69 char *label; /* File label. */
70 struct string_array documents; /* Documents. */
71 struct vector **vector; /* Vectors of variables. */
72 size_t vector_cnt; /* Number of vectors. */
73 struct attrset attributes; /* Custom attributes. */
74 struct mrset **mrsets; /* Multiple response sets. */
75 size_t n_mrsets; /* Number of multiple response sets. */
77 char *encoding; /* Character encoding of string data */
79 const struct dict_callbacks *callbacks; /* Callbacks on dictionary
81 void *cb_data ; /* Data passed to callbacks */
83 void (*changed) (struct dictionary *, void *); /* Generic change callback */
87 static void dict_unset_split_var (struct dictionary *, struct variable *);
88 static void dict_unset_mrset_var (struct dictionary *, struct variable *);
90 /* Returns the encoding for data in dictionary D. The return value is a
91 nonnull string that contains an IANA character set name. */
93 dict_get_encoding (const struct dictionary *d)
98 /* Returns true if UTF-8 string ID is an acceptable identifier in DICT's
99 encoding, false otherwise. If ISSUE_ERROR is true, issues an explanatory
100 error message on failure. */
102 dict_id_is_valid (const struct dictionary *dict, const char *id,
105 return id_is_valid (id, dict->encoding, issue_error);
109 dict_set_change_callback (struct dictionary *d,
110 void (*changed) (struct dictionary *, void*),
113 d->changed = changed;
114 d->changed_data = data;
117 /* Discards dictionary D's caseproto. (It will be regenerated
118 lazily, on demand.) */
120 invalidate_proto (struct dictionary *d)
122 caseproto_unref (d->proto);
126 /* Print a representation of dictionary D to stdout, for
127 debugging purposes. */
129 dict_dump (const struct dictionary *d)
132 for (i = 0 ; i < d->var_cnt ; ++i )
134 const struct variable *v = d->var[i].var;
135 printf ("Name: %s;\tdict_idx: %zu; case_idx: %zu\n",
137 var_get_dict_index (v),
138 var_get_case_index (v));
143 /* Associate CALLBACKS with DICT. Callbacks will be invoked whenever
144 the dictionary or any of the variables it contains are modified.
145 Each callback will get passed CALLBACK_DATA.
146 Any callback may be NULL, in which case it'll be ignored.
149 dict_set_callbacks (struct dictionary *dict,
150 const struct dict_callbacks *callbacks,
153 dict->callbacks = callbacks;
154 dict->cb_data = callback_data;
157 /* Shallow copy the callbacks from SRC to DEST */
159 dict_copy_callbacks (struct dictionary *dest,
160 const struct dictionary *src)
162 dest->callbacks = src->callbacks;
163 dest->cb_data = src->cb_data;
166 /* Creates and returns a new dictionary with the specified ENCODING. */
168 dict_create (const char *encoding)
170 struct dictionary *d = xzalloc (sizeof *d);
172 d->encoding = xstrdup (encoding);
173 hmap_init (&d->name_map);
174 attrset_init (&d->attributes);
179 /* Creates and returns a (deep) copy of an existing
182 The new dictionary's case indexes are copied from the old
183 dictionary. If the new dictionary won't be used to access
184 cases produced with the old dictionary, then the new
185 dictionary's case indexes should be compacted with
186 dict_compact_values to save space.
188 Callbacks are not cloned. */
190 dict_clone (const struct dictionary *s)
192 struct dictionary *d;
195 d = dict_create (s->encoding);
197 for (i = 0; i < s->var_cnt; i++)
199 struct variable *sv = s->var[i].var;
200 struct variable *dv = dict_clone_var_assert (d, sv);
203 for (i = 0; i < var_get_short_name_cnt (sv); i++)
204 var_set_short_name (dv, i, var_get_short_name (sv, i));
206 var_get_vardict (dv)->case_index = var_get_vardict (sv)->case_index;
209 d->next_value_idx = s->next_value_idx;
211 d->split_cnt = s->split_cnt;
212 if (d->split_cnt > 0)
214 d->split = xnmalloc (d->split_cnt, sizeof *d->split);
215 for (i = 0; i < d->split_cnt; i++)
216 d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
219 if (s->weight != NULL)
220 dict_set_weight (d, dict_lookup_var_assert (d, var_get_name (s->weight)));
222 if (s->filter != NULL)
223 dict_set_filter (d, dict_lookup_var_assert (d, var_get_name (s->filter)));
225 d->case_limit = s->case_limit;
226 dict_set_label (d, dict_get_label (s));
227 dict_set_documents (d, dict_get_documents (s));
229 d->vector_cnt = s->vector_cnt;
230 d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
231 for (i = 0; i < s->vector_cnt; i++)
232 d->vector[i] = vector_clone (s->vector[i], s, d);
234 dict_set_attributes (d, dict_get_attributes (s));
236 for (i = 0; i < s->n_mrsets; i++)
238 const struct mrset *old = s->mrsets[i];
242 /* Clone old mrset, then replace vars from D by vars from S. */
243 new = mrset_clone (old);
244 for (j = 0; j < new->n_vars; j++)
245 new->vars[j] = dict_lookup_var_assert (d, var_get_name (new->vars[j]));
247 dict_add_mrset (d, new);
253 /* Clears the contents from a dictionary without destroying the
254 dictionary itself. */
256 dict_clear (struct dictionary *d)
258 /* FIXME? Should we really clear case_limit, label, documents?
259 Others are necessarily cleared by deleting all the variables.*/
260 while (d->var_cnt > 0 )
262 dict_delete_var (d, d->var[d->var_cnt - 1].var);
267 d->var_cnt = d->var_cap = 0;
268 invalidate_proto (d);
269 hmap_clear (&d->name_map);
270 d->next_value_idx = 0;
271 dict_set_split_vars (d, NULL, 0);
272 dict_set_weight (d, NULL);
273 dict_set_filter (d, NULL);
277 string_array_clear (&d->documents);
278 dict_clear_vectors (d);
279 attrset_clear (&d->attributes);
282 /* Clears a dictionary and destroys it. */
284 dict_destroy (struct dictionary *d)
288 /* In general, we don't want callbacks occuring, if the dictionary
289 is being destroyed */
290 d->callbacks = NULL ;
293 string_array_destroy (&d->documents);
294 hmap_destroy (&d->name_map);
295 attrset_destroy (&d->attributes);
296 dict_clear_mrsets (d);
302 /* Returns the number of variables in D. */
304 dict_get_var_cnt (const struct dictionary *d)
309 /* Returns the variable in D with dictionary index IDX, which
310 must be between 0 and the count returned by
311 dict_get_var_cnt(), exclusive. */
313 dict_get_var (const struct dictionary *d, size_t idx)
315 assert (idx < d->var_cnt);
317 return d->var[idx].var;
320 /* Sets *VARS to an array of pointers to variables in D and *CNT
321 to the number of variables in *D. All variables are returned
322 except for those, if any, in the classes indicated by EXCLUDE.
323 (There is no point in putting DC_SYSTEM in EXCLUDE as
324 dictionaries never include system variables.) */
326 dict_get_vars (const struct dictionary *d, const struct variable ***vars,
327 size_t *cnt, enum dict_class exclude)
329 dict_get_vars_mutable (d, (struct variable ***) vars, cnt, exclude);
332 /* Sets *VARS to an array of pointers to variables in D and *CNT
333 to the number of variables in *D. All variables are returned
334 except for those, if any, in the classes indicated by EXCLUDE.
335 (There is no point in putting DC_SYSTEM in EXCLUDE as
336 dictionaries never include system variables.) */
338 dict_get_vars_mutable (const struct dictionary *d, struct variable ***vars,
339 size_t *cnt, enum dict_class exclude)
344 assert (exclude == (exclude & DC_ALL));
347 for (i = 0; i < d->var_cnt; i++)
349 enum dict_class class = var_get_dict_class (d->var[i].var);
350 if (!(class & exclude))
354 *vars = xnmalloc (count, sizeof **vars);
356 for (i = 0; i < d->var_cnt; i++)
358 enum dict_class class = var_get_dict_class (d->var[i].var);
359 if (!(class & exclude))
360 (*vars)[(*cnt)++] = d->var[i].var;
362 assert (*cnt == count);
365 static struct variable *
366 add_var_with_case_index (struct dictionary *d, struct variable *v,
369 struct vardict_info *vardict;
371 assert (case_index >= d->next_value_idx);
373 /* Update dictionary. */
374 if (d->var_cnt >= d->var_cap)
378 d->var = x2nrealloc (d->var, &d->var_cap, sizeof *d->var);
379 hmap_clear (&d->name_map);
380 for (i = 0; i < d->var_cnt; i++)
382 var_set_vardict (d->var[i].var, &d->var[i]);
383 hmap_insert_fast (&d->name_map, &d->var[i].name_node,
384 d->var[i].name_node.hash);
388 vardict = &d->var[d->var_cnt++];
391 hmap_insert (&d->name_map, &vardict->name_node,
392 utf8_hash_case_string (var_get_name (v), 0));
393 vardict->case_index = case_index;
394 var_set_vardict (v, vardict);
396 if ( d->changed ) d->changed (d, d->changed_data);
397 if ( d->callbacks && d->callbacks->var_added )
398 d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
400 invalidate_proto (d);
401 d->next_value_idx = case_index + 1;
406 static struct variable *
407 add_var (struct dictionary *d, struct variable *v)
409 return add_var_with_case_index (d, v, d->next_value_idx);
412 /* Creates and returns a new variable in D with the given NAME
413 and WIDTH. Returns a null pointer if the given NAME would
414 duplicate that of an existing variable in the dictionary. */
416 dict_create_var (struct dictionary *d, const char *name, int width)
418 return (dict_lookup_var (d, name) == NULL
419 ? dict_create_var_assert (d, name, width)
423 /* Creates and returns a new variable in D with the given NAME
424 and WIDTH. Assert-fails if the given NAME would duplicate
425 that of an existing variable in the dictionary. */
427 dict_create_var_assert (struct dictionary *d, const char *name, int width)
429 assert (dict_lookup_var (d, name) == NULL);
430 return add_var (d, var_create (name, width));
433 /* Creates and returns a new variable in D, as a copy of existing variable
434 OLD_VAR, which need not be in D or in any dictionary. Returns a null
435 pointer if OLD_VAR's name would duplicate that of an existing variable in
438 dict_clone_var (struct dictionary *d, const struct variable *old_var)
440 return dict_clone_var_as (d, old_var, var_get_name (old_var));
443 /* Creates and returns a new variable in D, as a copy of existing variable
444 OLD_VAR, which need not be in D or in any dictionary. Assert-fails if
445 OLD_VAR's name would duplicate that of an existing variable in the
448 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var)
450 return dict_clone_var_as_assert (d, old_var, var_get_name (old_var));
453 /* Creates and returns a new variable in D with name NAME, as a copy of
454 existing variable OLD_VAR, which need not be in D or in any dictionary.
455 Returns a null pointer if the given NAME would duplicate that of an existing
456 variable in the dictionary. */
458 dict_clone_var_as (struct dictionary *d, const struct variable *old_var,
461 return (dict_lookup_var (d, name) == NULL
462 ? dict_clone_var_as_assert (d, old_var, name)
466 /* Creates and returns a new variable in D with name NAME, as a copy of
467 existing variable OLD_VAR, which need not be in D or in any dictionary.
468 Assert-fails if the given NAME would duplicate that of an existing variable
469 in the dictionary. */
471 dict_clone_var_as_assert (struct dictionary *d, const struct variable *old_var,
474 struct variable *new_var = var_clone (old_var);
475 assert (dict_lookup_var (d, name) == NULL);
476 var_set_name (new_var, name);
477 return add_var (d, new_var);
481 dict_clone_var_in_place_assert (struct dictionary *d,
482 const struct variable *old_var)
484 assert (dict_lookup_var (d, var_get_name (old_var)) == NULL);
485 return add_var_with_case_index (d, var_clone (old_var),
486 var_get_case_index (old_var));
489 /* Returns the variable named NAME in D, or a null pointer if no
490 variable has that name. */
492 dict_lookup_var (const struct dictionary *d, const char *name)
494 struct vardict_info *vardict;
496 HMAP_FOR_EACH_WITH_HASH (vardict, struct vardict_info, name_node,
497 utf8_hash_case_string (name, 0), &d->name_map)
499 struct variable *var = vardict->var;
500 if (!utf8_strcasecmp (var_get_name (var), name))
507 /* Returns the variable named NAME in D. Assert-fails if no
508 variable has that name. */
510 dict_lookup_var_assert (const struct dictionary *d, const char *name)
512 struct variable *v = dict_lookup_var (d, name);
517 /* Returns true if variable V is in dictionary D,
520 dict_contains_var (const struct dictionary *d, const struct variable *v)
522 return (var_has_vardict (v)
523 && vardict_get_dictionary (var_get_vardict (v)) == d);
526 /* Compares two double pointers to variables, which should point
527 to elements of a struct dictionary's `var' member array. */
529 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED)
531 struct variable *const *a = a_;
532 struct variable *const *b = b_;
534 return *a < *b ? -1 : *a > *b;
538 unindex_var (struct dictionary *d, struct vardict_info *vardict)
540 hmap_delete (&d->name_map, &vardict->name_node);
543 /* This function assumes that vardict->name_node.hash is valid, that is, that
544 its name has not changed since it was hashed (rename_var() updates this
545 hash along with the name itself). */
547 reindex_var (struct dictionary *d, struct vardict_info *vardict)
549 struct variable *var = vardict->var;
551 var_set_vardict (var, vardict);
552 hmap_insert_fast (&d->name_map, &vardict->name_node,
553 vardict->name_node.hash);
555 if ( d->changed ) d->changed (d, d->changed_data);
556 if ( d->callbacks && d->callbacks->var_changed )
557 d->callbacks->var_changed (d, var_get_dict_index (var), d->cb_data);
560 /* Sets the case_index in V's vardict to CASE_INDEX. */
562 set_var_case_index (struct variable *v, int case_index)
564 var_get_vardict (v)->case_index = case_index;
567 /* Removes the dictionary variables with indexes from FROM to TO (exclusive)
570 unindex_vars (struct dictionary *d, size_t from, size_t to)
574 for (i = from; i < to; i++)
575 unindex_var (d, &d->var[i]);
578 /* Re-sets the dict_index in the dictionary variables with
579 indexes from FROM to TO (exclusive). */
581 reindex_vars (struct dictionary *d, size_t from, size_t to)
585 for (i = from; i < to; i++)
586 reindex_var (d, &d->var[i]);
589 /* Deletes variable V from dictionary D and frees V.
591 This is a very bad idea if there might be any pointers to V
592 from outside D. In general, no variable in the active dataset's
593 dictionary should be deleted when any transformations are
594 active on the dictionary's dataset, because those
595 transformations might reference the deleted variable. The
596 safest time to delete a variable is just after a procedure has
597 been executed, as done by DELETE VARIABLES.
599 Pointers to V within D are not a problem, because
600 dict_delete_var() knows to remove V from split variables,
601 weights, filters, etc. */
603 dict_delete_var (struct dictionary *d, struct variable *v)
605 int dict_index = var_get_dict_index (v);
606 const int case_index = var_get_case_index (v);
608 assert (dict_contains_var (d, v));
610 dict_unset_split_var (d, v);
611 dict_unset_mrset_var (d, v);
614 dict_set_weight (d, NULL);
617 dict_set_filter (d, NULL);
619 dict_clear_vectors (d);
621 /* Remove V from var array. */
622 unindex_vars (d, dict_index, d->var_cnt);
623 remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
626 /* Update dict_index for each affected variable. */
627 reindex_vars (d, dict_index, d->var_cnt);
630 var_clear_vardict (v);
632 if ( d->changed ) d->changed (d, d->changed_data);
634 invalidate_proto (d);
635 if (d->callbacks && d->callbacks->var_deleted )
636 d->callbacks->var_deleted (d, v, dict_index, case_index, d->cb_data);
641 /* Deletes the COUNT variables listed in VARS from D. This is
642 unsafe; see the comment on dict_delete_var() for details. */
644 dict_delete_vars (struct dictionary *d,
645 struct variable *const *vars, size_t count)
647 /* FIXME: this can be done in O(count) time, but this algorithm
649 assert (count == 0 || vars != NULL);
652 dict_delete_var (d, *vars++);
655 /* Deletes the COUNT variables in D starting at index IDX. This
656 is unsafe; see the comment on dict_delete_var() for
659 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
661 /* FIXME: this can be done in O(count) time, but this algorithm
663 assert (idx + count <= d->var_cnt);
666 dict_delete_var (d, d->var[idx].var);
669 /* Deletes scratch variables from dictionary D. */
671 dict_delete_scratch_vars (struct dictionary *d)
675 /* FIXME: this can be done in O(count) time, but this algorithm
677 for (i = 0; i < d->var_cnt; )
678 if (var_get_dict_class (d->var[i].var) == DC_SCRATCH)
679 dict_delete_var (d, d->var[i].var);
684 /* Moves V to 0-based position IDX in D. Other variables in D,
685 if any, retain their relative positions. Runs in time linear
686 in the distance moved. */
688 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index)
690 size_t old_index = var_get_dict_index (v);
692 assert (new_index < d->var_cnt);
694 unindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
695 move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
696 reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
699 /* Reorders the variables in D, placing the COUNT variables
700 listed in ORDER in that order at the beginning of D. The
701 other variables in D, if any, retain their relative
704 dict_reorder_vars (struct dictionary *d,
705 struct variable *const *order, size_t count)
707 struct vardict_info *new_var;
710 assert (count == 0 || order != NULL);
711 assert (count <= d->var_cnt);
713 new_var = xnmalloc (d->var_cap, sizeof *new_var);
715 /* Add variables in ORDER to new_var. */
716 for (i = 0; i < count; i++)
718 struct vardict_info *old_var;
720 assert (dict_contains_var (d, order[i]));
722 old_var = var_get_vardict (order[i]);
723 new_var[i] = *old_var;
724 old_var->dict = NULL;
727 /* Add remaining variables to new_var. */
728 for (i = 0; i < d->var_cnt; i++)
729 if (d->var[i].dict != NULL)
730 new_var[count++] = d->var[i];
731 assert (count == d->var_cnt);
733 /* Replace old vardicts by new ones. */
737 hmap_clear (&d->name_map);
738 reindex_vars (d, 0, d->var_cnt);
741 /* Changes the name of variable V that is currently in a dictionary to
744 rename_var (struct variable *v, const char *new_name)
746 struct vardict_info *vardict = var_get_vardict (v);
747 var_clear_vardict (v);
748 var_set_name (v, new_name);
749 vardict->name_node.hash = utf8_hash_case_string (new_name, 0);
750 var_set_vardict (v, vardict);
753 /* Changes the name of V in D to name NEW_NAME. Assert-fails if
754 a variable named NEW_NAME is already in D, except that
755 NEW_NAME may be the same as V's existing name. */
757 dict_rename_var (struct dictionary *d, struct variable *v,
758 const char *new_name)
760 assert (!utf8_strcasecmp (var_get_name (v), new_name)
761 || dict_lookup_var (d, new_name) == NULL);
763 unindex_var (d, var_get_vardict (v));
764 rename_var (v, new_name);
765 reindex_var (d, var_get_vardict (v));
767 if (settings_get_algorithm () == ENHANCED)
768 var_clear_short_names (v);
770 if ( d->changed ) d->changed (d, d->changed_data);
771 if ( d->callbacks && d->callbacks->var_changed )
772 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
775 /* Renames COUNT variables specified in VARS to the names given
776 in NEW_NAMES within dictionary D. If the renaming would
777 result in a duplicate variable name, returns false and stores a
778 name that would be duplicated into *ERR_NAME (if ERR_NAME is
779 non-null). Otherwise, the renaming is successful, and true
782 dict_rename_vars (struct dictionary *d,
783 struct variable **vars, char **new_names, size_t count,
790 assert (count == 0 || vars != NULL);
791 assert (count == 0 || new_names != NULL);
793 /* Save the names of the variables to be renamed. */
794 pool = pool_create ();
795 old_names = pool_nalloc (pool, count, sizeof *old_names);
796 for (i = 0; i < count; i++)
797 old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
799 /* Remove the variables to be renamed from the name hash,
801 for (i = 0; i < count; i++)
803 unindex_var (d, var_get_vardict (vars[i]));
804 rename_var (vars[i], new_names[i]);
807 /* Add the renamed variables back into the name hash,
808 checking for conflicts. */
809 for (i = 0; i < count; i++)
811 if (dict_lookup_var (d, var_get_name (vars[i])) != NULL)
813 /* There is a name conflict.
814 Back out all the name changes that have already
815 taken place, and indicate failure. */
817 if (err_name != NULL)
818 *err_name = new_names[i];
820 for (i = 0; i < fail_idx; i++)
821 unindex_var (d, var_get_vardict (vars[i]));
823 for (i = 0; i < count; i++)
825 rename_var (vars[i], old_names[i]);
826 reindex_var (d, var_get_vardict (vars[i]));
832 reindex_var (d, var_get_vardict (vars[i]));
835 /* Clear short names. */
836 if (settings_get_algorithm () == ENHANCED)
837 for (i = 0; i < count; i++)
838 var_clear_short_names (vars[i]);
844 /* Returns true if a variable named NAME may be inserted in DICT;
845 that is, if there is not already a variable with that name in
846 DICT and if NAME is not a reserved word. (The caller's checks
847 have already verified that NAME is otherwise acceptable as a
850 var_name_is_insertable (const struct dictionary *dict, const char *name)
852 return (dict_lookup_var (dict, name) == NULL
853 && lex_id_to_token (ss_cstr (name)) == T_ID);
857 make_hinted_name (const struct dictionary *dict, const char *hint)
859 size_t hint_len = strlen (hint);
860 bool dropped = false;
865 /* The allocation size here is OK: characters that are copied directly fit
866 OK, and characters that are not copied directly are replaced by a single
867 '_' byte. If u8_mbtouc() replaces bad input by 0xfffd, then that will get
868 replaced by '_' too. */
869 root = rp = xmalloc (hint_len + 1);
870 for (ofs = 0; ofs < hint_len; ofs += mblen)
874 mblen = u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, hint + ofs),
877 ? lex_uc_is_id1 (uc) && uc != '$'
878 : lex_uc_is_idn (uc))
885 rp += u8_uctomb (CHAR_CAST (uint8_t *, rp), uc, 6);
896 if (var_name_is_insertable (dict, root))
899 for (i = 0; i < ULONG_MAX; i++)
901 char suffix[INT_BUFSIZE_BOUND (i) + 1];
905 if (!str_format_26adic (i + 1, &suffix[1], sizeof suffix - 1))
908 name = utf8_encoding_concat (root, suffix, dict->encoding, 64);
909 if (var_name_is_insertable (dict, name))
924 make_numeric_name (const struct dictionary *dict, unsigned long int *num_start)
926 unsigned long int number;
928 for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
932 char name[3 + INT_STRLEN_BOUND (number) + 1];
934 sprintf (name, "VAR%03lu", number);
935 if (dict_lookup_var (dict, name) == NULL)
937 if (num_start != NULL)
938 *num_start = number + 1;
939 return xstrdup (name);
947 /* Devises and returns a variable name unique within DICT. The variable name
948 is owned by the caller, which must free it with free() when it is no longer
951 HINT, if it is non-null, is used as a suggestion that will be
952 modified for suitability as a variable name and for
955 If HINT is null or entirely unsuitable, a name in the form
956 "VAR%03d" will be generated, where the smallest unused integer
957 value is used. If NUM_START is non-null, then its value is
958 used as the minimum numeric value to check, and it is updated
959 to the next value to be checked.
962 dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
963 unsigned long int *num_start)
967 char *hinted_name = make_hinted_name (dict, hint);
968 if (hinted_name != NULL)
971 return make_numeric_name (dict, num_start);
974 /* Returns the weighting variable in dictionary D, or a null
975 pointer if the dictionary is unweighted. */
977 dict_get_weight (const struct dictionary *d)
979 assert (d->weight == NULL || dict_contains_var (d, d->weight));
984 /* Returns the value of D's weighting variable in case C, except
985 that a negative weight is returned as 0. Returns 1 if the
986 dictionary is unweighted. Will warn about missing, negative,
987 or zero values if *WARN_ON_INVALID is true. The function will
988 set *WARN_ON_INVALID to false if an invalid weight is
991 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
992 bool *warn_on_invalid)
996 if (d->weight == NULL)
1000 double w = case_num (c, d->weight);
1001 if (w < 0.0 || var_is_num_missing (d->weight, w, MV_ANY))
1003 if ( w == 0.0 && warn_on_invalid != NULL && *warn_on_invalid ) {
1004 *warn_on_invalid = false;
1005 msg (SW, _("At least one case in the data file had a weight value "
1006 "that was user-missing, system-missing, zero, or "
1007 "negative. These case(s) were ignored."));
1013 /* Sets the weighting variable of D to V, or turning off
1014 weighting if V is a null pointer. */
1016 dict_set_weight (struct dictionary *d, struct variable *v)
1018 assert (v == NULL || dict_contains_var (d, v));
1019 assert (v == NULL || var_is_numeric (v));
1023 if (d->changed) d->changed (d, d->changed_data);
1024 if ( d->callbacks && d->callbacks->weight_changed )
1025 d->callbacks->weight_changed (d,
1026 v ? var_get_dict_index (v) : -1,
1030 /* Returns the filter variable in dictionary D (see cmd_filter())
1031 or a null pointer if the dictionary is unfiltered. */
1033 dict_get_filter (const struct dictionary *d)
1035 assert (d->filter == NULL || dict_contains_var (d, d->filter));
1040 /* Sets V as the filter variable for dictionary D. Passing a
1041 null pointer for V turn off filtering. */
1043 dict_set_filter (struct dictionary *d, struct variable *v)
1045 assert (v == NULL || dict_contains_var (d, v));
1046 assert (v == NULL || var_is_numeric (v));
1050 if (d->changed) d->changed (d, d->changed_data);
1051 if ( d->callbacks && d->callbacks->filter_changed )
1052 d->callbacks->filter_changed (d,
1053 v ? var_get_dict_index (v) : -1,
1057 /* Returns the case limit for dictionary D, or zero if the number
1058 of cases is unlimited. */
1060 dict_get_case_limit (const struct dictionary *d)
1062 return d->case_limit;
1065 /* Sets CASE_LIMIT as the case limit for dictionary D. Use
1066 0 for CASE_LIMIT to indicate no limit. */
1068 dict_set_case_limit (struct dictionary *d, casenumber case_limit)
1070 d->case_limit = case_limit;
1073 /* Returns the prototype used for cases created by dictionary D. */
1074 const struct caseproto *
1075 dict_get_proto (const struct dictionary *d_)
1077 struct dictionary *d = CONST_CAST (struct dictionary *, d_);
1078 if (d->proto == NULL)
1082 d->proto = caseproto_create ();
1083 d->proto = caseproto_reserve (d->proto, d->var_cnt);
1084 for (i = 0; i < d->var_cnt; i++)
1085 d->proto = caseproto_set_width (d->proto,
1086 var_get_case_index (d->var[i].var),
1087 var_get_width (d->var[i].var));
1092 /* Returns the case index of the next value to be added to D.
1093 This value is the number of `union value's that need to be
1094 allocated to store a case for dictionary D. */
1096 dict_get_next_value_idx (const struct dictionary *d)
1098 return d->next_value_idx;
1101 /* Returns the number of bytes needed to store a case for
1104 dict_get_case_size (const struct dictionary *d)
1106 return sizeof (union value) * dict_get_next_value_idx (d);
1109 /* Reassigns values in dictionary D so that fragmentation is
1112 dict_compact_values (struct dictionary *d)
1116 d->next_value_idx = 0;
1117 for (i = 0; i < d->var_cnt; i++)
1119 struct variable *v = d->var[i].var;
1120 set_var_case_index (v, d->next_value_idx++);
1122 invalidate_proto (d);
1125 /* Returns the number of values occupied by the variables in
1126 dictionary D. All variables are considered if EXCLUDE_CLASSES
1127 is 0, or it may contain one or more of (1u << DC_ORDINARY),
1128 (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to exclude the
1129 corresponding type of variable.
1131 The return value may be less than the number of values in one
1132 of dictionary D's cases (as returned by
1133 dict_get_next_value_idx) even if E is 0, because there may be
1134 gaps in D's cases due to deleted variables. */
1136 dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
1141 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1143 | (1u << DC_SCRATCH))) == 0);
1146 for (i = 0; i < d->var_cnt; i++)
1148 enum dict_class class = var_get_dict_class (d->var[i].var);
1149 if (!(exclude_classes & (1u << class)))
1155 /* Returns the case prototype that would result after deleting
1156 all variables from D that are not in one of the
1157 EXCLUDE_CLASSES and compacting the dictionary with
1160 The caller must unref the returned caseproto when it is no
1163 dict_get_compacted_proto (const struct dictionary *d,
1164 unsigned int exclude_classes)
1166 struct caseproto *proto;
1169 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1171 | (1u << DC_SCRATCH))) == 0);
1173 proto = caseproto_create ();
1174 for (i = 0; i < d->var_cnt; i++)
1176 struct variable *v = d->var[i].var;
1177 if (!(exclude_classes & (1u << var_get_dict_class (v))))
1178 proto = caseproto_add_width (proto, var_get_width (v));
1183 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
1184 dict_get_split_cnt() to determine how many SPLIT FILE vars
1185 there are. Returns a null pointer if and only if there are no
1187 const struct variable *const *
1188 dict_get_split_vars (const struct dictionary *d)
1193 /* Returns the number of SPLIT FILE vars. */
1195 dict_get_split_cnt (const struct dictionary *d)
1197 return d->split_cnt;
1200 /* Removes variable V, which must be in D, from D's set of split
1203 dict_unset_split_var (struct dictionary *d, struct variable *v)
1207 assert (dict_contains_var (d, v));
1209 orig_count = d->split_cnt;
1210 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
1211 &v, compare_var_ptrs, NULL);
1212 if (orig_count != d->split_cnt)
1214 if (d->changed) d->changed (d, d->changed_data);
1215 /* We changed the set of split variables so invoke the
1217 if (d->callbacks && d->callbacks->split_changed)
1218 d->callbacks->split_changed (d, d->cb_data);
1222 /* Sets CNT split vars SPLIT in dictionary D. */
1224 dict_set_split_vars (struct dictionary *d,
1225 struct variable *const *split, size_t cnt)
1227 assert (cnt == 0 || split != NULL);
1232 d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
1233 memcpy (d->split, split, cnt * sizeof *d->split);
1241 if (d->changed) d->changed (d, d->changed_data);
1242 if ( d->callbacks && d->callbacks->split_changed )
1243 d->callbacks->split_changed (d, d->cb_data);
1246 /* Returns the file label for D, or a null pointer if D is
1247 unlabeled (see cmd_file_label()). */
1249 dict_get_label (const struct dictionary *d)
1254 /* Sets D's file label to LABEL, truncating it to at most 60 bytes in D's
1257 Removes D's label if LABEL is null or the empty string. */
1259 dict_set_label (struct dictionary *d, const char *label)
1262 if (label == NULL || label[0] == '\0')
1265 d->label = utf8_encoding_trunc (label, d->encoding, 60);
1268 /* Returns the documents for D, as an UTF-8 encoded string_array. The
1269 return value is always nonnull; if there are no documents then the
1270 string_arary is empty.*/
1271 const struct string_array *
1272 dict_get_documents (const struct dictionary *d)
1274 return &d->documents;
1277 /* Replaces the documents for D by NEW_DOCS, a UTF-8 encoded string_array. */
1279 dict_set_documents (struct dictionary *d, const struct string_array *new_docs)
1283 dict_clear_documents (d);
1285 for (i = 0; i < new_docs->n; i++)
1286 dict_add_document_line (d, new_docs->strings[i], false);
1289 /* Replaces the documents for D by UTF-8 encoded string NEW_DOCS, dividing it
1290 into individual lines at new-line characters. Each line is truncated to at
1291 most DOC_LINE_LENGTH bytes in D's encoding. */
1293 dict_set_documents_string (struct dictionary *d, const char *new_docs)
1297 dict_clear_documents (d);
1298 for (s = new_docs; *s != '\0'; )
1300 size_t len = strcspn (s, "\n");
1301 char *line = xmemdup0 (s, len);
1302 dict_add_document_line (d, line, false);
1311 /* Drops the documents from dictionary D. */
1313 dict_clear_documents (struct dictionary *d)
1315 string_array_clear (&d->documents);
1318 /* Appends the UTF-8 encoded LINE to the documents in D. LINE will be
1319 truncated so that it is no more than 80 bytes in the dictionary's
1320 encoding. If this causes some text to be lost, and ISSUE_WARNING is true,
1321 then a warning will be issued. */
1323 dict_add_document_line (struct dictionary *d, const char *line,
1329 trunc_len = utf8_encoding_trunc_len (line, d->encoding, DOC_LINE_LENGTH);
1330 truncated = line[trunc_len] != '\0';
1331 if (truncated && issue_warning)
1333 /* Note to translators: "bytes" is correct, not characters */
1334 msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH);
1337 string_array_append_nocopy (&d->documents, xmemdup0 (line, trunc_len));
1342 /* Returns the number of document lines in dictionary D. */
1344 dict_get_document_line_cnt (const struct dictionary *d)
1346 return d->documents.n;
1349 /* Returns document line number IDX in dictionary D. The caller must not
1350 modify or free the returned string. */
1352 dict_get_document_line (const struct dictionary *d, size_t idx)
1354 assert (idx < d->documents.n);
1355 return d->documents.strings[idx];
1358 /* Creates in D a vector named NAME that contains the CNT
1359 variables in VAR. Returns true if successful, or false if a
1360 vector named NAME already exists in D. */
1362 dict_create_vector (struct dictionary *d,
1364 struct variable **var, size_t cnt)
1369 for (i = 0; i < cnt; i++)
1370 assert (dict_contains_var (d, var[i]));
1372 if (dict_lookup_vector (d, name) == NULL)
1374 d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1375 d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1382 /* Creates in D a vector named NAME that contains the CNT
1383 variables in VAR. A vector named NAME must not already exist
1386 dict_create_vector_assert (struct dictionary *d,
1388 struct variable **var, size_t cnt)
1390 assert (dict_lookup_vector (d, name) == NULL);
1391 dict_create_vector (d, name, var, cnt);
1394 /* Returns the vector in D with index IDX, which must be less
1395 than dict_get_vector_cnt (D). */
1396 const struct vector *
1397 dict_get_vector (const struct dictionary *d, size_t idx)
1399 assert (idx < d->vector_cnt);
1401 return d->vector[idx];
1404 /* Returns the number of vectors in D. */
1406 dict_get_vector_cnt (const struct dictionary *d)
1408 return d->vector_cnt;
1411 /* Looks up and returns the vector within D with the given
1413 const struct vector *
1414 dict_lookup_vector (const struct dictionary *d, const char *name)
1417 for (i = 0; i < d->vector_cnt; i++)
1418 if (!utf8_strcasecmp (vector_get_name (d->vector[i]), name))
1419 return d->vector[i];
1423 /* Deletes all vectors from D. */
1425 dict_clear_vectors (struct dictionary *d)
1429 for (i = 0; i < d->vector_cnt; i++)
1430 vector_destroy (d->vector[i]);
1437 /* Multiple response sets. */
1439 /* Returns the multiple response set in DICT with index IDX, which must be
1440 between 0 and the count returned by dict_get_n_mrsets(), exclusive. */
1441 const struct mrset *
1442 dict_get_mrset (const struct dictionary *dict, size_t idx)
1444 assert (idx < dict->n_mrsets);
1445 return dict->mrsets[idx];
1448 /* Returns the number of multiple response sets in DICT. */
1450 dict_get_n_mrsets (const struct dictionary *dict)
1452 return dict->n_mrsets;
1455 /* Looks for a multiple response set named NAME in DICT. If it finds one,
1456 returns its index; otherwise, returns SIZE_MAX. */
1458 dict_lookup_mrset_idx (const struct dictionary *dict, const char *name)
1462 for (i = 0; i < dict->n_mrsets; i++)
1463 if (!utf8_strcasecmp (name, dict->mrsets[i]->name))
1469 /* Looks for a multiple response set named NAME in DICT. If it finds one,
1470 returns it; otherwise, returns NULL. */
1471 const struct mrset *
1472 dict_lookup_mrset (const struct dictionary *dict, const char *name)
1474 size_t idx = dict_lookup_mrset_idx (dict, name);
1475 return idx != SIZE_MAX ? dict->mrsets[idx] : NULL;
1478 /* Adds MRSET to DICT, replacing any existing set with the same name. Returns
1479 true if a set was replaced, false if none existed with the specified name.
1481 Ownership of MRSET is transferred to DICT. */
1483 dict_add_mrset (struct dictionary *dict, struct mrset *mrset)
1487 assert (mrset_ok (mrset, dict));
1489 idx = dict_lookup_mrset_idx (dict, mrset->name);
1490 if (idx == SIZE_MAX)
1492 dict->mrsets = xrealloc (dict->mrsets,
1493 (dict->n_mrsets + 1) * sizeof *dict->mrsets);
1494 dict->mrsets[dict->n_mrsets++] = mrset;
1499 mrset_destroy (dict->mrsets[idx]);
1500 dict->mrsets[idx] = mrset;
1505 /* Looks for a multiple response set in DICT named NAME. If found, removes it
1506 from DICT and returns true. If none is found, returns false without
1509 Deleting one multiple response set causes the indexes of other sets within
1512 dict_delete_mrset (struct dictionary *dict, const char *name)
1514 size_t idx = dict_lookup_mrset_idx (dict, name);
1515 if (idx != SIZE_MAX)
1517 mrset_destroy (dict->mrsets[idx]);
1518 dict->mrsets[idx] = dict->mrsets[--dict->n_mrsets];
1525 /* Deletes all multiple response sets from DICT. */
1527 dict_clear_mrsets (struct dictionary *dict)
1531 for (i = 0; i < dict->n_mrsets; i++)
1532 mrset_destroy (dict->mrsets[i]);
1533 free (dict->mrsets);
1534 dict->mrsets = NULL;
1538 /* Removes VAR, which must be in DICT, from DICT's multiple response sets. */
1540 dict_unset_mrset_var (struct dictionary *dict, struct variable *var)
1544 assert (dict_contains_var (dict, var));
1546 for (i = 0; i < dict->n_mrsets; )
1548 struct mrset *mrset = dict->mrsets[i];
1551 for (j = 0; j < mrset->n_vars; )
1552 if (mrset->vars[j] == var)
1553 remove_element (mrset->vars, mrset->n_vars--,
1554 sizeof *mrset->vars, j);
1558 if (mrset->n_vars < 2)
1560 mrset_destroy (mrset);
1561 dict->mrsets[i] = dict->mrsets[--dict->n_mrsets];
1568 /* Returns D's attribute set. The caller may examine or modify
1569 the attribute set, but must not destroy it. Destroying D or
1570 calling dict_set_attributes for D will also destroy D's
1573 dict_get_attributes (const struct dictionary *d)
1575 return CONST_CAST (struct attrset *, &d->attributes);
1578 /* Replaces D's attributes set by a copy of ATTRS. */
1580 dict_set_attributes (struct dictionary *d, const struct attrset *attrs)
1582 attrset_destroy (&d->attributes);
1583 attrset_clone (&d->attributes, attrs);
1586 /* Returns true if D has at least one attribute in its attribute
1587 set, false if D's attribute set is empty. */
1589 dict_has_attributes (const struct dictionary *d)
1591 return attrset_count (&d->attributes) > 0;
1594 /* Called from variable.c to notify the dictionary that some property of
1595 the variable has changed */
1597 dict_var_changed (const struct variable *v)
1599 if ( var_has_vardict (v))
1601 const struct vardict_info *vardict = var_get_vardict (v);
1602 struct dictionary *d = vardict->dict;
1607 if (d->changed ) d->changed (d, d->changed_data);
1608 if ( d->callbacks && d->callbacks->var_changed )
1609 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
1614 /* Called from variable.c to notify the dictionary that the variable's width
1617 dict_var_resized (const struct variable *v, int old_width)
1619 if ( var_has_vardict (v))
1621 const struct vardict_info *vardict = var_get_vardict (v);
1622 struct dictionary *d;
1626 if (d->changed) d->changed (d, d->changed_data);
1628 invalidate_proto (d);
1629 if ( d->callbacks && d->callbacks->var_resized )
1630 d->callbacks->var_resized (d, var_get_dict_index (v), old_width,
1635 /* Called from variable.c to notify the dictionary that the variable's display width
1638 dict_var_display_width_changed (const struct variable *v)
1640 if ( var_has_vardict (v))
1642 const struct vardict_info *vardict = var_get_vardict (v);
1643 struct dictionary *d;
1647 if (d->changed) d->changed (d, d->changed_data);
1648 if ( d->callbacks && d->callbacks->var_display_width_changed )
1649 d->callbacks->var_display_width_changed (d, var_get_dict_index (v), d->cb_data);
1653 /* Dictionary used to contain "internal variables". */
1654 static struct dictionary *internal_dict;
1656 /* Create a variable of the specified WIDTH to be used for internal
1657 calculations only. The variable is assigned case index CASE_IDX. */
1659 dict_create_internal_var (int case_idx, int width)
1661 if (internal_dict == NULL)
1662 internal_dict = dict_create ("UTF-8");
1666 static int counter = INT_MAX / 2;
1667 struct variable *var;
1670 if (++counter == INT_MAX)
1671 counter = INT_MAX / 2;
1673 sprintf (name, "$internal%d", counter);
1674 var = dict_create_var (internal_dict, name, width);
1677 set_var_case_index (var, case_idx);
1683 /* Destroys VAR, which must have been created with
1684 dict_create_internal_var(). */
1686 dict_destroy_internal_var (struct variable *var)
1690 dict_delete_var (internal_dict, var);
1692 /* Destroy internal_dict if it has no variables left, just so that
1693 valgrind --leak-check --show-reachable won't show internal_dict. */
1694 if (dict_get_var_cnt (internal_dict) == 0)
1696 dict_destroy (internal_dict);
1697 internal_dict = NULL;
1703 vardict_get_dict_index (const struct vardict_info *vardict)
1705 return vardict - vardict->dict->var;