3e16831b14ca430a1999268f94fdca04fe1fac44
[pspp-builds.git] / src / data / dictionary.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "dictionary.h"
23
24 #include <stdlib.h>
25 #include <ctype.h>
26
27 #include "case.h"
28 #include "cat-routines.h"
29 #include "category.h"
30 #include "settings.h"
31 #include "value-labels.h"
32 #include "vardict.h"
33 #include "variable.h"
34 #include "vector.h"
35 #include <libpspp/alloc.h>
36 #include <libpspp/array.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/hash.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/pool.h>
42 #include <libpspp/str.h>
43
44 #include "minmax.h"
45
46 #include "gettext.h"
47 #define _(msgid) gettext (msgid)
48
49 /* A dictionary. */
50 struct dictionary
51   {
52     struct variable **var;      /* Variables. */
53     size_t var_cnt, var_cap;    /* Number of variables, capacity. */
54     struct hsh_table *name_tab; /* Variable index by name. */
55     int next_value_idx;         /* Index of next `union value' to allocate. */
56     struct variable **split;    /* SPLIT FILE vars. */
57     size_t split_cnt;           /* SPLIT FILE count. */
58     struct variable *weight;    /* WEIGHT variable. */
59     struct variable *filter;    /* FILTER variable. */
60     size_t case_limit;          /* Current case limit (N command). */
61     char *label;                /* File label. */
62     char *documents;            /* Documents, as a string. */
63     struct vector **vector;     /* Vectors of variables. */
64     size_t vector_cnt;          /* Number of vectors. */
65   };
66
67 /* Creates and returns a new dictionary. */
68 struct dictionary *
69 dict_create (void) 
70 {
71   struct dictionary *d = xmalloc (sizeof *d);
72   
73   d->var = NULL;
74   d->var_cnt = d->var_cap = 0;
75   d->name_tab = hsh_create (8, compare_vars_by_name, hash_var_by_name,
76                             NULL, NULL);
77   d->next_value_idx = 0;
78   d->split = NULL;
79   d->split_cnt = 0;
80   d->weight = NULL;
81   d->filter = NULL;
82   d->case_limit = 0;
83   d->label = NULL;
84   d->documents = NULL;
85   d->vector = NULL;
86   d->vector_cnt = 0;
87
88   return d;
89 }
90
91 /* Creates and returns a (deep) copy of an existing
92    dictionary. */
93 struct dictionary *
94 dict_clone (const struct dictionary *s) 
95 {
96   struct dictionary *d;
97   size_t i;
98
99   assert (s != NULL);
100
101   d = dict_create ();
102
103   for (i = 0; i < s->var_cnt; i++) 
104     {
105       struct variable *sv = s->var[i];
106       struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv));
107       var_set_short_name (dv, var_get_short_name (sv));
108     }
109
110   d->next_value_idx = s->next_value_idx;
111
112   d->split_cnt = s->split_cnt;
113   if (d->split_cnt > 0) 
114     {
115       d->split = xnmalloc (d->split_cnt, sizeof *d->split);
116       for (i = 0; i < d->split_cnt; i++) 
117         d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
118     }
119
120   if (s->weight != NULL) 
121     d->weight = dict_lookup_var_assert (d, var_get_name (s->weight));
122
123   if (s->filter != NULL) 
124     d->filter = dict_lookup_var_assert (d, var_get_name (s->filter));
125
126   d->case_limit = s->case_limit;
127   dict_set_label (d, dict_get_label (s));
128   dict_set_documents (d, dict_get_documents (s));
129
130   d->vector_cnt = s->vector_cnt;
131   d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
132   for (i = 0; i < s->vector_cnt; i++)
133     d->vector[i] = vector_clone (s->vector[i], s, d);
134
135   return d;
136 }
137
138 /* Clears the contents from a dictionary without destroying the
139    dictionary itself. */
140 void
141 dict_clear (struct dictionary *d) 
142 {
143   /* FIXME?  Should we really clear case_limit, label, documents?
144      Others are necessarily cleared by deleting all the variables.*/
145   int i;
146
147   assert (d != NULL);
148
149   for (i = 0; i < d->var_cnt; i++)
150     {
151       var_clear_vardict (d->var[i]);
152       var_destroy (d->var[i]); 
153     }
154   free (d->var);
155   d->var = NULL;
156   d->var_cnt = d->var_cap = 0;
157   hsh_clear (d->name_tab);
158   d->next_value_idx = 0;
159   free (d->split);
160   d->split = NULL;
161   d->split_cnt = 0;
162   d->weight = NULL;
163   d->filter = NULL;
164   d->case_limit = 0;
165   free (d->label);
166   d->label = NULL;
167   free (d->documents);
168   d->documents = NULL;
169   dict_clear_vectors (d);
170 }
171
172 /* Destroys the aux data for every variable in D, by calling
173    var_clear_aux() for each variable. */
174 void
175 dict_clear_aux (struct dictionary *d) 
176 {
177   int i;
178   
179   assert (d != NULL);
180   
181   for (i = 0; i < d->var_cnt; i++)
182     var_clear_aux (d->var[i]);
183 }
184
185 /* Clears a dictionary and destroys it. */
186 void
187 dict_destroy (struct dictionary *d)
188 {
189   if (d != NULL) 
190     {
191       dict_clear (d);
192       hsh_destroy (d->name_tab);
193       free (d);
194     }
195 }
196
197 /* Returns the number of variables in D. */
198 size_t
199 dict_get_var_cnt (const struct dictionary *d) 
200 {
201   assert (d != NULL);
202
203   return d->var_cnt;
204 }
205
206 /* Returns the variable in D with dictionary index IDX, which
207    must be between 0 and the count returned by
208    dict_get_var_cnt(), exclusive. */
209 struct variable *
210 dict_get_var (const struct dictionary *d, size_t idx) 
211 {
212   assert (d != NULL);
213   assert (idx < d->var_cnt);
214
215   return d->var[idx];
216 }
217
218 /* Sets *VARS to an array of pointers to variables in D and *CNT
219    to the number of variables in *D.  All variables are returned
220    if EXCLUDE_CLASSES is 0, or it may contain one or more of (1u
221    << DC_ORDINARY), (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to
222    exclude the corresponding type of variable. */
223 void
224 dict_get_vars (const struct dictionary *d, struct variable ***vars,
225                size_t *cnt, unsigned exclude_classes)
226 {
227   size_t count;
228   size_t i;
229   
230   assert (d != NULL);
231   assert (vars != NULL);
232   assert (cnt != NULL);
233   assert ((exclude_classes & ~((1u << DC_ORDINARY)
234                                | (1u << DC_SYSTEM)
235                                | (1u << DC_SCRATCH))) == 0);
236   
237   count = 0;
238   for (i = 0; i < d->var_cnt; i++)
239     {
240       enum dict_class class = dict_class_from_id (var_get_name (d->var[i]));
241       if (!(exclude_classes & (1u << class)))
242         count++;
243     }
244   
245   *vars = xnmalloc (count, sizeof **vars);
246   *cnt = 0;
247   for (i = 0; i < d->var_cnt; i++) 
248     {
249       enum dict_class class = dict_class_from_id (var_get_name (d->var[i]));
250       if (!(exclude_classes & (1u << class)))
251         (*vars)[(*cnt)++] = d->var[i]; 
252     }
253   assert (*cnt == count);
254 }
255
256 static struct variable *
257 add_var (struct dictionary *d, struct variable *v) 
258 {
259   /* Add dictionary info to variable. */
260   struct vardict_info vdi;
261   vdi.case_index = d->next_value_idx;
262   vdi.dict_index = d->var_cnt;
263   var_set_vardict (v, &vdi);
264
265   /* Update dictionary. */
266   if (d->var_cnt >= d->var_cap) 
267     {
268       d->var_cap = 8 + 2 * d->var_cap; 
269       d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var);
270     }
271   d->var[d->var_cnt++] = v;
272   hsh_force_insert (d->name_tab, v);
273
274   d->next_value_idx += var_get_value_cnt (v);
275
276   return v;
277 }
278
279 /* Creates and returns a new variable in D with the given NAME
280    and WIDTH.  Returns a null pointer if the given NAME would
281    duplicate that of an existing variable in the dictionary. */
282 struct variable *
283 dict_create_var (struct dictionary *d, const char *name, int width)
284 {
285   return (dict_lookup_var (d, name) == NULL
286           ? dict_create_var_assert (d, name, width)
287           : NULL);
288 }
289
290 /* Creates and returns a new variable in D with the given NAME
291    and WIDTH.  Assert-fails if the given NAME would duplicate
292    that of an existing variable in the dictionary. */
293 struct variable *
294 dict_create_var_assert (struct dictionary *d, const char *name, int width)
295 {
296   assert (dict_lookup_var (d, name) == NULL);
297   return add_var (d, var_create (name, width));
298 }
299
300 /* Creates and returns a new variable in D with name NAME, as a
301    copy of existing variable OLD_VAR, which need not be in D or
302    in any dictionary.  Returns a null pointer if the given NAME
303    would duplicate that of an existing variable in the
304    dictionary. */
305 struct variable *
306 dict_clone_var (struct dictionary *d, const struct variable *old_var,
307                 const char *name)
308 {
309   return (dict_lookup_var (d, name) == NULL
310           ? dict_clone_var_assert (d, old_var, name)
311           : NULL);
312 }
313
314 /* Creates and returns a new variable in D with name NAME, as a
315    copy of existing variable OLD_VAR, which need not be in D or
316    in any dictionary.  Assert-fails if the given NAME would
317    duplicate that of an existing variable in the dictionary. */
318 struct variable *
319 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var,
320                        const char *name)
321 {
322   struct variable *new_var = var_clone (old_var);
323   assert (dict_lookup_var (d, name) == NULL);
324   var_set_name (new_var, name);
325   return add_var (d, new_var);
326 }
327
328 /* Returns the variable named NAME in D, or a null pointer if no
329    variable has that name. */
330 struct variable *
331 dict_lookup_var (const struct dictionary *d, const char *name)
332 {
333   struct variable *target = var_create (name, 0);
334   struct variable *result = hsh_find (d->name_tab, target);
335   var_destroy (target);
336   return result;
337 }
338
339 /* Returns the variable named NAME in D.  Assert-fails if no
340    variable has that name. */
341 struct variable *
342 dict_lookup_var_assert (const struct dictionary *d, const char *name)
343 {
344   struct variable *v = dict_lookup_var (d, name);
345   assert (v != NULL);
346   return v;
347 }
348
349 /* Returns true if variable V is in dictionary D,
350    false otherwise. */
351 bool
352 dict_contains_var (const struct dictionary *d, const struct variable *v)
353 {
354   if (var_has_vardict (v)) 
355     {
356       const struct vardict_info *vdi = var_get_vardict (v);
357       return (vdi->dict_index >= 0
358               && vdi->dict_index < d->var_cnt
359               && d->var[vdi->dict_index] == v); 
360     }
361   else
362     return false;
363 }
364
365 /* Compares two double pointers to variables, which should point
366    to elements of a struct dictionary's `var' member array. */
367 static int
368 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED) 
369 {
370   struct variable *const *a = a_;
371   struct variable *const *b = b_;
372
373   return *a < *b ? -1 : *a > *b;
374 }
375
376 /* Sets the dict_index in V's vardict to DICT_INDEX. */
377 static void
378 set_var_dict_index (struct variable *v, int dict_index)
379 {
380   struct vardict_info vdi = *var_get_vardict (v);
381   vdi.dict_index = dict_index;
382   var_set_vardict (v, &vdi); 
383 }
384
385 /* Sets the case_index in V's vardict to DICT_INDEX. */
386 static void
387 set_var_case_index (struct variable *v, int case_index)
388 {
389   struct vardict_info vdi = *var_get_vardict (v);
390   vdi.case_index = case_index;
391   var_set_vardict (v, &vdi); 
392 }
393
394 /* Re-sets the dict_index in the dictionary variables with
395    indexes from FROM to TO (exclusive). */
396 static void
397 reindex_vars (struct dictionary *d, size_t from, size_t to) 
398 {
399   size_t i;
400   
401   for (i = from; i < to; i++)
402     set_var_dict_index (d->var[i], i);
403 }
404
405 /* Deletes variable V from dictionary D and frees V.
406
407    This is a very bad idea if there might be any pointers to V
408    from outside D.  In general, no variable in should be deleted when 
409    any transformations are active on the dictionary's dataset, because
410    those transformations might reference the deleted variable.
411    The safest time to delete a variable is just after a procedure
412    has been executed, as done by MODIFY VARS.
413
414    Pointers to V within D are not a problem, because
415    dict_delete_var() knows to remove V from split variables,
416    weights, filters, etc. */
417 void
418 dict_delete_var (struct dictionary *d, struct variable *v) 
419 {
420   int dict_index = var_get_dict_index (v);
421
422   assert (dict_contains_var (d, v));
423
424   /* Delete aux data. */
425   var_clear_aux (v);
426
427   /* Remove V from splits, weight, filter variables. */
428   d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
429                                &v, compare_var_ptrs, NULL);
430   if (d->weight == v)
431     d->weight = NULL;
432   if (d->filter == v)
433     d->filter = NULL;
434   dict_clear_vectors (d);
435
436   /* Remove V from var array. */
437   remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
438   d->var_cnt--;
439
440   /* Update dict_index for each affected variable. */
441   reindex_vars (d, dict_index, d->var_cnt);
442
443   /* Update name hash. */
444   hsh_force_delete (d->name_tab, v);
445
446   /* Free memory. */
447   var_clear_vardict (v);
448   var_destroy (v);
449 }
450
451 /* Deletes the COUNT variables listed in VARS from D.  This is
452    unsafe; see the comment on dict_delete_var() for details. */
453 void 
454 dict_delete_vars (struct dictionary *d,
455                   struct variable *const *vars, size_t count) 
456 {
457   /* FIXME: this can be done in O(count) time, but this algorithm
458      is O(count**2). */
459   assert (d != NULL);
460   assert (count == 0 || vars != NULL);
461
462   while (count-- > 0)
463     dict_delete_var (d, *vars++);
464 }
465
466 /* Deletes the COUNT variables in D starting at index IDX.  This
467    is unsafe; see the comment on dict_delete_var() for
468    details. */
469 void
470 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count) 
471 {
472   /* FIXME: this can be done in O(count) time, but this algorithm
473      is O(count**2). */
474   assert (idx + count <= d->var_cnt);
475   
476   while (count-- > 0)
477     dict_delete_var (d, d->var[idx]);
478 }
479
480 /* Deletes scratch variables from dictionary D. */
481 void
482 dict_delete_scratch_vars (struct dictionary *d)
483 {
484   int i;
485
486   /* FIXME: this can be done in O(count) time, but this algorithm
487      is O(count**2). */
488   assert (d != NULL);
489
490   for (i = 0; i < d->var_cnt; )
491     if (dict_class_from_id (var_get_name (d->var[i])) == DC_SCRATCH)
492       dict_delete_var (d, d->var[i]);
493     else
494       i++;
495 }
496
497 /* Moves V to 0-based position IDX in D.  Other variables in D,
498    if any, retain their relative positions.  Runs in time linear
499    in the distance moved. */
500 void
501 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index) 
502 {
503   size_t old_index = var_get_dict_index (v);
504
505   assert (new_index < d->var_cnt);
506   move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
507   reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
508 }
509
510 /* Reorders the variables in D, placing the COUNT variables
511    listed in ORDER in that order at the beginning of D.  The
512    other variables in D, if any, retain their relative
513    positions. */
514 void 
515 dict_reorder_vars (struct dictionary *d,
516                    struct variable *const *order, size_t count) 
517 {
518   struct variable **new_var;
519   size_t i;
520   
521   assert (d != NULL);
522   assert (count == 0 || order != NULL);
523   assert (count <= d->var_cnt);
524
525   new_var = xnmalloc (d->var_cnt, sizeof *new_var);
526   memcpy (new_var, order, count * sizeof *new_var);
527   for (i = 0; i < count; i++) 
528     {
529       size_t index = var_get_dict_index (order[i]);
530       assert (d->var[index] == order[i]);
531       d->var[index] = NULL;
532       set_var_dict_index (order[i], i);
533     }
534   for (i = 0; i < d->var_cnt; i++)
535     if (d->var[i] != NULL)
536       {
537         assert (count < d->var_cnt);
538         new_var[count] = d->var[i];
539         set_var_dict_index (new_var[count], count);
540         count++;
541       }
542   free (d->var);
543   d->var = new_var;
544 }
545
546 /* Changes the name of variable V in dictionary D to NEW_NAME. */
547 static void
548 rename_var (struct dictionary *d, struct variable *v, const char *new_name) 
549 {
550   struct vardict_info vdi;
551
552   assert (dict_contains_var (d, v));
553
554   vdi = *var_get_vardict (v);
555   var_clear_vardict (v);
556   var_set_name (v, new_name);
557   var_set_vardict (v, &vdi);
558 }
559
560 /* Changes the name of V in D to name NEW_NAME.  Assert-fails if
561    a variable named NEW_NAME is already in D, except that
562    NEW_NAME may be the same as V's existing name. */
563 void 
564 dict_rename_var (struct dictionary *d, struct variable *v,
565                  const char *new_name) 
566 {
567   assert (!strcasecmp (var_get_name (v), new_name)
568           || dict_lookup_var (d, new_name) == NULL);
569
570   hsh_force_delete (d->name_tab, v);
571   rename_var (d, v, new_name);
572   hsh_force_insert (d->name_tab, v);
573
574   if (get_algorithm () == ENHANCED)
575     var_clear_short_name (v);
576 }
577
578 /* Renames COUNT variables specified in VARS to the names given
579    in NEW_NAMES within dictionary D.  If the renaming would
580    result in a duplicate variable name, returns false and stores a
581    name that would be duplicated into *ERR_NAME (if ERR_NAME is
582    non-null).  Otherwise, the renaming is successful, and true
583    is returned. */
584 bool
585 dict_rename_vars (struct dictionary *d,
586                   struct variable **vars, char **new_names, size_t count,
587                   char **err_name) 
588 {
589   struct pool *pool;
590   char **old_names;
591   size_t i;
592
593   assert (count == 0 || vars != NULL);
594   assert (count == 0 || new_names != NULL);
595
596   /* Save the names of the variables to be renamed. */
597   pool = pool_create ();
598   old_names = pool_nalloc (pool, count, sizeof *old_names);
599   for (i = 0; i < count; i++) 
600     old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
601   
602   /* Remove the variables to be renamed from the name hash,
603      and rename them. */
604   for (i = 0; i < count; i++) 
605     {
606       hsh_force_delete (d->name_tab, vars[i]);
607       rename_var (d, vars[i], new_names[i]);
608     }
609
610   /* Add the renamed variables back into the name hash,
611      checking for conflicts. */
612   for (i = 0; i < count; i++)
613     if (hsh_insert (d->name_tab, vars[i]) != NULL)
614       {
615         /* There is a name conflict.
616            Back out all the name changes that have already
617            taken place, and indicate failure. */
618         size_t fail_idx = i;
619         if (err_name != NULL) 
620           *err_name = new_names[i];
621
622         for (i = 0; i < fail_idx; i++)
623           hsh_force_delete (d->name_tab, vars[i]);
624           
625         for (i = 0; i < count; i++)
626           {
627             rename_var (d, vars[i], old_names[i]);
628             hsh_force_insert (d->name_tab, vars[i]);
629           }
630
631         pool_destroy (pool);
632         return false;
633       }
634
635   /* Clear short names. */
636   if (get_algorithm () == ENHANCED)
637     for (i = 0; i < count; i++)
638       var_clear_short_name (vars[i]);
639
640   pool_destroy (pool);
641   return true;
642 }
643
644 /* Returns the weighting variable in dictionary D, or a null
645    pointer if the dictionary is unweighted. */
646 struct variable *
647 dict_get_weight (const struct dictionary *d) 
648 {
649   assert (d != NULL);
650   assert (d->weight == NULL || dict_contains_var (d, d->weight));
651   
652   return d->weight;
653 }
654
655 /* Returns the value of D's weighting variable in case C, except that a
656    negative weight is returned as 0.  Returns 1 if the dictionary is
657    unweighted. Will warn about missing, negative, or zero values if
658    warn_on_invalid is true. The function will set warn_on_invalid to false
659    if an invalid weight is found. */
660 double
661 dict_get_case_weight (const struct dictionary *d, const struct ccase *c, 
662                       bool *warn_on_invalid)
663 {
664   assert (d != NULL);
665   assert (c != NULL);
666
667   if (d->weight == NULL)
668     return 1.0;
669   else 
670     {
671       double w = case_num (c, d->weight);
672       if (w < 0.0 || var_is_num_missing (d->weight, w))
673         w = 0.0;
674       if ( w == 0.0 && *warn_on_invalid ) {
675           *warn_on_invalid = false;
676           msg (SW, _("At least one case in the data file had a weight value "
677                      "that was user-missing, system-missing, zero, or "
678                      "negative.  These case(s) were ignored."));
679       }
680       return w;
681     }
682 }
683
684 /* Sets the weighting variable of D to V, or turning off
685    weighting if V is a null pointer. */
686 void
687 dict_set_weight (struct dictionary *d, struct variable *v) 
688 {
689   assert (d != NULL);
690   assert (v == NULL || dict_contains_var (d, v));
691   assert (v == NULL || var_is_numeric (v));
692
693   d->weight = v;
694 }
695
696 /* Returns the filter variable in dictionary D (see cmd_filter())
697    or a null pointer if the dictionary is unfiltered. */
698 struct variable *
699 dict_get_filter (const struct dictionary *d) 
700 {
701   assert (d != NULL);
702   assert (d->filter == NULL || dict_contains_var (d, d->filter));
703   
704   return d->filter;
705 }
706
707 /* Sets V as the filter variable for dictionary D.  Passing a
708    null pointer for V turn off filtering. */
709 void
710 dict_set_filter (struct dictionary *d, struct variable *v)
711 {
712   assert (d != NULL);
713   assert (v == NULL || dict_contains_var (d, v));
714
715   d->filter = v;
716 }
717
718 /* Returns the case limit for dictionary D, or zero if the number
719    of cases is unlimited. */
720 size_t
721 dict_get_case_limit (const struct dictionary *d) 
722 {
723   assert (d != NULL);
724
725   return d->case_limit;
726 }
727
728 /* Sets CASE_LIMIT as the case limit for dictionary D.  Use
729    0 for CASE_LIMIT to indicate no limit. */
730 void
731 dict_set_case_limit (struct dictionary *d, size_t case_limit) 
732 {
733   assert (d != NULL);
734
735   d->case_limit = case_limit;
736 }
737
738 /* Returns the case index of the next value to be added to D.
739    This value is the number of `union value's that need to be
740    allocated to store a case for dictionary D. */
741 int
742 dict_get_next_value_idx (const struct dictionary *d) 
743 {
744   assert (d != NULL);
745
746   return d->next_value_idx;
747 }
748
749 /* Returns the number of bytes needed to store a case for
750    dictionary D. */
751 size_t
752 dict_get_case_size (const struct dictionary *d) 
753 {
754   assert (d != NULL);
755
756   return sizeof (union value) * dict_get_next_value_idx (d);
757 }
758
759 /* Deletes scratch variables in dictionary D and reassigns values
760    so that fragmentation is eliminated. */
761 void
762 dict_compact_values (struct dictionary *d) 
763 {
764   size_t i;
765
766   d->next_value_idx = 0;
767   for (i = 0; i < d->var_cnt; )
768     {
769       struct variable *v = d->var[i];
770
771       if (dict_class_from_id (var_get_name (v)) != DC_SCRATCH) 
772         {
773           set_var_case_index (v, d->next_value_idx);
774           d->next_value_idx += var_get_value_cnt (v);
775           i++;
776         }
777       else
778         dict_delete_var (d, v);
779     }
780 }
781
782 /* Returns the number of values that would be used by a case if
783    dict_compact_values() were called. */
784 size_t
785 dict_get_compacted_value_cnt (const struct dictionary *d) 
786 {
787   size_t i;
788   size_t cnt;
789
790   cnt = 0;
791   for (i = 0; i < d->var_cnt; i++)
792     if (dict_class_from_id (var_get_name (d->var[i])) != DC_SCRATCH) 
793       cnt += var_get_value_cnt (d->var[i]);
794   return cnt;
795 }
796
797 /* Creates and returns an array mapping from a dictionary index
798    to the case index that the corresponding variable will have
799    after calling dict_compact_values().  Scratch variables
800    receive -1 for case index because dict_compact_values() will
801    delete them. */
802 int *
803 dict_get_compacted_dict_index_to_case_index (const struct dictionary *d) 
804 {
805   size_t i;
806   size_t next_value_idx;
807   int *map;
808   
809   map = xnmalloc (d->var_cnt, sizeof *map);
810   next_value_idx = 0;
811   for (i = 0; i < d->var_cnt; i++)
812     {
813       struct variable *v = d->var[i];
814
815       if (dict_class_from_id (var_get_name (v)) != DC_SCRATCH) 
816         {
817           map[i] = next_value_idx;
818           next_value_idx += var_get_value_cnt (v);
819         }
820       else 
821         map[i] = -1;
822     }
823   return map;
824 }
825
826 /* Returns true if a case for dictionary D would be smaller after
827    compacting, false otherwise.  Compacting a case eliminates
828    "holes" between values and after the last value.  Holes are
829    created by deleting variables (or by scratch variables).
830
831    The return value may differ from whether compacting a case
832    from dictionary D would *change* the case: compacting could
833    rearrange values even if it didn't reduce space
834    requirements. */
835 bool
836 dict_compacting_would_shrink (const struct dictionary *d) 
837 {
838   return dict_get_compacted_value_cnt (d) < dict_get_next_value_idx (d);
839 }
840
841 /* Returns true if a case for dictionary D would change after
842    compacting, false otherwise.  Compacting a case eliminates
843    "holes" between values and after the last value.  Holes are
844    created by deleting variables (or by scratch variables).
845
846    The return value may differ from whether compacting a case
847    from dictionary D would *shrink* the case: compacting could
848    rearrange values without reducing space requirements. */
849 bool
850 dict_compacting_would_change (const struct dictionary *d) 
851 {
852   size_t case_idx;
853   size_t i;
854
855   case_idx = 0;
856   for (i = 0; i < dict_get_var_cnt (d); i++) 
857     {
858       struct variable *v = dict_get_var (d, i);
859       if (var_get_case_index (v) != case_idx)
860         return true;
861       case_idx += var_get_value_cnt (v);
862     }
863   return false;
864 }
865 \f
866 /* How to copy a contiguous range of values between cases. */
867 struct copy_map
868   {
869     size_t src_idx;             /* Starting value index in source case. */
870     size_t dst_idx;             /* Starting value index in target case. */
871     size_t cnt;                 /* Number of values. */
872   };
873
874 /* How to compact a case. */
875 struct dict_compactor 
876   {
877     struct copy_map *maps;      /* Array of mappings. */
878     size_t map_cnt;             /* Number of mappings. */
879   };
880
881 /* Creates and returns a dict_compactor that can be used to
882    compact cases for dictionary D.
883
884    Compacting a case eliminates "holes" between values and after
885    the last value.  Holes are created by deleting variables (or
886    by scratch variables). */
887 struct dict_compactor *
888 dict_make_compactor (const struct dictionary *d)
889 {
890   struct dict_compactor *compactor;
891   struct copy_map *map;
892   size_t map_allocated;
893   size_t value_idx;
894   size_t i;
895
896   compactor = xmalloc (sizeof *compactor);
897   compactor->maps = NULL;
898   compactor->map_cnt = 0;
899   map_allocated = 0;
900
901   value_idx = 0;
902   map = NULL;
903   for (i = 0; i < d->var_cnt; i++) 
904     {
905       struct variable *v = d->var[i];
906
907       if (dict_class_from_id (var_get_name (v)) == DC_SCRATCH)
908         continue;
909       if (map != NULL && map->src_idx + map->cnt == var_get_case_index (v)) 
910         map->cnt += var_get_value_cnt (v);
911       else 
912         {
913           if (compactor->map_cnt == map_allocated)
914             compactor->maps = x2nrealloc (compactor->maps, &map_allocated,
915                                           sizeof *compactor->maps);
916           map = &compactor->maps[compactor->map_cnt++];
917           map->src_idx = var_get_case_index (v);
918           map->dst_idx = value_idx;
919           map->cnt = var_get_value_cnt (v);
920         }
921       value_idx += var_get_value_cnt (v);
922     }
923
924   return compactor;
925 }
926
927 /* Compacts SRC by copying it to DST according to the scheme in
928    COMPACTOR.
929
930    Compacting a case eliminates "holes" between values and after
931    the last value.  Holes are created by deleting variables (or
932    by scratch variables). */
933 void
934 dict_compactor_compact (const struct dict_compactor *compactor,
935                         struct ccase *dst, const struct ccase *src) 
936 {
937   size_t i;
938
939   for (i = 0; i < compactor->map_cnt; i++) 
940     {
941       const struct copy_map *map = &compactor->maps[i];
942       case_copy (dst, map->dst_idx, src, map->src_idx, map->cnt);
943     }
944 }
945
946 /* Destroys COMPACTOR. */
947 void
948 dict_compactor_destroy (struct dict_compactor *compactor) 
949 {
950   if (compactor != NULL) 
951     {
952       free (compactor->maps);
953       free (compactor);
954     }
955 }
956
957 /* Returns the SPLIT FILE vars (see cmd_split_file()).  Call
958    dict_get_split_cnt() to determine how many SPLIT FILE vars
959    there are.  Returns a null pointer if and only if there are no
960    SPLIT FILE vars. */
961 struct variable *const *
962 dict_get_split_vars (const struct dictionary *d) 
963 {
964   assert (d != NULL);
965   
966   return d->split;
967 }
968
969 /* Returns the number of SPLIT FILE vars. */
970 size_t
971 dict_get_split_cnt (const struct dictionary *d) 
972 {
973   assert (d != NULL);
974
975   return d->split_cnt;
976 }
977
978 /* Sets CNT split vars SPLIT in dictionary D. */
979 void
980 dict_set_split_vars (struct dictionary *d,
981                      struct variable *const *split, size_t cnt)
982 {
983   assert (d != NULL);
984   assert (cnt == 0 || split != NULL);
985
986   d->split_cnt = cnt;
987   d->split = xnrealloc (d->split, cnt, sizeof *d->split);
988   memcpy (d->split, split, cnt * sizeof *d->split);
989 }
990
991 /* Returns the file label for D, or a null pointer if D is
992    unlabeled (see cmd_file_label()). */
993 const char *
994 dict_get_label (const struct dictionary *d) 
995 {
996   assert (d != NULL);
997
998   return d->label;
999 }
1000
1001 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1002    characters. */
1003 void
1004 dict_set_label (struct dictionary *d, const char *label) 
1005 {
1006   assert (d != NULL);
1007
1008   free (d->label);
1009   if (label == NULL)
1010     d->label = NULL;
1011   else if (strlen (label) < 60)
1012     d->label = xstrdup (label);
1013   else 
1014     {
1015       d->label = xmalloc (61);
1016       memcpy (d->label, label, 60);
1017       d->label[60] = '\0';
1018     }
1019 }
1020
1021 /* Returns the documents for D, or a null pointer if D has no
1022    documents (see cmd_document()).. */
1023 const char *
1024 dict_get_documents (const struct dictionary *d) 
1025 {
1026   assert (d != NULL);
1027
1028   return d->documents;
1029 }
1030
1031 /* Sets the documents for D to DOCUMENTS, or removes D's
1032    documents if DOCUMENT is a null pointer. */
1033 void
1034 dict_set_documents (struct dictionary *d, const char *documents)
1035 {
1036   assert (d != NULL);
1037
1038   free (d->documents);
1039   if (documents == NULL)
1040     d->documents = NULL;
1041   else
1042     d->documents = xstrdup (documents);
1043 }
1044
1045 /* Creates in D a vector named NAME that contains the CNT
1046    variables in VAR.  Returns true if successful, or false if a
1047    vector named NAME already exists in D. */
1048 bool
1049 dict_create_vector (struct dictionary *d,
1050                     const char *name,
1051                     struct variable **var, size_t cnt) 
1052 {
1053   size_t i;
1054
1055   assert (var != NULL);
1056   assert (cnt > 0);
1057   for (i = 0; i < cnt; i++)
1058     assert (dict_contains_var (d, var[i]));
1059   
1060   if (dict_lookup_vector (d, name) == NULL)
1061     {
1062       d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1063       d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1064       return true; 
1065     }
1066   else
1067     return false;
1068 }
1069
1070 /* Returns the vector in D with index IDX, which must be less
1071    than dict_get_vector_cnt (D). */
1072 const struct vector *
1073 dict_get_vector (const struct dictionary *d, size_t idx) 
1074 {
1075   assert (d != NULL);
1076   assert (idx < d->vector_cnt);
1077
1078   return d->vector[idx];
1079 }
1080
1081 /* Returns the number of vectors in D. */
1082 size_t
1083 dict_get_vector_cnt (const struct dictionary *d) 
1084 {
1085   assert (d != NULL);
1086
1087   return d->vector_cnt;
1088 }
1089
1090 /* Looks up and returns the vector within D with the given
1091    NAME. */
1092 const struct vector *
1093 dict_lookup_vector (const struct dictionary *d, const char *name) 
1094 {
1095   size_t i;
1096   for (i = 0; i < d->vector_cnt; i++)
1097     if (!strcasecmp (vector_get_name (d->vector[i]), name))
1098       return d->vector[i];
1099   return NULL;
1100 }
1101
1102 /* Deletes all vectors from D. */
1103 void
1104 dict_clear_vectors (struct dictionary *d) 
1105 {
1106   size_t i;
1107   
1108   for (i = 0; i < d->vector_cnt; i++)
1109     vector_destroy (d->vector[i]);
1110   free (d->vector);
1111
1112   d->vector = NULL;
1113   d->vector_cnt = 0;
1114 }
1115
1116 /* Compares two strings. */
1117 static int
1118 compare_strings (const void *a, const void *b, const void *aux UNUSED) 
1119 {
1120   return strcmp (a, b);
1121 }
1122
1123 /* Hashes a string. */
1124 static unsigned
1125 hash_string (const void *s, const void *aux UNUSED) 
1126 {
1127   return hsh_hash_string (s);
1128 }
1129
1130
1131 /* Sets V's short name to BASE, followed by a suffix of the form
1132    _A, _B, _C, ..., _AA, _AB, etc. according to the value of
1133    SUFFIX_NUMBER.  Truncates BASE as necessary to fit. */
1134 static void
1135 set_var_short_name_suffix (struct variable *v, const char *base,
1136                            int suffix_number)
1137 {
1138   char suffix[SHORT_NAME_LEN + 1];
1139   char short_name[SHORT_NAME_LEN + 1];
1140   char *start, *end;
1141   int len, ofs;
1142
1143   assert (v != NULL);
1144   assert (suffix_number >= 0);
1145
1146   /* Set base name. */
1147   var_set_short_name (v, base);
1148
1149   /* Compose suffix. */
1150   start = end = suffix + sizeof suffix - 1;
1151   *end = '\0';
1152   do 
1153     {
1154       *--start = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[suffix_number % 26];
1155       if (start <= suffix + 1)
1156         msg (SE, _("Variable suffix too large."));
1157       suffix_number /= 26;
1158     }
1159   while (suffix_number > 0);
1160   *--start = '_';
1161
1162   /* Append suffix to V's short name. */
1163   str_copy_trunc (short_name, sizeof short_name, base);
1164   len = end - start;
1165   if (len + strlen (short_name) > SHORT_NAME_LEN)
1166     ofs = SHORT_NAME_LEN - len;
1167   else
1168     ofs = strlen (short_name);
1169   strcpy (short_name + ofs, start);
1170
1171   /* Set name. */
1172   var_set_short_name (v, short_name);
1173 }
1174
1175 /* Assigns a valid, unique short_name[] to each variable in D.
1176    Each variable whose actual name is short has highest priority
1177    for that short name.  Otherwise, variables with an existing
1178    short_name[] have the next highest priority for a given short
1179    name; if it is already taken, then the variable is treated as
1180    if short_name[] had been empty.  Otherwise, long names are
1181    truncated to form short names.  If that causes conflicts,
1182    variables are renamed as PREFIX_A, PREFIX_B, and so on. */
1183 void
1184 dict_assign_short_names (struct dictionary *d) 
1185 {
1186   struct hsh_table *short_names;
1187   size_t i;
1188
1189   /* Give variables whose names are short the corresponding short
1190      names, and clear short_names[] that conflict with a variable
1191      name. */
1192   for (i = 0; i < d->var_cnt; i++)
1193     {
1194       struct variable *v = d->var[i];
1195       const char *short_name = var_get_short_name (v);
1196       if (strlen (var_get_name (v)) <= SHORT_NAME_LEN)
1197         var_set_short_name (v, var_get_name (v));
1198       else if (short_name != NULL && dict_lookup_var (d, short_name) != NULL)
1199         var_clear_short_name (v);
1200     }
1201
1202   /* Each variable with an assigned short_name[] now gets it
1203      unless there is a conflict. */
1204   short_names = hsh_create (d->var_cnt, compare_strings, hash_string,
1205                             NULL, NULL);
1206   for (i = 0; i < d->var_cnt; i++)
1207     {
1208       struct variable *v = d->var[i];
1209       const char *name = var_get_short_name (v);
1210       if (name != NULL && hsh_insert (short_names, (char *) name) != NULL)
1211         var_clear_short_name (v);
1212     }
1213   
1214   /* Now assign short names to remaining variables. */
1215   for (i = 0; i < d->var_cnt; i++)
1216     {
1217       struct variable *v = d->var[i];
1218       const char *name = var_get_short_name (v);
1219       if (name == NULL) 
1220         {
1221           /* Form initial short_name from the variable name, then
1222              try _A, _B, ... _AA, _AB, etc., if needed.*/
1223           int trial = 0;
1224           do
1225             {
1226               if (trial == 0)
1227                 var_set_short_name (v, var_get_name (v));
1228               else
1229                 set_var_short_name_suffix (v, var_get_name (v), trial - 1);
1230
1231               trial++;
1232             }
1233           while (hsh_insert (short_names, (char *) var_get_short_name (v))
1234                  != NULL);
1235         } 
1236     }
1237
1238   /* Get rid of hash table. */
1239   hsh_destroy (short_names);
1240 }