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