Added a n_missing parameter to casereader_create_filter_missing.
[pspp-builds.git] / src / language / stats / rank.q
index fec22a8759fb43e9115be2b3eec31534472feb65..13facbdbbf59f85e43bd350578d4633cb07b880e 100644 (file)
@@ -1,43 +1,42 @@
-/* PSPP - RANK. -*-c-*-
-Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2005, 2006, 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
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-This program is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-02110-1301, USA. */
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 
 #include <limits.h>
 #include <math.h>
 
-#include <data/dictionary.h>
-#include <data/format.h>
-#include <data/missing-values.h>
-#include <data/procedure.h>
-#include <data/variable.h>
 #include <data/case-ordering.h>
 #include <data/case.h>
 #include <data/casegrouper.h>
 #include <data/casereader.h>
 #include <data/casewriter.h>
+#include <data/dictionary.h>
+#include <data/format.h>
+#include <data/missing-values.h>
+#include <data/procedure.h>
+#include <data/short-names.h>
+#include <data/variable.h>
 #include <language/command.h>
 #include <language/stats/sort-criteria.h>
 #include <libpspp/compiler.h>
 #include <libpspp/taint.h>
 #include <math/sort.h>
-#include <output/table.h>
 #include <output/manager.h>
+#include <output/table.h>
 
 #include <gsl/gsl_cdf.h>
 
@@ -237,50 +236,59 @@ static bool
 rank_cmd (struct dataset *ds, const struct case_ordering *sc,
          const struct rank_spec *rank_specs, int n_rank_specs)
 {
-  struct case_ordering *base_ordering;
+  struct dictionary *d = dataset_dict (ds);
   bool ok = true;
   int i;
-  const int n_splits = dict_get_split_cnt (dataset_dict (ds));
-
-  base_ordering = case_ordering_create (dataset_dict (ds));
-  for (i = 0; i < n_splits ; i++)
-    case_ordering_add_var (base_ordering,
-                           dict_get_split_vars (dataset_dict (ds))[i],
-                           SRT_ASCEND);
 
-  for (i = 0; i < n_group_vars; i++)
-    case_ordering_add_var (base_ordering, group_vars[i], SRT_ASCEND);
   for (i = 0 ; i < case_ordering_get_var_cnt (sc) ; ++i )
     {
-      struct case_ordering *ordering;
-      struct casegrouper *grouper;
-      struct casereader *group;
+      /* Rank variable at index I in SC. */
+      struct casegrouper *split_grouper;
+      struct casereader *split_group;
       struct casewriter *output;
-      struct casereader *ranked_file;
-
-      ordering = case_ordering_clone (base_ordering);
-      case_ordering_add_var (ordering,
-                             case_ordering_get_var (sc, i),
-                             case_ordering_get_direction (sc, i));
 
       proc_discard_output (ds);
-      grouper = casegrouper_create_case_ordering (sort_execute (proc_open (ds),
-                                                                ordering),
-                                                  base_ordering);
-      output = autopaging_writer_create (dict_get_next_value_idx (
-                                           dataset_dict (ds)));
-      while (casegrouper_get_next_group (grouper, &group))
-        rank_sorted_file (group, output, dataset_dict (ds),
-                          rank_specs, n_rank_specs,
-                          i, src_vars[i]);
-      ok = casegrouper_destroy (grouper);
+      split_grouper = casegrouper_create_splits (proc_open (ds), d);
+      output = autopaging_writer_create (dict_get_next_value_idx (d));
+
+      while (casegrouper_get_next_group (split_grouper, &split_group))
+        {
+          struct case_ordering *ordering;
+          struct casereader *ordered;
+          struct casegrouper *by_grouper;
+          struct casereader *by_group;
+          int j;
+
+          /* Sort this split group by the BY variables as primary
+             keys and the rank variable as secondary key. */
+          ordering = case_ordering_create ();
+          for (j = 0; j < n_group_vars; j++)
+            case_ordering_add_var (ordering, group_vars[j], SRT_ASCEND);
+          case_ordering_add_var (ordering,
+                                 case_ordering_get_var (sc, i),
+                                 case_ordering_get_direction (sc, i));
+          ordered = sort_execute (split_group, ordering);
+
+          /* Rank the rank variable within this split group. */
+          by_grouper = casegrouper_create_vars (ordered,
+                                                group_vars, n_group_vars);
+          while (casegrouper_get_next_group (by_grouper, &by_group))
+            {
+              /* Rank the rank variable within this BY group
+                 within the split group. */
+
+              rank_sorted_file (by_group, output, d, rank_specs, n_rank_specs,
+                                i, src_vars[i]);
+            }
+          ok = casegrouper_destroy (by_grouper) && ok;
+        }
+      ok = casegrouper_destroy (split_grouper);
       ok = proc_commit (ds) && ok;
-      ranked_file = casewriter_make_reader (output);
-      ok = proc_set_active_file_data (ds, ranked_file) && ok;
+      ok = (proc_set_active_file_data (ds, casewriter_make_reader (output))
+            && ok);
       if (!ok)
         break;
     }
-  case_ordering_destroy (base_ordering);
 
   return ok;
 }
@@ -478,7 +486,7 @@ rank_sorted_file (struct casereader *input,
 
 
   input = casereader_create_filter_missing (input, &rank_var, 1,
-                                            exclude_values, output);
+                                            exclude_values, NULL, output);
   input = casereader_create_filter_weight (input, dict, NULL, output);
 
   casereader_split (input, &pass1, &pass2);
@@ -770,7 +778,7 @@ cmd_rank (struct lexer *lexer, struct dataset *ds)
   /* Put the active file back in its original order.  Delete
      our sort key, which we don't need anymore.  */
   {
-    struct case_ordering *ordering = case_ordering_create (dataset_dict (ds));
+    struct case_ordering *ordering = case_ordering_create ();
     struct casereader *sorted;
     case_ordering_add_var (ordering, order, SRT_ASCEND);
     /* FIXME: loses error conditions. */
@@ -948,3 +956,9 @@ rank_custom_ntiles (struct lexer *lexer, struct dataset *ds, struct cmd_rank *cm
 
   return parse_rank_function (lexer, dict, cmd, NTILES);
 }
+
+/*
+  Local Variables:
+  mode: c
+  End:
+*/