1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009 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 "dictionary.h"
24 #include <data/attributes.h>
25 #include <data/case.h>
26 #include <data/identifier.h>
27 #include <data/settings.h>
28 #include <data/value-labels.h>
29 #include <data/vardict.h>
30 #include <data/variable.h>
31 #include <data/vector.h>
32 #include <libpspp/array.h>
33 #include <libpspp/assertion.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/message.h>
37 #include <libpspp/misc.h>
38 #include <libpspp/pool.h>
39 #include <libpspp/str.h>
46 #define _(msgid) gettext (msgid)
51 struct variable **var; /* Variables. */
52 size_t var_cnt, var_cap; /* Number of variables, capacity. */
53 struct caseproto *proto; /* Prototype for dictionary cases
55 struct hsh_table *name_tab; /* Variable index by name. */
56 int next_value_idx; /* Index of next `union value' to allocate. */
57 const struct variable **split; /* SPLIT FILE vars. */
58 size_t split_cnt; /* SPLIT FILE count. */
59 struct variable *weight; /* WEIGHT variable. */
60 struct variable *filter; /* FILTER variable. */
61 casenumber case_limit; /* Current case limit (N command). */
62 char *label; /* File label. */
63 struct string documents; /* Documents, as a string. */
64 struct vector **vector; /* Vectors of variables. */
65 size_t vector_cnt; /* Number of vectors. */
66 struct attrset attributes; /* Custom attributes. */
68 char *encoding; /* Character encoding of string data */
70 const struct dict_callbacks *callbacks; /* Callbacks on dictionary
72 void *cb_data ; /* Data passed to callbacks */
74 void (*changed) (struct dictionary *, void *); /* Generic change callback */
80 dict_set_encoding (struct dictionary *d, const char *enc)
83 d->encoding = xstrdup (enc);
87 dict_get_encoding (const struct dictionary *d)
94 dict_set_change_callback (struct dictionary *d,
95 void (*changed) (struct dictionary *, void*),
99 d->changed_data = data;
102 /* Discards dictionary D's caseproto. (It will be regenerated
103 lazily, on demand.) */
105 invalidate_proto (struct dictionary *d)
107 caseproto_unref (d->proto);
111 /* Print a representation of dictionary D to stdout, for
112 debugging purposes. */
114 dict_dump (const struct dictionary *d)
117 for (i = 0 ; i < d->var_cnt ; ++i )
119 const struct variable *v =
121 printf ("Name: %s;\tdict_idx: %zu; case_idx: %zu\n",
123 var_get_dict_index (v),
124 var_get_case_index (v));
129 /* Associate CALLBACKS with DICT. Callbacks will be invoked whenever
130 the dictionary or any of the variables it contains are modified.
131 Each callback will get passed CALLBACK_DATA.
132 Any callback may be NULL, in which case it'll be ignored.
135 dict_set_callbacks (struct dictionary *dict,
136 const struct dict_callbacks *callbacks,
139 dict->callbacks = callbacks;
140 dict->cb_data = callback_data;
143 /* Shallow copy the callbacks from SRC to DEST */
145 dict_copy_callbacks (struct dictionary *dest,
146 const struct dictionary *src)
148 dest->callbacks = src->callbacks;
149 dest->cb_data = src->cb_data;
152 /* Creates and returns a new dictionary. */
156 struct dictionary *d = xzalloc (sizeof *d);
158 d->name_tab = hsh_create (8, compare_vars_by_name, hash_var_by_name,
160 attrset_init (&d->attributes);
164 /* Creates and returns a (deep) copy of an existing
167 The new dictionary's case indexes are copied from the old
168 dictionary. If the new dictionary won't be used to access
169 cases produced with the old dictionary, then the new
170 dictionary's case indexes should be compacted with
171 dict_compact_values to save space. */
173 dict_clone (const struct dictionary *s)
175 struct dictionary *d;
182 for (i = 0; i < s->var_cnt; i++)
184 const struct vardict_info *svdi;
185 struct vardict_info dvdi;
186 struct variable *sv = s->var[i];
187 struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv));
190 for (i = 0; i < var_get_short_name_cnt (sv); i++)
191 var_set_short_name (dv, i, var_get_short_name (sv, i));
193 svdi = var_get_vardict (sv);
196 var_set_vardict (dv, &dvdi);
199 d->next_value_idx = s->next_value_idx;
201 d->split_cnt = s->split_cnt;
202 if (d->split_cnt > 0)
204 d->split = xnmalloc (d->split_cnt, sizeof *d->split);
205 for (i = 0; i < d->split_cnt; i++)
206 d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
209 if (s->weight != NULL)
210 dict_set_weight (d, dict_lookup_var_assert (d, var_get_name (s->weight)));
212 if (s->filter != NULL)
213 dict_set_filter (d, dict_lookup_var_assert (d, var_get_name (s->filter)));
215 d->case_limit = s->case_limit;
216 dict_set_label (d, dict_get_label (s));
217 dict_set_documents (d, dict_get_documents (s));
219 d->vector_cnt = s->vector_cnt;
220 d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
221 for (i = 0; i < s->vector_cnt; i++)
222 d->vector[i] = vector_clone (s->vector[i], s, d);
225 d->encoding = xstrdup (s->encoding);
227 dict_set_attributes (d, dict_get_attributes (s));
232 /* Clears the contents from a dictionary without destroying the
233 dictionary itself. */
235 dict_clear (struct dictionary *d)
237 /* FIXME? Should we really clear case_limit, label, documents?
238 Others are necessarily cleared by deleting all the variables.*/
241 while (d->var_cnt > 0 )
243 dict_delete_var (d, d->var[d->var_cnt - 1]);
248 d->var_cnt = d->var_cap = 0;
249 invalidate_proto (d);
250 hsh_clear (d->name_tab);
251 d->next_value_idx = 0;
252 dict_set_split_vars (d, NULL, 0);
253 dict_set_weight (d, NULL);
254 dict_set_filter (d, NULL);
258 ds_destroy (&d->documents);
259 dict_clear_vectors (d);
260 attrset_clear (&d->attributes);
263 /* Destroys the aux data for every variable in D, by calling
264 var_clear_aux() for each variable. */
266 dict_clear_aux (struct dictionary *d)
272 for (i = 0; i < d->var_cnt; i++)
273 var_clear_aux (d->var[i]);
276 /* Clears a dictionary and destroys it. */
278 dict_destroy (struct dictionary *d)
282 /* In general, we don't want callbacks occuring, if the dictionary
283 is being destroyed */
284 d->callbacks = NULL ;
287 hsh_destroy (d->name_tab);
288 attrset_destroy (&d->attributes);
293 /* Returns the number of variables in D. */
295 dict_get_var_cnt (const struct dictionary *d)
302 /* Returns the variable in D with dictionary index IDX, which
303 must be between 0 and the count returned by
304 dict_get_var_cnt(), exclusive. */
306 dict_get_var (const struct dictionary *d, size_t idx)
309 assert (idx < d->var_cnt);
314 /* Sets *VARS to an array of pointers to variables in D and *CNT
315 to the number of variables in *D. All variables are returned
316 except for those, if any, in the classes indicated by EXCLUDE.
317 (There is no point in putting DC_SYSTEM in EXCLUDE as
318 dictionaries never include system variables.) */
320 dict_get_vars (const struct dictionary *d, const struct variable ***vars,
321 size_t *cnt, enum dict_class exclude)
323 dict_get_vars_mutable (d, (struct variable ***) vars, cnt, exclude);
326 /* Sets *VARS to an array of pointers to variables in D and *CNT
327 to the number of variables in *D. All variables are returned
328 except for those, if any, in the classes indicated by EXCLUDE.
329 (There is no point in putting DC_SYSTEM in EXCLUDE as
330 dictionaries never include system variables.) */
332 dict_get_vars_mutable (const struct dictionary *d, struct variable ***vars,
333 size_t *cnt, enum dict_class exclude)
339 assert (vars != NULL);
340 assert (cnt != NULL);
341 assert (exclude == (exclude & DC_ALL));
344 for (i = 0; i < d->var_cnt; i++)
346 enum dict_class class = var_get_dict_class (d->var[i]);
347 if (!(class & exclude))
351 *vars = xnmalloc (count, sizeof **vars);
353 for (i = 0; i < d->var_cnt; i++)
355 enum dict_class class = var_get_dict_class (d->var[i]);
356 if (!(class & exclude))
357 (*vars)[(*cnt)++] = d->var[i];
359 assert (*cnt == count);
362 static struct variable *
363 add_var (struct dictionary *d, struct variable *v)
365 /* Add dictionary info to variable. */
366 struct vardict_info vdi;
367 vdi.case_index = d->next_value_idx;
368 vdi.dict_index = d->var_cnt;
370 var_set_vardict (v, &vdi);
372 /* Update dictionary. */
373 if (d->var_cnt >= d->var_cap)
375 d->var_cap = 8 + 2 * d->var_cap;
376 d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var);
378 d->var[d->var_cnt++] = v;
379 hsh_force_insert (d->name_tab, v);
381 if ( d->changed ) d->changed (d, d->changed_data);
382 if ( d->callbacks && d->callbacks->var_added )
383 d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
386 invalidate_proto (d);
391 /* Creates and returns a new variable in D with the given NAME
392 and WIDTH. Returns a null pointer if the given NAME would
393 duplicate that of an existing variable in the dictionary. */
395 dict_create_var (struct dictionary *d, const char *name, int width)
397 return (dict_lookup_var (d, name) == NULL
398 ? dict_create_var_assert (d, name, width)
402 /* Creates and returns a new variable in D with the given NAME
403 and WIDTH. Assert-fails if the given NAME would duplicate
404 that of an existing variable in the dictionary. */
406 dict_create_var_assert (struct dictionary *d, const char *name, int width)
408 assert (dict_lookup_var (d, name) == NULL);
409 return add_var (d, var_create (name, width));
412 /* Creates and returns a new variable in D with name NAME, as a
413 copy of existing variable OLD_VAR, which need not be in D or
414 in any dictionary. Returns a null pointer if the given NAME
415 would duplicate that of an existing variable in the
418 dict_clone_var (struct dictionary *d, const struct variable *old_var,
421 return (dict_lookup_var (d, name) == NULL
422 ? dict_clone_var_assert (d, old_var, name)
426 /* Creates and returns a new variable in D with name NAME, as a
427 copy of existing variable OLD_VAR, which need not be in D or
428 in any dictionary. Assert-fails if the given NAME would
429 duplicate that of an existing variable in the dictionary. */
431 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var,
434 struct variable *new_var = var_clone (old_var);
435 assert (dict_lookup_var (d, name) == NULL);
436 var_set_name (new_var, name);
437 return add_var (d, new_var);
440 /* Returns the variable named NAME in D, or a null pointer if no
441 variable has that name. */
443 dict_lookup_var (const struct dictionary *d, const char *name)
445 struct variable *target ;
446 struct variable *result ;
448 if ( ! var_is_plausible_name (name, false))
451 target = var_create (name, 0);
452 result = hsh_find (d->name_tab, target);
453 var_destroy (target);
455 if ( result && var_has_vardict (result))
457 const struct vardict_info *vdi = var_get_vardict (result);
458 assert (vdi->dict == d);
464 /* Returns the variable named NAME in D. Assert-fails if no
465 variable has that name. */
467 dict_lookup_var_assert (const struct dictionary *d, const char *name)
469 struct variable *v = dict_lookup_var (d, name);
474 /* Returns true if variable V is in dictionary D,
477 dict_contains_var (const struct dictionary *d, const struct variable *v)
479 if (var_has_vardict (v))
481 const struct vardict_info *vdi = var_get_vardict (v);
482 return (vdi->dict_index >= 0
483 && vdi->dict_index < d->var_cnt
484 && d->var[vdi->dict_index] == v);
490 /* Compares two double pointers to variables, which should point
491 to elements of a struct dictionary's `var' member array. */
493 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED)
495 struct variable *const *a = a_;
496 struct variable *const *b = b_;
498 return *a < *b ? -1 : *a > *b;
501 /* Sets the dict_index in V's vardict to DICT_INDEX. */
503 set_var_dict_index (struct variable *v, int dict_index)
505 struct vardict_info vdi = *var_get_vardict (v);
506 struct dictionary *d = vdi.dict;
507 vdi.dict_index = dict_index;
508 var_set_vardict (v, &vdi);
510 if ( d->changed ) d->changed (d, d->changed_data);
511 if ( d->callbacks && d->callbacks->var_changed )
512 d->callbacks->var_changed (d, dict_index, d->cb_data);
515 /* Sets the case_index in V's vardict to CASE_INDEX. */
517 set_var_case_index (struct variable *v, int case_index)
519 struct vardict_info vdi = *var_get_vardict (v);
520 vdi.case_index = case_index;
521 var_set_vardict (v, &vdi);
524 /* Re-sets the dict_index in the dictionary variables with
525 indexes from FROM to TO (exclusive). */
527 reindex_vars (struct dictionary *d, size_t from, size_t to)
531 for (i = from; i < to; i++)
532 set_var_dict_index (d->var[i], i);
535 /* Deletes variable V from dictionary D and frees V.
537 This is a very bad idea if there might be any pointers to V
538 from outside D. In general, no variable in the active file's
539 dictionary should be deleted when any transformations are
540 active on the dictionary's dataset, because those
541 transformations might reference the deleted variable. The
542 safest time to delete a variable is just after a procedure has
543 been executed, as done by DELETE VARIABLES.
545 Pointers to V within D are not a problem, because
546 dict_delete_var() knows to remove V from split variables,
547 weights, filters, etc. */
549 dict_delete_var (struct dictionary *d, struct variable *v)
551 int dict_index = var_get_dict_index (v);
552 const int case_index = var_get_case_index (v);
553 const int width = var_get_width (v);
555 assert (dict_contains_var (d, v));
557 /* Delete aux data. */
560 dict_unset_split_var (d, v);
563 dict_set_weight (d, NULL);
566 dict_set_filter (d, NULL);
568 dict_clear_vectors (d);
570 /* Remove V from var array. */
571 remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
574 /* Update dict_index for each affected variable. */
575 reindex_vars (d, dict_index, d->var_cnt);
577 /* Update name hash. */
578 hsh_force_delete (d->name_tab, v);
582 var_clear_vardict (v);
585 if ( d->changed ) d->changed (d, d->changed_data);
587 invalidate_proto (d);
588 if (d->callbacks && d->callbacks->var_deleted )
589 d->callbacks->var_deleted (d, dict_index, case_index, width, d->cb_data);
592 /* Deletes the COUNT variables listed in VARS from D. This is
593 unsafe; see the comment on dict_delete_var() for details. */
595 dict_delete_vars (struct dictionary *d,
596 struct variable *const *vars, size_t count)
598 /* FIXME: this can be done in O(count) time, but this algorithm
601 assert (count == 0 || vars != NULL);
604 dict_delete_var (d, *vars++);
607 /* Deletes the COUNT variables in D starting at index IDX. This
608 is unsafe; see the comment on dict_delete_var() for
611 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
613 /* FIXME: this can be done in O(count) time, but this algorithm
615 assert (idx + count <= d->var_cnt);
618 dict_delete_var (d, d->var[idx]);
621 /* Deletes scratch variables from dictionary D. */
623 dict_delete_scratch_vars (struct dictionary *d)
627 /* FIXME: this can be done in O(count) time, but this algorithm
631 for (i = 0; i < d->var_cnt; )
632 if (var_get_dict_class (d->var[i]) == DC_SCRATCH)
633 dict_delete_var (d, d->var[i]);
638 /* Moves V to 0-based position IDX in D. Other variables in D,
639 if any, retain their relative positions. Runs in time linear
640 in the distance moved. */
642 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index)
644 size_t old_index = var_get_dict_index (v);
646 assert (new_index < d->var_cnt);
647 move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
648 reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
651 /* Reorders the variables in D, placing the COUNT variables
652 listed in ORDER in that order at the beginning of D. The
653 other variables in D, if any, retain their relative
656 dict_reorder_vars (struct dictionary *d,
657 struct variable *const *order, size_t count)
659 struct variable **new_var;
663 assert (count == 0 || order != NULL);
664 assert (count <= d->var_cnt);
666 new_var = xnmalloc (d->var_cnt, sizeof *new_var);
667 memcpy (new_var, order, count * sizeof *new_var);
668 for (i = 0; i < count; i++)
670 size_t index = var_get_dict_index (order[i]);
671 assert (d->var[index] == order[i]);
672 d->var[index] = NULL;
673 set_var_dict_index (order[i], i);
675 for (i = 0; i < d->var_cnt; i++)
676 if (d->var[i] != NULL)
678 assert (count < d->var_cnt);
679 new_var[count] = d->var[i];
680 set_var_dict_index (new_var[count], count);
687 /* Changes the name of variable V in dictionary D to NEW_NAME. */
689 rename_var (struct dictionary *d, struct variable *v, const char *new_name)
691 struct vardict_info vdi;
693 assert (dict_contains_var (d, v));
695 vdi = *var_get_vardict (v);
696 var_clear_vardict (v);
697 var_set_name (v, new_name);
698 var_set_vardict (v, &vdi);
701 /* Changes the name of V in D to name NEW_NAME. Assert-fails if
702 a variable named NEW_NAME is already in D, except that
703 NEW_NAME may be the same as V's existing name. */
705 dict_rename_var (struct dictionary *d, struct variable *v,
706 const char *new_name)
708 assert (!strcasecmp (var_get_name (v), new_name)
709 || dict_lookup_var (d, new_name) == NULL);
711 hsh_force_delete (d->name_tab, v);
712 rename_var (d, v, new_name);
713 hsh_force_insert (d->name_tab, v);
715 if (settings_get_algorithm () == ENHANCED)
716 var_clear_short_names (v);
718 if ( d->changed ) d->changed (d, d->changed_data);
719 if ( d->callbacks && d->callbacks->var_changed )
720 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
723 /* Renames COUNT variables specified in VARS to the names given
724 in NEW_NAMES within dictionary D. If the renaming would
725 result in a duplicate variable name, returns false and stores a
726 name that would be duplicated into *ERR_NAME (if ERR_NAME is
727 non-null). Otherwise, the renaming is successful, and true
730 dict_rename_vars (struct dictionary *d,
731 struct variable **vars, char **new_names, size_t count,
738 assert (count == 0 || vars != NULL);
739 assert (count == 0 || new_names != NULL);
741 /* Save the names of the variables to be renamed. */
742 pool = pool_create ();
743 old_names = pool_nalloc (pool, count, sizeof *old_names);
744 for (i = 0; i < count; i++)
745 old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
747 /* Remove the variables to be renamed from the name hash,
749 for (i = 0; i < count; i++)
751 hsh_force_delete (d->name_tab, vars[i]);
752 rename_var (d, vars[i], new_names[i]);
755 /* Add the renamed variables back into the name hash,
756 checking for conflicts. */
757 for (i = 0; i < count; i++)
758 if (hsh_insert (d->name_tab, vars[i]) != NULL)
760 /* There is a name conflict.
761 Back out all the name changes that have already
762 taken place, and indicate failure. */
764 if (err_name != NULL)
765 *err_name = new_names[i];
767 for (i = 0; i < fail_idx; i++)
768 hsh_force_delete (d->name_tab, vars[i]);
770 for (i = 0; i < count; i++)
772 rename_var (d, vars[i], old_names[i]);
773 hsh_force_insert (d->name_tab, vars[i]);
780 /* Clear short names. */
781 if (settings_get_algorithm () == ENHANCED)
782 for (i = 0; i < count; i++)
783 var_clear_short_names (vars[i]);
789 /* Returns true if a variable named NAME may be inserted in DICT;
790 that is, if there is not already a variable with that name in
791 DICT and if NAME is not a reserved word. (The caller's checks
792 have already verified that NAME is otherwise acceptable as a
795 var_name_is_insertable (const struct dictionary *dict, const char *name)
797 return (dict_lookup_var (dict, name) == NULL
798 && lex_id_to_token (ss_cstr (name)) == T_ID);
802 make_hinted_name (const struct dictionary *dict, const char *hint,
803 char name[VAR_NAME_LEN + 1])
805 bool dropped = false;
808 for (cp = name; *hint && cp < name + VAR_NAME_LEN; hint++)
811 ? lex_is_id1 (*hint) && *hint != '$'
812 : lex_is_idn (*hint))
819 if (cp < name + VAR_NAME_LEN)
829 size_t len = strlen (name);
832 if (var_name_is_insertable (dict, name))
835 for (i = 0; i < ULONG_MAX; i++)
837 char suffix[INT_BUFSIZE_BOUND (i) + 1];
841 if (!str_format_26adic (i + 1, &suffix[1], sizeof suffix - 1))
844 ofs = MIN (VAR_NAME_LEN - strlen (suffix), len);
845 strcpy (&name[ofs], suffix);
847 if (var_name_is_insertable (dict, name))
856 make_numeric_name (const struct dictionary *dict, unsigned long int *num_start,
857 char name[VAR_NAME_LEN + 1])
859 unsigned long int number;
861 for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
865 sprintf (name, "VAR%03lu", number);
866 if (dict_lookup_var (dict, name) == NULL)
868 if (num_start != NULL)
869 *num_start = number + 1;
874 if (num_start != NULL)
875 *num_start = ULONG_MAX;
880 /* Attempts to devise a variable name unique within DICT.
881 Returns true if successful, in which case the new variable
882 name is stored into NAME. Returns false if all names that can
883 be generated have already been taken. (Returning false is
884 quite unlikely: at least ULONG_MAX unique names can be
887 HINT, if it is non-null, is used as a suggestion that will be
888 modified for suitability as a variable name and for
891 If HINT is null or entirely unsuitable, a name in the form
892 "VAR%03d" will be generated, where the smallest unused integer
893 value is used. If NUM_START is non-null, then its value is
894 used as the minimum numeric value to check, and it is updated
895 to the next value to be checked.
898 dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
899 unsigned long int *num_start,
900 char name[VAR_NAME_LEN + 1])
902 return ((hint != NULL && make_hinted_name (dict, hint, name))
903 || make_numeric_name (dict, num_start, name));
906 /* Returns the weighting variable in dictionary D, or a null
907 pointer if the dictionary is unweighted. */
909 dict_get_weight (const struct dictionary *d)
912 assert (d->weight == NULL || dict_contains_var (d, d->weight));
917 /* Returns the value of D's weighting variable in case C, except
918 that a negative weight is returned as 0. Returns 1 if the
919 dictionary is unweighted. Will warn about missing, negative,
920 or zero values if *WARN_ON_INVALID is true. The function will
921 set *WARN_ON_INVALID to false if an invalid weight is
924 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
925 bool *warn_on_invalid)
930 if (d->weight == NULL)
934 double w = case_num (c, d->weight);
935 if (w < 0.0 || var_is_num_missing (d->weight, w, MV_ANY))
937 if ( w == 0.0 && warn_on_invalid != NULL && *warn_on_invalid ) {
938 *warn_on_invalid = false;
939 msg (SW, _("At least one case in the data file had a weight value "
940 "that was user-missing, system-missing, zero, or "
941 "negative. These case(s) were ignored."));
947 /* Sets the weighting variable of D to V, or turning off
948 weighting if V is a null pointer. */
950 dict_set_weight (struct dictionary *d, struct variable *v)
953 assert (v == NULL || dict_contains_var (d, v));
954 assert (v == NULL || var_is_numeric (v));
958 if (d->changed) d->changed (d, d->changed_data);
959 if ( d->callbacks && d->callbacks->weight_changed )
960 d->callbacks->weight_changed (d,
961 v ? var_get_dict_index (v) : -1,
965 /* Returns the filter variable in dictionary D (see cmd_filter())
966 or a null pointer if the dictionary is unfiltered. */
968 dict_get_filter (const struct dictionary *d)
971 assert (d->filter == NULL || dict_contains_var (d, d->filter));
976 /* Sets V as the filter variable for dictionary D. Passing a
977 null pointer for V turn off filtering. */
979 dict_set_filter (struct dictionary *d, struct variable *v)
982 assert (v == NULL || dict_contains_var (d, v));
983 assert (v == NULL || var_is_numeric (v));
987 if (d->changed) d->changed (d, d->changed_data);
988 if ( d->callbacks && d->callbacks->filter_changed )
989 d->callbacks->filter_changed (d,
990 v ? var_get_dict_index (v) : -1,
994 /* Returns the case limit for dictionary D, or zero if the number
995 of cases is unlimited. */
997 dict_get_case_limit (const struct dictionary *d)
1001 return d->case_limit;
1004 /* Sets CASE_LIMIT as the case limit for dictionary D. Use
1005 0 for CASE_LIMIT to indicate no limit. */
1007 dict_set_case_limit (struct dictionary *d, casenumber case_limit)
1011 d->case_limit = case_limit;
1014 /* Returns the prototype used for cases created by dictionary D. */
1015 const struct caseproto *
1016 dict_get_proto (const struct dictionary *d_)
1018 struct dictionary *d = (struct dictionary *) d_;
1019 if (d->proto == NULL)
1023 d->proto = caseproto_create ();
1024 d->proto = caseproto_reserve (d->proto, d->var_cnt);
1025 for (i = 0; i < d->var_cnt; i++)
1026 d->proto = caseproto_set_width (d->proto,
1027 var_get_case_index (d->var[i]),
1028 var_get_width (d->var[i]));
1033 /* Returns the case index of the next value to be added to D.
1034 This value is the number of `union value's that need to be
1035 allocated to store a case for dictionary D. */
1037 dict_get_next_value_idx (const struct dictionary *d)
1041 return d->next_value_idx;
1044 /* Returns the number of bytes needed to store a case for
1047 dict_get_case_size (const struct dictionary *d)
1051 return sizeof (union value) * dict_get_next_value_idx (d);
1054 /* Reassigns values in dictionary D so that fragmentation is
1057 dict_compact_values (struct dictionary *d)
1061 d->next_value_idx = 0;
1062 for (i = 0; i < d->var_cnt; i++)
1064 struct variable *v = d->var[i];
1065 set_var_case_index (v, d->next_value_idx++);
1067 invalidate_proto (d);
1070 /* Returns the number of values occupied by the variables in
1071 dictionary D. All variables are considered if EXCLUDE_CLASSES
1072 is 0, or it may contain one or more of (1u << DC_ORDINARY),
1073 (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to exclude the
1074 corresponding type of variable.
1076 The return value may be less than the number of values in one
1077 of dictionary D's cases (as returned by
1078 dict_get_next_value_idx) even if E is 0, because there may be
1079 gaps in D's cases due to deleted variables. */
1081 dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
1086 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1088 | (1u << DC_SCRATCH))) == 0);
1091 for (i = 0; i < d->var_cnt; i++)
1093 enum dict_class class = var_get_dict_class (d->var[i]);
1094 if (!(exclude_classes & (1u << class)))
1100 /* Returns the case prototype that would result after deleting
1101 all variables from D that are not in one of the
1102 EXCLUDE_CLASSES and compacting the dictionary with
1105 The caller must unref the returned caseproto when it is no
1108 dict_get_compacted_proto (const struct dictionary *d,
1109 unsigned int exclude_classes)
1111 struct caseproto *proto;
1114 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1116 | (1u << DC_SCRATCH))) == 0);
1118 proto = caseproto_create ();
1119 for (i = 0; i < d->var_cnt; i++)
1121 struct variable *v = d->var[i];
1122 if (!(exclude_classes & (1u << var_get_dict_class (v))))
1123 proto = caseproto_add_width (proto, var_get_width (v));
1128 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
1129 dict_get_split_cnt() to determine how many SPLIT FILE vars
1130 there are. Returns a null pointer if and only if there are no
1132 const struct variable *const *
1133 dict_get_split_vars (const struct dictionary *d)
1140 /* Returns the number of SPLIT FILE vars. */
1142 dict_get_split_cnt (const struct dictionary *d)
1146 return d->split_cnt;
1149 /* Removes variable V, which must be in D, from D's set of split
1152 dict_unset_split_var (struct dictionary *d, struct variable *v)
1156 assert (dict_contains_var (d, v));
1158 orig_count = d->split_cnt;
1159 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
1160 &v, compare_var_ptrs, NULL);
1161 if (orig_count != d->split_cnt)
1163 if (d->changed) d->changed (d, d->changed_data);
1164 /* We changed the set of split variables so invoke the
1166 if (d->callbacks && d->callbacks->split_changed)
1167 d->callbacks->split_changed (d, d->cb_data);
1171 /* Sets CNT split vars SPLIT in dictionary D. */
1173 dict_set_split_vars (struct dictionary *d,
1174 struct variable *const *split, size_t cnt)
1177 assert (cnt == 0 || split != NULL);
1182 d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
1183 memcpy (d->split, split, cnt * sizeof *d->split);
1191 if (d->changed) d->changed (d, d->changed_data);
1192 if ( d->callbacks && d->callbacks->split_changed )
1193 d->callbacks->split_changed (d, d->cb_data);
1196 /* Returns the file label for D, or a null pointer if D is
1197 unlabeled (see cmd_file_label()). */
1199 dict_get_label (const struct dictionary *d)
1206 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1209 dict_set_label (struct dictionary *d, const char *label)
1214 d->label = label != NULL ? xstrndup (label, 60) : NULL;
1217 /* Returns the documents for D, or a null pointer if D has no
1218 documents. If the return value is nonnull, then the string
1219 will be an exact multiple of DOC_LINE_LENGTH bytes in length,
1220 with each segment corresponding to one line. */
1222 dict_get_documents (const struct dictionary *d)
1224 return ds_is_empty (&d->documents) ? NULL : ds_cstr (&d->documents);
1227 /* Sets the documents for D to DOCUMENTS, or removes D's
1228 documents if DOCUMENT is a null pointer. If DOCUMENTS is
1229 nonnull, then it should be an exact multiple of
1230 DOC_LINE_LENGTH bytes in length, with each segment
1231 corresponding to one line. */
1233 dict_set_documents (struct dictionary *d, const char *documents)
1237 ds_assign_cstr (&d->documents, documents != NULL ? documents : "");
1239 /* In case the caller didn't get it quite right, pad out the
1240 final line with spaces. */
1241 remainder = ds_length (&d->documents) % DOC_LINE_LENGTH;
1243 ds_put_char_multiple (&d->documents, ' ', DOC_LINE_LENGTH - remainder);
1246 /* Drops the documents from dictionary D. */
1248 dict_clear_documents (struct dictionary *d)
1250 ds_clear (&d->documents);
1253 /* Appends LINE to the documents in D. LINE will be truncated or
1254 padded on the right with spaces to make it exactly
1255 DOC_LINE_LENGTH bytes long. */
1257 dict_add_document_line (struct dictionary *d, const char *line)
1259 if (strlen (line) > DOC_LINE_LENGTH)
1261 /* Note to translators: "bytes" is correct, not characters */
1262 msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH);
1264 buf_copy_str_rpad (ds_put_uninit (&d->documents, DOC_LINE_LENGTH),
1265 DOC_LINE_LENGTH, line, ' ');
1268 /* Returns the number of document lines in dictionary D. */
1270 dict_get_document_line_cnt (const struct dictionary *d)
1272 return ds_length (&d->documents) / DOC_LINE_LENGTH;
1275 /* Copies document line number IDX from dictionary D into
1276 LINE, trimming off any trailing white space. */
1278 dict_get_document_line (const struct dictionary *d,
1279 size_t idx, struct string *line)
1281 assert (idx < dict_get_document_line_cnt (d));
1282 ds_assign_substring (line, ds_substr (&d->documents, idx * DOC_LINE_LENGTH,
1284 ds_rtrim (line, ss_cstr (CC_SPACES));
1287 /* Creates in D a vector named NAME that contains the CNT
1288 variables in VAR. Returns true if successful, or false if a
1289 vector named NAME already exists in D. */
1291 dict_create_vector (struct dictionary *d,
1293 struct variable **var, size_t cnt)
1297 assert (var != NULL);
1299 for (i = 0; i < cnt; i++)
1300 assert (dict_contains_var (d, var[i]));
1302 if (dict_lookup_vector (d, name) == NULL)
1304 d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1305 d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1312 /* Creates in D a vector named NAME that contains the CNT
1313 variables in VAR. A vector named NAME must not already exist
1316 dict_create_vector_assert (struct dictionary *d,
1318 struct variable **var, size_t cnt)
1320 assert (dict_lookup_vector (d, name) == NULL);
1321 dict_create_vector (d, name, var, cnt);
1324 /* Returns the vector in D with index IDX, which must be less
1325 than dict_get_vector_cnt (D). */
1326 const struct vector *
1327 dict_get_vector (const struct dictionary *d, size_t idx)
1330 assert (idx < d->vector_cnt);
1332 return d->vector[idx];
1335 /* Returns the number of vectors in D. */
1337 dict_get_vector_cnt (const struct dictionary *d)
1341 return d->vector_cnt;
1344 /* Looks up and returns the vector within D with the given
1346 const struct vector *
1347 dict_lookup_vector (const struct dictionary *d, const char *name)
1350 for (i = 0; i < d->vector_cnt; i++)
1351 if (!strcasecmp (vector_get_name (d->vector[i]), name))
1352 return d->vector[i];
1356 /* Deletes all vectors from D. */
1358 dict_clear_vectors (struct dictionary *d)
1362 for (i = 0; i < d->vector_cnt; i++)
1363 vector_destroy (d->vector[i]);
1370 /* Returns D's attribute set. The caller may examine or modify
1371 the attribute set, but must not destroy it. Destroying D or
1372 calling dict_set_attributes for D will also destroy D's
1375 dict_get_attributes (const struct dictionary *d)
1377 return (struct attrset *) &d->attributes;
1380 /* Replaces D's attributes set by a copy of ATTRS. */
1382 dict_set_attributes (struct dictionary *d, const struct attrset *attrs)
1384 attrset_destroy (&d->attributes);
1385 attrset_clone (&d->attributes, attrs);
1388 /* Returns true if D has at least one attribute in its attribute
1389 set, false if D's attribute set is empty. */
1391 dict_has_attributes (const struct dictionary *d)
1393 return attrset_count (&d->attributes) > 0;
1396 /* Called from variable.c to notify the dictionary that some property of
1397 the variable has changed */
1399 dict_var_changed (const struct variable *v)
1401 if ( var_has_vardict (v))
1403 const struct vardict_info *vdi = var_get_vardict (v);
1404 struct dictionary *d = vdi->dict;
1409 if (d->changed ) d->changed (d, d->changed_data);
1410 if ( d->callbacks && d->callbacks->var_changed )
1411 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
1416 /* Called from variable.c to notify the dictionary that the variable's width
1419 dict_var_resized (const struct variable *v, int old_width)
1421 if ( var_has_vardict (v))
1423 const struct vardict_info *vdi = var_get_vardict (v);
1424 struct dictionary *d;
1428 if (d->changed) d->changed (d, d->changed_data);
1430 invalidate_proto (d);
1431 if ( d->callbacks && d->callbacks->var_resized )
1432 d->callbacks->var_resized (d, var_get_dict_index (v), old_width,
1437 /* Called from variable.c to notify the dictionary that the variable's display width
1440 dict_var_display_width_changed (const struct variable *v)
1442 if ( var_has_vardict (v))
1444 const struct vardict_info *vdi = var_get_vardict (v);
1445 struct dictionary *d;
1449 if (d->changed) d->changed (d, d->changed_data);
1450 if ( d->callbacks && d->callbacks->var_display_width_changed )
1451 d->callbacks->var_display_width_changed (d, var_get_dict_index (v), d->cb_data);