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