checkin of 0.3.0
[pspp-builds.git] / src / frequencies.g
1 /* PSPP - computes sample statistics.                   -*- C -*-
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 /* Included by frequencies.q. */
21
22 #if WEIGHTING
23   #define WEIGHT w
24   #define FUNCNAME calc_weighting
25 #else /* !WEIGHTING */
26   #define WEIGHT 1.0
27   #define FUNCNAME calc_no_weight
28 #endif /* !WEIGHTING */
29
30 static int
31 FUNCNAME (struct ccase *c)
32 {
33   int i;
34 #if WEIGHTING
35   double w;
36
37   w = c->data[default_dict.var[default_dict.weight_index]->fv].f;
38 #endif
39
40   for (i = 0; i < n_variables; i++)
41     {
42       struct variable *v = v_variables[i];
43       union value *val = &c->data[v->fv];
44       struct freq_tab *ft = &v->p.frq.tab;
45
46       switch (v->p.frq.tab.mode)
47         {
48           case FRQM_GENERAL:
49             {
50               /* General mode.  This declaration and initialization are
51                  strictly conforming: see C89 section 6.5.2.1. */
52               struct freq *fp = avl_find (ft->tree, (struct freq *) val);
53           
54               if (fp)
55                 fp->c += WEIGHT;
56               else
57                 {
58                   fp = pool_alloc (gen_pool, sizeof *fp);
59                   fp->v = *val;
60                   fp->c = WEIGHT;
61                   avl_insert (ft->tree, fp);
62                   if (is_missing (val, v))
63                     v->p.frq.tab.n_missing++;
64                 }
65             }
66           break;
67         case FRQM_INTEGER:
68           /* Integer mode. */
69           if (val->f == SYSMIS)
70             v->p.frq.tab.sysmis += WEIGHT;
71           else if (val->f > INT_MIN+1 && val->f < INT_MAX-1)
72             {
73               int i = val->f;
74               if (i >= v->p.frq.tab.min && i <= v->p.frq.tab.max)
75                 v->p.frq.tab.vector[i - v->p.frq.tab.min] += WEIGHT;
76             }
77           else
78             v->p.frq.tab.out_of_range += WEIGHT;
79           break;
80         default:
81           assert (0);
82         }
83     }
84   return 1;
85 }
86
87 #undef WEIGHT
88 #undef WEIGHTING
89 #undef FUNCNAME