0b930d579c3defc7ec19dc0b81de6bb10a07b3d9
[pspp] / src / data / dictionary.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "data/dictionary.h"
20
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <unistr.h>
25
26 #include "data/attributes.h"
27 #include "data/case.h"
28 #include "data/identifier.h"
29 #include "data/mrset.h"
30 #include "data/settings.h"
31 #include "data/value-labels.h"
32 #include "data/vardict.h"
33 #include "data/variable.h"
34 #include "data/vector.h"
35 #include "libpspp/array.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/compiler.h"
38 #include "libpspp/hash-functions.h"
39 #include "libpspp/hmap.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/message.h"
42 #include "libpspp/misc.h"
43 #include "libpspp/pool.h"
44 #include "libpspp/str.h"
45 #include "libpspp/string-array.h"
46
47 #include "gl/intprops.h"
48 #include "gl/minmax.h"
49 #include "gl/xalloc.h"
50 #include "gl/xmemdup0.h"
51
52 #include "gettext.h"
53 #define _(msgid) gettext (msgid)
54
55 /* A dictionary. */
56 struct dictionary
57   {
58     struct vardict_info *var;   /* Variables. */
59     size_t var_cnt, var_cap;    /* Number of variables, capacity. */
60     struct caseproto *proto;    /* Prototype for dictionary cases
61                                    (updated lazily). */
62     struct hmap name_map;       /* Variable index by name. */
63     int next_value_idx;         /* Index of next `union value' to allocate. */
64     const struct variable **split;    /* SPLIT FILE vars. */
65     size_t split_cnt;           /* SPLIT FILE count. */
66     struct variable *weight;    /* WEIGHT variable. */
67     struct variable *filter;    /* FILTER variable. */
68     casenumber case_limit;      /* Current case limit (N command). */
69     char *label;                /* File label. */
70     struct string_array documents; /* Documents. */
71     struct vector **vector;     /* Vectors of variables. */
72     size_t vector_cnt;          /* Number of vectors. */
73     struct attrset attributes;  /* Custom attributes. */
74     struct mrset **mrsets;      /* Multiple response sets. */
75     size_t n_mrsets;            /* Number of multiple response sets. */
76
77     char *encoding;             /* Character encoding of string data */
78
79     const struct dict_callbacks *callbacks; /* Callbacks on dictionary
80                                                modification */
81     void *cb_data ;                  /* Data passed to callbacks */
82
83     void (*changed) (struct dictionary *, void *); /* Generic change callback */
84     void *changed_data;
85   };
86
87 static void dict_unset_split_var (struct dictionary *, struct variable *);
88 static void dict_unset_mrset_var (struct dictionary *, struct variable *);
89
90 /* Returns the encoding for data in dictionary D.  The return value is a
91    nonnull string that contains an IANA character set name. */
92 const char *
93 dict_get_encoding (const struct dictionary *d)
94 {
95   return d->encoding ;
96 }
97
98 /* Returns true if UTF-8 string ID is an acceptable identifier in DICT's
99    encoding, false otherwise.  If ISSUE_ERROR is true, issues an explanatory
100    error message on failure. */
101 bool
102 dict_id_is_valid (const struct dictionary *dict, const char *id,
103                   bool issue_error)
104 {
105   return id_is_valid (id, dict->encoding, issue_error);
106 }
107
108 void
109 dict_set_change_callback (struct dictionary *d,
110                           void (*changed) (struct dictionary *, void*),
111                           void *data)
112 {
113   d->changed = changed;
114   d->changed_data = data;
115 }
116
117 /* Discards dictionary D's caseproto.  (It will be regenerated
118    lazily, on demand.) */
119 static void
120 invalidate_proto (struct dictionary *d)
121 {
122   caseproto_unref (d->proto);
123   d->proto = NULL;
124 }
125
126 /* Print a representation of dictionary D to stdout, for
127    debugging purposes. */
128 void
129 dict_dump (const struct dictionary *d)
130 {
131   int i;
132   for (i = 0 ; i < d->var_cnt ; ++i )
133     {
134       const struct variable *v = d->var[i].var;
135       printf ("Name: %s;\tdict_idx: %zu; case_idx: %zu\n",
136               var_get_name (v),
137               var_get_dict_index (v),
138               var_get_case_index (v));
139
140     }
141 }
142
143 /* Associate CALLBACKS with DICT.  Callbacks will be invoked whenever
144    the dictionary or any of the variables it contains are modified.
145    Each callback will get passed CALLBACK_DATA.
146    Any callback may be NULL, in which case it'll be ignored.
147 */
148 void
149 dict_set_callbacks (struct dictionary *dict,
150                     const struct dict_callbacks *callbacks,
151                     void *callback_data)
152 {
153   dict->callbacks = callbacks;
154   dict->cb_data = callback_data;
155 }
156
157 /* Shallow copy the callbacks from SRC to DEST */
158 void
159 dict_copy_callbacks (struct dictionary *dest,
160                      const struct dictionary *src)
161 {
162   dest->callbacks = src->callbacks;
163   dest->cb_data = src->cb_data;
164 }
165
166 /* Creates and returns a new dictionary with the specified ENCODING. */
167 struct dictionary *
168 dict_create (const char *encoding)
169 {
170   struct dictionary *d = xzalloc (sizeof *d);
171
172   d->encoding = xstrdup (encoding);
173   hmap_init (&d->name_map);
174   attrset_init (&d->attributes);
175
176   return d;
177 }
178
179 /* Creates and returns a (deep) copy of an existing
180    dictionary.
181
182    The new dictionary's case indexes are copied from the old
183    dictionary.  If the new dictionary won't be used to access
184    cases produced with the old dictionary, then the new
185    dictionary's case indexes should be compacted with
186    dict_compact_values to save space.
187
188    Callbacks are not cloned. */
189 struct dictionary *
190 dict_clone (const struct dictionary *s)
191 {
192   struct dictionary *d;
193   size_t i;
194
195   d = dict_create (s->encoding);
196
197   for (i = 0; i < s->var_cnt; i++)
198     {
199       struct variable *sv = s->var[i].var;
200       struct variable *dv = dict_clone_var_assert (d, sv);
201       size_t i;
202
203       for (i = 0; i < var_get_short_name_cnt (sv); i++)
204         var_set_short_name (dv, i, var_get_short_name (sv, i));
205
206       var_get_vardict (dv)->case_index = var_get_vardict (sv)->case_index;
207     }
208
209   d->next_value_idx = s->next_value_idx;
210
211   d->split_cnt = s->split_cnt;
212   if (d->split_cnt > 0)
213     {
214       d->split = xnmalloc (d->split_cnt, sizeof *d->split);
215       for (i = 0; i < d->split_cnt; i++)
216         d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
217     }
218
219   if (s->weight != NULL)
220     dict_set_weight (d, dict_lookup_var_assert (d, var_get_name (s->weight)));
221
222   if (s->filter != NULL)
223     dict_set_filter (d, dict_lookup_var_assert (d, var_get_name (s->filter)));
224
225   d->case_limit = s->case_limit;
226   dict_set_label (d, dict_get_label (s));
227   dict_set_documents (d, dict_get_documents (s));
228
229   d->vector_cnt = s->vector_cnt;
230   d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
231   for (i = 0; i < s->vector_cnt; i++)
232     d->vector[i] = vector_clone (s->vector[i], s, d);
233
234   dict_set_attributes (d, dict_get_attributes (s));
235
236   for (i = 0; i < s->n_mrsets; i++)
237     {
238       const struct mrset *old = s->mrsets[i];
239       struct mrset *new;
240       size_t j;
241
242       /* Clone old mrset, then replace vars from D by vars from S. */
243       new = mrset_clone (old);
244       for (j = 0; j < new->n_vars; j++)
245         new->vars[j] = dict_lookup_var_assert (d, var_get_name (new->vars[j]));
246
247       dict_add_mrset (d, new);
248     }
249
250   return d;
251 }
252
253 /* Clears the contents from a dictionary without destroying the
254    dictionary itself. */
255 void
256 dict_clear (struct dictionary *d)
257 {
258   /* FIXME?  Should we really clear case_limit, label, documents?
259      Others are necessarily cleared by deleting all the variables.*/
260   while (d->var_cnt > 0 )
261     {
262       dict_delete_var (d, d->var[d->var_cnt - 1].var);
263     }
264
265   free (d->var);
266   d->var = NULL;
267   d->var_cnt = d->var_cap = 0;
268   invalidate_proto (d);
269   hmap_clear (&d->name_map);
270   d->next_value_idx = 0;
271   dict_set_split_vars (d, NULL, 0);
272   dict_set_weight (d, NULL);
273   dict_set_filter (d, NULL);
274   d->case_limit = 0;
275   free (d->label);
276   d->label = NULL;
277   string_array_clear (&d->documents);
278   dict_clear_vectors (d);
279   attrset_clear (&d->attributes);
280 }
281
282 /* Clears a dictionary and destroys it. */
283 void
284 dict_destroy (struct dictionary *d)
285 {
286   if (d != NULL)
287     {
288       /* In general, we don't want callbacks occurring, if the dictionary
289          is being destroyed */
290       d->callbacks  = NULL ;
291
292       dict_clear (d);
293       string_array_destroy (&d->documents);
294       hmap_destroy (&d->name_map);
295       attrset_destroy (&d->attributes);
296       dict_clear_mrsets (d);
297       free (d->encoding);
298       free (d);
299     }
300 }
301
302 /* Returns the number of variables in D. */
303 size_t
304 dict_get_var_cnt (const struct dictionary *d)
305 {
306   return d->var_cnt;
307 }
308
309 /* Returns the variable in D with dictionary index IDX, which
310    must be between 0 and the count returned by
311    dict_get_var_cnt(), exclusive. */
312 struct variable *
313 dict_get_var (const struct dictionary *d, size_t idx)
314 {
315   assert (idx < d->var_cnt);
316
317   return d->var[idx].var;
318 }
319
320 /* Sets *VARS to an array of pointers to variables in D and *CNT
321    to the number of variables in *D.  All variables are returned
322    except for those, if any, in the classes indicated by EXCLUDE.
323    (There is no point in putting DC_SYSTEM in EXCLUDE as
324    dictionaries never include system variables.) */
325 void
326 dict_get_vars (const struct dictionary *d, const struct variable ***vars,
327                size_t *cnt, enum dict_class exclude)
328 {
329   dict_get_vars_mutable (d, (struct variable ***) vars, cnt, exclude);
330 }
331
332 /* Sets *VARS to an array of pointers to variables in D and *CNT
333    to the number of variables in *D.  All variables are returned
334    except for those, if any, in the classes indicated by EXCLUDE.
335    (There is no point in putting DC_SYSTEM in EXCLUDE as
336    dictionaries never include system variables.) */
337 void
338 dict_get_vars_mutable (const struct dictionary *d, struct variable ***vars,
339                        size_t *cnt, enum dict_class exclude)
340 {
341   size_t count;
342   size_t i;
343
344   assert (exclude == (exclude & DC_ALL));
345
346   count = 0;
347   for (i = 0; i < d->var_cnt; i++)
348     {
349       enum dict_class class = var_get_dict_class (d->var[i].var);
350       if (!(class & exclude))
351         count++;
352     }
353
354   *vars = xnmalloc (count, sizeof **vars);
355   *cnt = 0;
356   for (i = 0; i < d->var_cnt; i++)
357     {
358       enum dict_class class = var_get_dict_class (d->var[i].var);
359       if (!(class & exclude))
360         (*vars)[(*cnt)++] = d->var[i].var;
361     }
362   assert (*cnt == count);
363 }
364
365 static struct variable *
366 add_var_with_case_index (struct dictionary *d, struct variable *v,
367                          int case_index)
368 {
369   struct vardict_info *vardict;
370
371   assert (case_index >= d->next_value_idx);
372
373   /* Update dictionary. */
374   if (d->var_cnt >= d->var_cap)
375     {
376       size_t i;
377
378       d->var = x2nrealloc (d->var, &d->var_cap, sizeof *d->var);
379       hmap_clear (&d->name_map);
380       for (i = 0; i < d->var_cnt; i++)
381         {
382           var_set_vardict (d->var[i].var, &d->var[i]);
383           hmap_insert_fast (&d->name_map, &d->var[i].name_node,
384                             d->var[i].name_node.hash);
385         }
386     }
387
388   vardict = &d->var[d->var_cnt++];
389   vardict->dict = d;
390   vardict->var = v;
391   hmap_insert (&d->name_map, &vardict->name_node,
392                utf8_hash_case_string (var_get_name (v), 0));
393   vardict->case_index = case_index;
394   var_set_vardict (v, vardict);
395
396   if ( d->changed ) d->changed (d, d->changed_data);
397   if ( d->callbacks &&  d->callbacks->var_added )
398     d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
399
400   invalidate_proto (d);
401   d->next_value_idx = case_index + 1;
402
403   return v;
404 }
405
406 static struct variable *
407 add_var (struct dictionary *d, struct variable *v)
408 {
409   return add_var_with_case_index (d, v, d->next_value_idx);
410 }
411
412 /* Creates and returns a new variable in D with the given NAME
413    and WIDTH.  Returns a null pointer if the given NAME would
414    duplicate that of an existing variable in the dictionary. */
415 struct variable *
416 dict_create_var (struct dictionary *d, const char *name, int width)
417 {
418   return (dict_lookup_var (d, name) == NULL
419           ? dict_create_var_assert (d, name, width)
420           : NULL);
421 }
422
423 /* Creates and returns a new variable in D with the given NAME
424    and WIDTH.  Assert-fails if the given NAME would duplicate
425    that of an existing variable in the dictionary. */
426 struct variable *
427 dict_create_var_assert (struct dictionary *d, const char *name, int width)
428 {
429   assert (dict_lookup_var (d, name) == NULL);
430   return add_var (d, var_create (name, width));
431 }
432
433 /* Creates and returns a new variable in D, as a copy of existing variable
434    OLD_VAR, which need not be in D or in any dictionary.  Returns a null
435    pointer if OLD_VAR's name would duplicate that of an existing variable in
436    the dictionary. */
437 struct variable *
438 dict_clone_var (struct dictionary *d, const struct variable *old_var)
439 {
440   return dict_clone_var_as (d, old_var, var_get_name (old_var));
441 }
442
443 /* Creates and returns a new variable in D, as a copy of existing variable
444    OLD_VAR, which need not be in D or in any dictionary.  Assert-fails if
445    OLD_VAR's name would duplicate that of an existing variable in the
446    dictionary. */
447 struct variable *
448 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var)
449 {
450   return dict_clone_var_as_assert (d, old_var, var_get_name (old_var));
451 }
452
453 /* Creates and returns a new variable in D with name NAME, as a copy of
454    existing variable OLD_VAR, which need not be in D or in any dictionary.
455    Returns a null pointer if the given NAME would duplicate that of an existing
456    variable in the dictionary. */
457 struct variable *
458 dict_clone_var_as (struct dictionary *d, const struct variable *old_var,
459                    const char *name)
460 {
461   return (dict_lookup_var (d, name) == NULL
462           ? dict_clone_var_as_assert (d, old_var, name)
463           : NULL);
464 }
465
466 /* Creates and returns a new variable in D with name NAME, as a copy of
467    existing variable OLD_VAR, which need not be in D or in any dictionary.
468    Assert-fails if the given NAME would duplicate that of an existing variable
469    in the dictionary. */
470 struct variable *
471 dict_clone_var_as_assert (struct dictionary *d, const struct variable *old_var,
472                           const char *name)
473 {
474   struct variable *new_var = var_clone (old_var);
475   assert (dict_lookup_var (d, name) == NULL);
476   var_set_name (new_var, name);
477   return add_var (d, new_var);
478 }
479
480 struct variable *
481 dict_clone_var_in_place_assert (struct dictionary *d,
482                                 const struct variable *old_var)
483 {
484   assert (dict_lookup_var (d, var_get_name (old_var)) == NULL);
485   return add_var_with_case_index (d, var_clone (old_var),
486                                   var_get_case_index (old_var));
487 }
488
489 /* Returns the variable named NAME in D, or a null pointer if no
490    variable has that name. */
491 struct variable *
492 dict_lookup_var (const struct dictionary *d, const char *name)
493 {
494   struct vardict_info *vardict;
495
496   HMAP_FOR_EACH_WITH_HASH (vardict, struct vardict_info, name_node,
497                            utf8_hash_case_string (name, 0), &d->name_map)
498     {
499       struct variable *var = vardict->var;
500       if (!utf8_strcasecmp (var_get_name (var), name))
501         return var;
502     }
503
504   return NULL;
505 }
506
507 /* Returns the variable named NAME in D.  Assert-fails if no
508    variable has that name. */
509 struct variable *
510 dict_lookup_var_assert (const struct dictionary *d, const char *name)
511 {
512   struct variable *v = dict_lookup_var (d, name);
513   assert (v != NULL);
514   return v;
515 }
516
517 /* Returns true if variable V is in dictionary D,
518    false otherwise. */
519 bool
520 dict_contains_var (const struct dictionary *d, const struct variable *v)
521 {
522   return (var_has_vardict (v)
523           && vardict_get_dictionary (var_get_vardict (v)) == d);
524 }
525
526 /* Compares two double pointers to variables, which should point
527    to elements of a struct dictionary's `var' member array. */
528 static int
529 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED)
530 {
531   struct variable *const *a = a_;
532   struct variable *const *b = b_;
533
534   return *a < *b ? -1 : *a > *b;
535 }
536
537 static void
538 unindex_var (struct dictionary *d, struct vardict_info *vardict)
539 {
540   hmap_delete (&d->name_map, &vardict->name_node);
541 }
542
543 /* This function assumes that vardict->name_node.hash is valid, that is, that
544    its name has not changed since it was hashed (rename_var() updates this
545    hash along with the name itself). */
546 static void
547 reindex_var (struct dictionary *d, struct vardict_info *vardict)
548 {
549   struct variable *old = (d->callbacks && d->callbacks->var_changed
550                           ? var_clone (vardict->var)
551                           : NULL);
552
553   struct variable *var = vardict->var;
554   var_set_vardict (var, vardict);
555   hmap_insert_fast (&d->name_map, &vardict->name_node,
556                     vardict->name_node.hash);
557
558   if ( d->changed ) d->changed (d, d->changed_data);
559   if (old)
560     {
561       d->callbacks->var_changed (d, var_get_dict_index (var), VAR_TRAIT_POSITION, old, d->cb_data);
562       var_destroy (old);
563     }
564 }
565
566 /* Sets the case_index in V's vardict to CASE_INDEX. */
567 static void
568 set_var_case_index (struct variable *v, int case_index)
569 {
570   var_get_vardict (v)->case_index = case_index;
571 }
572
573 /* Removes the dictionary variables with indexes from FROM to TO (exclusive)
574    from name_map. */
575 static void
576 unindex_vars (struct dictionary *d, size_t from, size_t to)
577 {
578   size_t i;
579
580   for (i = from; i < to; i++)
581     unindex_var (d, &d->var[i]);
582 }
583
584 /* Re-sets the dict_index in the dictionary variables with
585    indexes from FROM to TO (exclusive). */
586 static void
587 reindex_vars (struct dictionary *d, size_t from, size_t to)
588 {
589   size_t i;
590
591   for (i = from; i < to; i++)
592     reindex_var (d, &d->var[i]);
593 }
594
595 /* Deletes variable V from dictionary D and frees V.
596
597    This is a very bad idea if there might be any pointers to V
598    from outside D.  In general, no variable in the active dataset's
599    dictionary should be deleted when any transformations are
600    active on the dictionary's dataset, because those
601    transformations might reference the deleted variable.  The
602    safest time to delete a variable is just after a procedure has
603    been executed, as done by DELETE VARIABLES.
604
605    Pointers to V within D are not a problem, because
606    dict_delete_var() knows to remove V from split variables,
607    weights, filters, etc. */
608 void
609 dict_delete_var (struct dictionary *d, struct variable *v)
610 {
611   int dict_index = var_get_dict_index (v);
612   const int case_index = var_get_case_index (v);
613
614   assert (dict_contains_var (d, v));
615
616   dict_unset_split_var (d, v);
617   dict_unset_mrset_var (d, v);
618
619   if (d->weight == v)
620     dict_set_weight (d, NULL);
621
622   if (d->filter == v)
623     dict_set_filter (d, NULL);
624
625   dict_clear_vectors (d);
626
627   /* Remove V from var array. */
628   unindex_vars (d, dict_index, d->var_cnt);
629   remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
630   d->var_cnt--;
631
632   /* Update dict_index for each affected variable. */
633   reindex_vars (d, dict_index, d->var_cnt);
634
635   /* Free memory. */
636   var_clear_vardict (v);
637
638   if ( d->changed ) d->changed (d, d->changed_data);
639
640   invalidate_proto (d);
641   if (d->callbacks &&  d->callbacks->var_deleted )
642     d->callbacks->var_deleted (d, v, dict_index, case_index, d->cb_data);
643
644   var_destroy (v);
645 }
646
647 /* Deletes the COUNT variables listed in VARS from D.  This is
648    unsafe; see the comment on dict_delete_var() for details. */
649 void
650 dict_delete_vars (struct dictionary *d,
651                   struct variable *const *vars, size_t count)
652 {
653   /* FIXME: this can be done in O(count) time, but this algorithm
654      is O(count**2). */
655   assert (count == 0 || vars != NULL);
656
657   while (count-- > 0)
658     dict_delete_var (d, *vars++);
659 }
660
661 /* Deletes the COUNT variables in D starting at index IDX.  This
662    is unsafe; see the comment on dict_delete_var() for
663    details. */
664 void
665 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
666 {
667   /* FIXME: this can be done in O(count) time, but this algorithm
668      is O(count**2). */
669   assert (idx + count <= d->var_cnt);
670
671   while (count-- > 0)
672     dict_delete_var (d, d->var[idx].var);
673 }
674
675 /* Deletes scratch variables from dictionary D. */
676 void
677 dict_delete_scratch_vars (struct dictionary *d)
678 {
679   int i;
680
681   /* FIXME: this can be done in O(count) time, but this algorithm
682      is O(count**2). */
683   for (i = 0; i < d->var_cnt; )
684     if (var_get_dict_class (d->var[i].var) == DC_SCRATCH)
685       dict_delete_var (d, d->var[i].var);
686     else
687       i++;
688 }
689
690 /* Moves V to 0-based position IDX in D.  Other variables in D,
691    if any, retain their relative positions.  Runs in time linear
692    in the distance moved. */
693 void
694 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index)
695 {
696   size_t old_index = var_get_dict_index (v);
697
698   assert (new_index < d->var_cnt);
699
700   unindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
701   move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
702   reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
703 }
704
705 /* Reorders the variables in D, placing the COUNT variables
706    listed in ORDER in that order at the beginning of D.  The
707    other variables in D, if any, retain their relative
708    positions. */
709 void
710 dict_reorder_vars (struct dictionary *d,
711                    struct variable *const *order, size_t count)
712 {
713   struct vardict_info *new_var;
714   size_t i;
715
716   assert (count == 0 || order != NULL);
717   assert (count <= d->var_cnt);
718
719   new_var = xnmalloc (d->var_cap, sizeof *new_var);
720
721   /* Add variables in ORDER to new_var. */
722   for (i = 0; i < count; i++)
723     {
724       struct vardict_info *old_var;
725
726       assert (dict_contains_var (d, order[i]));
727
728       old_var = var_get_vardict (order[i]);
729       new_var[i] = *old_var;
730       old_var->dict = NULL;
731     }
732
733   /* Add remaining variables to new_var. */
734   for (i = 0; i < d->var_cnt; i++)
735     if (d->var[i].dict != NULL)
736       new_var[count++] = d->var[i];
737   assert (count == d->var_cnt);
738
739   /* Replace old vardicts by new ones. */
740   free (d->var);
741   d->var = new_var;
742
743   hmap_clear (&d->name_map);
744   reindex_vars (d, 0, d->var_cnt);
745 }
746
747 /* Changes the name of variable V that is currently in a dictionary to
748    NEW_NAME. */
749 static void
750 rename_var (struct variable *v, const char *new_name)
751 {
752   struct vardict_info *vardict = var_get_vardict (v);
753   var_clear_vardict (v);
754   var_set_name (v, new_name);
755   vardict->name_node.hash = utf8_hash_case_string (new_name, 0);
756   var_set_vardict (v, vardict);
757 }
758
759 /* Tries to changes the name of V in D to name NEW_NAME.  Returns true if
760    successful, false if a variable (other than V) with the given name already
761    exists in D. */
762 bool
763 dict_try_rename_var (struct dictionary *d, struct variable *v,
764                      const char *new_name)
765 {
766   struct variable *conflict = dict_lookup_var (d, new_name);
767   if (conflict && v != conflict)
768     return false;
769
770   struct variable *old = var_clone (v);
771   unindex_var (d, var_get_vardict (v));
772   rename_var (v, new_name);
773   reindex_var (d, var_get_vardict (v));
774
775   if (settings_get_algorithm () == ENHANCED)
776     var_clear_short_names (v);
777
778   if ( d->changed ) d->changed (d, d->changed_data);
779   if ( d->callbacks &&  d->callbacks->var_changed )
780     d->callbacks->var_changed (d, var_get_dict_index (v), VAR_TRAIT_NAME, old, d->cb_data);
781
782   var_destroy (old);
783
784   return true;
785 }
786
787 /* Changes the name of V in D to name NEW_NAME.  Assert-fails if
788    a variable named NEW_NAME is already in D, except that
789    NEW_NAME may be the same as V's existing name. */
790 void
791 dict_rename_var (struct dictionary *d, struct variable *v,
792                  const char *new_name)
793 {
794   bool ok UNUSED = dict_try_rename_var (d, v, new_name);
795   assert (ok);
796 }
797
798 /* Renames COUNT variables specified in VARS to the names given
799    in NEW_NAMES within dictionary D.  If the renaming would
800    result in a duplicate variable name, returns false and stores a
801    name that would be duplicated into *ERR_NAME (if ERR_NAME is
802    non-null).  Otherwise, the renaming is successful, and true
803    is returned. */
804 bool
805 dict_rename_vars (struct dictionary *d,
806                   struct variable **vars, char **new_names, size_t count,
807                   char **err_name)
808 {
809   struct pool *pool;
810   char **old_names;
811   size_t i;
812
813   assert (count == 0 || vars != NULL);
814   assert (count == 0 || new_names != NULL);
815
816   /* Save the names of the variables to be renamed. */
817   pool = pool_create ();
818   old_names = pool_nalloc (pool, count, sizeof *old_names);
819   for (i = 0; i < count; i++)
820     old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
821
822   /* Remove the variables to be renamed from the name hash,
823      and rename them. */
824   for (i = 0; i < count; i++)
825     {
826       unindex_var (d, var_get_vardict (vars[i]));
827       rename_var (vars[i], new_names[i]);
828     }
829
830   /* Add the renamed variables back into the name hash,
831      checking for conflicts. */
832   for (i = 0; i < count; i++)
833     {
834       if (dict_lookup_var (d, var_get_name (vars[i])) != NULL)
835         {
836           /* There is a name conflict.
837              Back out all the name changes that have already
838              taken place, and indicate failure. */
839           size_t fail_idx = i;
840           if (err_name != NULL)
841             *err_name = new_names[i];
842
843           for (i = 0; i < fail_idx; i++)
844             unindex_var (d, var_get_vardict (vars[i]));
845
846           for (i = 0; i < count; i++)
847             {
848               rename_var (vars[i], old_names[i]);
849               reindex_var (d, var_get_vardict (vars[i]));
850             }
851
852           pool_destroy (pool);
853           return false;
854         }
855       reindex_var (d, var_get_vardict (vars[i]));
856     }
857
858   /* Clear short names. */
859   if (settings_get_algorithm () == ENHANCED)
860     for (i = 0; i < count; i++)
861       var_clear_short_names (vars[i]);
862
863   pool_destroy (pool);
864   return true;
865 }
866
867 /* Returns true if a variable named NAME may be inserted in DICT;
868    that is, if there is not already a variable with that name in
869    DICT and if NAME is not a reserved word.  (The caller's checks
870    have already verified that NAME is otherwise acceptable as a
871    variable name.) */
872 static bool
873 var_name_is_insertable (const struct dictionary *dict, const char *name)
874 {
875   return (dict_lookup_var (dict, name) == NULL
876           && lex_id_to_token (ss_cstr (name)) == T_ID);
877 }
878
879 static char *
880 make_hinted_name (const struct dictionary *dict, const char *hint)
881 {
882   size_t hint_len = strlen (hint);
883   bool dropped = false;
884   char *root, *rp;
885   size_t ofs;
886   int mblen;
887
888   /* The allocation size here is OK: characters that are copied directly fit
889      OK, and characters that are not copied directly are replaced by a single
890      '_' byte.  If u8_mbtouc() replaces bad input by 0xfffd, then that will get
891      replaced by '_' too.  */
892   root = rp = xmalloc (hint_len + 1);
893   for (ofs = 0; ofs < hint_len; ofs += mblen)
894     {
895       ucs4_t uc;
896
897       mblen = u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, hint + ofs),
898                          hint_len - ofs);
899       if (rp == root
900           ? lex_uc_is_id1 (uc) && uc != '$'
901           : lex_uc_is_idn (uc))
902         {
903           if (dropped)
904             {
905               *rp++ = '_';
906               dropped = false;
907             }
908           rp += u8_uctomb (CHAR_CAST (uint8_t *, rp), uc, 6);
909         }
910       else if (rp != root)
911         dropped = true;
912     }
913   *rp = '\0';
914
915   if (root[0] != '\0')
916     {
917       unsigned long int i;
918
919       if (var_name_is_insertable (dict, root))
920         return root;
921
922       for (i = 0; i < ULONG_MAX; i++)
923         {
924           char suffix[INT_BUFSIZE_BOUND (i) + 1];
925           char *name;
926
927           suffix[0] = '_';
928           if (!str_format_26adic (i + 1, true, &suffix[1], sizeof suffix - 1))
929             NOT_REACHED ();
930
931           name = utf8_encoding_concat (root, suffix, dict->encoding, 64);
932           if (var_name_is_insertable (dict, name))
933             {
934               free (root);
935               return name;
936             }
937           free (name);
938         }
939     }
940
941   free (root);
942
943   return NULL;
944 }
945
946 static char *
947 make_numeric_name (const struct dictionary *dict, unsigned long int *num_start)
948 {
949   unsigned long int number;
950
951   for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
952        number < ULONG_MAX;
953        number++)
954     {
955       char name[3 + INT_STRLEN_BOUND (number) + 1];
956
957       sprintf (name, "VAR%03lu", number);
958       if (dict_lookup_var (dict, name) == NULL)
959         {
960           if (num_start != NULL)
961             *num_start = number + 1;
962           return xstrdup (name);
963         }
964     }
965
966   NOT_REACHED ();
967 }
968
969
970 /* Devises and returns a variable name unique within DICT.  The variable name
971    is owned by the caller, which must free it with free() when it is no longer
972    needed.
973
974    HINT, if it is non-null, is used as a suggestion that will be
975    modified for suitability as a variable name and for
976    uniqueness.
977
978    If HINT is null or entirely unsuitable, a name in the form
979    "VAR%03d" will be generated, where the smallest unused integer
980    value is used.  If NUM_START is non-null, then its value is
981    used as the minimum numeric value to check, and it is updated
982    to the next value to be checked.
983 */
984 char *
985 dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
986                            unsigned long int *num_start)
987 {
988   if (hint != NULL)
989     {
990       char *hinted_name = make_hinted_name (dict, hint);
991       if (hinted_name != NULL)
992         return hinted_name;
993     }
994   return make_numeric_name (dict, num_start);
995 }
996
997 /* Returns the weighting variable in dictionary D, or a null
998    pointer if the dictionary is unweighted. */
999 struct variable *
1000 dict_get_weight (const struct dictionary *d)
1001 {
1002   assert (d->weight == NULL || dict_contains_var (d, d->weight));
1003
1004   return d->weight;
1005 }
1006
1007 /* Returns the value of D's weighting variable in case C, except
1008    that a negative weight is returned as 0.  Returns 1 if the
1009    dictionary is unweighted.  Will warn about missing, negative,
1010    or zero values if *WARN_ON_INVALID is true.  The function will
1011    set *WARN_ON_INVALID to false if an invalid weight is
1012    found. */
1013 double
1014 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
1015                       bool *warn_on_invalid)
1016 {
1017   assert (c != NULL);
1018
1019   if (d->weight == NULL)
1020     return 1.0;
1021   else
1022     {
1023       double w = case_num (c, d->weight);
1024
1025       return var_force_valid_weight (d->weight, w, warn_on_invalid);
1026     }
1027 }
1028
1029 /* Sets the weighting variable of D to V, or turning off
1030    weighting if V is a null pointer. */
1031 void
1032 dict_set_weight (struct dictionary *d, struct variable *v)
1033 {
1034   assert (v == NULL || dict_contains_var (d, v));
1035   assert (v == NULL || var_is_numeric (v));
1036
1037   d->weight = v;
1038
1039   if (d->changed) d->changed (d, d->changed_data);
1040   if ( d->callbacks &&  d->callbacks->weight_changed )
1041     d->callbacks->weight_changed (d,
1042                                   v ? var_get_dict_index (v) : -1,
1043                                   d->cb_data);
1044 }
1045
1046 /* Returns the filter variable in dictionary D (see cmd_filter())
1047    or a null pointer if the dictionary is unfiltered. */
1048 struct variable *
1049 dict_get_filter (const struct dictionary *d)
1050 {
1051   assert (d->filter == NULL || dict_contains_var (d, d->filter));
1052
1053   return d->filter;
1054 }
1055
1056 /* Sets V as the filter variable for dictionary D.  Passing a
1057    null pointer for V turn off filtering. */
1058 void
1059 dict_set_filter (struct dictionary *d, struct variable *v)
1060 {
1061   assert (v == NULL || dict_contains_var (d, v));
1062   assert (v == NULL || var_is_numeric (v));
1063
1064   d->filter = v;
1065
1066   if (d->changed) d->changed (d, d->changed_data);
1067   if ( d->callbacks && d->callbacks->filter_changed )
1068     d->callbacks->filter_changed (d,
1069                                   v ? var_get_dict_index (v) : -1,
1070                                   d->cb_data);
1071 }
1072
1073 /* Returns the case limit for dictionary D, or zero if the number
1074    of cases is unlimited. */
1075 casenumber
1076 dict_get_case_limit (const struct dictionary *d)
1077 {
1078   return d->case_limit;
1079 }
1080
1081 /* Sets CASE_LIMIT as the case limit for dictionary D.  Use
1082    0 for CASE_LIMIT to indicate no limit. */
1083 void
1084 dict_set_case_limit (struct dictionary *d, casenumber case_limit)
1085 {
1086   d->case_limit = case_limit;
1087 }
1088
1089 /* Returns the prototype used for cases created by dictionary D. */
1090 const struct caseproto *
1091 dict_get_proto (const struct dictionary *d_)
1092 {
1093   struct dictionary *d = CONST_CAST (struct dictionary *, d_);
1094   if (d->proto == NULL)
1095     {
1096       size_t i;
1097
1098       d->proto = caseproto_create ();
1099       d->proto = caseproto_reserve (d->proto, d->var_cnt);
1100       for (i = 0; i < d->var_cnt; i++)
1101         d->proto = caseproto_set_width (d->proto,
1102                                         var_get_case_index (d->var[i].var),
1103                                         var_get_width (d->var[i].var));
1104     }
1105   return d->proto;
1106 }
1107
1108 /* Returns the case index of the next value to be added to D.
1109    This value is the number of `union value's that need to be
1110    allocated to store a case for dictionary D. */
1111 int
1112 dict_get_next_value_idx (const struct dictionary *d)
1113 {
1114   return d->next_value_idx;
1115 }
1116
1117 /* Returns the number of bytes needed to store a case for
1118    dictionary D. */
1119 size_t
1120 dict_get_case_size (const struct dictionary *d)
1121 {
1122   return sizeof (union value) * dict_get_next_value_idx (d);
1123 }
1124
1125 /* Reassigns values in dictionary D so that fragmentation is
1126    eliminated. */
1127 void
1128 dict_compact_values (struct dictionary *d)
1129 {
1130   size_t i;
1131
1132   d->next_value_idx = 0;
1133   for (i = 0; i < d->var_cnt; i++)
1134     {
1135       struct variable *v = d->var[i].var;
1136       set_var_case_index (v, d->next_value_idx++);
1137     }
1138   invalidate_proto (d);
1139 }
1140
1141 /* Returns the number of values occupied by the variables in
1142    dictionary D.  All variables are considered if EXCLUDE_CLASSES
1143    is 0, or it may contain one or more of (1u << DC_ORDINARY),
1144    (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to exclude the
1145    corresponding type of variable.
1146
1147    The return value may be less than the number of values in one
1148    of dictionary D's cases (as returned by
1149    dict_get_next_value_idx) even if E is 0, because there may be
1150    gaps in D's cases due to deleted variables. */
1151 size_t
1152 dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
1153 {
1154   size_t i;
1155   size_t cnt;
1156
1157   assert ((exclude_classes & ~((1u << DC_ORDINARY)
1158                                | (1u << DC_SYSTEM)
1159                                | (1u << DC_SCRATCH))) == 0);
1160
1161   cnt = 0;
1162   for (i = 0; i < d->var_cnt; i++)
1163     {
1164       enum dict_class class = var_get_dict_class (d->var[i].var);
1165       if (!(exclude_classes & (1u << class)))
1166         cnt++;
1167     }
1168   return cnt;
1169 }
1170
1171 /* Returns the case prototype that would result after deleting
1172    all variables from D that are not in one of the
1173    EXCLUDE_CLASSES and compacting the dictionary with
1174    dict_compact().
1175
1176    The caller must unref the returned caseproto when it is no
1177    longer needed. */
1178 struct caseproto *
1179 dict_get_compacted_proto (const struct dictionary *d,
1180                           unsigned int exclude_classes)
1181 {
1182   struct caseproto *proto;
1183   size_t i;
1184
1185   assert ((exclude_classes & ~((1u << DC_ORDINARY)
1186                                | (1u << DC_SYSTEM)
1187                                | (1u << DC_SCRATCH))) == 0);
1188
1189   proto = caseproto_create ();
1190   for (i = 0; i < d->var_cnt; i++)
1191     {
1192       struct variable *v = d->var[i].var;
1193       if (!(exclude_classes & (1u << var_get_dict_class (v))))
1194         proto = caseproto_add_width (proto, var_get_width (v));
1195     }
1196   return proto;
1197 }
1198 \f
1199 /* Returns the SPLIT FILE vars (see cmd_split_file()).  Call
1200    dict_get_split_cnt() to determine how many SPLIT FILE vars
1201    there are.  Returns a null pointer if and only if there are no
1202    SPLIT FILE vars. */
1203 const struct variable *const *
1204 dict_get_split_vars (const struct dictionary *d)
1205 {
1206   return d->split;
1207 }
1208
1209 /* Returns the number of SPLIT FILE vars. */
1210 size_t
1211 dict_get_split_cnt (const struct dictionary *d)
1212 {
1213   return d->split_cnt;
1214 }
1215
1216 /* Removes variable V, which must be in D, from D's set of split
1217    variables. */
1218 static void
1219 dict_unset_split_var (struct dictionary *d, struct variable *v)
1220 {
1221   int orig_count;
1222
1223   assert (dict_contains_var (d, v));
1224
1225   orig_count = d->split_cnt;
1226   d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
1227                                &v, compare_var_ptrs, NULL);
1228   if (orig_count != d->split_cnt)
1229     {
1230       if (d->changed) d->changed (d, d->changed_data);
1231       /* We changed the set of split variables so invoke the
1232          callback. */
1233       if (d->callbacks &&  d->callbacks->split_changed)
1234         d->callbacks->split_changed (d, d->cb_data);
1235     }
1236 }
1237
1238 /* Sets CNT split vars SPLIT in dictionary D. */
1239 void
1240 dict_set_split_vars (struct dictionary *d,
1241                      struct variable *const *split, size_t cnt)
1242 {
1243   assert (cnt == 0 || split != NULL);
1244
1245   d->split_cnt = cnt;
1246   if ( cnt > 0 )
1247    {
1248     d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
1249     memcpy (d->split, split, cnt * sizeof *d->split);
1250    }
1251   else
1252    {
1253     free (d->split);
1254     d->split = NULL;
1255    }
1256
1257   if (d->changed) d->changed (d, d->changed_data);
1258   if ( d->callbacks &&  d->callbacks->split_changed )
1259     d->callbacks->split_changed (d, d->cb_data);
1260 }
1261
1262 /* Returns the file label for D, or a null pointer if D is
1263    unlabeled (see cmd_file_label()). */
1264 const char *
1265 dict_get_label (const struct dictionary *d)
1266 {
1267   return d->label;
1268 }
1269
1270 /* Sets D's file label to LABEL, truncating it to at most 60 bytes in D's
1271    encoding.
1272
1273    Removes D's label if LABEL is null or the empty string. */
1274 void
1275 dict_set_label (struct dictionary *d, const char *label)
1276 {
1277   free (d->label);
1278   if (label == NULL || label[0] == '\0')
1279     d->label = NULL;
1280   else
1281     d->label = utf8_encoding_trunc (label, d->encoding, 60);
1282 }
1283
1284 /* Returns the documents for D, as an UTF-8 encoded string_array.  The
1285    return value is always nonnull; if there are no documents then the
1286    string_arary is empty.*/
1287 const struct string_array *
1288 dict_get_documents (const struct dictionary *d)
1289 {
1290   return &d->documents;
1291 }
1292
1293 /* Replaces the documents for D by NEW_DOCS, a UTF-8 encoded string_array. */
1294 void
1295 dict_set_documents (struct dictionary *d, const struct string_array *new_docs)
1296 {
1297   size_t i;
1298
1299   dict_clear_documents (d);
1300
1301   for (i = 0; i < new_docs->n; i++)
1302     dict_add_document_line (d, new_docs->strings[i], false);
1303 }
1304
1305 /* Replaces the documents for D by UTF-8 encoded string NEW_DOCS, dividing it
1306    into individual lines at new-line characters.  Each line is truncated to at
1307    most DOC_LINE_LENGTH bytes in D's encoding. */
1308 void
1309 dict_set_documents_string (struct dictionary *d, const char *new_docs)
1310 {
1311   const char *s;
1312
1313   dict_clear_documents (d);
1314   for (s = new_docs; *s != '\0'; )
1315     {
1316       size_t len = strcspn (s, "\n");
1317       char *line = xmemdup0 (s, len);
1318       dict_add_document_line (d, line, false);
1319       free (line);
1320
1321       s += len;
1322       if (*s == '\n')
1323         s++;
1324     }
1325 }
1326
1327 /* Drops the documents from dictionary D. */
1328 void
1329 dict_clear_documents (struct dictionary *d)
1330 {
1331   string_array_clear (&d->documents);
1332 }
1333
1334 /* Appends the UTF-8 encoded LINE to the documents in D.  LINE will be
1335    truncated so that it is no more than 80 bytes in the dictionary's
1336    encoding.  If this causes some text to be lost, and ISSUE_WARNING is true,
1337    then a warning will be issued. */
1338 bool
1339 dict_add_document_line (struct dictionary *d, const char *line,
1340                         bool issue_warning)
1341 {
1342   size_t trunc_len;
1343   bool truncated;
1344
1345   trunc_len = utf8_encoding_trunc_len (line, d->encoding, DOC_LINE_LENGTH);
1346   truncated = line[trunc_len] != '\0';
1347   if (truncated && issue_warning)
1348     {
1349       /* Note to translators: "bytes" is correct, not characters */
1350       msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH);
1351     }
1352
1353   string_array_append_nocopy (&d->documents, xmemdup0 (line, trunc_len));
1354
1355   return !truncated;
1356 }
1357
1358 /* Returns the number of document lines in dictionary D. */
1359 size_t
1360 dict_get_document_line_cnt (const struct dictionary *d)
1361 {
1362   return d->documents.n;
1363 }
1364
1365 /* Returns document line number IDX in dictionary D.  The caller must not
1366    modify or free the returned string. */
1367 const char *
1368 dict_get_document_line (const struct dictionary *d, size_t idx)
1369 {
1370   assert (idx < d->documents.n);
1371   return d->documents.strings[idx];
1372 }
1373
1374 /* Creates in D a vector named NAME that contains the CNT
1375    variables in VAR.  Returns true if successful, or false if a
1376    vector named NAME already exists in D. */
1377 bool
1378 dict_create_vector (struct dictionary *d,
1379                     const char *name,
1380                     struct variable **var, size_t cnt)
1381 {
1382   size_t i;
1383
1384   assert (cnt > 0);
1385   for (i = 0; i < cnt; i++)
1386     assert (dict_contains_var (d, var[i]));
1387
1388   if (dict_lookup_vector (d, name) == NULL)
1389     {
1390       d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1391       d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1392       return true;
1393     }
1394   else
1395     return false;
1396 }
1397
1398 /* Creates in D a vector named NAME that contains the CNT
1399    variables in VAR.  A vector named NAME must not already exist
1400    in D. */
1401 void
1402 dict_create_vector_assert (struct dictionary *d,
1403                            const char *name,
1404                            struct variable **var, size_t cnt)
1405 {
1406   assert (dict_lookup_vector (d, name) == NULL);
1407   dict_create_vector (d, name, var, cnt);
1408 }
1409
1410 /* Returns the vector in D with index IDX, which must be less
1411    than dict_get_vector_cnt (D). */
1412 const struct vector *
1413 dict_get_vector (const struct dictionary *d, size_t idx)
1414 {
1415   assert (idx < d->vector_cnt);
1416
1417   return d->vector[idx];
1418 }
1419
1420 /* Returns the number of vectors in D. */
1421 size_t
1422 dict_get_vector_cnt (const struct dictionary *d)
1423 {
1424   return d->vector_cnt;
1425 }
1426
1427 /* Looks up and returns the vector within D with the given
1428    NAME. */
1429 const struct vector *
1430 dict_lookup_vector (const struct dictionary *d, const char *name)
1431 {
1432   size_t i;
1433   for (i = 0; i < d->vector_cnt; i++)
1434     if (!utf8_strcasecmp (vector_get_name (d->vector[i]), name))
1435       return d->vector[i];
1436   return NULL;
1437 }
1438
1439 /* Deletes all vectors from D. */
1440 void
1441 dict_clear_vectors (struct dictionary *d)
1442 {
1443   size_t i;
1444
1445   for (i = 0; i < d->vector_cnt; i++)
1446     vector_destroy (d->vector[i]);
1447   free (d->vector);
1448
1449   d->vector = NULL;
1450   d->vector_cnt = 0;
1451 }
1452 \f
1453 /* Multiple response sets. */
1454
1455 /* Returns the multiple response set in DICT with index IDX, which must be
1456    between 0 and the count returned by dict_get_n_mrsets(), exclusive. */
1457 const struct mrset *
1458 dict_get_mrset (const struct dictionary *dict, size_t idx)
1459 {
1460   assert (idx < dict->n_mrsets);
1461   return dict->mrsets[idx];
1462 }
1463
1464 /* Returns the number of multiple response sets in DICT. */
1465 size_t
1466 dict_get_n_mrsets (const struct dictionary *dict)
1467 {
1468   return dict->n_mrsets;
1469 }
1470
1471 /* Looks for a multiple response set named NAME in DICT.  If it finds one,
1472    returns its index; otherwise, returns SIZE_MAX. */
1473 static size_t
1474 dict_lookup_mrset_idx (const struct dictionary *dict, const char *name)
1475 {
1476   size_t i;
1477
1478   for (i = 0; i < dict->n_mrsets; i++)
1479     if (!utf8_strcasecmp (name, dict->mrsets[i]->name))
1480       return i;
1481
1482   return SIZE_MAX;
1483 }
1484
1485 /* Looks for a multiple response set named NAME in DICT.  If it finds one,
1486    returns it; otherwise, returns NULL. */
1487 const struct mrset *
1488 dict_lookup_mrset (const struct dictionary *dict, const char *name)
1489 {
1490   size_t idx = dict_lookup_mrset_idx (dict, name);
1491   return idx != SIZE_MAX ? dict->mrsets[idx] : NULL;
1492 }
1493
1494 /* Adds MRSET to DICT, replacing any existing set with the same name.  Returns
1495    true if a set was replaced, false if none existed with the specified name.
1496
1497    Ownership of MRSET is transferred to DICT. */
1498 bool
1499 dict_add_mrset (struct dictionary *dict, struct mrset *mrset)
1500 {
1501   size_t idx;
1502
1503   assert (mrset_ok (mrset, dict));
1504
1505   idx = dict_lookup_mrset_idx (dict, mrset->name);
1506   if (idx == SIZE_MAX)
1507     {
1508       dict->mrsets = xrealloc (dict->mrsets,
1509                                (dict->n_mrsets + 1) * sizeof *dict->mrsets);
1510       dict->mrsets[dict->n_mrsets++] = mrset;
1511       return true;
1512     }
1513   else
1514     {
1515       mrset_destroy (dict->mrsets[idx]);
1516       dict->mrsets[idx] = mrset;
1517       return false;
1518     }
1519 }
1520
1521 /* Looks for a multiple response set in DICT named NAME.  If found, removes it
1522    from DICT and returns true.  If none is found, returns false without
1523    modifying DICT.
1524
1525    Deleting one multiple response set causes the indexes of other sets within
1526    DICT to change. */
1527 bool
1528 dict_delete_mrset (struct dictionary *dict, const char *name)
1529 {
1530   size_t idx = dict_lookup_mrset_idx (dict, name);
1531   if (idx != SIZE_MAX)
1532     {
1533       mrset_destroy (dict->mrsets[idx]);
1534       dict->mrsets[idx] = dict->mrsets[--dict->n_mrsets];
1535       return true;
1536     }
1537   else
1538     return false;
1539 }
1540
1541 /* Deletes all multiple response sets from DICT. */
1542 void
1543 dict_clear_mrsets (struct dictionary *dict)
1544 {
1545   size_t i;
1546
1547   for (i = 0; i < dict->n_mrsets; i++)
1548     mrset_destroy (dict->mrsets[i]);
1549   free (dict->mrsets);
1550   dict->mrsets = NULL;
1551   dict->n_mrsets = 0;
1552 }
1553
1554 /* Removes VAR, which must be in DICT, from DICT's multiple response sets. */
1555 static void
1556 dict_unset_mrset_var (struct dictionary *dict, struct variable *var)
1557 {
1558   size_t i;
1559
1560   assert (dict_contains_var (dict, var));
1561
1562   for (i = 0; i < dict->n_mrsets; )
1563     {
1564       struct mrset *mrset = dict->mrsets[i];
1565       size_t j;
1566
1567       for (j = 0; j < mrset->n_vars; )
1568         if (mrset->vars[j] == var)
1569           remove_element (mrset->vars, mrset->n_vars--,
1570                           sizeof *mrset->vars, j);
1571         else
1572           j++;
1573
1574       if (mrset->n_vars < 2)
1575         {
1576           mrset_destroy (mrset);
1577           dict->mrsets[i] = dict->mrsets[--dict->n_mrsets];
1578         }
1579       else
1580         i++;
1581     }
1582 }
1583 \f
1584 /* Returns D's attribute set.  The caller may examine or modify
1585    the attribute set, but must not destroy it.  Destroying D or
1586    calling dict_set_attributes for D will also destroy D's
1587    attribute set. */
1588 struct attrset *
1589 dict_get_attributes (const struct dictionary *d)
1590 {
1591   return CONST_CAST (struct attrset *, &d->attributes);
1592 }
1593
1594 /* Replaces D's attributes set by a copy of ATTRS. */
1595 void
1596 dict_set_attributes (struct dictionary *d, const struct attrset *attrs)
1597 {
1598   attrset_destroy (&d->attributes);
1599   attrset_clone (&d->attributes, attrs);
1600 }
1601
1602 /* Returns true if D has at least one attribute in its attribute
1603    set, false if D's attribute set is empty. */
1604 bool
1605 dict_has_attributes (const struct dictionary *d)
1606 {
1607   return attrset_count (&d->attributes) > 0;
1608 }
1609
1610 /* Called from variable.c to notify the dictionary that some property (indicated
1611    by WHAT) of the variable has changed.  OLDVAR is a copy of V as it existed
1612    prior to the change.  OLDVAR is destroyed by this function.
1613 */
1614 void
1615 dict_var_changed (const struct variable *v, unsigned int what, struct variable *oldvar)
1616 {
1617   if ( var_has_vardict (v))
1618     {
1619       const struct vardict_info *vardict = var_get_vardict (v);
1620       struct dictionary *d = vardict->dict;
1621
1622       if ( NULL == d)
1623         return;
1624
1625       if (d->changed ) d->changed (d, d->changed_data);
1626       if ( d->callbacks && d->callbacks->var_changed )
1627         d->callbacks->var_changed (d, var_get_dict_index (v), what, oldvar, d->cb_data);
1628     }
1629   var_destroy (oldvar);
1630 }
1631
1632
1633 \f
1634 /* Dictionary used to contain "internal variables". */
1635 static struct dictionary *internal_dict;
1636
1637 /* Create a variable of the specified WIDTH to be used for internal
1638    calculations only.  The variable is assigned case index CASE_IDX. */
1639 struct variable *
1640 dict_create_internal_var (int case_idx, int width)
1641 {
1642   if (internal_dict == NULL)
1643     internal_dict = dict_create ("UTF-8");
1644
1645   for (;;)
1646     {
1647       static int counter = INT_MAX / 2;
1648       struct variable *var;
1649       char name[64];
1650
1651       if (++counter == INT_MAX)
1652         counter = INT_MAX / 2;
1653
1654       sprintf (name, "$internal%d", counter);
1655       var = dict_create_var (internal_dict, name, width);
1656       if (var != NULL)
1657         {
1658           set_var_case_index (var, case_idx);
1659           return var;
1660         }
1661     }
1662 }
1663
1664 /* Destroys VAR, which must have been created with
1665    dict_create_internal_var(). */
1666 void
1667 dict_destroy_internal_var (struct variable *var)
1668 {
1669   if (var != NULL)
1670     {
1671       dict_delete_var (internal_dict, var);
1672
1673       /* Destroy internal_dict if it has no variables left, just so that
1674          valgrind --leak-check --show-reachable won't show internal_dict. */
1675       if (dict_get_var_cnt (internal_dict) == 0)
1676         {
1677           dict_destroy (internal_dict);
1678           internal_dict = NULL;
1679         }
1680     }
1681 }
1682 \f
1683 int
1684 vardict_get_dict_index (const struct vardict_info *vardict)
1685 {
1686   return vardict - vardict->dict->var;
1687 }