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