1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 #include "dictionary.h"
23 #include "algorithm.h"
30 #include "value-labels.h"
36 struct variable **var; /* Variables. */
37 size_t var_cnt, var_cap; /* Number of variables, capacity. */
38 struct hsh_table *name_tab; /* Variable index by name. */
39 int next_value_idx; /* Index of next `union value' to allocate. */
40 struct variable **split; /* SPLIT FILE vars. */
41 size_t split_cnt; /* SPLIT FILE count. */
42 struct variable *weight; /* WEIGHT variable. */
43 struct variable *filter; /* FILTER variable. */
44 int case_limit; /* Current case limit (N command). */
45 char *label; /* File label. */
46 char *documents; /* Documents, as a string. */
47 struct vector **vector; /* Vectors of variables. */
48 size_t vector_cnt; /* Number of vectors. */
51 /* Creates and returns a new dictionary. */
55 struct dictionary *d = xmalloc (sizeof *d);
58 d->var_cnt = d->var_cap = 0;
59 d->name_tab = hsh_create (8, compare_var_names, hash_var_name, NULL, NULL);
60 d->next_value_idx = 0;
74 /* Creates and returns a (deep) copy of an existing
77 dict_clone (const struct dictionary *s)
85 for (i = 0; i < s->var_cnt; i++)
86 dict_clone_var (d, s->var[i], s->var[i]->name);
87 d->next_value_idx = s->next_value_idx;
89 d->split_cnt = s->split_cnt;
92 d->split = xmalloc (d->split_cnt * sizeof *d->split);
93 for (i = 0; i < d->split_cnt; i++)
94 d->split[i] = dict_lookup_var_assert (d, s->split[i]->name);
97 if (s->weight != NULL)
98 d->weight = dict_lookup_var_assert (d, s->weight->name);
100 if (s->filter != NULL)
101 d->filter = dict_lookup_var_assert (d, s->filter->name);
103 d->case_limit = s->case_limit;
104 dict_set_label (d, dict_get_label (s));
105 dict_set_documents (d, dict_get_documents (s));
107 for (i = 0; i < s->vector_cnt; i++)
108 dict_create_vector (d, s->vector[i]->name,
109 s->vector[i]->var, s->vector[i]->cnt);
114 /* Clears the contents from a dictionary without destroying the
115 dictionary itself. */
117 dict_clear (struct dictionary *d)
119 /* FIXME? Should we really clear case_limit, label, documents?
120 Others are necessarily cleared by deleting all the variables.*/
125 for (i = 0; i < d->var_cnt; i++)
127 struct variable *v = d->var[i];
129 val_labs_destroy (v->val_labs);
135 d->var_cnt = d->var_cap = 0;
136 hsh_clear (d->name_tab);
137 d->next_value_idx = 0;
148 dict_clear_vectors (d);
151 /* Destroys the aux data for every variable in D, by calling
152 var_clear_aux() for each variable. */
154 dict_clear_aux (struct dictionary *d)
160 for (i = 0; i < d->var_cnt; i++)
161 var_clear_aux (d->var[i]);
164 /* Clears a dictionary and destroys it. */
166 dict_destroy (struct dictionary *d)
171 hsh_destroy (d->name_tab);
176 /* Returns the number of variables in D. */
178 dict_get_var_cnt (const struct dictionary *d)
185 /* Returns the variable in D with index IDX, which must be
186 between 0 and the count returned by dict_get_var_cnt(),
189 dict_get_var (const struct dictionary *d, size_t idx)
192 assert (idx < d->var_cnt);
197 /* Sets *VARS to an array of pointers to variables in D and *CNT
198 to the number of variables in *D. By default all variables
199 are returned, but bits may be set in EXCLUDE_CLASSES to
200 exclude ordinary, system, and/or scratch variables. */
202 dict_get_vars (const struct dictionary *d, struct variable ***vars,
203 size_t *cnt, unsigned exclude_classes)
209 assert (vars != NULL);
210 assert (cnt != NULL);
211 assert ((exclude_classes & ~((1u << DC_ORDINARY)
213 | (1u << DC_SCRATCH))) == 0);
216 for (i = 0; i < d->var_cnt; i++)
217 if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
220 *vars = xmalloc (count * sizeof **vars);
222 for (i = 0; i < d->var_cnt; i++)
223 if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
224 (*vars)[(*cnt)++] = d->var[i];
225 assert (*cnt == count);
228 /* Creates and returns a new variable in D with the given NAME
229 and WIDTH. Returns a null pointer if the given NAME would
230 duplicate that of an existing variable in the dictionary. */
232 dict_create_var (struct dictionary *d, const char *name, int width)
237 assert (name != NULL);
238 assert (strlen (name) >= 1 && strlen (name) <= 8);
239 assert (width >= 0 && width < 256);
241 /* Make sure there's not already a variable by that name. */
242 if (dict_lookup_var (d, name) != NULL)
245 /* Allocate and initialize variable. */
246 v = xmalloc (sizeof *v);
247 strncpy (v->name, name, sizeof v->name);
249 v->index = d->var_cnt;
250 v->type = width == 0 ? NUMERIC : ALPHA;
252 v->fv = d->next_value_idx;
253 v->nv = width == 0 ? 1 : DIV_RND_UP (width, 8);
255 v->reinit = dict_class_from_id (name) != DC_SCRATCH;
256 v->miss_type = MISSING_NONE;
257 if (v->type == NUMERIC)
259 v->print.type = FMT_F;
265 v->print.type = FMT_A;
266 v->print.w = v->width;
270 v->val_labs = val_labs_create (v->width);
275 /* Update dictionary. */
276 if (d->var_cnt >= d->var_cap)
278 d->var_cap = 8 + 2 * d->var_cap;
279 d->var = xrealloc (d->var, d->var_cap * sizeof *d->var);
281 d->var[v->index] = v;
283 hsh_force_insert (d->name_tab, v);
284 d->next_value_idx += v->nv;
289 /* Creates and returns a new variable in D with the given NAME
290 and WIDTH. Assert-fails if the given NAME would duplicate
291 that of an existing variable in the dictionary. */
293 dict_create_var_assert (struct dictionary *d, const char *name, int width)
295 struct variable *v = dict_create_var (d, name, width);
300 /* Creates a new variable in D named NAME, as a copy of existing
301 variable OV, which need not be in D or in any dictionary. */
303 dict_clone_var (struct dictionary *d, const struct variable *ov,
310 assert (name != NULL);
311 assert (strlen (name) >= 1 && strlen (name) <= 8);
313 nv = dict_create_var (d, name, ov->width);
318 nv->reinit = ov->reinit;
319 nv->miss_type = ov->miss_type;
320 memcpy (nv->missing, ov->missing, sizeof nv->missing);
321 nv->print = ov->print;
322 nv->write = ov->write;
323 val_labs_destroy (nv->val_labs);
324 nv->val_labs = val_labs_copy (ov->val_labs);
325 if (ov->label != NULL)
326 nv->label = xstrdup (ov->label);
331 /* Changes the name of V in D to name NEW_NAME. Assert-fails if
332 a variable named NEW_NAME is already in D, except that
333 NEW_NAME may be the same as V's existing name. */
335 dict_rename_var (struct dictionary *d, struct variable *v,
336 const char *new_name)
340 assert (new_name != NULL);
341 assert (strlen (new_name) >= 1 && strlen (new_name) <= 8);
342 assert (dict_contains_var (d, v));
344 if (!strcmp (v->name, new_name))
347 assert (dict_lookup_var (d, new_name) == NULL);
349 hsh_force_delete (d->name_tab, v);
350 strncpy (v->name, new_name, sizeof v->name);
352 hsh_force_insert (d->name_tab, v);
355 /* Returns the variable named NAME in D, or a null pointer if no
356 variable has that name. */
358 dict_lookup_var (const struct dictionary *d, const char *name)
363 assert (name != NULL);
364 assert (strlen (name) >= 1 && strlen (name) <= 8);
366 strncpy (v.name, name, sizeof v.name);
369 return hsh_find (d->name_tab, &v);
372 /* Returns the variable named NAME in D. Assert-fails if no
373 variable has that name. */
375 dict_lookup_var_assert (const struct dictionary *d, const char *name)
377 struct variable *v = dict_lookup_var (d, name);
382 /* Returns nonzero if variable V is in dictionary D. */
384 dict_contains_var (const struct dictionary *d, const struct variable *v)
389 return v->index >= 0 && v->index < d->var_cnt && d->var[v->index] == v;
392 /* Compares two double pointers to variables, which should point
393 to elements of a struct dictionary's `var' member array. */
395 compare_var_ptrs (const void *a_, const void *b_, void *aux UNUSED)
397 struct variable *const *a = a_;
398 struct variable *const *b = b_;
400 return *a < *b ? -1 : *a > *b;
403 /* Deletes variable V from dictionary D and frees V.
405 This is a very bad idea if there might be any pointers to V
406 from outside D. In general, no variable in default_dict
407 should be deleted when any transformations are active, because
408 those transformations might reference the deleted variable.
409 The safest time to delete a variable is just after a procedure
410 has been executed, as done by MODIFY VARS.
412 Pointers to V within D are not a problem, because
413 dict_delete_var() knows to remove V from split variables,
414 weights, filters, etc. */
416 dict_delete_var (struct dictionary *d, struct variable *v)
422 assert (dict_contains_var (d, v));
423 assert (d->var[v->index] == v);
425 /* Delete aux data. */
428 /* Remove V from splits, weight, filter variables. */
429 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
430 &v, compare_var_ptrs, NULL);
435 dict_clear_vectors (d);
437 /* Remove V from var array. */
438 remove_element (d->var, d->var_cnt, sizeof *d->var, v->index);
442 for (i = v->index; i < d->var_cnt; i++)
443 d->var[i]->index = i;
445 /* Update name hash. */
446 hsh_force_delete (d->name_tab, v);
449 val_labs_destroy (v->val_labs);
454 /* Deletes the COUNT variables listed in VARS from D. This is
455 unsafe; see the comment on dict_delete_var() for details. */
457 dict_delete_vars (struct dictionary *d,
458 struct variable *const *vars, size_t count)
460 /* FIXME: this can be done in O(count) time, but this algorithm
463 assert (count == 0 || vars != NULL);
466 dict_delete_var (d, *vars++);
469 /* Reorders the variables in D, placing the COUNT variables
470 listed in ORDER in that order at the beginning of D. The
471 other variables in D, if any, retain their relative
474 dict_reorder_vars (struct dictionary *d,
475 struct variable *const *order, size_t count)
477 struct variable **new_var;
481 assert (count == 0 || order != NULL);
482 assert (count <= d->var_cnt);
484 new_var = xmalloc (d->var_cnt * sizeof *new_var);
485 memcpy (new_var, order, count * sizeof *new_var);
486 for (i = 0; i < count; i++)
488 assert (d->var[order[i]->index] != NULL);
489 d->var[order[i]->index] = NULL;
492 for (i = 0; i < d->var_cnt; i++)
493 if (d->var[i] != NULL)
495 assert (count < d->var_cnt);
496 new_var[count] = d->var[i];
497 new_var[count]->index = count;
504 /* Renames COUNT variables specified in VARS to the names given
505 in NEW_NAMES within dictionary D. If the renaming would
506 result in a duplicate variable name, returns zero and stores a
507 name that would be duplicated into *ERR_NAME (if ERR_NAME is
508 non-null). Otherwise, the renaming is successful, and nonzero
511 dict_rename_vars (struct dictionary *d,
512 struct variable **vars, char **new_names,
513 size_t count, char **err_name)
520 assert (count == 0 || vars != NULL);
521 assert (count == 0 || new_names != NULL);
523 old_names = xmalloc (count * sizeof *old_names);
524 for (i = 0; i < count; i++)
526 assert (d->var[vars[i]->index] == vars[i]);
527 hsh_force_delete (d->name_tab, vars[i]);
528 old_names[i] = xstrdup (vars[i]->name);
531 for (i = 0; i < count; i++)
533 assert (new_names[i] != NULL);
534 assert (*new_names[i] != '\0');
535 assert (strlen (new_names[i]) < 9);
536 strcpy (vars[i]->name, new_names[i]);
537 if (hsh_insert (d->name_tab, vars[i]) != NULL)
540 if (err_name != NULL)
541 *err_name = new_names[i];
543 for (i = 0; i < fail_idx; i++)
544 hsh_force_delete (d->name_tab, vars[i]);
546 for (i = 0; i < count; i++)
548 strcpy (vars[i]->name, old_names[i]);
549 hsh_force_insert (d->name_tab, vars[i]);
557 for (i = 0; i < count; i++)
564 /* Returns the weighting variable in dictionary D, or a null
565 pointer if the dictionary is unweighted. */
567 dict_get_weight (const struct dictionary *d)
570 assert (d->weight == NULL || dict_contains_var (d, d->weight));
575 /* Returns the value of D's weighting variable in case C, except that a
576 negative weight is returned as 0. Returns 1 if the dictionary is
577 unweighted. Will warn about missing, negative, or zero values if
578 warn_on_invalid is nonzero. The function will set warn_on_invalid to zero
579 if an invalid weight is found. */
581 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
582 int *warn_on_invalid)
587 if (d->weight == NULL)
591 double w = case_num (c, d->weight->fv);
592 if ( w < 0.0 || w == SYSMIS || is_num_user_missing(w, d->weight) )
594 if ( w == 0.0 && *warn_on_invalid ) {
595 *warn_on_invalid = 0;
596 msg (SW, _("At least one case in the data file had a weight value "
597 "that was user-missing, system-missing, zero, or "
598 "negative. These case(s) were ignored."));
604 /* Sets the weighting variable of D to V, or turning off
605 weighting if V is a null pointer. */
607 dict_set_weight (struct dictionary *d, struct variable *v)
610 assert (v == NULL || dict_contains_var (d, v));
611 assert (v == NULL || v->type == NUMERIC);
616 /* Returns the filter variable in dictionary D (see cmd_filter())
617 or a null pointer if the dictionary is unfiltered. */
619 dict_get_filter (const struct dictionary *d)
622 assert (d->filter == NULL || dict_contains_var (d, d->filter));
627 /* Sets V as the filter variable for dictionary D. Passing a
628 null pointer for V turn off filtering. */
630 dict_set_filter (struct dictionary *d, struct variable *v)
633 assert (v == NULL || dict_contains_var (d, v));
638 /* Returns the case limit for dictionary D, or zero if the number
639 of cases is unlimited (see cmd_n()). */
641 dict_get_case_limit (const struct dictionary *d)
645 return d->case_limit;
648 /* Sets CASE_LIMIT as the case limit for dictionary D. Zero for
649 CASE_LIMIT indicates no limit. */
651 dict_set_case_limit (struct dictionary *d, int case_limit)
654 assert (case_limit >= 0);
656 d->case_limit = case_limit;
659 /* Returns the index of the next value to be added to D. This
660 value is the number of `union value's that need to be
661 allocated to store a case for dictionary D. */
663 dict_get_next_value_idx (const struct dictionary *d)
667 return d->next_value_idx;
670 /* Returns the number of bytes needed to store a case for
673 dict_get_case_size (const struct dictionary *d)
677 return sizeof (union value) * dict_get_next_value_idx (d);
680 /* Deletes scratch variables in dictionary D and reassigns values
681 so that fragmentation is eliminated. */
683 dict_compact_values (struct dictionary *d)
687 d->next_value_idx = 0;
688 for (i = 0; i < d->var_cnt; )
690 struct variable *v = d->var[i];
692 if (dict_class_from_id (v->name) != DC_SCRATCH)
694 v->fv = d->next_value_idx;
695 d->next_value_idx += v->nv;
699 dict_delete_var (default_dict, v);
703 /* Copies values from SRC, which represents a case arranged
704 according to dictionary D, to DST, which represents a case
705 arranged according to the dictionary that will be produced by
706 dict_compact_values(D). */
708 dict_compact_case (const struct dictionary *d,
709 struct ccase *dst, const struct ccase *src)
715 for (i = 0; i < d->var_cnt; i++)
717 struct variable *v = d->var[i];
719 if (dict_class_from_id (v->name) != DC_SCRATCH)
721 case_copy (dst, value_idx, src, v->fv, v->nv);
727 /* Returns the number of values that would be used by a case if
728 dict_compact_values() were called. */
730 dict_get_compacted_value_cnt (const struct dictionary *d)
736 for (i = 0; i < d->var_cnt; i++)
737 if (dict_class_from_id (d->var[i]->name) != DC_SCRATCH)
738 cnt += d->var[i]->nv;
742 /* Creates and returns an array mapping from a dictionary index
743 to the `fv' that the corresponding variable will have after
744 calling dict_compact_values(). Scratch variables receive -1
745 for `fv' because dict_compact_values() will delete them. */
747 dict_get_compacted_idx_to_fv (const struct dictionary *d)
750 size_t next_value_idx;
753 idx_to_fv = xmalloc (d->var_cnt * sizeof *idx_to_fv);
755 for (i = 0; i < d->var_cnt; i++)
757 struct variable *v = d->var[i];
759 if (dict_class_from_id (v->name) != DC_SCRATCH)
761 idx_to_fv[i] = next_value_idx;
762 next_value_idx += v->nv;
770 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
771 dict_get_split_cnt() to determine how many SPLIT FILE vars
772 there are. Returns a null pointer if and only if there are no
774 struct variable *const *
775 dict_get_split_vars (const struct dictionary *d)
782 /* Returns the number of SPLIT FILE vars. */
784 dict_get_split_cnt (const struct dictionary *d)
791 /* Sets CNT split vars SPLIT in dictionary D. */
793 dict_set_split_vars (struct dictionary *d,
794 struct variable *const *split, size_t cnt)
797 assert (cnt == 0 || split != NULL);
800 d->split = xrealloc (d->split, cnt * sizeof *d->split);
801 memcpy (d->split, split, cnt * sizeof *d->split);
804 /* Returns the file label for D, or a null pointer if D is
805 unlabeled (see cmd_file_label()). */
807 dict_get_label (const struct dictionary *d)
814 /* Sets D's file label to LABEL, truncating it to a maximum of 60
817 dict_set_label (struct dictionary *d, const char *label)
824 else if (strlen (label) < 60)
825 d->label = xstrdup (label);
828 d->label = xmalloc (61);
829 memcpy (d->label, label, 60);
834 /* Returns the documents for D, or a null pointer if D has no
835 documents (see cmd_document()).. */
837 dict_get_documents (const struct dictionary *d)
844 /* Sets the documents for D to DOCUMENTS, or removes D's
845 documents if DOCUMENT is a null pointer. */
847 dict_set_documents (struct dictionary *d, const char *documents)
852 if (documents == NULL)
855 d->documents = xstrdup (documents);
858 /* Creates in D a vector named NAME that contains CNT variables
859 VAR (see cmd_vector()). Returns nonzero if successful, or
860 zero if a vector named NAME already exists in D. */
862 dict_create_vector (struct dictionary *d,
864 struct variable **var, size_t cnt)
866 struct vector *vector;
869 assert (name != NULL);
870 assert (strlen (name) > 0 && strlen (name) < 9);
871 assert (var != NULL);
874 if (dict_lookup_vector (d, name) != NULL)
877 d->vector = xrealloc (d->vector, (d->vector_cnt + 1) * sizeof *d->vector);
878 vector = d->vector[d->vector_cnt] = xmalloc (sizeof *vector);
879 vector->idx = d->vector_cnt++;
880 strncpy (vector->name, name, 8);
881 vector->name[8] = '\0';
882 vector->var = xmalloc (cnt * sizeof *var);
883 memcpy (vector->var, var, cnt * sizeof *var);
889 /* Returns the vector in D with index IDX, which must be less
890 than dict_get_vector_cnt (D). */
891 const struct vector *
892 dict_get_vector (const struct dictionary *d, size_t idx)
895 assert (idx < d->vector_cnt);
897 return d->vector[idx];
900 /* Returns the number of vectors in D. */
902 dict_get_vector_cnt (const struct dictionary *d)
906 return d->vector_cnt;
909 /* Looks up and returns the vector within D with the given
911 const struct vector *
912 dict_lookup_vector (const struct dictionary *d, const char *name)
917 assert (name != NULL);
919 for (i = 0; i < d->vector_cnt; i++)
920 if (!strcmp (d->vector[i]->name, name))
925 /* Deletes all vectors from D. */
927 dict_clear_vectors (struct dictionary *d)
933 for (i = 0; i < d->vector_cnt; i++)
935 free (d->vector[i]->var);