8f3de2f36890ae80443fe1a6d2d5e44c161d2e36
[pspp-builds.git] / src / data / vector.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2006  Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "vector.h"
22
23 #include "dictionary.h"
24
25 #include <libpspp/assertion.h>
26 #include <libpspp/str.h>
27
28 #include "xalloc.h"
29
30 /* Vector of variables. */
31 struct vector
32   {
33     char name[LONG_NAME_LEN + 1];       /* 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 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,
55                struct variable **vars, size_t var_cnt) 
56 {
57   struct vector *vector = xmalloc (sizeof *vector);
58
59   assert (var_cnt > 0);
60   assert (var_is_plausible_name (name, false));
61   str_copy_trunc (vector->name, sizeof vector->name, name);
62
63   vector->vars = xmemdup (vars, var_cnt * sizeof *vector->vars);
64   vector->var_cnt = var_cnt;
65   check_widths (vector);
66
67   return vector;
68 }
69
70 /* Creates and returns a new vector as a clone of OLD, but that
71    contains variables from NEW_DICT that are in the same position
72    as those in OLD are in OLD_DICT. 
73    All variables in the new vector must have the same type and
74    width. */
75 struct vector *
76 vector_clone (const struct vector *old,
77               const struct dictionary *old_dict,
78               const struct dictionary *new_dict) 
79 {
80   struct vector *new = xmalloc (sizeof *new);
81   size_t i;
82   
83   strcpy (new->name, old->name);
84
85   new->vars = xnmalloc (old->var_cnt, sizeof *new->vars);
86   new->var_cnt = old->var_cnt;
87   for (i = 0; i < new->var_cnt; i++) 
88     {
89       assert (dict_contains_var (old_dict, old->vars[i]));
90       new->vars[i] = dict_get_var (new_dict,
91                                    var_get_dict_index (old->vars[i]));
92     }
93   check_widths (new);
94   
95   return new;
96 }
97
98 /* Destroys VECTOR. */
99 void
100 vector_destroy (struct vector *vector) 
101 {
102   free (vector->vars);
103   free (vector);
104 }
105
106 /* Returns VECTOR's name. */
107 const char *
108 vector_get_name (const struct vector *vector) 
109 {
110   return vector->name;
111 }
112
113 /* Returns the type of the variables in VECTOR. */
114 enum var_type vector_get_type (const struct vector *vector) 
115 {
116   return var_get_type (vector->vars[0]);
117 }
118
119 /* Returns the variable in VECTOR with the given INDEX. */
120 struct variable *
121 vector_get_var (const struct vector *vector, size_t index) 
122 {
123   assert (index < vector->var_cnt);
124   return vector->vars[index];
125 }
126
127 /* Returns the number of variables in VECTOR. */
128 size_t
129 vector_get_var_cnt (const struct vector *vector) 
130 {
131   return vector->var_cnt;
132 }
133
134 /* Compares two pointers to vectors represented by A and B and
135    returns a strcmp()-type result. */
136 int
137 compare_vector_ptrs_by_name (const void *a_, const void *b_)
138 {
139   struct vector *const *pa = a_;
140   struct vector *const *pb = b_;
141   struct vector *a = *pa;
142   struct vector *b = *pb;
143   
144   return strcasecmp (a->name, b->name);
145 }
146