Make the missing value code do more work, so that its callers can do
[pspp-builds.git] / src / language / stats / crosstabs.q
index fc8571c46aee0f8c59c9066c27f94b085b728de9..775e415a53faaa71d8e96534ceb4cd8e7b9948d7 100644 (file)
@@ -1,6 +1,5 @@
 /* PSPP - computes sample statistics.
    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -39,6 +38,7 @@
 #include <data/case.h>
 #include <data/data-out.h>
 #include <data/dictionary.h>
+#include <data/format.h>
 #include <data/procedure.h>
 #include <data/value-labels.h>
 #include <data/variable.h>
@@ -131,9 +131,7 @@ struct var_range
 static inline struct var_range *
 get_var_range (struct variable *v) 
 {
-  assert (v != NULL);
-  assert (v->aux != NULL);
-  return v->aux;
+  return var_get_aux (v);
 }
 
 /* Indexes into crosstab.v. */
@@ -571,6 +569,11 @@ calc_general (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
 {
   bool bad_warn = true;
 
+  /* Missing values to exclude. */
+  enum mv_class exclude = (cmd.miss == CRS_TABLE ? MV_ANY
+                           : cmd.miss == CRS_INCLUDE ? MV_SYSTEM
+                           : MV_NEVER);
+
   /* Case weight. */
   double weight = dict_get_case_weight (dataset_dict (ds), c, &bad_warn);
 
@@ -592,20 +595,18 @@ calc_general (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
        assert (x != NULL);
        for (j = 0; j < x->nvar; j++)
          {
-            const union value *v = case_data (c, x->vars[j]->fv);
-           if ((cmd.miss == CRS_TABLE && var_is_value_missing (x->vars[j], v))
-               || (cmd.miss == CRS_INCLUDE
-                   && var_is_value_system_missing (x->vars[j], v)))
+            const union value *v = case_data (c, x->vars[j]);
+            if (var_is_value_missing (x->vars[j], v, exclude))
              {
                x->missing += weight;
                goto next_crosstab;
              }
              
            if (var_is_numeric (x->vars[j]))
-             te->values[j].f = case_num (c, x->vars[j]->fv);
+             te->values[j].f = case_num (c, x->vars[j]);
            else
              {
-               memcpy (te->values[j].s, case_str (c, x->vars[j]->fv),
+               memcpy (te->values[j].s, case_str (c, x->vars[j]),
                         var_get_width (x->vars[j]));
              
                /* Necessary in order to simplify comparisons. */
@@ -661,11 +662,12 @@ calc_integer (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
        {
          struct variable *const v = x->vars[i];
           struct var_range *vr = get_var_range (v);
-         double value = case_num (c, v->fv);
+         double value = case_num (c, v);
          
          /* Note that the first test also rules out SYSMIS. */
          if ((value < vr->min || value >= vr->max)
-             || (cmd.miss == CRS_TABLE && var_is_num_user_missing (v, value)))
+             || (cmd.miss == CRS_TABLE
+                  && var_is_num_missing (v, value, MV_USER)))
            {
              x->missing += weight;
              goto next_crosstab;
@@ -680,10 +682,10 @@ calc_integer (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
       
       {
         struct variable *row_var = x->vars[ROW_VAR];
-       const int row = case_num (c, row_var->fv) - get_var_range (row_var)->min;
+       const int row = case_num (c, row_var) - get_var_range (row_var)->min;
 
         struct variable *col_var = x->vars[COL_VAR];
-       const int col = case_num (c, col_var->fv) - get_var_range (col_var)->min;
+       const int col = case_num (c, col_var) - get_var_range (col_var)->min;
 
        const int col_dim = get_var_range (col_var)->count;
 
@@ -1423,7 +1425,7 @@ delete_missing (void)
     int r;
 
     for (r = 0; r < n_rows; r++)
-      if (var_is_num_user_missing (x->vars[ROW_VAR], rows[r].f))
+      if (var_is_num_missing (x->vars[ROW_VAR], rows[r].f, MV_USER))
        {
          int c;
 
@@ -1437,7 +1439,7 @@ delete_missing (void)
     int c;
 
     for (c = 0; c < n_cols; c++)
-      if (var_is_num_user_missing (x->vars[COL_VAR], cols[c].f))
+      if (var_is_num_missing (x->vars[COL_VAR], cols[c].f, MV_USER))
        {
          int r;
 
@@ -1667,7 +1669,7 @@ table_value_missing (struct tab_table *table, int c, int r, unsigned char opt,
   struct substring s;
   const struct fmt_spec *print = var_get_print_format (var);
 
-  const char *label = val_labs_find (var->val_labs, *v);
+  const char *label = var_lookup_value_label (var, v);
   if (label) 
     {
       tab_text (table, c, r, TAB_LEFT, label);
@@ -1677,7 +1679,7 @@ table_value_missing (struct tab_table *table, int c, int r, unsigned char opt,
   s.string = tab_alloc (table, print->w);
   format_short (s.string, print, v);
   s.length = strlen (s.string);
-  if (cmd.miss == CRS_REPORT && var_is_num_user_missing (var, v->f))
+  if (cmd.miss == CRS_REPORT && var_is_num_missing (var, v->f, MV_USER))
     s.string[s.length++] = 'M';
   while (s.length && *s.string == ' ')
     {
@@ -1760,8 +1762,9 @@ display_crosstabulation (void)
             bool mark_missing = false;
             double expected_value = row_tot[r] * col_tot[c] / W;
             if (cmd.miss == CRS_REPORT
-                && (var_is_num_user_missing (x->vars[COL_VAR], cols[c].f)
-                    || var_is_num_user_missing (x->vars[ROW_VAR], rows[r].f)))
+                && (var_is_num_missing (x->vars[COL_VAR], cols[c].f, MV_USER)
+                    || var_is_num_missing (x->vars[ROW_VAR], rows[r].f,
+                                           MV_USER)))
               mark_missing = true;
            for (i = 0; i < num_cells; i++)
              {
@@ -1825,7 +1828,7 @@ display_crosstabulation (void)
         bool mark_missing = false;
 
         if (cmd.miss == CRS_REPORT
-            && var_is_num_user_missing (x->vars[ROW_VAR], rows[r].f))
+            && var_is_num_missing (x->vars[ROW_VAR], rows[r].f, MV_USER))
           mark_missing = true;
 
         for (i = 0; i < num_cells; i++)
@@ -1880,7 +1883,7 @@ display_crosstabulation (void)
         int i;
            
         if (cmd.miss == CRS_REPORT && c < n_cols 
-            && var_is_num_user_missing (x->vars[COL_VAR], cols[c].f))
+            && var_is_num_missing (x->vars[COL_VAR], cols[c].f, MV_USER))
           mark_missing = true;
 
         for (i = 0; i < num_cells; i++)