Remove some unnecessary #include directives
[pspp-builds.git] / src / data / dictionary.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2009 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 "dictionary.h"
20
21 #include <stdlib.h>
22 #include <ctype.h>
23
24 #include <data/attributes.h>
25 #include <data/case.h>
26 #include <data/identifier.h>
27 #include <data/settings.h>
28 #include <data/value-labels.h>
29 #include <data/vardict.h>
30 #include <data/variable.h>
31 #include <data/vector.h>
32 #include <libpspp/array.h>
33 #include <libpspp/assertion.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/message.h>
37 #include <libpspp/misc.h>
38 #include <libpspp/pool.h>
39 #include <libpspp/str.h>
40
41 #include "intprops.h"
42 #include "minmax.h"
43 #include "xalloc.h"
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47
48 /* A dictionary. */
49 struct dictionary
50   {
51     struct variable **var;      /* Variables. */
52     size_t var_cnt, var_cap;    /* Number of variables, capacity. */
53     struct caseproto *proto;    /* Prototype for dictionary cases
54                                    (updated lazily). */
55     struct hsh_table *name_tab; /* Variable index by name. */
56     int next_value_idx;         /* Index of next `union value' to allocate. */
57     const struct variable **split;    /* SPLIT FILE vars. */
58     size_t split_cnt;           /* SPLIT FILE count. */
59     struct variable *weight;    /* WEIGHT variable. */
60     struct variable *filter;    /* FILTER variable. */
61     casenumber case_limit;      /* Current case limit (N command). */
62     char *label;                /* File label. */
63     struct string documents;    /* Documents, as a string. */
64     struct vector **vector;     /* Vectors of variables. */
65     size_t vector_cnt;          /* Number of vectors. */
66     struct attrset attributes;  /* Custom attributes. */
67
68     char *encoding;             /* Character encoding of string data */
69
70     const struct dict_callbacks *callbacks; /* Callbacks on dictionary
71                                                modification */
72     void *cb_data ;                  /* Data passed to callbacks */
73
74     void (*changed) (struct dictionary *, void *); /* Generic change callback */
75     void *changed_data;
76   };
77
78
79 void
80 dict_set_encoding (struct dictionary *d, const char *enc)
81 {
82   if (enc)
83     d->encoding = xstrdup (enc);
84 }
85
86 const char *
87 dict_get_encoding (const struct dictionary *d)
88 {
89   return d->encoding ;
90 }
91
92
93 void
94 dict_set_change_callback (struct dictionary *d,
95                           void (*changed) (struct dictionary *, void*),
96                           void *data)
97 {
98   d->changed = changed;
99   d->changed_data = data;
100 }
101
102 /* Discards dictionary D's caseproto.  (It will be regenerated
103    lazily, on demand.) */
104 static void
105 invalidate_proto (struct dictionary *d)
106 {
107   caseproto_unref (d->proto);
108   d->proto = NULL;
109 }
110
111 /* Print a representation of dictionary D to stdout, for
112    debugging purposes. */
113 void
114 dict_dump (const struct dictionary *d)
115 {
116   int i;
117   for (i = 0 ; i < d->var_cnt ; ++i )
118     {
119       const struct variable *v =
120         d->var[i];
121       printf ("Name: %s;\tdict_idx: %zu; case_idx: %zu\n",
122               var_get_name (v),
123               var_get_dict_index (v),
124               var_get_case_index (v));
125
126     }
127 }
128
129 /* Associate CALLBACKS with DICT.  Callbacks will be invoked whenever
130    the dictionary or any of the variables it contains are modified.
131    Each callback will get passed CALLBACK_DATA.
132    Any callback may be NULL, in which case it'll be ignored.
133 */
134 void
135 dict_set_callbacks (struct dictionary *dict,
136                     const struct dict_callbacks *callbacks,
137                     void *callback_data)
138 {
139   dict->callbacks = callbacks;
140   dict->cb_data = callback_data;
141 }
142
143 /* Shallow copy the callbacks from SRC to DEST */
144 void
145 dict_copy_callbacks (struct dictionary *dest,
146                      const struct dictionary *src)
147 {
148   dest->callbacks = src->callbacks;
149   dest->cb_data = src->cb_data;
150 }
151
152 /* Creates and returns a new dictionary. */
153 struct dictionary *
154 dict_create (void)
155 {
156   struct dictionary *d = xzalloc (sizeof *d);
157
158   d->name_tab = hsh_create (8, compare_vars_by_name, hash_var_by_name,
159                             NULL, NULL);
160   attrset_init (&d->attributes);
161   return d;
162 }
163
164 /* Creates and returns a (deep) copy of an existing
165    dictionary.
166
167    The new dictionary's case indexes are copied from the old
168    dictionary.  If the new dictionary won't be used to access
169    cases produced with the old dictionary, then the new
170    dictionary's case indexes should be compacted with
171    dict_compact_values to save space. */
172 struct dictionary *
173 dict_clone (const struct dictionary *s)
174 {
175   struct dictionary *d;
176   size_t i;
177
178   assert (s != NULL);
179
180   d = dict_create ();
181
182   for (i = 0; i < s->var_cnt; i++)
183     {
184       const struct vardict_info *svdi;
185       struct vardict_info dvdi;
186       struct variable *sv = s->var[i];
187       struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv));
188       size_t i;
189
190       for (i = 0; i < var_get_short_name_cnt (sv); i++)
191         var_set_short_name (dv, i, var_get_short_name (sv, i));
192
193       svdi = var_get_vardict (sv);
194       dvdi = *svdi;
195       dvdi.dict = d;
196       var_set_vardict (dv, &dvdi);
197     }
198
199   d->next_value_idx = s->next_value_idx;
200
201   d->split_cnt = s->split_cnt;
202   if (d->split_cnt > 0)
203     {
204       d->split = xnmalloc (d->split_cnt, sizeof *d->split);
205       for (i = 0; i < d->split_cnt; i++)
206         d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i]));
207     }
208
209   if (s->weight != NULL)
210     dict_set_weight (d, dict_lookup_var_assert (d, var_get_name (s->weight)));
211
212   if (s->filter != NULL)
213     dict_set_filter (d, dict_lookup_var_assert (d, var_get_name (s->filter)));
214
215   d->case_limit = s->case_limit;
216   dict_set_label (d, dict_get_label (s));
217   dict_set_documents (d, dict_get_documents (s));
218
219   d->vector_cnt = s->vector_cnt;
220   d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
221   for (i = 0; i < s->vector_cnt; i++)
222     d->vector[i] = vector_clone (s->vector[i], s, d);
223
224   if ( s->encoding)
225     d->encoding = xstrdup (s->encoding);
226
227   dict_set_attributes (d, dict_get_attributes (s));
228
229   return d;
230 }
231
232 /* Clears the contents from a dictionary without destroying the
233    dictionary itself. */
234 void
235 dict_clear (struct dictionary *d)
236 {
237   /* FIXME?  Should we really clear case_limit, label, documents?
238      Others are necessarily cleared by deleting all the variables.*/
239   assert (d != NULL);
240
241   while (d->var_cnt > 0 )
242     {
243       dict_delete_var (d, d->var[d->var_cnt - 1]);
244     }
245
246   free (d->var);
247   d->var = NULL;
248   d->var_cnt = d->var_cap = 0;
249   invalidate_proto (d);
250   hsh_clear (d->name_tab);
251   d->next_value_idx = 0;
252   dict_set_split_vars (d, NULL, 0);
253   dict_set_weight (d, NULL);
254   dict_set_filter (d, NULL);
255   d->case_limit = 0;
256   free (d->label);
257   d->label = NULL;
258   ds_destroy (&d->documents);
259   dict_clear_vectors (d);
260   attrset_clear (&d->attributes);
261 }
262
263 /* Destroys the aux data for every variable in D, by calling
264    var_clear_aux() for each variable. */
265 void
266 dict_clear_aux (struct dictionary *d)
267 {
268   int i;
269
270   assert (d != NULL);
271
272   for (i = 0; i < d->var_cnt; i++)
273     var_clear_aux (d->var[i]);
274 }
275
276 /* Clears a dictionary and destroys it. */
277 void
278 dict_destroy (struct dictionary *d)
279 {
280   if (d != NULL)
281     {
282       /* In general, we don't want callbacks occuring, if the dictionary
283          is being destroyed */
284       d->callbacks  = NULL ;
285
286       dict_clear (d);
287       hsh_destroy (d->name_tab);
288       attrset_destroy (&d->attributes);
289       free (d);
290     }
291 }
292
293 /* Returns the number of variables in D. */
294 size_t
295 dict_get_var_cnt (const struct dictionary *d)
296 {
297   assert (d != NULL);
298
299   return d->var_cnt;
300 }
301
302 /* Returns the variable in D with dictionary index IDX, which
303    must be between 0 and the count returned by
304    dict_get_var_cnt(), exclusive. */
305 struct variable *
306 dict_get_var (const struct dictionary *d, size_t idx)
307 {
308   assert (d != NULL);
309   assert (idx < d->var_cnt);
310
311   return d->var[idx];
312 }
313
314 /* Sets *VARS to an array of pointers to variables in D and *CNT
315    to the number of variables in *D.  All variables are returned
316    except for those, if any, in the classes indicated by EXCLUDE.
317    (There is no point in putting DC_SYSTEM in EXCLUDE as
318    dictionaries never include system variables.) */
319 void
320 dict_get_vars (const struct dictionary *d, const struct variable ***vars,
321                size_t *cnt, enum dict_class exclude)
322 {
323   dict_get_vars_mutable (d, (struct variable ***) vars, cnt, exclude);
324 }
325
326 /* Sets *VARS to an array of pointers to variables in D and *CNT
327    to the number of variables in *D.  All variables are returned
328    except for those, if any, in the classes indicated by EXCLUDE.
329    (There is no point in putting DC_SYSTEM in EXCLUDE as
330    dictionaries never include system variables.) */
331 void
332 dict_get_vars_mutable (const struct dictionary *d, struct variable ***vars,
333                        size_t *cnt, enum dict_class exclude)
334 {
335   size_t count;
336   size_t i;
337
338   assert (d != NULL);
339   assert (vars != NULL);
340   assert (cnt != NULL);
341   assert (exclude == (exclude & DC_ALL));
342
343   count = 0;
344   for (i = 0; i < d->var_cnt; i++)
345     {
346       enum dict_class class = var_get_dict_class (d->var[i]);
347       if (!(class & exclude))
348         count++;
349     }
350
351   *vars = xnmalloc (count, sizeof **vars);
352   *cnt = 0;
353   for (i = 0; i < d->var_cnt; i++)
354     {
355       enum dict_class class = var_get_dict_class (d->var[i]);
356       if (!(class & exclude))
357         (*vars)[(*cnt)++] = d->var[i];
358     }
359   assert (*cnt == count);
360 }
361
362 static struct variable *
363 add_var (struct dictionary *d, struct variable *v)
364 {
365   /* Add dictionary info to variable. */
366   struct vardict_info vdi;
367   vdi.case_index = d->next_value_idx;
368   vdi.dict_index = d->var_cnt;
369   vdi.dict = d;
370   var_set_vardict (v, &vdi);
371
372   /* Update dictionary. */
373   if (d->var_cnt >= d->var_cap)
374     {
375       d->var_cap = 8 + 2 * d->var_cap;
376       d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var);
377     }
378   d->var[d->var_cnt++] = v;
379   hsh_force_insert (d->name_tab, v);
380
381   if ( d->changed ) d->changed (d, d->changed_data);
382   if ( d->callbacks &&  d->callbacks->var_added )
383     d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
384
385   d->next_value_idx++;
386   invalidate_proto (d);
387
388   return v;
389 }
390
391 /* Creates and returns a new variable in D with the given NAME
392    and WIDTH.  Returns a null pointer if the given NAME would
393    duplicate that of an existing variable in the dictionary. */
394 struct variable *
395 dict_create_var (struct dictionary *d, const char *name, int width)
396 {
397   return (dict_lookup_var (d, name) == NULL
398           ? dict_create_var_assert (d, name, width)
399           : NULL);
400 }
401
402 /* Creates and returns a new variable in D with the given NAME
403    and WIDTH.  Assert-fails if the given NAME would duplicate
404    that of an existing variable in the dictionary. */
405 struct variable *
406 dict_create_var_assert (struct dictionary *d, const char *name, int width)
407 {
408   assert (dict_lookup_var (d, name) == NULL);
409   return add_var (d, var_create (name, width));
410 }
411
412 /* Creates and returns a new variable in D with name NAME, as a
413    copy of existing variable OLD_VAR, which need not be in D or
414    in any dictionary.  Returns a null pointer if the given NAME
415    would duplicate that of an existing variable in the
416    dictionary. */
417 struct variable *
418 dict_clone_var (struct dictionary *d, const struct variable *old_var,
419                 const char *name)
420 {
421   return (dict_lookup_var (d, name) == NULL
422           ? dict_clone_var_assert (d, old_var, name)
423           : NULL);
424 }
425
426 /* Creates and returns a new variable in D with name NAME, as a
427    copy of existing variable OLD_VAR, which need not be in D or
428    in any dictionary.  Assert-fails if the given NAME would
429    duplicate that of an existing variable in the dictionary. */
430 struct variable *
431 dict_clone_var_assert (struct dictionary *d, const struct variable *old_var,
432                        const char *name)
433 {
434   struct variable *new_var = var_clone (old_var);
435   assert (dict_lookup_var (d, name) == NULL);
436   var_set_name (new_var, name);
437   return add_var (d, new_var);
438 }
439
440 /* Returns the variable named NAME in D, or a null pointer if no
441    variable has that name. */
442 struct variable *
443 dict_lookup_var (const struct dictionary *d, const char *name)
444 {
445   struct variable *target ;
446   struct variable *result ;
447
448   if ( ! var_is_plausible_name (name, false))
449     return NULL;
450
451   target = var_create (name, 0);
452   result = hsh_find (d->name_tab, target);
453   var_destroy (target);
454
455   if ( result && var_has_vardict (result)) 
456   {
457       const struct vardict_info *vdi = var_get_vardict (result);
458       assert (vdi->dict == d);
459   }
460
461   return result;
462 }
463
464 /* Returns the variable named NAME in D.  Assert-fails if no
465    variable has that name. */
466 struct variable *
467 dict_lookup_var_assert (const struct dictionary *d, const char *name)
468 {
469   struct variable *v = dict_lookup_var (d, name);
470   assert (v != NULL);
471   return v;
472 }
473
474 /* Returns true if variable V is in dictionary D,
475    false otherwise. */
476 bool
477 dict_contains_var (const struct dictionary *d, const struct variable *v)
478 {
479   if (var_has_vardict (v))
480     {
481       const struct vardict_info *vdi = var_get_vardict (v);
482       return (vdi->dict_index >= 0
483               && vdi->dict_index < d->var_cnt
484               && d->var[vdi->dict_index] == v);
485     }
486   else
487     return false;
488 }
489
490 /* Compares two double pointers to variables, which should point
491    to elements of a struct dictionary's `var' member array. */
492 static int
493 compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED)
494 {
495   struct variable *const *a = a_;
496   struct variable *const *b = b_;
497
498   return *a < *b ? -1 : *a > *b;
499 }
500
501 /* Sets the dict_index in V's vardict to DICT_INDEX. */
502 static void
503 set_var_dict_index (struct variable *v, int dict_index)
504 {
505   struct vardict_info vdi = *var_get_vardict (v);
506   struct dictionary *d = vdi.dict;
507   vdi.dict_index = dict_index;
508   var_set_vardict (v, &vdi);
509
510   if ( d->changed ) d->changed (d, d->changed_data);
511   if ( d->callbacks &&  d->callbacks->var_changed )
512     d->callbacks->var_changed (d, dict_index, d->cb_data);
513 }
514
515 /* Sets the case_index in V's vardict to CASE_INDEX. */
516 static void
517 set_var_case_index (struct variable *v, int case_index)
518 {
519   struct vardict_info vdi = *var_get_vardict (v);
520   vdi.case_index = case_index;
521   var_set_vardict (v, &vdi);
522 }
523
524 /* Re-sets the dict_index in the dictionary variables with
525    indexes from FROM to TO (exclusive). */
526 static void
527 reindex_vars (struct dictionary *d, size_t from, size_t to)
528 {
529   size_t i;
530
531   for (i = from; i < to; i++)
532     set_var_dict_index (d->var[i], i);
533 }
534
535 /* Deletes variable V from dictionary D and frees V.
536
537    This is a very bad idea if there might be any pointers to V
538    from outside D.  In general, no variable in the active file's
539    dictionary should be deleted when any transformations are
540    active on the dictionary's dataset, because those
541    transformations might reference the deleted variable.  The
542    safest time to delete a variable is just after a procedure has
543    been executed, as done by DELETE VARIABLES.
544
545    Pointers to V within D are not a problem, because
546    dict_delete_var() knows to remove V from split variables,
547    weights, filters, etc. */
548 void
549 dict_delete_var (struct dictionary *d, struct variable *v)
550 {
551   int dict_index = var_get_dict_index (v);
552   const int case_index = var_get_case_index (v);
553   const int width = var_get_width (v);
554
555   assert (dict_contains_var (d, v));
556
557   /* Delete aux data. */
558   var_clear_aux (v);
559
560   dict_unset_split_var (d, v);
561
562   if (d->weight == v)
563     dict_set_weight (d, NULL);
564
565   if (d->filter == v)
566     dict_set_filter (d, NULL);
567
568   dict_clear_vectors (d);
569
570   /* Remove V from var array. */
571   remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
572   d->var_cnt--;
573
574   /* Update dict_index for each affected variable. */
575   reindex_vars (d, dict_index, d->var_cnt);
576
577   /* Update name hash. */
578   hsh_force_delete (d->name_tab, v);
579
580
581   /* Free memory. */
582   var_clear_vardict (v);
583   var_destroy (v);
584
585   if ( d->changed ) d->changed (d, d->changed_data);
586
587   invalidate_proto (d);
588   if (d->callbacks &&  d->callbacks->var_deleted )
589     d->callbacks->var_deleted (d, dict_index, case_index, width, d->cb_data);
590 }
591
592 /* Deletes the COUNT variables listed in VARS from D.  This is
593    unsafe; see the comment on dict_delete_var() for details. */
594 void
595 dict_delete_vars (struct dictionary *d,
596                   struct variable *const *vars, size_t count)
597 {
598   /* FIXME: this can be done in O(count) time, but this algorithm
599      is O(count**2). */
600   assert (d != NULL);
601   assert (count == 0 || vars != NULL);
602
603   while (count-- > 0)
604     dict_delete_var (d, *vars++);
605 }
606
607 /* Deletes the COUNT variables in D starting at index IDX.  This
608    is unsafe; see the comment on dict_delete_var() for
609    details. */
610 void
611 dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
612 {
613   /* FIXME: this can be done in O(count) time, but this algorithm
614      is O(count**2). */
615   assert (idx + count <= d->var_cnt);
616
617   while (count-- > 0)
618     dict_delete_var (d, d->var[idx]);
619 }
620
621 /* Deletes scratch variables from dictionary D. */
622 void
623 dict_delete_scratch_vars (struct dictionary *d)
624 {
625   int i;
626
627   /* FIXME: this can be done in O(count) time, but this algorithm
628      is O(count**2). */
629   assert (d != NULL);
630
631   for (i = 0; i < d->var_cnt; )
632     if (var_get_dict_class (d->var[i]) == DC_SCRATCH)
633       dict_delete_var (d, d->var[i]);
634     else
635       i++;
636 }
637
638 /* Moves V to 0-based position IDX in D.  Other variables in D,
639    if any, retain their relative positions.  Runs in time linear
640    in the distance moved. */
641 void
642 dict_reorder_var (struct dictionary *d, struct variable *v, size_t new_index)
643 {
644   size_t old_index = var_get_dict_index (v);
645
646   assert (new_index < d->var_cnt);
647   move_element (d->var, d->var_cnt, sizeof *d->var, old_index, new_index);
648   reindex_vars (d, MIN (old_index, new_index), MAX (old_index, new_index) + 1);
649 }
650
651 /* Reorders the variables in D, placing the COUNT variables
652    listed in ORDER in that order at the beginning of D.  The
653    other variables in D, if any, retain their relative
654    positions. */
655 void
656 dict_reorder_vars (struct dictionary *d,
657                    struct variable *const *order, size_t count)
658 {
659   struct variable **new_var;
660   size_t i;
661
662   assert (d != NULL);
663   assert (count == 0 || order != NULL);
664   assert (count <= d->var_cnt);
665
666   new_var = xnmalloc (d->var_cnt, sizeof *new_var);
667   memcpy (new_var, order, count * sizeof *new_var);
668   for (i = 0; i < count; i++)
669     {
670       size_t index = var_get_dict_index (order[i]);
671       assert (d->var[index] == order[i]);
672       d->var[index] = NULL;
673       set_var_dict_index (order[i], i);
674     }
675   for (i = 0; i < d->var_cnt; i++)
676     if (d->var[i] != NULL)
677       {
678         assert (count < d->var_cnt);
679         new_var[count] = d->var[i];
680         set_var_dict_index (new_var[count], count);
681         count++;
682       }
683   free (d->var);
684   d->var = new_var;
685 }
686
687 /* Changes the name of variable V in dictionary D to NEW_NAME. */
688 static void
689 rename_var (struct dictionary *d, struct variable *v, const char *new_name)
690 {
691   struct vardict_info vdi;
692
693   assert (dict_contains_var (d, v));
694
695   vdi = *var_get_vardict (v);
696   var_clear_vardict (v);
697   var_set_name (v, new_name);
698   var_set_vardict (v, &vdi);
699 }
700
701 /* Changes the name of V in D to name NEW_NAME.  Assert-fails if
702    a variable named NEW_NAME is already in D, except that
703    NEW_NAME may be the same as V's existing name. */
704 void
705 dict_rename_var (struct dictionary *d, struct variable *v,
706                  const char *new_name)
707 {
708   assert (!strcasecmp (var_get_name (v), new_name)
709           || dict_lookup_var (d, new_name) == NULL);
710
711   hsh_force_delete (d->name_tab, v);
712   rename_var (d, v, new_name);
713   hsh_force_insert (d->name_tab, v);
714
715   if (settings_get_algorithm () == ENHANCED)
716     var_clear_short_names (v);
717
718   if ( d->changed ) d->changed (d, d->changed_data);
719   if ( d->callbacks &&  d->callbacks->var_changed )
720     d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
721 }
722
723 /* Renames COUNT variables specified in VARS to the names given
724    in NEW_NAMES within dictionary D.  If the renaming would
725    result in a duplicate variable name, returns false and stores a
726    name that would be duplicated into *ERR_NAME (if ERR_NAME is
727    non-null).  Otherwise, the renaming is successful, and true
728    is returned. */
729 bool
730 dict_rename_vars (struct dictionary *d,
731                   struct variable **vars, char **new_names, size_t count,
732                   char **err_name)
733 {
734   struct pool *pool;
735   char **old_names;
736   size_t i;
737
738   assert (count == 0 || vars != NULL);
739   assert (count == 0 || new_names != NULL);
740
741   /* Save the names of the variables to be renamed. */
742   pool = pool_create ();
743   old_names = pool_nalloc (pool, count, sizeof *old_names);
744   for (i = 0; i < count; i++)
745     old_names[i] = pool_strdup (pool, var_get_name (vars[i]));
746
747   /* Remove the variables to be renamed from the name hash,
748      and rename them. */
749   for (i = 0; i < count; i++)
750     {
751       hsh_force_delete (d->name_tab, vars[i]);
752       rename_var (d, vars[i], new_names[i]);
753     }
754
755   /* Add the renamed variables back into the name hash,
756      checking for conflicts. */
757   for (i = 0; i < count; i++)
758     if (hsh_insert (d->name_tab, vars[i]) != NULL)
759       {
760         /* There is a name conflict.
761            Back out all the name changes that have already
762            taken place, and indicate failure. */
763         size_t fail_idx = i;
764         if (err_name != NULL)
765           *err_name = new_names[i];
766
767         for (i = 0; i < fail_idx; i++)
768           hsh_force_delete (d->name_tab, vars[i]);
769
770         for (i = 0; i < count; i++)
771           {
772             rename_var (d, vars[i], old_names[i]);
773             hsh_force_insert (d->name_tab, vars[i]);
774           }
775
776         pool_destroy (pool);
777         return false;
778       }
779
780   /* Clear short names. */
781   if (settings_get_algorithm () == ENHANCED)
782     for (i = 0; i < count; i++)
783       var_clear_short_names (vars[i]);
784
785   pool_destroy (pool);
786   return true;
787 }
788
789 /* Returns true if a variable named NAME may be inserted in DICT;
790    that is, if there is not already a variable with that name in
791    DICT and if NAME is not a reserved word.  (The caller's checks
792    have already verified that NAME is otherwise acceptable as a
793    variable name.) */
794 static bool
795 var_name_is_insertable (const struct dictionary *dict, const char *name)
796 {
797   return (dict_lookup_var (dict, name) == NULL
798           && lex_id_to_token (ss_cstr (name)) == T_ID);
799 }
800
801 static bool
802 make_hinted_name (const struct dictionary *dict, const char *hint,
803                   char name[VAR_NAME_LEN + 1])
804 {
805   bool dropped = false;
806   char *cp;
807
808   for (cp = name; *hint && cp < name + VAR_NAME_LEN; hint++)
809     {
810       if (cp == name
811           ? lex_is_id1 (*hint) && *hint != '$'
812           : lex_is_idn (*hint))
813         {
814           if (dropped)
815             {
816               *cp++ = '_';
817               dropped = false;
818             }
819           if (cp < name + VAR_NAME_LEN)
820             *cp++ = *hint;
821         }
822       else if (cp > name)
823         dropped = true;
824     }
825   *cp = '\0';
826
827   if (name[0] != '\0')
828     {
829       size_t len = strlen (name);
830       unsigned long int i;
831
832       if (var_name_is_insertable (dict, name))
833         return true;
834
835       for (i = 0; i < ULONG_MAX; i++)
836         {
837           char suffix[INT_BUFSIZE_BOUND (i) + 1];
838           int ofs;
839
840           suffix[0] = '_';
841           if (!str_format_26adic (i + 1, &suffix[1], sizeof suffix - 1))
842             NOT_REACHED ();
843
844           ofs = MIN (VAR_NAME_LEN - strlen (suffix), len);
845           strcpy (&name[ofs], suffix);
846
847           if (var_name_is_insertable (dict, name))
848             return true;
849         }
850     }
851
852   return false;
853 }
854
855 static bool
856 make_numeric_name (const struct dictionary *dict, unsigned long int *num_start,
857                    char name[VAR_NAME_LEN + 1])
858 {
859   unsigned long int number;
860
861   for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
862        number < ULONG_MAX;
863        number++)
864     {
865       sprintf (name, "VAR%03lu", number);
866       if (dict_lookup_var (dict, name) == NULL)
867         {
868           if (num_start != NULL)
869             *num_start = number + 1;
870           return true;
871         }
872     }
873
874   if (num_start != NULL)
875     *num_start = ULONG_MAX;
876   return false;
877 }
878
879
880 /* Attempts to devise a variable name unique within DICT.
881    Returns true if successful, in which case the new variable
882    name is stored into NAME.  Returns false if all names that can
883    be generated have already been taken.  (Returning false is
884    quite unlikely: at least ULONG_MAX unique names can be
885    generated.)
886
887    HINT, if it is non-null, is used as a suggestion that will be
888    modified for suitability as a variable name and for
889    uniqueness.
890
891    If HINT is null or entirely unsuitable, a name in the form
892    "VAR%03d" will be generated, where the smallest unused integer
893    value is used.  If NUM_START is non-null, then its value is
894    used as the minimum numeric value to check, and it is updated
895    to the next value to be checked.
896    */
897 bool
898 dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
899                            unsigned long int *num_start,
900                            char name[VAR_NAME_LEN + 1])
901 {
902   return ((hint != NULL && make_hinted_name (dict, hint, name))
903           || make_numeric_name (dict, num_start, name));
904 }
905
906 /* Returns the weighting variable in dictionary D, or a null
907    pointer if the dictionary is unweighted. */
908 struct variable *
909 dict_get_weight (const struct dictionary *d)
910 {
911   assert (d != NULL);
912   assert (d->weight == NULL || dict_contains_var (d, d->weight));
913
914   return d->weight;
915 }
916
917 /* Returns the value of D's weighting variable in case C, except
918    that a negative weight is returned as 0.  Returns 1 if the
919    dictionary is unweighted.  Will warn about missing, negative,
920    or zero values if *WARN_ON_INVALID is true.  The function will
921    set *WARN_ON_INVALID to false if an invalid weight is
922    found. */
923 double
924 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
925                       bool *warn_on_invalid)
926 {
927   assert (d != NULL);
928   assert (c != NULL);
929
930   if (d->weight == NULL)
931     return 1.0;
932   else
933     {
934       double w = case_num (c, d->weight);
935       if (w < 0.0 || var_is_num_missing (d->weight, w, MV_ANY))
936         w = 0.0;
937       if ( w == 0.0 && warn_on_invalid != NULL && *warn_on_invalid ) {
938           *warn_on_invalid = false;
939           msg (SW, _("At least one case in the data file had a weight value "
940                      "that was user-missing, system-missing, zero, or "
941                      "negative.  These case(s) were ignored."));
942       }
943       return w;
944     }
945 }
946
947 /* Sets the weighting variable of D to V, or turning off
948    weighting if V is a null pointer. */
949 void
950 dict_set_weight (struct dictionary *d, struct variable *v)
951 {
952   assert (d != NULL);
953   assert (v == NULL || dict_contains_var (d, v));
954   assert (v == NULL || var_is_numeric (v));
955
956   d->weight = v;
957
958   if (d->changed) d->changed (d, d->changed_data);
959   if ( d->callbacks &&  d->callbacks->weight_changed )
960     d->callbacks->weight_changed (d,
961                                   v ? var_get_dict_index (v) : -1,
962                                   d->cb_data);
963 }
964
965 /* Returns the filter variable in dictionary D (see cmd_filter())
966    or a null pointer if the dictionary is unfiltered. */
967 struct variable *
968 dict_get_filter (const struct dictionary *d)
969 {
970   assert (d != NULL);
971   assert (d->filter == NULL || dict_contains_var (d, d->filter));
972
973   return d->filter;
974 }
975
976 /* Sets V as the filter variable for dictionary D.  Passing a
977    null pointer for V turn off filtering. */
978 void
979 dict_set_filter (struct dictionary *d, struct variable *v)
980 {
981   assert (d != NULL);
982   assert (v == NULL || dict_contains_var (d, v));
983   assert (v == NULL || var_is_numeric (v));
984
985   d->filter = v;
986
987   if (d->changed) d->changed (d, d->changed_data);
988   if ( d->callbacks && d->callbacks->filter_changed )
989     d->callbacks->filter_changed (d,
990                                   v ? var_get_dict_index (v) : -1,
991                                   d->cb_data);
992 }
993
994 /* Returns the case limit for dictionary D, or zero if the number
995    of cases is unlimited. */
996 casenumber
997 dict_get_case_limit (const struct dictionary *d)
998 {
999   assert (d != NULL);
1000
1001   return d->case_limit;
1002 }
1003
1004 /* Sets CASE_LIMIT as the case limit for dictionary D.  Use
1005    0 for CASE_LIMIT to indicate no limit. */
1006 void
1007 dict_set_case_limit (struct dictionary *d, casenumber case_limit)
1008 {
1009   assert (d != NULL);
1010
1011   d->case_limit = case_limit;
1012 }
1013
1014 /* Returns the prototype used for cases created by dictionary D. */
1015 const struct caseproto *
1016 dict_get_proto (const struct dictionary *d_)
1017 {
1018   struct dictionary *d = (struct dictionary *) d_;
1019   if (d->proto == NULL)
1020     {
1021       size_t i;
1022
1023       d->proto = caseproto_create ();
1024       d->proto = caseproto_reserve (d->proto, d->var_cnt);
1025       for (i = 0; i < d->var_cnt; i++)
1026         d->proto = caseproto_set_width (d->proto,
1027                                         var_get_case_index (d->var[i]),
1028                                         var_get_width (d->var[i]));
1029     }
1030   return d->proto;
1031 }
1032
1033 /* Returns the case index of the next value to be added to D.
1034    This value is the number of `union value's that need to be
1035    allocated to store a case for dictionary D. */
1036 int
1037 dict_get_next_value_idx (const struct dictionary *d)
1038 {
1039   assert (d != NULL);
1040
1041   return d->next_value_idx;
1042 }
1043
1044 /* Returns the number of bytes needed to store a case for
1045    dictionary D. */
1046 size_t
1047 dict_get_case_size (const struct dictionary *d)
1048 {
1049   assert (d != NULL);
1050
1051   return sizeof (union value) * dict_get_next_value_idx (d);
1052 }
1053
1054 /* Reassigns values in dictionary D so that fragmentation is
1055    eliminated. */
1056 void
1057 dict_compact_values (struct dictionary *d)
1058 {
1059   size_t i;
1060
1061   d->next_value_idx = 0;
1062   for (i = 0; i < d->var_cnt; i++)
1063     {
1064       struct variable *v = d->var[i];
1065       set_var_case_index (v, d->next_value_idx++);
1066     }
1067   invalidate_proto (d);
1068 }
1069
1070 /* Returns the number of values occupied by the variables in
1071    dictionary D.  All variables are considered if EXCLUDE_CLASSES
1072    is 0, or it may contain one or more of (1u << DC_ORDINARY),
1073    (1u << DC_SYSTEM), or (1u << DC_SCRATCH) to exclude the
1074    corresponding type of variable.
1075
1076    The return value may be less than the number of values in one
1077    of dictionary D's cases (as returned by
1078    dict_get_next_value_idx) even if E is 0, because there may be
1079    gaps in D's cases due to deleted variables. */
1080 size_t
1081 dict_count_values (const struct dictionary *d, unsigned int exclude_classes)
1082 {
1083   size_t i;
1084   size_t cnt;
1085
1086   assert ((exclude_classes & ~((1u << DC_ORDINARY)
1087                                | (1u << DC_SYSTEM)
1088                                | (1u << DC_SCRATCH))) == 0);
1089
1090   cnt = 0;
1091   for (i = 0; i < d->var_cnt; i++)
1092     {
1093       enum dict_class class = var_get_dict_class (d->var[i]);
1094       if (!(exclude_classes & (1u << class)))
1095         cnt++;
1096     }
1097   return cnt;
1098 }
1099
1100 /* Returns the case prototype that would result after deleting
1101    all variables from D that are not in one of the
1102    EXCLUDE_CLASSES and compacting the dictionary with
1103    dict_compact().
1104
1105    The caller must unref the returned caseproto when it is no
1106    longer needed. */
1107 struct caseproto *
1108 dict_get_compacted_proto (const struct dictionary *d,
1109                           unsigned int exclude_classes)
1110 {
1111   struct caseproto *proto;
1112   size_t i;
1113
1114   assert ((exclude_classes & ~((1u << DC_ORDINARY)
1115                                | (1u << DC_SYSTEM)
1116                                | (1u << DC_SCRATCH))) == 0);
1117
1118   proto = caseproto_create ();
1119   for (i = 0; i < d->var_cnt; i++)
1120     {
1121       struct variable *v = d->var[i];
1122       if (!(exclude_classes & (1u << var_get_dict_class (v))))
1123         proto = caseproto_add_width (proto, var_get_width (v));
1124     }
1125   return proto;
1126 }
1127 \f
1128 /* Returns the SPLIT FILE vars (see cmd_split_file()).  Call
1129    dict_get_split_cnt() to determine how many SPLIT FILE vars
1130    there are.  Returns a null pointer if and only if there are no
1131    SPLIT FILE vars. */
1132 const struct variable *const *
1133 dict_get_split_vars (const struct dictionary *d)
1134 {
1135   assert (d != NULL);
1136
1137   return d->split;
1138 }
1139
1140 /* Returns the number of SPLIT FILE vars. */
1141 size_t
1142 dict_get_split_cnt (const struct dictionary *d)
1143 {
1144   assert (d != NULL);
1145
1146   return d->split_cnt;
1147 }
1148
1149 /* Removes variable V, which must be in D, from D's set of split
1150    variables. */
1151 void
1152 dict_unset_split_var (struct dictionary *d, struct variable *v)
1153 {
1154   int orig_count;
1155
1156   assert (dict_contains_var (d, v));
1157
1158   orig_count = d->split_cnt;
1159   d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
1160                                &v, compare_var_ptrs, NULL);
1161   if (orig_count != d->split_cnt)
1162     {
1163       if (d->changed) d->changed (d, d->changed_data);
1164       /* We changed the set of split variables so invoke the
1165          callback. */
1166       if (d->callbacks &&  d->callbacks->split_changed)
1167         d->callbacks->split_changed (d, d->cb_data);
1168     }
1169 }
1170
1171 /* Sets CNT split vars SPLIT in dictionary D. */
1172 void
1173 dict_set_split_vars (struct dictionary *d,
1174                      struct variable *const *split, size_t cnt)
1175 {
1176   assert (d != NULL);
1177   assert (cnt == 0 || split != NULL);
1178
1179   d->split_cnt = cnt;
1180   if ( cnt > 0 )
1181    {
1182     d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
1183     memcpy (d->split, split, cnt * sizeof *d->split);
1184    }
1185   else
1186    {
1187     free (d->split);
1188     d->split = NULL;
1189    }
1190
1191   if (d->changed) d->changed (d, d->changed_data);
1192   if ( d->callbacks &&  d->callbacks->split_changed )
1193     d->callbacks->split_changed (d, d->cb_data);
1194 }
1195
1196 /* Returns the file label for D, or a null pointer if D is
1197    unlabeled (see cmd_file_label()). */
1198 const char *
1199 dict_get_label (const struct dictionary *d)
1200 {
1201   assert (d != NULL);
1202
1203   return d->label;
1204 }
1205
1206 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1207    characters. */
1208 void
1209 dict_set_label (struct dictionary *d, const char *label)
1210 {
1211   assert (d != NULL);
1212
1213   free (d->label);
1214   d->label = label != NULL ? xstrndup (label, 60) : NULL;
1215 }
1216
1217 /* Returns the documents for D, or a null pointer if D has no
1218    documents.  If the return value is nonnull, then the string
1219    will be an exact multiple of DOC_LINE_LENGTH bytes in length,
1220    with each segment corresponding to one line. */
1221 const char *
1222 dict_get_documents (const struct dictionary *d)
1223 {
1224   return ds_is_empty (&d->documents) ? NULL : ds_cstr (&d->documents);
1225 }
1226
1227 /* Sets the documents for D to DOCUMENTS, or removes D's
1228    documents if DOCUMENT is a null pointer.  If DOCUMENTS is
1229    nonnull, then it should be an exact multiple of
1230    DOC_LINE_LENGTH bytes in length, with each segment
1231    corresponding to one line. */
1232 void
1233 dict_set_documents (struct dictionary *d, const char *documents)
1234 {
1235   size_t remainder;
1236
1237   ds_assign_cstr (&d->documents, documents != NULL ? documents : "");
1238
1239   /* In case the caller didn't get it quite right, pad out the
1240      final line with spaces. */
1241   remainder = ds_length (&d->documents) % DOC_LINE_LENGTH;
1242   if (remainder != 0)
1243     ds_put_char_multiple (&d->documents, ' ', DOC_LINE_LENGTH - remainder);
1244 }
1245
1246 /* Drops the documents from dictionary D. */
1247 void
1248 dict_clear_documents (struct dictionary *d)
1249 {
1250   ds_clear (&d->documents);
1251 }
1252
1253 /* Appends LINE to the documents in D.  LINE will be truncated or
1254    padded on the right with spaces to make it exactly
1255    DOC_LINE_LENGTH bytes long. */
1256 void
1257 dict_add_document_line (struct dictionary *d, const char *line)
1258 {
1259   if (strlen (line) > DOC_LINE_LENGTH)
1260     {
1261       /* Note to translators: "bytes" is correct, not characters */
1262       msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH);
1263     }
1264   buf_copy_str_rpad (ds_put_uninit (&d->documents, DOC_LINE_LENGTH),
1265                      DOC_LINE_LENGTH, line, ' ');
1266 }
1267
1268 /* Returns the number of document lines in dictionary D. */
1269 size_t
1270 dict_get_document_line_cnt (const struct dictionary *d)
1271 {
1272   return ds_length (&d->documents) / DOC_LINE_LENGTH;
1273 }
1274
1275 /* Copies document line number IDX from dictionary D into
1276    LINE, trimming off any trailing white space. */
1277 void
1278 dict_get_document_line (const struct dictionary *d,
1279                         size_t idx, struct string *line)
1280 {
1281   assert (idx < dict_get_document_line_cnt (d));
1282   ds_assign_substring (line, ds_substr (&d->documents, idx * DOC_LINE_LENGTH,
1283                                         DOC_LINE_LENGTH));
1284   ds_rtrim (line, ss_cstr (CC_SPACES));
1285 }
1286
1287 /* Creates in D a vector named NAME that contains the CNT
1288    variables in VAR.  Returns true if successful, or false if a
1289    vector named NAME already exists in D. */
1290 bool
1291 dict_create_vector (struct dictionary *d,
1292                     const char *name,
1293                     struct variable **var, size_t cnt)
1294 {
1295   size_t i;
1296
1297   assert (var != NULL);
1298   assert (cnt > 0);
1299   for (i = 0; i < cnt; i++)
1300     assert (dict_contains_var (d, var[i]));
1301
1302   if (dict_lookup_vector (d, name) == NULL)
1303     {
1304       d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
1305       d->vector[d->vector_cnt++] = vector_create (name, var, cnt);
1306       return true;
1307     }
1308   else
1309     return false;
1310 }
1311
1312 /* Creates in D a vector named NAME that contains the CNT
1313    variables in VAR.  A vector named NAME must not already exist
1314    in D. */
1315 void
1316 dict_create_vector_assert (struct dictionary *d,
1317                            const char *name,
1318                            struct variable **var, size_t cnt)
1319 {
1320   assert (dict_lookup_vector (d, name) == NULL);
1321   dict_create_vector (d, name, var, cnt);
1322 }
1323
1324 /* Returns the vector in D with index IDX, which must be less
1325    than dict_get_vector_cnt (D). */
1326 const struct vector *
1327 dict_get_vector (const struct dictionary *d, size_t idx)
1328 {
1329   assert (d != NULL);
1330   assert (idx < d->vector_cnt);
1331
1332   return d->vector[idx];
1333 }
1334
1335 /* Returns the number of vectors in D. */
1336 size_t
1337 dict_get_vector_cnt (const struct dictionary *d)
1338 {
1339   assert (d != NULL);
1340
1341   return d->vector_cnt;
1342 }
1343
1344 /* Looks up and returns the vector within D with the given
1345    NAME. */
1346 const struct vector *
1347 dict_lookup_vector (const struct dictionary *d, const char *name)
1348 {
1349   size_t i;
1350   for (i = 0; i < d->vector_cnt; i++)
1351     if (!strcasecmp (vector_get_name (d->vector[i]), name))
1352       return d->vector[i];
1353   return NULL;
1354 }
1355
1356 /* Deletes all vectors from D. */
1357 void
1358 dict_clear_vectors (struct dictionary *d)
1359 {
1360   size_t i;
1361
1362   for (i = 0; i < d->vector_cnt; i++)
1363     vector_destroy (d->vector[i]);
1364   free (d->vector);
1365
1366   d->vector = NULL;
1367   d->vector_cnt = 0;
1368 }
1369
1370 /* Returns D's attribute set.  The caller may examine or modify
1371    the attribute set, but must not destroy it.  Destroying D or
1372    calling dict_set_attributes for D will also destroy D's
1373    attribute set. */
1374 struct attrset *
1375 dict_get_attributes (const struct dictionary *d) 
1376 {
1377   return (struct attrset *) &d->attributes;
1378 }
1379
1380 /* Replaces D's attributes set by a copy of ATTRS. */
1381 void
1382 dict_set_attributes (struct dictionary *d, const struct attrset *attrs)
1383 {
1384   attrset_destroy (&d->attributes);
1385   attrset_clone (&d->attributes, attrs);
1386 }
1387
1388 /* Returns true if D has at least one attribute in its attribute
1389    set, false if D's attribute set is empty. */
1390 bool
1391 dict_has_attributes (const struct dictionary *d) 
1392 {
1393   return attrset_count (&d->attributes) > 0;
1394 }
1395
1396 /* Called from variable.c to notify the dictionary that some property of
1397    the variable has changed */
1398 void
1399 dict_var_changed (const struct variable *v)
1400 {
1401   if ( var_has_vardict (v))
1402     {
1403       const struct vardict_info *vdi = var_get_vardict (v);
1404       struct dictionary *d = vdi->dict;
1405
1406       if ( NULL == d)
1407         return;
1408
1409       if (d->changed ) d->changed (d, d->changed_data);
1410       if ( d->callbacks && d->callbacks->var_changed )
1411         d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);
1412     }
1413 }
1414
1415
1416 /* Called from variable.c to notify the dictionary that the variable's width
1417    has changed */
1418 void
1419 dict_var_resized (const struct variable *v, int old_width)
1420 {
1421   if ( var_has_vardict (v))
1422     {
1423       const struct vardict_info *vdi = var_get_vardict (v);
1424       struct dictionary *d;
1425
1426       d = vdi->dict;
1427
1428       if (d->changed) d->changed (d, d->changed_data);
1429
1430       invalidate_proto (d);
1431       if ( d->callbacks && d->callbacks->var_resized )
1432         d->callbacks->var_resized (d, var_get_dict_index (v), old_width,
1433                                    d->cb_data);
1434     }
1435 }
1436
1437 /* Called from variable.c to notify the dictionary that the variable's display width
1438    has changed */
1439 void
1440 dict_var_display_width_changed (const struct variable *v)
1441 {
1442   if ( var_has_vardict (v))
1443     {
1444       const struct vardict_info *vdi = var_get_vardict (v);
1445       struct dictionary *d;
1446
1447       d = vdi->dict;
1448
1449       if (d->changed) d->changed (d, d->changed_data);
1450       if ( d->callbacks && d->callbacks->var_display_width_changed )
1451         d->callbacks->var_display_width_changed (d, var_get_dict_index (v), d->cb_data);
1452     }
1453 }
1454