Implement DELETE VARIABLES.
[pspp-builds.git] / src / language / lexer / variable-parser.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <language/lexer/variable-parser.h>
22
23 #include <ctype.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26
27 #include "lexer.h"
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/variable.h>
31 #include <libpspp/alloc.h>
32 #include <libpspp/assertion.h>
33 #include <libpspp/bit-vector.h>
34 #include <libpspp/hash.h>
35 #include <libpspp/message.h>
36 #include <libpspp/misc.h>
37 #include <libpspp/pool.h>
38 #include <libpspp/str.h>
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 /* Parses a name as a variable within VS.  Sets *IDX to the
44    variable's index and returns true if successful.  On failure
45    emits an error message and returns false. */
46 static bool
47 parse_vs_variable_idx (struct lexer *lexer, const struct var_set *vs, 
48                 size_t *idx)
49 {
50   assert (idx != NULL);
51   
52   if (lex_token (lexer) != T_ID)
53     {
54       lex_error (lexer, _("expecting variable name"));
55       return false;
56     }
57   else if (var_set_lookup_var_idx (vs, lex_tokid (lexer), idx)) 
58     {
59       lex_get (lexer);
60       return true;
61     }
62   else 
63     {
64       msg (SE, _("%s is not a variable name."), lex_tokid (lexer));
65       return false;
66     }
67 }
68
69 /* Parses a name as a variable within VS and returns the variable
70    if successful.  On failure emits an error message and returns
71    a null pointer. */
72 static struct variable *
73 parse_vs_variable (struct lexer *lexer, const struct var_set *vs)
74 {
75   size_t idx;
76   return parse_vs_variable_idx (lexer, vs, &idx) ? var_set_get_var (vs, idx) : NULL;
77 }
78
79 /* Parses a variable name in dictionary D and returns the
80    variable if successful.  On failure emits an error message and
81    returns a null pointer. */
82 struct variable *
83 parse_variable (struct lexer *lexer, const struct dictionary *d) 
84 {
85   struct var_set *vs = var_set_create_from_dict (d);
86   struct variable *var = parse_vs_variable (lexer, vs);
87   var_set_destroy (vs);
88   return var;
89 }
90
91 /* Parses a set of variables from dictionary D given options
92    OPTS.  Resulting list of variables stored in *VAR and the
93    number of variables into *CNT.  Returns true only if
94    successful. */
95 bool
96 parse_variables (struct lexer *lexer, const struct dictionary *d, 
97                         struct variable ***var,
98                         size_t *cnt, int opts) 
99 {
100   struct var_set *vs;
101   int success;
102
103   assert (d != NULL);
104   assert (var != NULL);
105   assert (cnt != NULL);
106
107   vs = var_set_create_from_dict (d);
108   success = parse_var_set_vars (lexer, vs, var, cnt, opts);
109   if ( success == 0 ) 
110     {
111       free ( *var ) ;
112       *var = NULL;
113       *cnt = 0;
114     }
115   var_set_destroy (vs);
116   return success;
117 }
118
119 /* Parses a set of variables from dictionary D given options
120    OPTS.  Resulting list of variables stored in *VARS and the
121    number of variables into *VAR_CNT.  Returns true only if
122    successful.  Same behavior as parse_variables, except that all
123    allocations are taken from the given POOL. */
124 bool
125 parse_variables_pool (struct lexer *lexer, struct pool *pool, 
126                 const struct dictionary *dict,
127                 struct variable ***vars, size_t *var_cnt, int opts) 
128 {
129   int retval;
130
131   /* PV_APPEND is unsafe because parse_variables would free the
132      existing names on failure, but those names are presumably
133      already in the pool, which would attempt to re-free it
134      later. */
135   assert (!(opts & PV_APPEND));
136   
137   retval = parse_variables (lexer, dict, vars, var_cnt, opts);
138   if (retval)
139     pool_register (pool, free, *vars);
140   return retval;
141 }
142
143 /* Parses a variable name from VS.  If successful, sets *IDX to
144    the variable's index in VS, *CLASS to the variable's
145    dictionary class, and returns true.  Returns false on
146    failure. */
147 static bool
148 parse_var_idx_class (struct lexer *lexer, const struct var_set *vs, 
149                         size_t *idx,
150                         enum dict_class *class)
151 {
152   if (!parse_vs_variable_idx (lexer, vs, idx))
153     return false;
154
155   *class = dict_class_from_id (var_get_name (var_set_get_var (vs, *idx)));
156   return true;
157 }
158
159 /* Add the variable from VS with index IDX to the list of
160    variables V that has *NV elements and room for *MV.
161    Uses and updates INCLUDED to avoid duplicates if indicated by
162    PV_OPTS, which also affects what variables are allowed in
163    appropriate ways. */
164 static void
165 add_variable (struct variable ***v, size_t *nv, size_t *mv,
166               char *included, int pv_opts,
167               const struct var_set *vs, size_t idx)
168 {
169   struct variable *add = var_set_get_var (vs, idx);
170   const char *add_name = var_get_name (add);
171
172   if ((pv_opts & PV_NUMERIC) && !var_is_numeric (add)) 
173     msg (SW, _("%s is not a numeric variable.  It will not be "
174                "included in the variable list."), add_name);
175   else if ((pv_opts & PV_STRING) && !var_is_alpha (add)) 
176     msg (SE, _("%s is not a string variable.  It will not be "
177                "included in the variable list."), add_name);
178   else if ((pv_opts & PV_NO_SCRATCH)
179            && dict_class_from_id (add_name) == DC_SCRATCH)
180     msg (SE, _("Scratch variables (such as %s) are not allowed "
181                "here."), add_name);
182   else if ((pv_opts & (PV_SAME_TYPE | PV_SAME_WIDTH)) && *nv
183            && var_get_type (add) != var_get_type ((*v)[0])) 
184     msg (SE, _("%s and %s are not the same type.  All variables in "
185                "this variable list must be of the same type.  %s "
186                "will be omitted from the list."),
187          var_get_name ((*v)[0]), add_name, add_name);
188   else if ((pv_opts & PV_SAME_WIDTH) && *nv
189            && var_get_width (add) != var_get_width ((*v)[0]))
190     msg (SE, _("%s and %s are string variables with different widths.  "
191                "All variables in this variable list must have the "
192                "same width.  %s will be omttied from the list."),
193          var_get_name ((*v)[0]), add_name, add_name);
194   else if ((pv_opts & PV_NO_DUPLICATE) && included[idx]) 
195     msg (SE, _("Variable %s appears twice in variable list."), add_name);
196   else if ((pv_opts & PV_DUPLICATE) || !included[idx])
197     {
198       if (*nv >= *mv)
199         {
200           *mv = 2 * (*nv + 1);
201           *v = xnrealloc (*v, *mv, sizeof **v);
202         }
203       (*v)[(*nv)++] = add;
204       if (included != NULL)
205         included[idx] = 1;
206     }
207 }
208
209 /* Adds the variables in VS with indexes FIRST_IDX through
210    LAST_IDX, inclusive, to the list of variables V that has *NV
211    elements and room for *MV.  Uses and updates INCLUDED to avoid
212    duplicates if indicated by PV_OPTS, which also affects what
213    variables are allowed in appropriate ways. */
214 static void
215 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
216                int pv_opts,
217                const struct var_set *vs, int first_idx, int last_idx,
218                enum dict_class class) 
219 {
220   size_t i;
221   
222   for (i = first_idx; i <= last_idx; i++)
223     if (dict_class_from_id (var_get_name (var_set_get_var (vs, i))) == class)
224       add_variable (v, nv, mv, included, pv_opts, vs, i);
225 }
226
227 /* Note that if parse_variables() returns false, *v is free()'d.
228    Conversely, if parse_variables() returns true, then *nv is
229    nonzero and *v is non-NULL. */
230 bool
231 parse_var_set_vars (struct lexer *lexer, const struct var_set *vs, 
232                     struct variable ***v, size_t *nv,
233                     int pv_opts)
234 {
235   size_t mv;
236   char *included;
237
238   assert (vs != NULL);
239   assert (v != NULL);
240   assert (nv != NULL);
241
242   /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE,
243      PV_SAME_WIDTH may be specified. */
244   assert (((pv_opts & PV_NUMERIC) != 0)
245           + ((pv_opts & PV_STRING) != 0)
246           + ((pv_opts & PV_SAME_TYPE) != 0)
247           + ((pv_opts & PV_SAME_WIDTH) != 0) <= 1);
248
249   /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
250   assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
251
252   if (!(pv_opts & PV_APPEND))
253     {
254       *v = NULL;
255       *nv = 0;
256       mv = 0;
257     }
258   else
259     mv = *nv;
260
261   if (!(pv_opts & PV_DUPLICATE))
262     {
263       size_t i;
264       
265       included = xcalloc (var_set_get_cnt (vs), sizeof *included);
266       for (i = 0; i < *nv; i++) 
267         {
268           size_t index;
269           if (!var_set_lookup_var_idx (vs, var_get_name ((*v)[i]), &index))
270             NOT_REACHED ();
271           included[index] = 1; 
272         }
273     }
274   else
275     included = NULL;
276
277   do
278     {
279       if (lex_match (lexer, T_ALL))
280         add_variables (v, nv, &mv, included, pv_opts,
281                        vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
282       else 
283         {
284           enum dict_class class;
285           size_t first_idx;
286
287           if (!parse_var_idx_class (lexer, vs, &first_idx, &class))
288             goto fail;
289
290           if (!lex_match (lexer, T_TO))
291             add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
292           else 
293             {
294               size_t last_idx;
295               enum dict_class last_class;
296               struct variable *first_var, *last_var;
297
298               if (!parse_var_idx_class (lexer, vs, &last_idx, &last_class))
299                 goto fail;
300
301               first_var = var_set_get_var (vs, first_idx);
302               last_var = var_set_get_var (vs, last_idx);
303
304               if (last_idx < first_idx)
305                 {
306                   const char *first_name = var_get_name (first_var);
307                   const char *last_name = var_get_name (last_var);
308                   msg (SE, _("%s TO %s is not valid syntax since %s "
309                              "precedes %s in the dictionary."),
310                        first_name, last_name, first_name, last_name);
311                   goto fail;
312                 }
313
314               if (class != last_class)
315                 {
316                   msg (SE, _("When using the TO keyword to specify several "
317                              "variables, both variables must be from "
318                              "the same variable dictionaries, of either "
319                              "ordinary, scratch, or system variables.  "
320                              "%s is a %s variable, whereas %s is %s."),
321                        var_get_name (first_var), dict_class_to_name (class),
322                        var_get_name (last_var),
323                        dict_class_to_name (last_class));
324                   goto fail;
325                 }
326
327               add_variables (v, nv, &mv, included, pv_opts,
328                              vs, first_idx, last_idx, class);
329             } 
330         }
331
332       if (pv_opts & PV_SINGLE)
333         break;
334       lex_match (lexer, ',');
335     }
336   while (lex_token (lexer) == T_ALL
337          || (lex_token (lexer) == T_ID && var_set_lookup_var (vs, lex_tokid (lexer)) != NULL));
338   
339   if (*nv == 0)
340     goto fail;
341
342   free (included);
343   return 1;
344
345 fail:
346   free (included);
347   free (*v);
348   *v = NULL;
349   *nv = 0;
350   return 0;
351 }
352
353 /* Extracts a numeric suffix from variable name S, copying it
354    into string R.  Sets *D to the length of R and *N to its
355    value. */
356 static int
357 extract_num (char *s, char *r, int *n, int *d)
358 {
359   char *cp;
360
361   /* Find first digit. */
362   cp = s + strlen (s) - 1;
363   while (isdigit ((unsigned char) *cp) && cp > s)
364     cp--;
365   cp++;
366
367   /* Extract root. */
368   strncpy (r, s, cp - s);
369   r[cp - s] = 0;
370
371   /* Count initial zeros. */
372   *n = *d = 0;
373   while (*cp == '0')
374     {
375       (*d)++;
376       cp++;
377     }
378
379   /* Extract value. */
380   while (isdigit ((unsigned char) *cp))
381     {
382       (*d)++;
383       *n = (*n * 10) + (*cp - '0');
384       cp++;
385     }
386
387   /* Sanity check. */
388   if (*n == 0 && *d == 0)
389     {
390       msg (SE, _("incorrect use of TO convention"));
391       return 0;
392     }
393   return 1;
394 }
395
396 /* Parses a list of variable names according to the DATA LIST version
397    of the TO convention.  */
398 bool
399 parse_DATA_LIST_vars (struct lexer *lexer, char ***names, size_t *nnames, int pv_opts)
400 {
401   int n1, n2;
402   int d1, d2;
403   int n;
404   size_t nvar, mvar;
405   char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
406   char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
407   int success = 0;
408
409   assert (names != NULL);
410   assert (nnames != NULL);
411   assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
412                        | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
413   /* FIXME: PV_NO_DUPLICATE is not implemented. */
414
415   if (pv_opts & PV_APPEND)
416     nvar = mvar = *nnames;
417   else
418     {
419       nvar = mvar = 0;
420       *names = NULL;
421     }
422
423   do
424     {
425       if (lex_token (lexer) != T_ID)
426         {
427           lex_error (lexer, "expecting variable name");
428           goto fail;
429         }
430       if (dict_class_from_id (lex_tokid (lexer)) == DC_SCRATCH
431           && (pv_opts & PV_NO_SCRATCH))
432         {
433           msg (SE, _("Scratch variables not allowed here."));
434           goto fail;
435         }
436       strcpy (name1, lex_tokid (lexer));
437       lex_get (lexer);
438       if (lex_token (lexer) == T_TO)
439         {
440           lex_get (lexer);
441           if (lex_token (lexer) != T_ID)
442             {
443               lex_error (lexer, "expecting variable name");
444               goto fail;
445             }
446           strcpy (name2, lex_tokid (lexer));
447           lex_get (lexer);
448
449           if (!extract_num (name1, root1, &n1, &d1)
450               || !extract_num (name2, root2, &n2, &d2))
451             goto fail;
452
453           if (strcasecmp (root1, root2))
454             {
455               msg (SE, _("Prefixes don't match in use of TO convention."));
456               goto fail;
457             }
458           if (n1 > n2)
459             {
460               msg (SE, _("Bad bounds in use of TO convention."));
461               goto fail;
462             }
463           if (d2 > d1)
464             d2 = d1;
465
466           if (mvar < nvar + (n2 - n1 + 1))
467             {
468               mvar += ROUND_UP (n2 - n1 + 1, 16);
469               *names = xnrealloc (*names, mvar, sizeof **names);
470             }
471
472           for (n = n1; n <= n2; n++)
473             {
474               char name[LONG_NAME_LEN + 1];
475               sprintf (name, "%s%0*d", root1, d1, n);
476               (*names)[nvar] = xstrdup (name);
477               nvar++;
478             }
479         }
480       else
481         {
482           if (nvar >= mvar)
483             {
484               mvar += 16;
485               *names = xnrealloc (*names, mvar, sizeof **names);
486             }
487           (*names)[nvar++] = xstrdup (name1);
488         }
489
490       lex_match (lexer, ',');
491
492       if (pv_opts & PV_SINGLE)
493         break;
494     }
495   while (lex_token (lexer) == T_ID);
496   success = 1;
497
498 fail:
499   *nnames = nvar;
500   if (!success)
501     {
502       int i;
503       for (i = 0; i < nvar; i++)
504         free ((*names)[i]);
505       free (*names);
506       *names = NULL;
507       *nnames = 0;
508     }
509   return success;
510 }
511
512 /* Registers each of the NAMES[0...NNAMES - 1] in POOL, as well
513    as NAMES itself. */
514 static void
515 register_vars_pool (struct pool *pool, char **names, size_t nnames)
516 {
517   size_t i;
518
519   for (i = 0; i < nnames; i++)
520     pool_register (pool, free, names[i]);
521   pool_register (pool, free, names);
522 }
523
524 /* Parses a list of variable names according to the DATA LIST
525    version of the TO convention.  Same args as
526    parse_DATA_LIST_vars(), except that all allocations are taken
527    from the given POOL. */
528 bool
529 parse_DATA_LIST_vars_pool (struct lexer *lexer, struct pool *pool,
530                            char ***names, size_t *nnames, int pv_opts)
531 {
532   int retval;
533
534   /* PV_APPEND is unsafe because parse_DATA_LIST_vars would free
535      the existing names on failure, but those names are
536      presumably already in the pool, which would attempt to
537      re-free it later. */
538   assert (!(pv_opts & PV_APPEND));
539   
540   retval = parse_DATA_LIST_vars (lexer, names, nnames, pv_opts);
541   if (retval)
542     register_vars_pool (pool, *names, *nnames);
543   return retval;
544 }
545
546 /* Parses a list of variables where some of the variables may be
547    existing and the rest are to be created.  Same args as
548    parse_DATA_LIST_vars(). */
549 bool
550 parse_mixed_vars (struct lexer *lexer, const struct dictionary *dict, 
551                   char ***names, size_t *nnames, int pv_opts)
552 {
553   size_t i;
554
555   assert (names != NULL);
556   assert (nnames != NULL);
557   assert ((pv_opts & ~PV_APPEND) == 0);
558
559   if (!(pv_opts & PV_APPEND))
560     {
561       *names = NULL;
562       *nnames = 0;
563     }
564   while (lex_token (lexer) == T_ID || lex_token (lexer) == T_ALL)
565     {
566       if (lex_token (lexer) == T_ALL || dict_lookup_var (dict, lex_tokid (lexer)) != NULL)
567         {
568           struct variable **v;
569           size_t nv;
570
571           if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
572             goto fail;
573           *names = xnrealloc (*names, *nnames + nv, sizeof **names);
574           for (i = 0; i < nv; i++)
575             (*names)[*nnames + i] = xstrdup (var_get_name (v[i]));
576           free (v);
577           *nnames += nv;
578         }
579       else if (!parse_DATA_LIST_vars (lexer, names, nnames, PV_APPEND))
580         goto fail;
581     }
582   return 1;
583
584 fail:
585   for (i = 0; i < *nnames; i++)
586     free ((*names)[i]);
587   free (*names);
588   *names = NULL;
589   *nnames = 0;
590   return 0;
591 }
592
593 /* Parses a list of variables where some of the variables may be
594    existing and the rest are to be created.  Same args as
595    parse_mixed_vars(), except that all allocations are taken
596    from the given POOL. */
597 bool
598 parse_mixed_vars_pool (struct lexer *lexer, const struct dictionary *dict, struct pool *pool,
599                        char ***names, size_t *nnames, int pv_opts)
600 {
601   int retval;
602
603   /* PV_APPEND is unsafe because parse_mixed_vars_pool would free
604      the existing names on failure, but those names are
605      presumably already in the pool, which would attempt to
606      re-free it later. */
607   assert (!(pv_opts & PV_APPEND));
608
609   retval = parse_mixed_vars (lexer, dict, names, nnames, pv_opts);
610   if (retval)
611     register_vars_pool (pool, *names, *nnames);
612   return retval;
613 }
614 \f
615 /* A set of variables. */
616 struct var_set 
617   {
618     size_t (*get_cnt) (const struct var_set *);
619     struct variable *(*get_var) (const struct var_set *, size_t idx);
620     bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
621     void (*destroy) (struct var_set *);
622     void *aux;
623   };
624
625 /* Returns the number of variables in VS. */
626 size_t
627 var_set_get_cnt (const struct var_set *vs) 
628 {
629   assert (vs != NULL);
630
631   return vs->get_cnt (vs);
632 }
633
634 /* Return variable with index IDX in VS.
635    IDX must be less than the number of variables in VS. */
636 struct variable *
637 var_set_get_var (const struct var_set *vs, size_t idx) 
638 {
639   assert (vs != NULL);
640   assert (idx < var_set_get_cnt (vs));
641
642   return vs->get_var (vs, idx);
643 }
644
645 /* Returns the variable in VS named NAME, or a null pointer if VS
646    contains no variable with that name. */
647 struct variable *
648 var_set_lookup_var (const struct var_set *vs, const char *name) 
649 {
650   size_t idx;
651   return (var_set_lookup_var_idx (vs, name, &idx)
652           ? var_set_get_var (vs, idx)
653           : NULL);
654 }
655
656 /* If VS contains a variable named NAME, sets *IDX to its index
657    and returns true.  Otherwise, returns false. */
658 bool
659 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
660                         size_t *idx)
661 {
662   assert (vs != NULL);
663   assert (name != NULL);
664   assert (strlen (name) <= LONG_NAME_LEN);
665
666   return vs->lookup_var_idx (vs, name, idx);
667 }
668
669 /* Destroys VS. */
670 void
671 var_set_destroy (struct var_set *vs) 
672 {
673   if (vs != NULL)
674     vs->destroy (vs);
675 }
676 \f
677 /* Returns the number of variables in VS. */
678 static size_t
679 dict_var_set_get_cnt (const struct var_set *vs) 
680 {
681   struct dictionary *d = vs->aux;
682
683   return dict_get_var_cnt (d);
684 }
685
686 /* Return variable with index IDX in VS.
687    IDX must be less than the number of variables in VS. */
688 static struct variable *
689 dict_var_set_get_var (const struct var_set *vs, size_t idx) 
690 {
691   struct dictionary *d = vs->aux;
692
693   return dict_get_var (d, idx);
694 }
695
696 /* If VS contains a variable named NAME, sets *IDX to its index
697    and returns true.  Otherwise, returns false. */
698 static bool
699 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
700                              size_t *idx) 
701 {
702   struct dictionary *d = vs->aux;
703   struct variable *v = dict_lookup_var (d, name);
704   if (v != NULL) 
705     {
706       *idx = var_get_dict_index (v);
707       return true;
708     }
709   else
710     return false;
711 }
712
713 /* Destroys VS. */
714 static void
715 dict_var_set_destroy (struct var_set *vs) 
716 {
717   free (vs);
718 }
719
720 /* Returns a variable set based on D. */
721 struct var_set *
722 var_set_create_from_dict (const struct dictionary *d) 
723 {
724   struct var_set *vs = xmalloc (sizeof *vs);
725   vs->get_cnt = dict_var_set_get_cnt;
726   vs->get_var = dict_var_set_get_var;
727   vs->lookup_var_idx = dict_var_set_lookup_var_idx;
728   vs->destroy = dict_var_set_destroy;
729   vs->aux = (void *) d;
730   return vs;
731 }
732 \f
733 /* A variable set based on an array. */
734 struct array_var_set 
735   {
736     struct variable *const *var;/* Array of variables. */
737     size_t var_cnt;             /* Number of elements in var. */
738     struct hsh_table *name_tab; /* Hash from variable names to variables. */
739   };
740
741 /* Returns the number of variables in VS. */
742 static size_t
743 array_var_set_get_cnt (const struct var_set *vs) 
744 {
745   struct array_var_set *avs = vs->aux;
746
747   return avs->var_cnt;
748 }
749
750 /* Return variable with index IDX in VS.
751    IDX must be less than the number of variables in VS. */
752 static struct variable *
753 array_var_set_get_var (const struct var_set *vs, size_t idx) 
754 {
755   struct array_var_set *avs = vs->aux;
756
757   return (struct variable *) avs->var[idx];
758 }
759
760 /* If VS contains a variable named NAME, sets *IDX to its index
761    and returns true.  Otherwise, returns false. */
762 static bool
763 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
764                               size_t *idx) 
765 {
766   struct array_var_set *avs = vs->aux;
767   struct variable *v, *const *vpp;
768
769   v = var_create (name, 0);
770   vpp = hsh_find (avs->name_tab, &v);
771   var_destroy (v);
772   
773   if (vpp != NULL) 
774     {
775       *idx = vpp - avs->var;
776       return true;
777     }
778   else
779     return false;
780 }
781
782 /* Destroys VS. */
783 static void
784 array_var_set_destroy (struct var_set *vs) 
785 {
786   struct array_var_set *avs = vs->aux;
787
788   hsh_destroy (avs->name_tab);
789   free (avs);
790   free (vs);
791 }
792
793 /* Returns a variable set based on the VAR_CNT variables in
794    VAR. */
795 struct var_set *
796 var_set_create_from_array (struct variable *const *var, size_t var_cnt) 
797 {
798   struct var_set *vs;
799   struct array_var_set *avs;
800   size_t i;
801
802   vs = xmalloc (sizeof *vs);
803   vs->get_cnt = array_var_set_get_cnt;
804   vs->get_var = array_var_set_get_var;
805   vs->lookup_var_idx = array_var_set_lookup_var_idx;
806   vs->destroy = array_var_set_destroy;
807   vs->aux = avs = xmalloc (sizeof *avs);
808   avs->var = var;
809   avs->var_cnt = var_cnt;
810   avs->name_tab = hsh_create (2 * var_cnt,
811                               compare_var_ptrs_by_name, hash_var_ptr_by_name,
812                               NULL, NULL);
813   for (i = 0; i < var_cnt; i++)
814     if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL) 
815       {
816         var_set_destroy (vs);
817         return NULL;
818       }
819   
820   return vs;
821 }