Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / language / stats / oneway.q
index 674cf710f085f4cbbeb2b81f07537921af86e6d7..78300723ab948525364cf41dad6c6333d919518b 100644 (file)
@@ -1,7 +1,6 @@
 /* PSPP - One way ANOVA. -*-c-*-
 
-Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
-Author: John Darrington 2004
+Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
@@ -26,7 +25,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 #include <stdlib.h>
 
 #include <data/case.h>
-#include <data/casefile.h>
+#include <data/casegrouper.h>
+#include <data/casereader.h>
 #include <data/dictionary.h>
 #include <data/procedure.h>
 #include <data/value-labels.h>
@@ -39,9 +39,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 #include <libpspp/hash.h>
 #include <libpspp/magic.h>
 #include <libpspp/message.h>
-#include <libpspp/message.h>
 #include <libpspp/misc.h>
 #include <libpspp/str.h>
+#include <libpspp/taint.h>
 #include <math/group-proc.h>
 #include <math/group.h>
 #include <math/levene.h>
@@ -57,29 +57,24 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 /* (specification)
    "ONEWAY" (oneway_):
    *^variables=custom;
-   +missing=miss:!analysis/listwise,
-   incl:include/!exclude;
-   contrast= double list;
-   statistics[st_]=descriptives,homogeneity.
+   missing=miss:!analysis/listwise,
+           incl:include/!exclude;
+   +contrast= double list;
+   +statistics[st_]=descriptives,homogeneity.
 */
 /* (declarations) */
 /* (functions) */
 
-
-
-static int bad_weight_warn = 1;
-
-
 static struct cmd_oneway cmd;
 
 /* The independent variable */
-static struct variable *indep_var;
+static const struct variable *indep_var;
 
 /* Number of dependent variables */
 static size_t n_vars;
 
 /* The dependent variables */
-static struct variable **vars;
+static const struct variable **vars;
 
 
 /* A  hash table containing all the distinct values of the independent
@@ -88,15 +83,11 @@ static struct hsh_table *global_group_hash ;
 
 /* The number of distinct values of the independent variable, when all 
    missing values are disregarded */
-static int ostensible_number_of_groups=-1;
+static int ostensible_number_of_groups = -1;
 
 
-/* Function to use for testing for missing values */
-static is_missing_func *value_is_missing;
-
-
-static bool run_oneway(const struct ccase *first,
-                       const struct casefile *cf, void *_mode);
+static void run_oneway (struct cmd_oneway *, struct casereader *, 
+                        const struct dataset *);
 
 
 /* Routines to show the output tables */
@@ -116,20 +107,16 @@ void output_oneway(void);
 
 
 int
-cmd_oneway(void)
+cmd_oneway (struct lexer *lexer, struct dataset *ds)
 {
+  struct casegrouper *grouper;
+  struct casereader *group;
   int i;
   bool ok;
 
-  if ( !parse_oneway(&cmd, NULL) )
+  if ( !parse_oneway (lexer, ds, &cmd, NULL) )
     return CMD_FAILURE;
 
-  /* If /MISSING=INCLUDE is set, then user missing values are ignored */
-  if (cmd.incl == ONEWAY_INCLUDE ) 
-    value_is_missing = mv_is_value_system_missing;
-  else
-    value_is_missing = mv_is_value_missing;
-
   /* What statistics were requested */
   if ( cmd.sbc_statistics ) 
     {
@@ -149,7 +136,12 @@ cmd_oneway(void)
        }
     }
 
-  ok = multipass_procedure_with_splits (run_oneway, &cmd);
+  /* Data pass.  FIXME: error handling. */
+  grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
+  while (casegrouper_get_next_group (grouper, &group)) 
+    run_oneway (&cmd, group, ds);
+  ok = casegrouper_destroy (grouper);
+  ok = proc_commit (ds) && ok;
 
   free (vars);
   free_oneway (&cmd);
@@ -186,7 +178,8 @@ output_oneway(void)
        sum += subc_list_double_at(&cmd.dl_contrast[i],j);
 
       if ( sum != 0.0 ) 
-       msg(SW,_("Coefficients for contrast %d do not total zero"),i + 1);
+       msg(SW,_("Coefficients for contrast %d do not total zero"),
+            (int) i + 1);
     }
 
   if ( stat_tables & STAT_DESC ) 
@@ -223,17 +216,19 @@ output_oneway(void)
 
 /* Parser for the variables sub command */
 static int
-oneway_custom_variables(struct cmd_oneway *cmd UNUSED, void *aux UNUSED)
+oneway_custom_variables (struct lexer *lexer, 
+                       struct dataset *ds, struct cmd_oneway *cmd UNUSED, 
+                       void *aux UNUSED)
 {
+  struct dictionary *dict = dataset_dict (ds);
 
-  lex_match('=');
+  lex_match (lexer, '=');
 
-  if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
-      && token != T_ALL)
+  if ((lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
+      && lex_token (lexer) != T_ALL)
     return 2;
-  
 
-  if (!parse_variables (default_dict, &vars, &n_vars,
+  if (!parse_variables_const (lexer, dict, &vars, &n_vars,
                        PV_DUPLICATE 
                        | PV_NUMERIC | PV_NO_SCRATCH) )
     {
@@ -243,19 +238,17 @@ oneway_custom_variables(struct cmd_oneway *cmd UNUSED, void *aux UNUSED)
 
   assert(n_vars);
 
-  if ( ! lex_match(T_BY))
+  if ( ! lex_match (lexer, T_BY))
     return 2;
 
-
-  indep_var = parse_variable();
+  indep_var = parse_variable (lexer, dict);
 
   if ( !indep_var ) 
     {
-      msg(SE,_("`%s' is not a variable name"),tokid);
+      msg(SE,_("`%s' is not a variable name"),lex_tokid (lexer));
       return 0;
     }
 
-
   return 1;
 }
 
@@ -362,10 +355,9 @@ show_anova_table(void)
 
   tab_title (t, _("ANOVA"));
   tab_submit (t);
-
-
 }
 
+
 /* Show the descriptives table */
 static void  
 show_descriptives(void)
@@ -446,7 +438,8 @@ show_descriptives(void)
          gs = gs_array[count];
 
          tab_text (t, 1, row + count, 
-                   TAB_LEFT | TAT_TITLE ,value_to_string(&gs->id,indep_var));
+                   TAB_LEFT | TAT_TITLE, var_get_value_name(indep_var,
+                                                             &gs->id));
 
          /* Now fill in the numbers ... */
 
@@ -572,14 +565,12 @@ show_homogeneity(void)
     }
 
   tab_submit (t);
-
-
 }
 
 
 /* Show the contrast coefficients table */
 static void 
-show_contrast_coeffs(short *bad_contrast)
+show_contrast_coeffs (short *bad_contrast)
 {
   int n_cols = 2 + ostensible_number_of_groups;
   int n_rows = 2 + cmd.sbc_contrast;
@@ -634,7 +625,7 @@ show_contrast_coeffs(short *bad_contrast)
       group_value = group_values[count];
 
       tab_text (t, count + 2, 1, TAB_CENTER | TAT_TITLE, 
-               value_to_string(group_value, indep_var));
+               var_get_value_name (indep_var, group_value));
 
       for (i = 0 ; i < cmd.sbc_contrast ; ++i ) 
        {
@@ -880,7 +871,7 @@ precalc ( struct cmd_oneway *cmd UNUSED )
                   (hsh_compare_func *) compare_group,
                   (hsh_hash_func *) hash_group,
                   (hsh_free_func *) free_group,
-                  (void *) indep_var->width );
+                  (void *) var_get_width (indep_var) );
 
 
       totals->sum=0;
@@ -892,63 +883,64 @@ precalc ( struct cmd_oneway *cmd UNUSED )
     }
 }
 
+static void
+free_value (void *value_, const void *aux UNUSED) 
+{
+  union value *value = value_;
+  free (value);
+}
 
-static bool
-run_oneway(const struct ccase *first, const struct casefile *cf, void *cmd_)
+static void
+run_oneway (struct cmd_oneway *cmd,
+            struct casereader *input, 
+            const struct dataset *ds)
 {
-  struct casereader *r;
+  struct taint *taint;
+  struct dictionary *dict = dataset_dict (ds);
+  enum mv_class exclude;
+  struct casereader *reader;
   struct ccase c;
 
-  struct cmd_oneway *cmd = (struct cmd_oneway *) cmd_;
+  if (!casereader_peek (input, 0, &c))
+    return;
+  output_split_file_values (ds, &c);
+  case_destroy (&c);
 
-  output_split_file_values (first);
+  taint = taint_clone (casereader_get_taint (input));
 
   global_group_hash = hsh_create(4, 
                                 (hsh_compare_func *) compare_values,
                                 (hsh_hash_func *) hash_value,
-                                0,
-                                (void *) indep_var->width );
+                                free_value,
+                                (void *) var_get_width (indep_var) );
+
   precalc(cmd);
 
-  for(r = casefile_get_reader (cf);
-      casereader_read (r, &c) ;
-      case_destroy (&c)) 
+  exclude = cmd->incl != ONEWAY_INCLUDE ? MV_ANY : MV_SYSTEM;
+  input = casereader_create_filter_missing (input, &indep_var, 1,
+                                            exclude, NULL);
+  if (cmd->miss == ONEWAY_LISTWISE)
+    input = casereader_create_filter_missing (input, vars, n_vars,
+                                              exclude, NULL);
+  input = casereader_create_filter_weight (input, dict, NULL, NULL);
+
+  reader = casereader_clone (input);
+  for (; casereader_read (reader, &c); case_destroy (&c)) 
     {
       size_t i;
 
-      const double weight = 
-       dict_get_case_weight(default_dict,&c,&bad_weight_warn);
-      
-      const union value *indep_val = case_data (&c, indep_var->fv);
-
-      /* Deal with missing values */
-      if ( value_is_missing(&indep_var->miss, indep_val) )
-       continue;
-
-      /* Skip the entire case if /MISSING=LISTWISE is set */
-      if ( cmd->miss == ONEWAY_LISTWISE ) 
-       {
-         for(i = 0; i < n_vars ; ++i) 
-           {
-             const struct variable *v = vars[i];
-             const union value *val = case_data (&c, v->fv);
-
-             if (value_is_missing(&v->miss, val) )
-               break;
-           }
-         if ( i != n_vars ) 
-           continue;
-
-       }
+      const double weight = dict_get_case_weight (dict, &c, NULL);
       
-         
-      hsh_insert ( global_group_hash, (void *) indep_val );
+      const union value *indep_val = case_data (&c, indep_var);
+      void **p = hsh_probe (global_group_hash, indep_val);
+      if (*p == NULL)
+        *p = value_dup (indep_val, var_get_width (indep_var));
 
       for ( i = 0 ; i < n_vars ; ++i ) 
        {
          const struct variable *v = vars[i];
 
-         const union value *val = case_data (&c, v->fv);
+         const union value *val = case_data (&c, v);
 
           struct group_proc *gp = group_proc_get (vars[i]);
          struct hsh_table *group_hash = gp->group_hash;
@@ -970,8 +962,8 @@ run_oneway(const struct ccase *first, const struct casefile *cf, void *cmd_)
 
              hsh_insert ( group_hash, (void *) gs );
            }
-         
-         if (! value_is_missing(&v->miss, val) )
+
+         if (!var_is_value_missing (v, val, exclude))
            {
              struct group_statistics *totals = &gp->ugs;
 
@@ -1000,22 +992,21 @@ run_oneway(const struct ccase *first, const struct casefile *cf, void *cmd_)
        }
   
     }
-  casereader_destroy (r);
+  casereader_destroy (reader);
 
   postcalc(cmd);
 
   
   if ( stat_tables & STAT_HOMO ) 
-    levene(cf, indep_var, n_vars, vars, 
-          (cmd->miss == ONEWAY_LISTWISE) ? LEV_LISTWISE : LEV_ANALYSIS ,
-          value_is_missing);
-
-  ostensible_number_of_groups = hsh_count (global_group_hash);
+    levene (dict, casereader_clone (input), indep_var, n_vars, vars, exclude);
 
+  casereader_destroy (input);
 
-  output_oneway();
+  ostensible_number_of_groups = hsh_count (global_group_hash);
 
-  return true;
+  if (!taint_has_tainted_successor (taint))
+    output_oneway();
+  taint_destroy (taint);
 }