1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010 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)
86 d->encoding = xstrdup (enc);
91 dict_get_encoding (const struct dictionary *d)
98 dict_set_change_callback (struct dictionary *d,
99 void (*changed) (struct dictionary *, void*),
102 d->changed = changed;
103 d->changed_data = data;
106 /* Discards dictionary D's caseproto. (It will be regenerated
107 lazily, on demand.) */
109 invalidate_proto (struct dictionary *d)
111 caseproto_unref (d->proto);
115 /* Print a representation of dictionary D to stdout, for
116 debugging purposes. */
118 dict_dump (const struct dictionary *d)
121 for (i = 0 ; i < d->var_cnt ; ++i )
123 const struct variable *v =
125 printf ("Name: %s;\tdict_idx: %zu; case_idx: %zu\n",
127 var_get_dict_index (v),
128 var_get_case_index (v));
133 /* Associate CALLBACKS with DICT. Callbacks will be invoked whenever
134 the dictionary or any of the variables it contains are modified.
135 Each callback will get passed CALLBACK_DATA.
136 Any callback may be NULL, in which case it'll be ignored.
139 dict_set_callbacks (struct dictionary *dict,
140 const struct dict_callbacks *callbacks,
143 dict->callbacks = callbacks;
144 dict->cb_data = callback_data;
147 /* Shallow copy the callbacks from SRC to DEST */
149 dict_copy_callbacks (struct dictionary *dest,
150 const struct dictionary *src)
152 dest->callbacks = src->callbacks;
153 dest->cb_data = src->cb_data;
156 /* Creates and returns a new dictionary. */
160 struct dictionary *d = xzalloc (sizeof *d);
162 d->name_tab = hsh_create (8, compare_vars_by_name, hash_var_by_name,
164 attrset_init (&d->attributes);
168 /* Creates and returns a (deep) copy of an existing
171 The new dictionary's case indexes are copied from the old
172 dictionary. If the new dictionary won't be used to access
173 cases produced with the old dictionary, then the new
174 dictionary's case indexes should be compacted with
175 dict_compact_values to save space. */
177 dict_clone (const struct dictionary *s)
179 struct dictionary *d;
186 for (i = 0; i < s->var_cnt; i++)
188 const struct vardict_info *svdi;
189 struct vardict_info dvdi;
190 struct variable *sv = s->var[i];
191 struct variable *dv = dict_clone_var_assert (d, sv);
194 for (i = 0; i < var_get_short_name_cnt (sv); i++)
195 var_set_short_name (dv, i, var_get_short_name (sv, i));
197 svdi = var_get_vardict (sv);
200 var_set_vardict (dv, &dvdi);
203 d->next_value_idx = s->next_value_idx;
205 d->split_cnt = s->split_cnt;
206 if (d->split_cnt > 0)
208 d->split = xnmalloc (d->split_cnt, sizeof *d->split);
209 for (i = 0; i < d->split_cnt; i++)
210 d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
213 if (s->weight != NULL)
214 dict_set_weight (d, dict_lookup_var_assert (d, var_get_name (s->weight)));
216 if (s->filter != NULL)
217 dict_set_filter (d, dict_lookup_var_assert (d, var_get_name (s->filter)));
219 d->case_limit = s->case_limit;
220 dict_set_label (d, dict_get_label (s));
221 dict_set_documents (d, dict_get_documents (s));
223 d->vector_cnt = s->vector_cnt;
224 d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
225 for (i = 0; i < s->vector_cnt; i++)
226 d->vector[i] = vector_clone (s->vector[i], s, d);
229 d->encoding = xstrdup (s->encoding);
231 dict_set_attributes (d, dict_get_attributes (s));
236 /* Clears the contents from a dictionary without destroying the
237 dictionary itself. */
239 dict_clear (struct dictionary *d)
241 /* FIXME? Should we really clear case_limit, label, documents?
242 Others are necessarily cleared by deleting all the variables.*/
245 while (d->var_cnt > 0 )
247 dict_delete_var (d, d->var[d->var_cnt - 1]);
252 d->var_cnt = d->var_cap = 0;
253 invalidate_proto (d);
254 hsh_clear (d->name_tab);
255 d->next_value_idx = 0;
256 dict_set_split_vars (d, NULL, 0);
257 dict_set_weight (d, NULL);
258 dict_set_filter (d, NULL);
262 ds_destroy (&d->documents);
263 dict_clear_vectors (d);
264 attrset_clear (&d->attributes);
267 /* Destroys the aux data for every variable in D, by calling
268 var_clear_aux() for each variable. */
270 dict_clear_aux (struct dictionary *d)
276 for (i = 0; i < d->var_cnt; i++)
277 var_clear_aux (d->var[i]);
280 /* Clears a dictionary and destroys it. */
282 dict_destroy (struct dictionary *d)
286 /* In general, we don't want callbacks occuring, if the dictionary
287 is being destroyed */
288 d->callbacks = NULL ;
291 hsh_destroy (d->name_tab);
292 attrset_destroy (&d->attributes);
297 /* Returns the number of variables in D. */
299 dict_get_var_cnt (const struct dictionary *d)
306 /* Returns the variable in D with dictionary index IDX, which
307 must be between 0 and the count returned by
308 dict_get_var_cnt(), exclusive. */
310 dict_get_var (const struct dictionary *d, size_t idx)
313 assert (idx < d->var_cnt);
318 /* Sets *VARS to an array of pointers to variables in D and *CNT
319 to the number of variables in *D. All variables are returned
320 except for those, if any, in the classes indicated by EXCLUDE.
321 (There is no point in putting DC_SYSTEM in EXCLUDE as
322 dictionaries never include system variables.) */
324 dict_get_vars (const struct dictionary *d, const struct variable ***vars,
325 size_t *cnt, enum dict_class exclude)
327 dict_get_vars_mutable (d, (struct variable ***) vars, cnt, exclude);
330 /* Sets *VARS to an array of pointers to variables in D and *CNT
331 to the number of variables in *D. All variables are returned
332 except for those, if any, in the classes indicated by EXCLUDE.
333 (There is no point in putting DC_SYSTEM in EXCLUDE as
334 dictionaries never include system variables.) */
336 dict_get_vars_mutable (const struct dictionary *d, struct variable ***vars,
337 size_t *cnt, enum dict_class exclude)
343 assert (vars != NULL);
344 assert (cnt != NULL);
345 assert (exclude == (exclude & DC_ALL));
348 for (i = 0; i < d->var_cnt; i++)
350 enum dict_class class = var_get_dict_class (d->var[i]);
351 if (!(class & exclude))
355 *vars = xnmalloc (count, sizeof **vars);
357 for (i = 0; i < d->var_cnt; i++)
359 enum dict_class class = var_get_dict_class (d->var[i]);
360 if (!(class & exclude))
361 (*vars)[(*cnt)++] = d->var[i];
363 assert (*cnt == count);
366 static struct variable *
367 add_var (struct dictionary *d, struct variable *v)
369 /* Add dictionary info to variable. */
370 struct vardict_info vdi;
371 vdi.case_index = d->next_value_idx;
372 vdi.dict_index = d->var_cnt;
374 var_set_vardict (v, &vdi);
376 /* Update dictionary. */
377 if (d->var_cnt >= d->var_cap)
379 d->var_cap = 8 + 2 * d->var_cap;
380 d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var);
382 d->var[d->var_cnt++] = v;
383 hsh_force_insert (d->name_tab, v);
385 if ( d->changed ) d->changed (d, d->changed_data);
386 if ( d->callbacks && d->callbacks->var_added )
387 d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
390 invalidate_proto (d);
395 /* Creates and returns a new variable in D with the given NAME
396 and WIDTH. Returns a null pointer if the given NAME would
397 duplicate that of an existing variable in the dictionary. */
399 dict_create_var (struct dictionary *d, const char *name, int width)
401 return (dict_lookup_var (d, name) == NULL
402 ? dict_create_var_assert (d, name, width)
406 /* Creates and returns a new variable in D with the given NAME
407 and WIDTH. Assert-fails if the given NAME would duplicate
408 that of an existing variable in the dictionary. */
410 dict_create_var_assert (struct dictionary *d, const char *name, int width)
412 assert (dict_lookup_var (d, name) == NULL);
413 return add_var (d, var_create (name, width));
416 /* Creates and returns a new variable in D, as a copy of existing variable
417 OLD_VAR, which need not be in D or in any dictionary. Returns a null
418 pointer if OLD_VAR's name would duplicate that of an existing variable in
421 dict_clone_var (struct dictionary *d, const struct variable *old_var)
423 return dict_clone_var_as (d, old_var, var_get_name (old_var));
426 /* Creates and returns a new variable in D, as a copy of existing variable
427 OLD_VAR, which need not be in D or in any dictionary. Assert-fails if
428 OLD_VAR's name would duplicate that of an existing variable in the
431 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var)
433 return dict_clone_var_as_assert (d, old_var, var_get_name (old_var));
436 /* Creates and returns a new variable in D with name NAME, as a copy of
437 existing variable OLD_VAR, which need not be in D or in any dictionary.
438 Returns a null pointer if the given NAME would duplicate that of an existing
439 variable in the dictionary. */
441 dict_clone_var_as (struct dictionary *d, const struct variable *old_var,
444 return (dict_lookup_var (d, name) == NULL
445 ? dict_clone_var_as_assert (d, old_var, name)
449 /* Creates and returns a new variable in D with name NAME, as a copy of
450 existing variable OLD_VAR, which need not be in D or in any dictionary.
451 Assert-fails if the given NAME would duplicate that of an existing variable
452 in the dictionary. */
454 dict_clone_var_as_assert (struct dictionary *d, const struct variable *old_var,
457 struct variable *new_var = var_clone (old_var);
458 assert (dict_lookup_var (d, name) == NULL);
459 var_set_name (new_var, name);
460 return add_var (d, new_var);
463 /* Returns the variable named NAME in D, or a null pointer if no
464 variable has that name. */
466 dict_lookup_var (const struct dictionary *d, const char *name)
468 struct variable *target ;
469 struct variable *result ;
471 if ( ! var_is_plausible_name (name, false))
474 target = var_create (name, 0);
475 result = hsh_find (d->name_tab, target);
476 var_destroy (target);
478 if ( result && var_has_vardict (result))
480 const struct vardict_info *vdi = var_get_vardict (result);
481 assert (vdi->dict == d);
487 /* Returns the variable named NAME in D. Assert-fails if no
488 variable has that name. */
490 dict_lookup_var_assert (const struct dictionary *d, const char *name)
492 struct variable *v = dict_lookup_var (d, name);
497 /* Returns true if variable V is in dictionary D,
500 dict_contains_var (const struct dictionary *d, const struct variable *v)
502 if (var_has_vardict (v))
504 const struct vardict_info *vdi = var_get_vardict (v);
505 return (vdi->dict_index >= 0
506 && vdi->dict_index < d->var_cnt
507 && d->var[vdi->dict_index] == v);
513 /* Compares two double pointers to variables, which should point
514 to elements of a struct dictionary's `var' member array. */
516 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED)
518 struct variable *const *a = a_;
519 struct variable *const *b = b_;
521 return *a < *b ? -1 : *a > *b;
524 /* Sets the dict_index in V's vardict to DICT_INDEX. */
526 set_var_dict_index (struct variable *v, int dict_index)
528 struct vardict_info vdi = *var_get_vardict (v);
529 struct dictionary *d = vdi.dict;
530 vdi.dict_index = dict_index;
531 var_set_vardict (v, &vdi);
533 if ( d->changed ) d->changed (d, d->changed_data);
534 if ( d->callbacks && d->callbacks->var_changed )
535 d->callbacks->var_changed (d, dict_index, d->cb_data);
538 /* Sets the case_index in V's vardict to CASE_INDEX. */
540 set_var_case_index (struct variable *v, int case_index)
542 struct vardict_info vdi = *var_get_vardict (v);
543 vdi.case_index = case_index;
544 var_set_vardict (v, &vdi);
547 /* Re-sets the dict_index in the dictionary variables with
548 indexes from FROM to TO (exclusive). */
550 reindex_vars (struct dictionary *d, size_t from, size_t to)
554 for (i = from; i < to; i++)
555 set_var_dict_index (d->var[i], i);
558 /* Deletes variable V from dictionary D and frees V.
560 This is a very bad idea if there might be any pointers to V
561 from outside D. In general, no variable in the active file's
562 dictionary should be deleted when any transformations are
563 active on the dictionary's dataset, because those
564 transformations might reference the deleted variable. The
565 safest time to delete a variable is just after a procedure has
566 been executed, as done by DELETE VARIABLES.
568 Pointers to V within D are not a problem, because
569 dict_delete_var() knows to remove V from split variables,
570 weights, filters, etc. */
572 dict_delete_var (struct dictionary *d, struct variable *v)
574 int dict_index = var_get_dict_index (v);
575 const int case_index = var_get_case_index (v);
576 const int width = var_get_width (v);
578 assert (dict_contains_var (d, v));
580 /* Delete aux data. */
583 dict_unset_split_var (d, v);
586 dict_set_weight (d, NULL);
589 dict_set_filter (d, NULL);
591 dict_clear_vectors (d);
593 /* Remove V from var array. */
594 remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
597 /* Update dict_index for each affected variable. */
598 reindex_vars (d, dict_index, d->var_cnt);
600 /* Update name hash. */
601 hsh_force_delete (d->name_tab, v);
605 var_clear_vardict (v);
608 if ( d->changed ) d->changed (d, d->changed_data);
610 invalidate_proto (d);
611 if (d->callbacks && d->callbacks->var_deleted )
612 d->callbacks->var_deleted (d, dict_index, case_index, width, d->cb_data);
615 /* Deletes the COUNT variables listed in VARS from D. This is
616 unsafe; see the comment on dict_delete_var() for details. */
618 dict_delete_vars (struct dictionary *d,
619 struct variable *const *vars, size_t count)
621 /* FIXME: this can be done in O(count) time, but this algorithm
624 assert (count == 0 || vars != NULL);
627 dict_delete_var (d, *vars++);
630 /* Deletes the COUNT variables in D starting at index IDX. This
631 is unsafe; see the comment on dict_delete_var() for
634 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
636 /* FIXME: this can be done in O(count) time, but this algorithm
638 assert (idx + count <= d->var_cnt);
641 dict_delete_var (d, d->var[idx]);
644 /* Deletes scratch variables from dictionary D. */
646 dict_delete_scratch_vars (struct dictionary *d)
650 /* FIXME: this can be done in O(count) time, but this algorithm
654 for (i = 0; i < d->var_cnt; )
655 if (var_get_dict_class (d->var[i]) == DC_SCRATCH)
656 dict_delete_var (d, d->var[i]);
661 /* Moves V to 0-based position IDX in D. Other variables in D,
662 if any, retain their relative positions. Runs in time linear
663 in the distance moved. */
665 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index)
667 size_t old_index = var_get_dict_index (v);
669 assert (new_index < d->var_cnt);
670 move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
671 reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
674 /* Reorders the variables in D, placing the COUNT variables
675 listed in ORDER in that order at the beginning of D. The
676 other variables in D, if any, retain their relative
679 dict_reorder_vars (struct dictionary *d,
680 struct variable *const *order, size_t count)
682 struct variable **new_var;
686 assert (count == 0 || order != NULL);
687 assert (count <= d->var_cnt);
689 new_var = xnmalloc (d->var_cap, sizeof *new_var);
690 memcpy (new_var, order, count * sizeof *new_var);
691 for (i = 0; i < count; i++)
693 size_t index = var_get_dict_index (order[i]);
694 assert (d->var[index] == order[i]);
695 d->var[index] = NULL;
696 set_var_dict_index (order[i], i);
698 for (i = 0; i < d->var_cnt; i++)
699 if (d->var[i] != NULL)
701 assert (count < d->var_cnt);
702 new_var[count] = d->var[i];
703 set_var_dict_index (new_var[count], count);
710 /* Changes the name of variable V in dictionary D to NEW_NAME. */
712 rename_var (struct dictionary *d, struct variable *v, const char *new_name)
714 struct vardict_info vdi;
716 assert (dict_contains_var (d, v));
718 vdi = *var_get_vardict (v);
719 var_clear_vardict (v);
720 var_set_name (v, new_name);
721 var_set_vardict (v, &vdi);
724 /* Changes the name of V in D to name NEW_NAME. Assert-fails if
725 a variable named NEW_NAME is already in D, except that
726 NEW_NAME may be the same as V's existing name. */
728 dict_rename_var (struct dictionary *d, struct variable *v,
729 const char *new_name)
731 assert (!strcasecmp (var_get_name (v), new_name)
732 || dict_lookup_var (d, new_name) == NULL);
734 hsh_force_delete (d->name_tab, v);
735 rename_var (d, v, new_name);
736 hsh_force_insert (d->name_tab, v);
738 if (settings_get_algorithm () == ENHANCED)
739 var_clear_short_names (v);
741 if ( d->changed ) d->changed (d, d->changed_data);
742 if ( d->callbacks && d->callbacks->var_changed )
743 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
746 /* Renames COUNT variables specified in VARS to the names given
747 in NEW_NAMES within dictionary D. If the renaming would
748 result in a duplicate variable name, returns false and stores a
749 name that would be duplicated into *ERR_NAME (if ERR_NAME is
750 non-null). Otherwise, the renaming is successful, and true
753 dict_rename_vars (struct dictionary *d,
754 struct variable **vars, char **new_names, size_t count,
761 assert (count == 0 || vars != NULL);
762 assert (count == 0 || new_names != NULL);
764 /* Save the names of the variables to be renamed. */
765 pool = pool_create ();
766 old_names = pool_nalloc (pool, count, sizeof *old_names);
767 for (i = 0; i < count; i++)
768 old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
770 /* Remove the variables to be renamed from the name hash,
772 for (i = 0; i < count; i++)
774 hsh_force_delete (d->name_tab, vars[i]);
775 rename_var (d, vars[i], new_names[i]);
778 /* Add the renamed variables back into the name hash,
779 checking for conflicts. */
780 for (i = 0; i < count; i++)
781 if (hsh_insert (d->name_tab, vars[i]) != NULL)
783 /* There is a name conflict.
784 Back out all the name changes that have already
785 taken place, and indicate failure. */
787 if (err_name != NULL)
788 *err_name = new_names[i];
790 for (i = 0; i < fail_idx; i++)
791 hsh_force_delete (d->name_tab, vars[i]);
793 for (i = 0; i < count; i++)
795 rename_var (d, vars[i], old_names[i]);
796 hsh_force_insert (d->name_tab, vars[i]);
803 /* Clear short names. */
804 if (settings_get_algorithm () == ENHANCED)
805 for (i = 0; i < count; i++)
806 var_clear_short_names (vars[i]);
812 /* Returns true if a variable named NAME may be inserted in DICT;
813 that is, if there is not already a variable with that name in
814 DICT and if NAME is not a reserved word. (The caller's checks
815 have already verified that NAME is otherwise acceptable as a
818 var_name_is_insertable (const struct dictionary *dict, const char *name)
820 return (dict_lookup_var (dict, name) == NULL
821 && lex_id_to_token (ss_cstr (name)) == T_ID);
825 make_hinted_name (const struct dictionary *dict, const char *hint,
826 char name[VAR_NAME_LEN + 1])
828 bool dropped = false;
831 for (cp = name; *hint && cp < name + VAR_NAME_LEN; hint++)
834 ? lex_is_id1 (*hint) && *hint != '$'
835 : lex_is_idn (*hint))
842 if (cp < name + VAR_NAME_LEN)
852 size_t len = strlen (name);
855 if (var_name_is_insertable (dict, name))
858 for (i = 0; i < ULONG_MAX; i++)
860 char suffix[INT_BUFSIZE_BOUND (i) + 1];
864 if (!str_format_26adic (i + 1, &suffix[1], sizeof suffix - 1))
867 ofs = MIN (VAR_NAME_LEN - strlen (suffix), len);
868 strcpy (&name[ofs], suffix);
870 if (var_name_is_insertable (dict, name))
879 make_numeric_name (const struct dictionary *dict, unsigned long int *num_start,
880 char name[VAR_NAME_LEN + 1])
882 unsigned long int number;
884 for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
888 sprintf (name, "VAR%03lu", number);
889 if (dict_lookup_var (dict, name) == NULL)
891 if (num_start != NULL)
892 *num_start = number + 1;
897 if (num_start != NULL)
898 *num_start = ULONG_MAX;
903 /* Attempts to devise a variable name unique within DICT.
904 Returns true if successful, in which case the new variable
905 name is stored into NAME. Returns false if all names that can
906 be generated have already been taken. (Returning false is
907 quite unlikely: at least ULONG_MAX unique names can be
910 HINT, if it is non-null, is used as a suggestion that will be
911 modified for suitability as a variable name and for
914 If HINT is null or entirely unsuitable, a name in the form
915 "VAR%03d" will be generated, where the smallest unused integer
916 value is used. If NUM_START is non-null, then its value is
917 used as the minimum numeric value to check, and it is updated
918 to the next value to be checked.
921 dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
922 unsigned long int *num_start,
923 char name[VAR_NAME_LEN + 1])
925 return ((hint != NULL && make_hinted_name (dict, hint, name))
926 || make_numeric_name (dict, num_start, name));
929 /* Returns the weighting variable in dictionary D, or a null
930 pointer if the dictionary is unweighted. */
932 dict_get_weight (const struct dictionary *d)
935 assert (d->weight == NULL || dict_contains_var (d, d->weight));
940 /* Returns the value of D's weighting variable in case C, except
941 that a negative weight is returned as 0. Returns 1 if the
942 dictionary is unweighted. Will warn about missing, negative,
943 or zero values if *WARN_ON_INVALID is true. The function will
944 set *WARN_ON_INVALID to false if an invalid weight is
947 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
948 bool *warn_on_invalid)
953 if (d->weight == NULL)
957 double w = case_num (c, d->weight);
958 if (w < 0.0 || var_is_num_missing (d->weight, w, MV_ANY))
960 if ( w == 0.0 && warn_on_invalid != NULL && *warn_on_invalid ) {
961 *warn_on_invalid = false;
962 msg (SW, _("At least one case in the data file had a weight value "
963 "that was user-missing, system-missing, zero, or "
964 "negative. These case(s) were ignored."));
970 /* Sets the weighting variable of D to V, or turning off
971 weighting if V is a null pointer. */
973 dict_set_weight (struct dictionary *d, struct variable *v)
976 assert (v == NULL || dict_contains_var (d, v));
977 assert (v == NULL || var_is_numeric (v));
981 if (d->changed) d->changed (d, d->changed_data);
982 if ( d->callbacks && d->callbacks->weight_changed )
983 d->callbacks->weight_changed (d,
984 v ? var_get_dict_index (v) : -1,
988 /* Returns the filter variable in dictionary D (see cmd_filter())
989 or a null pointer if the dictionary is unfiltered. */
991 dict_get_filter (const struct dictionary *d)
994 assert (d->filter == NULL || dict_contains_var (d, d->filter));
999 /* Sets V as the filter variable for dictionary D. Passing a
1000 null pointer for V turn off filtering. */
1002 dict_set_filter (struct dictionary *d, struct variable *v)
1005 assert (v == NULL || dict_contains_var (d, v));
1006 assert (v == NULL || var_is_numeric (v));
1010 if (d->changed) d->changed (d, d->changed_data);
1011 if ( d->callbacks && d->callbacks->filter_changed )
1012 d->callbacks->filter_changed (d,
1013 v ? var_get_dict_index (v) : -1,
1017 /* Returns the case limit for dictionary D, or zero if the number
1018 of cases is unlimited. */
1020 dict_get_case_limit (const struct dictionary *d)
1024 return d->case_limit;
1027 /* Sets CASE_LIMIT as the case limit for dictionary D. Use
1028 0 for CASE_LIMIT to indicate no limit. */
1030 dict_set_case_limit (struct dictionary *d, casenumber case_limit)
1034 d->case_limit = case_limit;
1037 /* Returns the prototype used for cases created by dictionary D. */
1038 const struct caseproto *
1039 dict_get_proto (const struct dictionary *d_)
1041 struct dictionary *d = CONST_CAST (struct dictionary *, d_);
1042 if (d->proto == NULL)
1046 d->proto = caseproto_create ();
1047 d->proto = caseproto_reserve (d->proto, d->var_cnt);
1048 for (i = 0; i < d->var_cnt; i++)
1049 d->proto = caseproto_set_width (d->proto,
1050 var_get_case_index (d->var[i]),
1051 var_get_width (d->var[i]));
1056 /* Returns the case index of the next value to be added to D.
1057 This value is the number of `union value's that need to be
1058 allocated to store a case for dictionary D. */
1060 dict_get_next_value_idx (const struct dictionary *d)
1064 return d->next_value_idx;
1067 /* Returns the number of bytes needed to store a case for
1070 dict_get_case_size (const struct dictionary *d)
1074 return sizeof (union value) * dict_get_next_value_idx (d);
1077 /* Reassigns values in dictionary D so that fragmentation is
1080 dict_compact_values (struct dictionary *d)
1084 d->next_value_idx = 0;
1085 for (i = 0; i < d->var_cnt; i++)
1087 struct variable *v = d->var[i];
1088 set_var_case_index (v, d->next_value_idx++);
1090 invalidate_proto (d);
1093 /* Returns the number of values occupied by the variables in
1094 dictionary D. All variables are considered if EXCLUDE_CLASSES
1095 is 0, or it may contain one or more of (1u << DC_ORDINARY),
1096 (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to exclude the
1097 corresponding type of variable.
1099 The return value may be less than the number of values in one
1100 of dictionary D's cases (as returned by
1101 dict_get_next_value_idx) even if E is 0, because there may be
1102 gaps in D's cases due to deleted variables. */
1104 dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
1109 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1111 | (1u << DC_SCRATCH))) == 0);
1114 for (i = 0; i < d->var_cnt; i++)
1116 enum dict_class class = var_get_dict_class (d->var[i]);
1117 if (!(exclude_classes & (1u << class)))
1123 /* Returns the case prototype that would result after deleting
1124 all variables from D that are not in one of the
1125 EXCLUDE_CLASSES and compacting the dictionary with
1128 The caller must unref the returned caseproto when it is no
1131 dict_get_compacted_proto (const struct dictionary *d,
1132 unsigned int exclude_classes)
1134 struct caseproto *proto;
1137 assert ((exclude_classes & ~((1u << DC_ORDINARY)
1139 | (1u << DC_SCRATCH))) == 0);
1141 proto = caseproto_create ();
1142 for (i = 0; i < d->var_cnt; i++)
1144 struct variable *v = d->var[i];
1145 if (!(exclude_classes & (1u << var_get_dict_class (v))))
1146 proto = caseproto_add_width (proto, var_get_width (v));
1151 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
1152 dict_get_split_cnt() to determine how many SPLIT FILE vars
1153 there are. Returns a null pointer if and only if there are no
1155 const struct variable *const *
1156 dict_get_split_vars (const struct dictionary *d)
1163 /* Returns the number of SPLIT FILE vars. */
1165 dict_get_split_cnt (const struct dictionary *d)
1169 return d->split_cnt;
1172 /* Removes variable V, which must be in D, from D's set of split
1175 dict_unset_split_var (struct dictionary *d, struct variable *v)
1179 assert (dict_contains_var (d, v));
1181 orig_count = d->split_cnt;
1182 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
1183 &v, compare_var_ptrs, NULL);
1184 if (orig_count != d->split_cnt)
1186 if (d->changed) d->changed (d, d->changed_data);
1187 /* We changed the set of split variables so invoke the
1189 if (d->callbacks && d->callbacks->split_changed)
1190 d->callbacks->split_changed (d, d->cb_data);
1194 /* Sets CNT split vars SPLIT in dictionary D. */
1196 dict_set_split_vars (struct dictionary *d,
1197 struct variable *const *split, size_t cnt)
1200 assert (cnt == 0 || split != NULL);
1205 d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
1206 memcpy (d->split, split, cnt * sizeof *d->split);
1214 if (d->changed) d->changed (d, d->changed_data);
1215 if ( d->callbacks && d->callbacks->split_changed )
1216 d->callbacks->split_changed (d, d->cb_data);
1219 /* Returns the file label for D, or a null pointer if D is
1220 unlabeled (see cmd_file_label()). */
1222 dict_get_label (const struct dictionary *d)
1229 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1232 dict_set_label (struct dictionary *d, const char *label)
1237 d->label = label != NULL ? xstrndup (label, 60) : NULL;
1240 /* Returns the documents for D, or a null pointer if D has no
1241 documents. If the return value is nonnull, then the string
1242 will be an exact multiple of DOC_LINE_LENGTH bytes in length,
1243 with each segment corresponding to one line. */
1245 dict_get_documents (const struct dictionary *d)
1247 return ds_is_empty (&d->documents) ? NULL : ds_cstr (&d->documents);
1250 /* Sets the documents for D to DOCUMENTS, or removes D's
1251 documents if DOCUMENT is a null pointer. If DOCUMENTS is
1252 nonnull, then it should be an exact multiple of
1253 DOC_LINE_LENGTH bytes in length, with each segment
1254 corresponding to one line. */
1256 dict_set_documents (struct dictionary *d, const char *documents)
1260 ds_assign_cstr (&d->documents, documents != NULL ? documents : "");
1262 /* In case the caller didn't get it quite right, pad out the
1263 final line with spaces. */
1264 remainder = ds_length (&d->documents) % DOC_LINE_LENGTH;
1266 ds_put_char_multiple (&d->documents, ' ', DOC_LINE_LENGTH - remainder);
1269 /* Drops the documents from dictionary D. */
1271 dict_clear_documents (struct dictionary *d)
1273 ds_clear (&d->documents);
1276 /* Appends LINE to the documents in D. LINE will be truncated or
1277 padded on the right with spaces to make it exactly
1278 DOC_LINE_LENGTH bytes long. */
1280 dict_add_document_line (struct dictionary *d, const char *line)
1282 if (strlen (line) > DOC_LINE_LENGTH)
1284 /* Note to translators: "bytes" is correct, not characters */
1285 msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH);
1287 buf_copy_str_rpad (ds_put_uninit (&d->documents, DOC_LINE_LENGTH),
1288 DOC_LINE_LENGTH, line, ' ');
1291 /* Returns the number of document lines in dictionary D. */
1293 dict_get_document_line_cnt (const struct dictionary *d)
1295 return ds_length (&d->documents) / DOC_LINE_LENGTH;
1298 /* Copies document line number IDX from dictionary D into
1299 LINE, trimming off any trailing white space. */
1301 dict_get_document_line (const struct dictionary *d,
1302 size_t idx, struct string *line)
1304 assert (idx < dict_get_document_line_cnt (d));
1305 ds_assign_substring (line, ds_substr (&d->documents, idx * DOC_LINE_LENGTH,
1307 ds_rtrim (line, ss_cstr (CC_SPACES));
1310 /* Creates in D a vector named NAME that contains the CNT
1311 variables in VAR. Returns true if successful, or false if a
1312 vector named NAME already exists in D. */
1314 dict_create_vector (struct dictionary *d,
1316 struct variable **var, size_t cnt)
1320 assert (var != NULL);
1322 for (i = 0; i < cnt; i++)
1323 assert (dict_contains_var (d, var[i]));
1325 if (dict_lookup_vector (d, name) == NULL)
1327 d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1328 d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1335 /* Creates in D a vector named NAME that contains the CNT
1336 variables in VAR. A vector named NAME must not already exist
1339 dict_create_vector_assert (struct dictionary *d,
1341 struct variable **var, size_t cnt)
1343 assert (dict_lookup_vector (d, name) == NULL);
1344 dict_create_vector (d, name, var, cnt);
1347 /* Returns the vector in D with index IDX, which must be less
1348 than dict_get_vector_cnt (D). */
1349 const struct vector *
1350 dict_get_vector (const struct dictionary *d, size_t idx)
1353 assert (idx < d->vector_cnt);
1355 return d->vector[idx];
1358 /* Returns the number of vectors in D. */
1360 dict_get_vector_cnt (const struct dictionary *d)
1364 return d->vector_cnt;
1367 /* Looks up and returns the vector within D with the given
1369 const struct vector *
1370 dict_lookup_vector (const struct dictionary *d, const char *name)
1373 for (i = 0; i < d->vector_cnt; i++)
1374 if (!strcasecmp (vector_get_name (d->vector[i]), name))
1375 return d->vector[i];
1379 /* Deletes all vectors from D. */
1381 dict_clear_vectors (struct dictionary *d)
1385 for (i = 0; i < d->vector_cnt; i++)
1386 vector_destroy (d->vector[i]);
1393 /* Returns D's attribute set. The caller may examine or modify
1394 the attribute set, but must not destroy it. Destroying D or
1395 calling dict_set_attributes for D will also destroy D's
1398 dict_get_attributes (const struct dictionary *d)
1400 return CONST_CAST (struct attrset *, &d->attributes);
1403 /* Replaces D's attributes set by a copy of ATTRS. */
1405 dict_set_attributes (struct dictionary *d, const struct attrset *attrs)
1407 attrset_destroy (&d->attributes);
1408 attrset_clone (&d->attributes, attrs);
1411 /* Returns true if D has at least one attribute in its attribute
1412 set, false if D's attribute set is empty. */
1414 dict_has_attributes (const struct dictionary *d)
1416 return attrset_count (&d->attributes) > 0;
1419 /* Called from variable.c to notify the dictionary that some property of
1420 the variable has changed */
1422 dict_var_changed (const struct variable *v)
1424 if ( var_has_vardict (v))
1426 const struct vardict_info *vdi = var_get_vardict (v);
1427 struct dictionary *d = vdi->dict;
1432 if (d->changed ) d->changed (d, d->changed_data);
1433 if ( d->callbacks && d->callbacks->var_changed )
1434 d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
1439 /* Called from variable.c to notify the dictionary that the variable's width
1442 dict_var_resized (const struct variable *v, int old_width)
1444 if ( var_has_vardict (v))
1446 const struct vardict_info *vdi = var_get_vardict (v);
1447 struct dictionary *d;
1451 if (d->changed) d->changed (d, d->changed_data);
1453 invalidate_proto (d);
1454 if ( d->callbacks && d->callbacks->var_resized )
1455 d->callbacks->var_resized (d, var_get_dict_index (v), old_width,
1460 /* Called from variable.c to notify the dictionary that the variable's display width
1463 dict_var_display_width_changed (const struct variable *v)
1465 if ( var_has_vardict (v))
1467 const struct vardict_info *vdi = var_get_vardict (v);
1468 struct dictionary *d;
1472 if (d->changed) d->changed (d, d->changed_data);
1473 if ( d->callbacks && d->callbacks->var_display_width_changed )
1474 d->callbacks->var_display_width_changed (d, var_get_dict_index (v), d->cb_data);
1478 /* Dictionary used to contain "internal variables". */
1479 static struct dictionary *internal_dict;
1481 /* Create a variable of the specified WIDTH to be used for internal
1482 calculations only. The variable is assigned case index CASE_IDX. */
1484 dict_create_internal_var (int case_idx, int width)
1486 if (internal_dict == NULL)
1487 internal_dict = dict_create ();
1491 static int counter = INT_MAX / 2;
1492 struct variable *var;
1495 if (++counter == INT_MAX)
1496 counter = INT_MAX / 2;
1498 sprintf (name, "$internal%d", counter);
1499 var = dict_create_var (internal_dict, name, width);
1502 set_var_case_index (var, case_idx);
1508 /* Destroys VAR, which must have been created with
1509 dict_create_internal_var(). */
1511 dict_destroy_internal_var (struct variable *var)
1515 dict_delete_var (internal_dict, var);
1517 /* Destroy internal_dict if it has no variables left, just so that
1518 valgrind --leak-check --show-reachable won't show internal_dict. */
1519 if (dict_get_var_cnt (internal_dict) == 0)
1521 dict_destroy (internal_dict);
1522 internal_dict = NULL;