Added a --enable-debug option to configure and
[pspp-builds.git] / 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 /* AIX requires this to be the first thing in the file.  */
21 #include <config.h>
22 #if __GNUC__
23 #define alloca __builtin_alloca
24 #else
25 #if HAVE_ALLOCA_H
26 #include <alloca.h>
27 #else
28 #ifdef _AIX
29 #pragma alloca
30 #else
31 #ifndef alloca                  /* predefined by HP cc +Olibcalls */
32 char *alloca ();
33 #endif
34 #endif
35 #endif
36 #endif
37
38 #include <assert.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include "alloc.h"
42 #include "cases.h"
43 #include "var.h"
44 #include "vfm.h"
45
46 #include "debug-print.h"
47
48 /* Initializes V. */
49 void
50 vec_init (struct long_vec * v)
51 {
52   v->vec = NULL;
53   v->n = v->m = 0;
54 }
55
56 /* Deletes the contents of V. */
57 void
58 vec_clear (struct long_vec * v)
59 {
60   free (v->vec);
61   v->vec = NULL;
62   v->n = v->m = 0;
63 }
64
65 /* Inserts ELEM into V. */
66 void
67 vec_insert (struct long_vec * v, long elem)
68 {
69   if (v->n >= v->m)
70     {
71       v->m = (v->m == 0 ? 16 : 2 * v->m);
72       v->vec = xrealloc (v->vec, v->m * sizeof *v->vec);
73     }
74   v->vec[v->n++] = elem;
75 }
76
77 /* Deletes all occurrences of values A through B exclusive from V. */
78 void
79 vec_delete (struct long_vec * v, long a, long b)
80 {
81   int i;
82
83   for (i = v->n - 1; i >= 0; i--)
84     if (v->vec[i] >= a && v->vec[i] < b)
85       v->vec[i] = v->vec[--v->n];
86 }
87
88 /* Sticks V->FV in the proper vector. */
89 void
90 envector (const struct variable *v)
91 {
92   if (v->type == NUMERIC)
93     {
94       if (v->left)
95         vec_insert (&init_zero, v->fv);
96       else
97         vec_insert (&reinit_sysmis, v->fv);
98     }
99   else
100     {
101       int i;
102
103       if (v->left)
104         for (i = v->fv; i < v->fv + v->nv; i++)
105           vec_insert (&init_blanks, i);
106       else
107         for (i = v->fv; i < v->fv + v->nv; i++)
108           vec_insert (&reinit_blanks, i);
109     }
110 }
111
112 /* Removes V->FV from the proper vector. */
113 void
114 devector (const struct variable *v)
115 {
116   if (v->type == NUMERIC)
117     {
118       if (v->left)
119         vec_delete (&init_zero, v->fv, v->fv + 1);
120       else
121         vec_delete (&reinit_sysmis, v->fv, v->fv + 1);
122     }
123   else if (v->left)
124     vec_delete (&init_blanks, v->fv, v->fv + v->nv);
125   else
126     vec_delete (&reinit_blanks, v->fv, v->fv + v->nv);
127 }