lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / data / vector.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2010, 2011  Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "data/vector.h"
20
21 #include <stdlib.h>
22
23 #include "data/dictionary.h"
24 #include "data/identifier.h"
25 #include "libpspp/assertion.h"
26 #include "libpspp/str.h"
27
28 #include "gl/xalloc.h"
29
30 /* Vector of variables. */
31 struct vector
32   {
33     char *name;                         /* Name. */
34     struct variable **vars;             /* Set of variables. */
35     size_t var_cnt;                     /* Number of variables. */
36   };
37
38 /* Checks that all the variables in VECTOR have consistent
39    width. */
40 static void
41 check_widths (const struct vector *vector)
42 {
43   int width = var_get_width (vector->vars[0]);
44   size_t i;
45
46   for (i = 1; i < vector->var_cnt; i++)
47     assert (width == var_get_width (vector->vars[i]));
48 }
49
50 /* Creates and returns a new vector with the given UTF-8 encoded NAME
51    that contains the VAR_CNT variables in VARS.
52    All variables in VARS must have the same type and width. */
53 struct vector *
54 vector_create (const char *name, struct variable **vars, size_t var_cnt)
55 {
56   struct vector *vector = xmalloc (sizeof *vector);
57
58   assert (var_cnt > 0);
59   assert (id_is_plausible (name, false));
60
61   vector->name = xstrdup (name);
62   vector->vars = xmemdup (vars, var_cnt * sizeof *vector->vars);
63   vector->var_cnt = var_cnt;
64   check_widths (vector);
65
66   return vector;
67 }
68
69 /* Creates and returns a new vector as a clone of OLD, but that
70    contains variables from NEW_DICT that are in the same position
71    as those in OLD are in OLD_DICT.
72    All variables in the new vector must have the same type and
73    width. */
74 struct vector *
75 vector_clone (const struct vector *old,
76               const struct dictionary *old_dict,
77               const struct dictionary *new_dict)
78 {
79   struct vector *new = xmalloc (sizeof *new);
80   size_t i;
81
82   new->name = xstrdup (old->name);
83   new->vars = xnmalloc (old->var_cnt, sizeof *new->vars);
84   new->var_cnt = old->var_cnt;
85   for (i = 0; i < new->var_cnt; i++)
86     {
87       assert (dict_contains_var (old_dict, old->vars[i]));
88       new->vars[i] = dict_get_var (new_dict,
89                                    var_get_dict_index (old->vars[i]));
90     }
91   check_widths (new);
92
93   return new;
94 }
95
96 /* Destroys VECTOR. */
97 void
98 vector_destroy (struct vector *vector)
99 {
100   free (vector->name);
101   free (vector->vars);
102   free (vector);
103 }
104
105 /* Returns VECTOR's name, as a UTF-8 encoded string. */
106 const char *
107 vector_get_name (const struct vector *vector)
108 {
109   return vector->name;
110 }
111
112 /* Returns the type of the variables in VECTOR. */
113 enum val_type vector_get_type (const struct vector *vector)
114 {
115   return var_get_type (vector->vars[0]);
116 }
117
118 /* Returns the variable in VECTOR with the given INDEX. */
119 struct variable *
120 vector_get_var (const struct vector *vector, size_t index)
121 {
122   assert (index < vector->var_cnt);
123   return vector->vars[index];
124 }
125
126 /* Returns the number of variables in VECTOR. */
127 size_t
128 vector_get_var_cnt (const struct vector *vector)
129 {
130   return vector->var_cnt;
131 }
132
133 /* Compares two pointers to vectors represented by A and B and
134    returns a strcmp()-type result. */
135 int
136 compare_vector_ptrs_by_name (const void *a_, const void *b_)
137 {
138   struct vector *const *pa = a_;
139   struct vector *const *pb = b_;
140   struct vector *a = *pa;
141   struct vector *b = *pb;
142
143   return strcasecmp (a->name, b->name);
144 }
145