Fixed a bug in frequencies.q which would crash on alpha values
[pspp] / src / cases.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "alloc.h"
25 #include "cases.h"
26 #include "var.h"
27 #include "vfm.h"
28
29 #include "debug-print.h"
30
31 /* Initializes V. */
32 void
33 vec_init (struct long_vec * v)
34 {
35   v->vec = NULL;
36   v->n = v->m = 0;
37 }
38
39 /* Deletes the contents of V. */
40 void
41 vec_clear (struct long_vec * v)
42 {
43   free (v->vec);
44   v->vec = NULL;
45   v->n = v->m = 0;
46 }
47
48 /* Inserts ELEM into V. */
49 void
50 vec_insert (struct long_vec * v, long elem)
51 {
52   if (v->n >= v->m)
53     {
54       v->m = (v->m == 0 ? 16 : 2 * v->m);
55       v->vec = xrealloc (v->vec, v->m * sizeof *v->vec);
56     }
57   v->vec[v->n++] = elem;
58 }
59
60 /* Deletes all occurrences of values A through B exclusive from V. */
61 void
62 vec_delete (struct long_vec * v, long a, long b)
63 {
64   int i;
65
66   for (i = v->n - 1; i >= 0; i--)
67     if (v->vec[i] >= a && v->vec[i] < b)
68       v->vec[i] = v->vec[--v->n];
69 }
70
71 /* Sticks V->FV in the proper vector. */
72 void
73 envector (const struct variable *v)
74 {
75   if (v->type == NUMERIC)
76     {
77       if (v->left)
78         vec_insert (&init_zero, v->fv);
79       else
80         vec_insert (&reinit_sysmis, v->fv);
81     }
82   else
83     {
84       int i;
85
86       if (v->left)
87         for (i = v->fv; i < v->fv + v->nv; i++)
88           vec_insert (&init_blanks, i);
89       else
90         for (i = v->fv; i < v->fv + v->nv; i++)
91           vec_insert (&reinit_blanks, i);
92     }
93 }
94
95 /* Removes V->FV from the proper vector. */
96 void
97 devector (const struct variable *v)
98 {
99   if (v->type == NUMERIC)
100     {
101       if (v->left)
102         vec_delete (&init_zero, v->fv, v->fv + 1);
103       else
104         vec_delete (&reinit_sysmis, v->fv, v->fv + 1);
105     }
106   else if (v->left)
107     vec_delete (&init_blanks, v->fv, v->fv + v->nv);
108   else
109     vec_delete (&reinit_blanks, v->fv, v->fv + v->nv);
110 }