checkin of 0.3.0
[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 #undef DEBUGGING
47 /*#define DEBUGGING 1*/
48 #include "debug-print.h"
49
50 /* Initializes V. */
51 void
52 vec_init (struct long_vec * v)
53 {
54   v->vec = NULL;
55   v->n = v->m = 0;
56 }
57
58 /* Deletes the contents of V. */
59 void
60 vec_clear (struct long_vec * v)
61 {
62   free (v->vec);
63   v->vec = NULL;
64   v->n = v->m = 0;
65 }
66
67 /* Inserts ELEM into V. */
68 void
69 vec_insert (struct long_vec * v, long elem)
70 {
71   if (v->n >= v->m)
72     {
73       v->m = (v->m == 0 ? 16 : 2 * v->m);
74       v->vec = xrealloc (v->vec, v->m * sizeof *v->vec);
75     }
76   v->vec[v->n++] = elem;
77 }
78
79 /* Deletes all occurrences of values A through B exclusive from V. */
80 void
81 vec_delete (struct long_vec * v, long a, long b)
82 {
83   int i;
84
85   for (i = v->n - 1; i >= 0; i--)
86     if (v->vec[i] >= a && v->vec[i] < b)
87       v->vec[i] = v->vec[--v->n];
88 }
89
90 /* Sticks V->FV in the proper vector. */
91 void
92 envector (const struct variable *v)
93 {
94   if (v->type == NUMERIC)
95     {
96       if (v->left)
97         vec_insert (&init_zero, v->fv);
98       else
99         vec_insert (&reinit_sysmis, v->fv);
100     }
101   else
102     {
103       int i;
104
105       if (v->left)
106         for (i = v->fv; i < v->fv + v->nv; i++)
107           vec_insert (&init_blanks, i);
108       else
109         for (i = v->fv; i < v->fv + v->nv; i++)
110           vec_insert (&reinit_blanks, i);
111     }
112 }
113
114 /* Removes V->FV from the proper vector. */
115 void
116 devector (const struct variable *v)
117 {
118   if (v->type == NUMERIC)
119     {
120       if (v->left)
121         vec_delete (&init_zero, v->fv, v->fv + 1);
122       else
123         vec_delete (&reinit_sysmis, v->fv, v->fv + 1);
124     }
125   else if (v->left)
126     vec_delete (&init_blanks, v->fv, v->fv + v->nv);
127   else
128     vec_delete (&reinit_blanks, v->fv, v->fv + v->nv);
129 }