1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006 Free Software Foundation, Inc.
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.
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.
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/>. */
20 #include "dictionary.h"
22 #include <libpspp/assertion.h>
23 #include <libpspp/str.h>
27 /* Vector of variables. */
30 char name[LONG_NAME_LEN + 1]; /* Name. */
31 struct variable **vars; /* Set of variables. */
32 size_t var_cnt; /* Number of variables. */
35 /* Checks that all the variables in VECTOR have consistent
38 check_widths (const struct vector *vector)
40 int width = var_get_width (vector->vars[0]);
43 for (i = 1; i < vector->var_cnt; i++)
44 assert (width == var_get_width (vector->vars[i]));
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. */
51 vector_create (const char *name,
52 struct variable **vars, size_t var_cnt)
54 struct vector *vector = xmalloc (sizeof *vector);
57 assert (var_is_plausible_name (name, false));
58 str_copy_trunc (vector->name, sizeof vector->name, name);
60 vector->vars = xmemdup (vars, var_cnt * sizeof *vector->vars);
61 vector->var_cnt = var_cnt;
62 check_widths (vector);
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
73 vector_clone (const struct vector *old,
74 const struct dictionary *old_dict,
75 const struct dictionary *new_dict)
77 struct vector *new = xmalloc (sizeof *new);
80 strcpy (new->name, old->name);
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++)
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]));
95 /* Destroys VECTOR. */
97 vector_destroy (struct vector *vector)
103 /* Returns VECTOR's name. */
105 vector_get_name (const struct vector *vector)
110 /* Returns the type of the variables in VECTOR. */
111 enum var_type vector_get_type (const struct vector *vector)
113 return var_get_type (vector->vars[0]);
116 /* Returns the variable in VECTOR with the given INDEX. */
118 vector_get_var (const struct vector *vector, size_t index)
120 assert (index < vector->var_cnt);
121 return vector->vars[index];
124 /* Returns the number of variables in VECTOR. */
126 vector_get_var_cnt (const struct vector *vector)
128 return vector->var_cnt;
131 /* Compares two pointers to vectors represented by A and B and
132 returns a strcmp()-type result. */
134 compare_vector_ptrs_by_name (const void *a_, const void *b_)
136 struct vector *const *pa = a_;
137 struct vector *const *pb = b_;
138 struct vector *a = *pa;
139 struct vector *b = *pb;
141 return strcasecmp (a->name, b->name);