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/category.h>
27 #include <data/identifier.h>
28 #include <data/settings.h>
29 #include <data/value-labels.h>
30 #include <data/vardict.h>
31 #include <data/variable.h>
32 #include <data/vector.h>
33 #include <libpspp/array.h>
34 #include <libpspp/assertion.h>
35 #include <libpspp/compiler.h>
36 #include <libpspp/hash.h>
37 #include <libpspp/message.h>
38 #include <libpspp/misc.h>
39 #include <libpspp/pool.h>
40 #include <libpspp/str.h>
47 #define _(msgid) gettext (msgid)
52 struct variable **var; /* Variables. */
53 size_t var_cnt, var_cap; /* Number of variables, capacity. */
54 struct caseproto *proto; /* Prototype for dictionary cases
56 struct hsh_table *name_tab; /* Variable index by name. */
57 int next_value_idx; /* Index of next `union value' to allocate. */
58 const struct variable **split; /* SPLIT FILE vars. */
59 size_t split_cnt; /* SPLIT FILE count. */
60 struct variable *weight; /* WEIGHT variable. */
61 struct variable *filter; /* FILTER variable. */
62 casenumber case_limit; /* Current case limit (N command). */
63 char *label; /* File label. */
64 struct string documents; /* Documents, as a string. */
65 struct vector **vector; /* Vectors of variables. */
66 size_t vector_cnt; /* Number of vectors. */
67 struct attrset attributes; /* Custom attributes. */
69 char *encoding; /* Character encoding of string data */
71 const struct dict_callbacks *callbacks; /* Callbacks on dictionary
73 void *cb_data ; /* Data passed to callbacks */
75 void (*changed) (struct dictionary *, void *); /* Generic change callback */
81 dict_set_encoding (struct dictionary *d, const char *enc)
84 d->encoding = xstrdup (enc);
88 dict_get_encoding (const struct dictionary *d)
95 dict_set_change_callback (struct dictionary *d,
96 void (*changed) (struct dictionary *, void*),
100 d->changed_data = data;
103 /* Discards dictionary D's caseproto. (It will be regenerated
104 lazily, on demand.) */
106 invalidate_proto (struct dictionary *d)
108 caseproto_unref (d->proto);
112 /* Print a representation of dictionary D to stdout, for
113 debugging purposes. */
115 dict_dump (const struct dictionary *d)
118 for (i = 0 ; i < d->var_cnt ; ++i )
120 const struct variable *v =
122 printf ("Name: %s;\tdict_idx: %zu; case_idx: %zu\n",
124 var_get_dict_index (v),
125 var_get_case_index (v));
130 /* Associate CALLBACKS with DICT. Callbacks will be invoked whenever
131 the dictionary or any of the variables it contains are modified.
132 Each callback will get passed CALLBACK_DATA.
133 Any callback may be NULL, in which case it'll be ignored.
136 dict_set_callbacks (struct dictionary *dict,
137 const struct dict_callbacks *callbacks,
140 dict->callbacks = callbacks;
141 dict->cb_data = callback_data;
144 /* Shallow copy the callbacks from SRC to DEST */
146 dict_copy_callbacks (struct dictionary *dest,
147 const struct dictionary *src)
149 dest->callbacks = src->callbacks;
150 dest->cb_data = src->cb_data;
153 /* Creates and returns a new dictionary. */
157 struct dictionary *d = xzalloc (sizeof *d);
159 d->name_tab = hsh_create (8, compare_vars_by_name, hash_var_by_name,
161 attrset_init (&d->attributes);
165 /* Creates and returns a (deep) copy of an existing
168 The new dictionary's case indexes are copied from the old
169 dictionary. If the new dictionary won't be used to access
170 cases produced with the old dictionary, then the new
171 dictionary's case indexes should be compacted with
172 dict_compact_values to save space. */
174 dict_clone (const struct dictionary *s)
176 struct dictionary *d;
183 for (i = 0; i < s->var_cnt; i++)
185 const struct vardict_info *svdi;
186 struct vardict_info dvdi;
187 struct variable *sv = s->var[i];
188 struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv));
191 for (i = 0; i < var_get_short_name_cnt (sv); i++)
192 var_set_short_name (dv, i, var_get_short_name (sv, i));
194 svdi = var_get_vardict (sv);
197 var_set_vardict (dv, &dvdi);
200 d->next_value_idx = s->next_value_idx;
202 d->split_cnt = s->split_cnt;
203 if (d->split_cnt > 0)
205 d->split = xnmalloc (d->split_cnt, sizeof *d->split);
206 for (i = 0; i < d->split_cnt; i++)
207 d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
210 if (s->weight != NULL)
211 dict_set_weight (d, dict_lookup_var_assert (d, var_get_name (s->weight)));
213 if (s->filter != NULL)
214 dict_set_filter (d, dict_lookup_var_assert (d, var_get_name (s->filter)));
216 d->case_limit = s->case_limit;
217 dict_set_label (d, dict_get_label (s));
218 dict_set_documents (d, dict_get_documents (s));
220 d->vector_cnt = s->vector_cnt;
221 d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
222 for (i = 0; i < s->vector_cnt; i++)
223 d->vector[i] = vector_clone (s->vector[i], s, d);
226 d->encoding = xstrdup (s->encoding);
228 dict_set_attributes (d, dict_get_attributes (s));
233 /* Clears the contents from a dictionary without destroying the
234 dictionary itself. */
236 dict_clear (struct dictionary *d)
238 /* FIXME? Should we really clear case_limit, label, documents?
239 Others are necessarily cleared by deleting all the variables.*/
242 while (d->var_cnt > 0 )
244 dict_delete_var (d, d->var[d->var_cnt - 1]);
249 d->var_cnt = d->var_cap = 0;
250 invalidate_proto (d);
251 hsh_clear (d->name_tab);
252 d->next_value_idx = 0;
253 dict_set_split_vars (d, NULL, 0);
254 dict_set_weight (d, NULL);
255 dict_set_filter (d, NULL);
259 ds_destroy (&d->documents);
260 dict_clear_vectors (d);
261 attrset_clear (&d->attributes);
264 /* Destroys the aux data for every variable in D, by calling
265 var_clear_aux() for each variable. */
267 dict_clear_aux (struct dictionary *d)
273 for (i = 0; i < d->var_cnt; i++)
274 var_clear_aux (d->var[i]);
277 /* Clears a dictionary and destroys it. */
279 dict_destroy (struct dictionary *d)
283 /* In general, we don't want callbacks occuring, if the dictionary
284 is being destroyed */
285 d->callbacks = NULL ;
288 hsh_destroy (d->name_tab);
289 attrset_destroy (&d->attributes);
294 /* Returns the number of variables in D. */
296 dict_get_var_cnt (const struct dictionary *d)
303 /* Returns the variable in D with dictionary index IDX, which
304 must be between 0 and the count returned by
305 dict_get_var_cnt(), exclusive. */
307 dict_get_var (const struct dictionary *d, size_t idx)
310 assert (idx < d->var_cnt);
315 /* Sets *VARS to an array of pointers to variables in D and *CNT
316 to the number of variables in *D. All variables are returned
317 except for those, if any, in the classes indicated by EXCLUDE.
318 (There is no point in putting DC_SYSTEM in EXCLUDE as
319 dictionaries never include system variables.) */
321 dict_get_vars (const struct dictionary *d, const struct variable ***vars,
322 size_t *cnt, enum dict_class exclude)
324 dict_get_vars_mutable (d, (struct variable ***) vars, cnt, exclude);
327 /* Sets *VARS to an array of pointers to variables in D and *CNT
328 to the number of variables in *D. All variables are returned
329 except for those, if any, in the classes indicated by EXCLUDE.
330 (There is no point in putting DC_SYSTEM in EXCLUDE as
331 dictionaries never include system variables.) */
333 dict_get_vars_mutable (const struct dictionary *d, struct variable ***vars,
334 size_t *cnt, enum dict_class exclude)
340 assert (vars != NULL);
341 assert (cnt != NULL);
342 assert (exclude == (exclude & DC_ALL));
345 for (i = 0; i < d->var_cnt; i++)
347 enum dict_class class = var_get_dict_class (d->var[i]);
348 if (!(class & exclude))
352 *vars = xnmalloc (count, sizeof **vars);
354 for (i = 0; i < d->var_cnt; i++)
356 enum dict_class class = var_get_dict_class (d->var[i]);
357 if (!(class & exclude))
358 (*vars)[(*cnt)++] = d->var[i];
360 assert (*cnt == count);
363 static struct variable *
364 add_var (struct dictionary *d, struct variable *v)
366 /* Add dictionary info to variable. */
367 struct vardict_info vdi;
368 vdi.case_index = d->next_value_idx;
369 vdi.dict_index = d->var_cnt;
371 var_set_vardict (v, &vdi);
373 /* Update dictionary. */
374 if (d->var_cnt >= d->var_cap)
376 d->var_cap = 8 + 2 * d->var_cap;
377 d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var);
379 d->var[d->var_cnt++] = v;
380 hsh_force_insert (d->name_tab, v);
382 if ( d->changed ) d->changed (d, d->changed_data);
383 if ( d->callbacks && d->callbacks->var_added )
384 d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
387 invalidate_proto (d);
392 /* Creates and returns a new variable in D with the given NAME
393 and WIDTH. Returns a null pointer if the given NAME would
394 duplicate that of an existing variable in the dictionary. */
396 dict_create_var (struct dictionary *d, const char *name, int width)
398 return (dict_lookup_var (d, name) == NULL
399 ? dict_create_var_assert (d, name, width)
403 /* Creates and returns a new variable in D with the given NAME
404 and WIDTH. Assert-fails if the given NAME would duplicate
405 that of an existing variable in the dictionary. */
407 dict_create_var_assert (struct dictionary *d, const char *name, int width)
409 assert (dict_lookup_var (d, name) == NULL);
410 return add_var (d, var_create (name, width));
413 /* Creates and returns a new variable in D with name NAME, as a
414 copy of existing variable OLD_VAR, which need not be in D or
415 in any dictionary. Returns a null pointer if the given NAME
416 would duplicate that of an existing variable in the
419 dict_clone_var (struct dictionary *d, const struct variable *old_var,
422 return (dict_lookup_var (d, name) == NULL
423 ? dict_clone_var_assert (d, old_var, name)
427 /* Creates and returns a new variable in D with name NAME, as a
428 copy of existing variable OLD_VAR, which need not be in D or
429 in any dictionary. Assert-fails if the given NAME would
430 duplicate that of an existing variable in the dictionary. */
432 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var,
435 struct variable *new_var = var_clone (old_var);
436 assert (dict_lookup_var (d, name) == NULL);
437 var_set_name (new_var, name);
438 return add_var (d, new_var);
441 /* Returns the variable named NAME in D, or a null pointer if no
442 variable has that name. */
444 dict_lookup_var (const struct dictionary *d, const char *name)
446 struct variable *target ;
447 struct variable *result ;
449 if ( ! var_is_plausible_name (name, false))
452 target = var_create (name, 0);
453 result = hsh_find (d->name_tab, target);
454 var_destroy (target);
456 if ( result && var_has_vardict (result))
458 const struct vardict_info *vdi = var_get_vardict (result);
459 assert (vdi->dict == d);
465 /* Returns the variable named NAME in D. Assert-fails if no
466 variable has that name. */
468 dict_lookup_var_assert (const struct dictionary *d, const char *name)
470 struct variable *v = dict_lookup_var (d, name);
475 /* Returns true if variable V is in dictionary D,
478 dict_contains_var (const struct dictionary *d, const struct variable *v)
480 if (var_has_vardict (v))
482 const struct vardict_info *vdi = var_get_vardict (v);
483 return (vdi->dict_index >= 0
484 && vdi->dict_index < d->var_cnt
485 && d->var[vdi->dict_index] == v);
491 /* Compares two double pointers to variables, which should point
492 to elements of a struct dictionary's `var' member array. */
494 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED)
496 struct variable *const *a = a_;
497 struct variable *const *b = b_;
499 return *a < *b ? -1 : *a > *b;
502 /* Sets the dict_index in V's vardict to DICT_INDEX. */
504 set_var_dict_index (struct variable *v, int dict_index)
506 struct vardict_info vdi = *var_get_vardict (v);
507 struct dictionary *d = vdi.dict;
508 vdi.dict_index = dict_index;
509 var_set_vardict (v, &vdi);
511 if ( d->changed ) d->changed (d, d->changed_data);
512 if ( d->callbacks && d->callbacks->var_changed )
513 d->callbacks->var_changed (d, dict_index, d->cb_data);
516 /* Sets the case_index in V's vardict to CASE_INDEX. */
518 set_var_case_index (struct variable *v, int case_index)
520 struct vardict_info vdi = *var_get_vardict (v);
521 vdi.case_index = case_index;
522 var_set_vardict (v, &vdi);
525 /* Re-sets the dict_index in the dictionary variables with
526 indexes from FROM to TO (exclusive). */
528 reindex_vars (struct dictionary *d, size_t from, size_t to)
532 for (i = from; i < to; i++)
533 set_var_dict_index (d->var[i], i);
536 /* Deletes variable V from dictionary D and frees V.
538 This is a very bad idea if there might be any pointers to V
539 from outside D. In general, no variable in the active file's
540 dictionary should be deleted when any transformations are
541 active on the dictionary's dataset, because those
542 transformations might reference the deleted variable. The
543 safest time to delete a variable is just after a procedure has
544 been executed, as done by DELETE VARIABLES.
546 Pointers to V within D are not a problem, because
547 dict_delete_var() knows to remove V from split variables,
548 weights, filters, etc. */
550 dict_delete_var (struct dictionary *d, struct variable *v)
552 int dict_index = var_get_dict_index (v);
553 const int case_index = var_get_case_index (v);
554 const int width = var_get_width (v);
556 assert (dict_contains_var (d, v));
558 /* Delete aux data. */
561 dict_unset_split_var (d, v);
564 dict_set_weight (d, NULL);
567 dict_set_filter (d, NULL);
569 dict_clear_vectors (d);
571 /* Remove V from var array. */
572 remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
575 /* Update dict_index for each affected variable. */
576 reindex_vars (d, dict_index, d->var_cnt);
578 /* Update name hash. */
579 hsh_force_delete (d->name_tab, v);
583 var_clear_vardict (v);
586 if ( d->changed ) d->changed (d, d->changed_data);
588 invalidate_proto (d);
589 if (d->callbacks && d->callbacks->var_deleted )
590 d->callbacks->var_deleted (d, dict_index, case_index, width, d->cb_data);
593 /* Deletes the COUNT variables listed in VARS from D. This is
594 unsafe; see the comment on dict_delete_var() for details. */
596 dict_delete_vars (struct dictionary *d,
597 struct variable *const *vars, size_t count)
599 /* FIXME: this can be done in O(count) time, but this algorithm
602 assert (count == 0 || vars != NULL);
605 dict_delete_var (d, *vars++);
608 /* Deletes the COUNT variables in D starting at index IDX. This
609 is unsafe; see the comment on dict_delete_var() for
612 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
614 /* FIXME: this can be done in O(count) time, but this algorithm
616 assert (idx + count <= d->var_cnt);
619 dict_delete_var (d, d->var[idx]);
622 /* Deletes scratch variables from dictionary D. */
624 dict_delete_scratch_vars (struct dictionary *d)
628 /* FIXME: this can be done in O(count) time, but this algorithm
632 for (i = 0; i < d->var_cnt; )
633 if (var_get_dict_class (d->var[i]) == DC_SCRATCH)
634 dict_delete_var (d, d->var[i]);
639 /* Moves V to 0-based position IDX in D. Other variables in D,
640 if any, retain their relative positions. Runs in time linear
641 in the distance moved. */
643 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index)
645 size_t old_index = var_get_dict_index (v);
647 assert (new_index < d->var_cnt);
648 move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
649 reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
652 /* Reorders the variables in D, placing the COUNT variables
653 listed in ORDER in that order at the beginning of D. The
654 other variables in D, if any, retain their relative
657 dict_reorder_vars (struct dictionary *d,
658 struct variable *const *order, size_t count)
660 struct variable **new_var;
664 assert (count == 0 || order != NULL);
665 assert (count <= d->var_cnt);
667 new_var = xnmalloc (d->var_cnt, sizeof *new_var);
668 memcpy (new_var, order, count * sizeof *new_var);
669 for (i = 0; i < count; i++)
671 size_t index = var_get_dict_index (order[i]);
672 assert (d->var[index] == order[i]);
673 d->var[index] = NULL;
674 set_var_dict_index (order[i], i);
676 for (i = 0; i < d->var_cnt; i++)
677 if (d->var[i] != NULL)
679 assert (count < d->var_cnt);
680 new_var[count] = d->var[i];
681 set_var_dict_index (new_var[count], count);
688 /* Changes the name of variable V in dictionary D to NEW_NAME. */
690 rename_var (struct dictionary *d, struct variable *v, const char *new_name)
692 struct vardict_info vdi;
694 assert (dict_contains_var (d, v));
696 vdi = *var_get_vardict (v);
697 var_clear_vardict (v);
698 var_set_name (v, new_name);
699 var_set_vardict (v, &vdi);
702 /* Changes the name of V in D to name NEW_NAME. Assert-fails if
703 a variable named NEW_NAME is already in D, except that
704 NEW_NAME may be the same as V's existing name. */
706 dict_rename_var (struct dictionary *d, struct variable *v,
707 const char *new_name)
709 assert (!strcasecmp (var_get_name (v), new_name)
710 || dict_lookup_var (d, new_name) == NULL);
712 hsh_force_delete (d->name_tab, v);
713 rename_var (d, v, new_name);
714 hsh_force_insert (d->name_tab, v);
716 if (settings_get_algorithm () == ENHANCED)
717 var_clear_short_names (v);
719 if ( d->changed ) d->changed (d, d->changed_data);
720 if ( d->callbacks && d->callbacks->var_changed )
721 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
724 /* Renames COUNT variables specified in VARS to the names given
725 in NEW_NAMES within dictionary D. If the renaming would
726 result in a duplicate variable name, returns false and stores a
727 name that would be duplicated into *ERR_NAME (if ERR_NAME is
728 non-null). Otherwise, the renaming is successful, and true
731 dict_rename_vars (struct dictionary *d,
732 struct variable **vars, char **new_names, size_t count,
739 assert (count == 0 || vars != NULL);
740 assert (count == 0 || new_names != NULL);
742 /* Save the names of the variables to be renamed. */
743 pool = pool_create ();
744 old_names = pool_nalloc (pool, count, sizeof *old_names);
745 for (i = 0; i < count; i++)
746 old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
748 /* Remove the variables to be renamed from the name hash,
750 for (i = 0; i < count; i++)
752 hsh_force_delete (d->name_tab, vars[i]);
753 rename_var (d, vars[i], new_names[i]);
756 /* Add the renamed variables back into the name hash,
757 checking for conflicts. */
758 for (i = 0; i < count; i++)
759 if (hsh_insert (d->name_tab, vars[i]) != NULL)
761 /* There is a name conflict.
762 Back out all the name changes that have already
763 taken place, and indicate failure. */
765 if (err_name != NULL)
766 *err_name = new_names[i];
768 for (i = 0; i < fail_idx; i++)
769 hsh_force_delete (d->name_tab, vars[i]);
771 for (i = 0; i < count; i++)
773 rename_var (d, vars[i], old_names[i]);
774 hsh_force_insert (d->name_tab, vars[i]);
781 /* Clear short names. */
782 if (settings_get_algorithm () == ENHANCED)
783 for (i = 0; i < count; i++)
784 var_clear_short_names (vars[i]);
790 /* Returns true if a variable named NAME may be inserted in DICT;
791 that is, if there is not already a variable with that name in
792 DICT and if NAME is not a reserved word. (The caller's checks
793 have already verified that NAME is otherwise acceptable as a
796 var_name_is_insertable (const struct dictionary *dict, const char *name)
798 return (dict_lookup_var (dict, name) == NULL
799 && lex_id_to_token (ss_cstr (name)) == T_ID);
803 make_hinted_name (const struct dictionary *dict, const char *hint,
804 char name[VAR_NAME_LEN + 1])
806 bool dropped = false;
809 for (cp = name; *hint && cp < name + VAR_NAME_LEN; hint++)
812 ? lex_is_id1 (*hint) && *hint != '$'
813 : lex_is_idn (*hint))
820 if (cp < name + VAR_NAME_LEN)
830 size_t len = strlen (name);
833 if (var_name_is_insertable (dict, name))
836 for (i = 0; i < ULONG_MAX; i++)
838 char suffix[INT_BUFSIZE_BOUND (i) + 1];
842 if (!str_format_26adic (i + 1, &suffix[1], sizeof suffix - 1))
845 ofs = MIN (VAR_NAME_LEN - strlen (suffix), len);
846 strcpy (&name[ofs], suffix);
848 if (var_name_is_insertable (dict, name))
857 make_numeric_name (const struct dictionary *dict, unsigned long int *num_start,
858 char name[VAR_NAME_LEN + 1])
860 unsigned long int number;
862 for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
866 sprintf (name, "VAR%03lu", number);
867 if (dict_lookup_var (dict, name) == NULL)
869 if (num_start != NULL)
870 *num_start = number + 1;
875 if (num_start != NULL)
876 *num_start = ULONG_MAX;
881 /* Attempts to devise a variable name unique within DICT.
882 Returns true if successful, in which case the new variable
883 name is stored into NAME. Returns false if all names that can
884 be generated have already been taken. (Returning false is
885 quite unlikely: at least ULONG_MAX unique names can be
888 HINT, if it is non-null, is used as a suggestion that will be
889 modified for suitability as a variable name and for
892 If HINT is null or entirely unsuitable, a name in the form
893 "VAR%03d" will be generated, where the smallest unused integer
894 value is used. If NUM_START is non-null, then its value is
895 used as the minimum numeric value to check, and it is updated
896 to the next value to be checked.
899 dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
900 unsigned long int *num_start,
901 char name[VAR_NAME_LEN + 1])
903 return ((hint != NULL && make_hinted_name (dict, hint, name))
904 || make_numeric_name (dict, num_start, name));
907 /* Returns the weighting variable in dictionary D, or a null
908 pointer if the dictionary is unweighted. */
910 dict_get_weight (const struct dictionary *d)
913 assert (d->weight == NULL || dict_contains_var (d, d->weight));
918 /* Returns the value of D's weighting variable in case C, except
919 that a negative weight is returned as 0. Returns 1 if the
920 dictionary is unweighted. Will warn about missing, negative,
921 or zero values if *WARN_ON_INVALID is true. The function will
922 set *WARN_ON_INVALID to false if an invalid weight is
925 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
926 bool *warn_on_invalid)
931 if (d->weight == NULL)
935 double w = case_num (c, d->weight);
936 if (w < 0.0 || var_is_num_missing (d->weight, w, MV_ANY))
938 if ( w == 0.0 && warn_on_invalid != NULL && *warn_on_invalid ) {
939 *warn_on_invalid = false;
940 msg (SW, _("At least one case in the data file had a weight value "
941 "that was user-missing, system-missing, zero, or "
942 "negative. These case(s) were ignored."));
948 /* Sets the weighting variable of D to V, or turning off
949 weighting if V is a null pointer. */
951 dict_set_weight (struct dictionary *d, struct variable *v)
954 assert (v == NULL || dict_contains_var (d, v));
955 assert (v == NULL || var_is_numeric (v));
959 if (d->changed) d->changed (d, d->changed_data);
960 if ( d->callbacks && d->callbacks->weight_changed )
961 d->callbacks->weight_changed (d,
962 v ? var_get_dict_index (v) : -1,
966 /* Returns the filter variable in dictionary D (see cmd_filter())
967 or a null pointer if the dictionary is unfiltered. */
969 dict_get_filter (const struct dictionary *d)
972 assert (d->filter == NULL || dict_contains_var (d, d->filter));
977 /* Sets V as the filter variable for dictionary D. Passing a
978 null pointer for V turn off filtering. */
980 dict_set_filter (struct dictionary *d, struct variable *v)
983 assert (v == NULL || dict_contains_var (d, v));
984 assert (v == NULL || var_is_numeric (v));
988 if (d->changed) d->changed (d, d->changed_data);
989 if ( d->callbacks && d->callbacks->filter_changed )
990 d->callbacks->filter_changed (d,
991 v ? var_get_dict_index (v) : -1,
995 /* Returns the case limit for dictionary D, or zero if the number
996 of cases is unlimited. */
998 dict_get_case_limit (const struct dictionary *d)
1002 return d->case_limit;
1005 /* Sets CASE_LIMIT as the case limit for dictionary D. Use
1006 0 for CASE_LIMIT to indicate no limit. */
1008 dict_set_case_limit (struct dictionary *d, casenumber case_limit)
1012 d->case_limit = case_limit;
1015 /* Returns the prototype used for cases created by dictionary D. */
1016 const struct caseproto *
1017 dict_get_proto (const struct dictionary *d_)
1019 struct dictionary *d = CONST_CAST (struct dictionary *, d_);
1020 if (d->proto == NULL)
1024 d->proto = caseproto_create ();
1025 d->proto = caseproto_reserve (d->proto, d->var_cnt);
1026 for (i = 0; i < d->var_cnt; i++)
1027 d->proto = caseproto_set_width (d->proto,
1028 var_get_case_index (d->var[i]),
1029 var_get_width (d->var[i]));
1034 /* Returns the case index of the next value to be added to D.
1035 This value is the number of `union value's that need to be
1036 allocated to store a case for dictionary D. */
1038 dict_get_next_value_idx (const struct dictionary *d)
1042 return d->next_value_idx;
1045 /* Returns the number of bytes needed to store a case for
1048 dict_get_case_size (const struct dictionary *d)
1052 return sizeof (union value) * dict_get_next_value_idx (d);
1055 /* Reassigns values in dictionary D so that fragmentation is
1058 dict_compact_values (struct dictionary *d)
1062 d->next_value_idx = 0;
1063 for (i = 0; i < d->var_cnt; i++)
1065 struct variable *v = d->var[i];
1066 set_var_case_index (v, d->next_value_idx++);
1068 invalidate_proto (d);
1071 /* Returns the number of values occupied by the variables in
1072 dictionary D. All variables are considered if EXCLUDE_CLASSES
1073 is 0, or it may contain one or more of (1u << DC_ORDINARY),
1074 (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to exclude the
1075 corresponding type of variable.
1077 The return value may be less than the number of values in one
1078 of dictionary D's cases (as returned by
1079 dict_get_next_value_idx) even if E is 0, because there may be
1080 gaps in D's cases due to deleted variables. */
1082 dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
1087 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1089 | (1u << DC_SCRATCH))) == 0);
1092 for (i = 0; i < d->var_cnt; i++)
1094 enum dict_class class = var_get_dict_class (d->var[i]);
1095 if (!(exclude_classes & (1u << class)))
1101 /* Returns the case prototype that would result after deleting
1102 all variables from D that are not in one of the
1103 EXCLUDE_CLASSES and compacting the dictionary with
1106 The caller must unref the returned caseproto when it is no
1109 dict_get_compacted_proto (const struct dictionary *d,
1110 unsigned int exclude_classes)
1112 struct caseproto *proto;
1115 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1117 | (1u << DC_SCRATCH))) == 0);
1119 proto = caseproto_create ();
1120 for (i = 0; i < d->var_cnt; i++)
1122 struct variable *v = d->var[i];
1123 if (!(exclude_classes & (1u << var_get_dict_class (v))))
1124 proto = caseproto_add_width (proto, var_get_width (v));
1129 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
1130 dict_get_split_cnt() to determine how many SPLIT FILE vars
1131 there are. Returns a null pointer if and only if there are no
1133 const struct variable *const *
1134 dict_get_split_vars (const struct dictionary *d)
1141 /* Returns the number of SPLIT FILE vars. */
1143 dict_get_split_cnt (const struct dictionary *d)
1147 return d->split_cnt;
1150 /* Removes variable V, which must be in D, from D's set of split
1153 dict_unset_split_var (struct dictionary *d, struct variable *v)
1157 assert (dict_contains_var (d, v));
1159 orig_count = d->split_cnt;
1160 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
1161 &v, compare_var_ptrs, NULL);
1162 if (orig_count != d->split_cnt)
1164 if (d->changed) d->changed (d, d->changed_data);
1165 /* We changed the set of split variables so invoke the
1167 if (d->callbacks && d->callbacks->split_changed)
1168 d->callbacks->split_changed (d, d->cb_data);
1172 /* Sets CNT split vars SPLIT in dictionary D. */
1174 dict_set_split_vars (struct dictionary *d,
1175 struct variable *const *split, size_t cnt)
1178 assert (cnt == 0 || split != NULL);
1183 d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
1184 memcpy (d->split, split, cnt * sizeof *d->split);
1192 if (d->changed) d->changed (d, d->changed_data);
1193 if ( d->callbacks && d->callbacks->split_changed )
1194 d->callbacks->split_changed (d, d->cb_data);
1197 /* Returns the file label for D, or a null pointer if D is
1198 unlabeled (see cmd_file_label()). */
1200 dict_get_label (const struct dictionary *d)
1207 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1210 dict_set_label (struct dictionary *d, const char *label)
1215 d->label = label != NULL ? xstrndup (label, 60) : NULL;
1218 /* Returns the documents for D, or a null pointer if D has no
1219 documents. If the return value is nonnull, then the string
1220 will be an exact multiple of DOC_LINE_LENGTH bytes in length,
1221 with each segment corresponding to one line. */
1223 dict_get_documents (const struct dictionary *d)
1225 return ds_is_empty (&d->documents) ? NULL : ds_cstr (&d->documents);
1228 /* Sets the documents for D to DOCUMENTS, or removes D's
1229 documents if DOCUMENT is a null pointer. If DOCUMENTS is
1230 nonnull, then it should be an exact multiple of
1231 DOC_LINE_LENGTH bytes in length, with each segment
1232 corresponding to one line. */
1234 dict_set_documents (struct dictionary *d, const char *documents)
1238 ds_assign_cstr (&d->documents, documents != NULL ? documents : "");
1240 /* In case the caller didn't get it quite right, pad out the
1241 final line with spaces. */
1242 remainder = ds_length (&d->documents) % DOC_LINE_LENGTH;
1244 ds_put_char_multiple (&d->documents, ' ', DOC_LINE_LENGTH - remainder);
1247 /* Drops the documents from dictionary D. */
1249 dict_clear_documents (struct dictionary *d)
1251 ds_clear (&d->documents);
1254 /* Appends LINE to the documents in D. LINE will be truncated or
1255 padded on the right with spaces to make it exactly
1256 DOC_LINE_LENGTH bytes long. */
1258 dict_add_document_line (struct dictionary *d, const char *line)
1260 if (strlen (line) > DOC_LINE_LENGTH)
1262 /* Note to translators: "bytes" is correct, not characters */
1263 msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH);
1265 buf_copy_str_rpad (ds_put_uninit (&d->documents, DOC_LINE_LENGTH),
1266 DOC_LINE_LENGTH, line, ' ');
1269 /* Returns the number of document lines in dictionary D. */
1271 dict_get_document_line_cnt (const struct dictionary *d)
1273 return ds_length (&d->documents) / DOC_LINE_LENGTH;
1276 /* Copies document line number IDX from dictionary D into
1277 LINE, trimming off any trailing white space. */
1279 dict_get_document_line (const struct dictionary *d,
1280 size_t idx, struct string *line)
1282 assert (idx < dict_get_document_line_cnt (d));
1283 ds_assign_substring (line, ds_substr (&d->documents, idx * DOC_LINE_LENGTH,
1285 ds_rtrim (line, ss_cstr (CC_SPACES));
1288 /* Creates in D a vector named NAME that contains the CNT
1289 variables in VAR. Returns true if successful, or false if a
1290 vector named NAME already exists in D. */
1292 dict_create_vector (struct dictionary *d,
1294 struct variable **var, size_t cnt)
1298 assert (var != NULL);
1300 for (i = 0; i < cnt; i++)
1301 assert (dict_contains_var (d, var[i]));
1303 if (dict_lookup_vector (d, name) == NULL)
1305 d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1306 d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1313 /* Creates in D a vector named NAME that contains the CNT
1314 variables in VAR. A vector named NAME must not already exist
1317 dict_create_vector_assert (struct dictionary *d,
1319 struct variable **var, size_t cnt)
1321 assert (dict_lookup_vector (d, name) == NULL);
1322 dict_create_vector (d, name, var, cnt);
1325 /* Returns the vector in D with index IDX, which must be less
1326 than dict_get_vector_cnt (D). */
1327 const struct vector *
1328 dict_get_vector (const struct dictionary *d, size_t idx)
1331 assert (idx < d->vector_cnt);
1333 return d->vector[idx];
1336 /* Returns the number of vectors in D. */
1338 dict_get_vector_cnt (const struct dictionary *d)
1342 return d->vector_cnt;
1345 /* Looks up and returns the vector within D with the given
1347 const struct vector *
1348 dict_lookup_vector (const struct dictionary *d, const char *name)
1351 for (i = 0; i < d->vector_cnt; i++)
1352 if (!strcasecmp (vector_get_name (d->vector[i]), name))
1353 return d->vector[i];
1357 /* Deletes all vectors from D. */
1359 dict_clear_vectors (struct dictionary *d)
1363 for (i = 0; i < d->vector_cnt; i++)
1364 vector_destroy (d->vector[i]);
1371 /* Returns D's attribute set. The caller may examine or modify
1372 the attribute set, but must not destroy it. Destroying D or
1373 calling dict_set_attributes for D will also destroy D's
1376 dict_get_attributes (const struct dictionary *d)
1378 return CONST_CAST (struct attrset *, &d->attributes);
1381 /* Replaces D's attributes set by a copy of ATTRS. */
1383 dict_set_attributes (struct dictionary *d, const struct attrset *attrs)
1385 attrset_destroy (&d->attributes);
1386 attrset_clone (&d->attributes, attrs);
1389 /* Returns true if D has at least one attribute in its attribute
1390 set, false if D's attribute set is empty. */
1392 dict_has_attributes (const struct dictionary *d)
1394 return attrset_count (&d->attributes) > 0;
1397 /* Called from variable.c to notify the dictionary that some property of
1398 the variable has changed */
1400 dict_var_changed (const struct variable *v)
1402 if ( var_has_vardict (v))
1404 const struct vardict_info *vdi = var_get_vardict (v);
1405 struct dictionary *d = vdi->dict;
1410 if (d->changed ) d->changed (d, d->changed_data);
1411 if ( d->callbacks && d->callbacks->var_changed )
1412 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
1417 /* Called from variable.c to notify the dictionary that the variable's width
1420 dict_var_resized (const struct variable *v, int old_width)
1422 if ( var_has_vardict (v))
1424 const struct vardict_info *vdi = var_get_vardict (v);
1425 struct dictionary *d;
1429 if (d->changed) d->changed (d, d->changed_data);
1431 invalidate_proto (d);
1432 if ( d->callbacks && d->callbacks->var_resized )
1433 d->callbacks->var_resized (d, var_get_dict_index (v), old_width,
1438 /* Called from variable.c to notify the dictionary that the variable's display width
1441 dict_var_display_width_changed (const struct variable *v)
1443 if ( var_has_vardict (v))
1445 const struct vardict_info *vdi = var_get_vardict (v);
1446 struct dictionary *d;
1450 if (d->changed) d->changed (d, d->changed_data);
1451 if ( d->callbacks && d->callbacks->var_display_width_changed )
1452 d->callbacks->var_display_width_changed (d, var_get_dict_index (v), d->cb_data);