fd65d284d43ea6d75d1db2885f1b9ad87d37bce9
[pspp-builds.git] / src / data / vector.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006  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 #include "vector.h"
19
20 #include "dictionary.h"
21
22 #include <libpspp/assertion.h>
23 #include <libpspp/str.h>
24
25 #include "xalloc.h"
26
27 /* Vector of variables. */
28 struct vector
29   {
30     char name[VAR_NAME_LEN + 1];       /* Name. */
31     struct variable **vars;             /* Set of variables. */
32     size_t var_cnt;                     /* Number of variables. */
33   };
34
35 /* Checks that all the variables in VECTOR have consistent
36    width. */
37 static void
38 check_widths (const struct vector *vector)
39 {
40   int width = var_get_width (vector->vars[0]);
41   size_t i;
42
43   for (i = 1; i < vector->var_cnt; i++)
44     assert (width == var_get_width (vector->vars[i]));
45 }
46
47 /* Creates and returns a new vector with the given NAME
48    that contains the VAR_CNT variables in VARS.
49    All variables in VARS must have the same type and width. */
50 struct vector *
51 vector_create (const char *name,
52                struct variable **vars, size_t var_cnt)
53 {
54   struct vector *vector = xmalloc (sizeof *vector);
55
56   assert (var_cnt > 0);
57   assert (var_is_plausible_name (name, false));
58   str_copy_trunc (vector->name, sizeof vector->name, name);
59
60   vector->vars = xmemdup (vars, var_cnt * sizeof *vector->vars);
61   vector->var_cnt = var_cnt;
62   check_widths (vector);
63
64   return vector;
65 }
66
67 /* Creates and returns a new vector as a clone of OLD, but that
68    contains variables from NEW_DICT that are in the same position
69    as those in OLD are in OLD_DICT.
70    All variables in the new vector must have the same type and
71    width. */
72 struct vector *
73 vector_clone (const struct vector *old,
74               const struct dictionary *old_dict,
75               const struct dictionary *new_dict)
76 {
77   struct vector *new = xmalloc (sizeof *new);
78   size_t i;
79
80   strcpy (new->name, old->name);
81
82   new->vars = xnmalloc (old->var_cnt, sizeof *new->vars);
83   new->var_cnt = old->var_cnt;
84   for (i = 0; i < new->var_cnt; i++)
85     {
86       assert (dict_contains_var (old_dict, old->vars[i]));
87       new->vars[i] = dict_get_var (new_dict,
88                                    var_get_dict_index (old->vars[i]));
89     }
90   check_widths (new);
91
92   return new;
93 }
94
95 /* Destroys VECTOR. */
96 void
97 vector_destroy (struct vector *vector)
98 {
99   free (vector->vars);
100   free (vector);
101 }
102
103 /* Returns VECTOR's name. */
104 const char *
105 vector_get_name (const struct vector *vector)
106 {
107   return vector->name;
108 }
109
110 /* Returns the type of the variables in VECTOR. */
111 enum val_type vector_get_type (const struct vector *vector)
112 {
113   return var_get_type (vector->vars[0]);
114 }
115
116 /* Returns the variable in VECTOR with the given INDEX. */
117 struct variable *
118 vector_get_var (const struct vector *vector, size_t index)
119 {
120   assert (index < vector->var_cnt);
121   return vector->vars[index];
122 }
123
124 /* Returns the number of variables in VECTOR. */
125 size_t
126 vector_get_var_cnt (const struct vector *vector)
127 {
128   return vector->var_cnt;
129 }
130
131 /* Compares two pointers to vectors represented by A and B and
132    returns a strcmp()-type result. */
133 int
134 compare_vector_ptrs_by_name (const void *a_, const void *b_)
135 {
136   struct vector *const *pa = a_;
137   struct vector *const *pb = b_;
138   struct vector *a = *pa;
139   struct vector *b = *pb;
140
141   return strcasecmp (a->name, b->name);
142 }
143