Avoid a crash when attempting to allocate very large matrices.
[pspp.git] / src / data / vector.c
blob53378b4b048979b2f15abae2c60735f393fd35c7
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2010, 2011, 2012 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/>. */
17 #include <config.h>
19 #include "data/vector.h"
21 #include <stdlib.h>
23 #include "data/dictionary.h"
24 #include "data/identifier.h"
25 #include "libpspp/assertion.h"
26 #include "libpspp/i18n.h"
27 #include "libpspp/str.h"
29 #include "gl/xalloc.h"
31 /* Vector of variables. */
32 struct vector
34 char *name; /* Name. */
35 struct variable **vars; /* Set of variables. */
36 size_t n_vars; /* Number of variables. */
39 /* Checks that all the variables in VECTOR have consistent
40 width. */
41 static void
42 check_widths (const struct vector *vector)
44 int width = var_get_width (vector->vars[0]);
45 size_t i;
47 for (i = 1; i < vector->n_vars; i++)
48 assert (width == var_get_width (vector->vars[i]));
51 /* Creates and returns a new vector with the given UTF-8 encoded NAME
52 that contains the N_VARS variables in VARS.
53 All variables in VARS must have the same type and width. */
54 struct vector *
55 vector_create (const char *name, struct variable **vars, size_t n_vars)
57 struct vector *vector = xmalloc (sizeof *vector);
59 assert (n_vars > 0);
60 assert (id_is_plausible (name));
62 vector->name = xstrdup (name);
63 vector->vars = xmemdup (vars, n_vars * sizeof *vector->vars);
64 vector->n_vars = n_vars;
65 check_widths (vector);
67 return vector;
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)
80 struct vector *new = xmalloc (sizeof *new);
81 size_t i;
83 new->name = xstrdup (old->name);
84 new->vars = xnmalloc (old->n_vars, sizeof *new->vars);
85 new->n_vars = old->n_vars;
86 for (i = 0; i < new->n_vars; i++)
88 assert (dict_contains_var (old_dict, old->vars[i]));
89 new->vars[i] = dict_get_var (new_dict,
90 var_get_dict_index (old->vars[i]));
92 check_widths (new);
94 return new;
97 /* Destroys VECTOR. */
98 void
99 vector_destroy (struct vector *vector)
101 free (vector->name);
102 free (vector->vars);
103 free (vector);
106 /* Returns VECTOR's name, as a UTF-8 encoded string. */
107 const char *
108 vector_get_name (const struct vector *vector)
110 return vector->name;
113 /* Returns the type of the variables in VECTOR. */
114 enum val_type vector_get_type (const struct vector *vector)
116 return var_get_type (vector->vars[0]);
119 /* Returns the variable in VECTOR with the given INDEX. */
120 struct variable *
121 vector_get_var (const struct vector *vector, size_t index)
123 assert (index < vector->n_vars);
124 return vector->vars[index];
127 /* Returns the number of variables in VECTOR. */
128 size_t
129 vector_get_n_vars (const struct vector *vector)
131 return vector->n_vars;
134 /* Compares two pointers to vectors represented by A and B and
135 returns a strcmp()-type result. */
137 compare_vector_ptrs_by_name (const void *a_, const void *b_)
139 struct vector *const *pa = a_;
140 struct vector *const *pb = b_;
141 struct vector *a = *pa;
142 struct vector *b = *pb;
144 return utf8_strcasecmp (a->name, b->name);