Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / language / stats / frequencies.q
index 34223e104fea78361dbfec63e378dcc2ffed4c17..5f0e96e9e1eeb3e7c6e24a6b8fba8bd211138576 100644 (file)
@@ -29,6 +29,8 @@
 #include <gsl/gsl_histogram.h>
 
 #include <data/case.h>
+#include <data/casegrouper.h>
+#include <data/casereader.h>
 #include <data/dictionary.h>
 #include <data/format.h>
 #include <data/procedure.h>
@@ -45,7 +47,6 @@
 #include <libpspp/hash.h>
 #include <libpspp/magic.h>
 #include <libpspp/message.h>
-#include <libpspp/message.h>
 #include <libpspp/misc.h>
 #include <libpspp/pool.h>
 #include <libpspp/str.h>
@@ -197,7 +198,7 @@ static int normal;          /* FIXME */
 
 /* Variables for which to calculate statistics. */
 static size_t n_variables;
-static struct variable **v_variables;
+static const struct variable **v_variables;
 
 /* Arenas used to store semi-permanent storage. */
 static struct pool *int_pool;  /* Integer mode. */
@@ -269,17 +270,17 @@ get_var_freqs (const struct variable *v)
 
 static void determine_charts (void);
 
-static void calc_stats (struct variable *v, double d[frq_n_stats]);
+static void calc_stats (const struct variable *v, double d[frq_n_stats]);
 
-static void precalc (const struct ccase *, void *, const struct dataset *);
-static bool calc (const struct ccase *, void *, const struct dataset *);
-static bool postcalc (void *, const struct dataset *);
+static void precalc (struct casereader *, struct dataset *);
+static void calc (const struct ccase *, const struct dataset *);
+static void postcalc (void);
 
-static void postprocess_freq_tab (struct variable *);
-static void dump_full (struct variable *);
-static void dump_condensed (struct variable *);
-static void dump_statistics (struct variable *, int show_varname);
-static void cleanup_freq_tab (struct variable *);
+static void postprocess_freq_tab (const struct variable *);
+static void dump_full (const struct variable *);
+static void dump_condensed (const struct variable *);
+static void dump_statistics (const struct variable *, int show_varname);
+static void cleanup_freq_tab (const struct variable *);
 
 static hsh_compare_func compare_value_numeric_a, compare_value_alpha_a;
 static hsh_compare_func compare_value_numeric_d, compare_value_alpha_d;
@@ -318,8 +319,10 @@ cmd_frequencies (struct lexer *lexer, struct dataset *ds)
 static int
 internal_cmd_frequencies (struct lexer *lexer, struct dataset *ds)
 {
-  int i;
+  struct casegrouper *grouper;
+  struct casereader *input, *group;
   bool ok;
+  int i;
 
   n_percentiles = 0;
   percentiles = NULL;
@@ -383,7 +386,21 @@ internal_cmd_frequencies (struct lexer *lexer, struct dataset *ds)
   
 
   /* Do it! */
-  ok = procedure_with_splits (ds, precalc, calc, postcalc, NULL);
+  input = casereader_create_filter_weight (proc_open (ds), dataset_dict (ds),
+                                           NULL, NULL);
+  grouper = casegrouper_create_splits (input, dataset_dict (ds));
+  for (; casegrouper_get_next_group (grouper, &group);
+       casereader_destroy (group)) 
+    {
+      struct ccase c;
+      
+      precalc (group, ds);
+      for (; casereader_read (group, &c); case_destroy (&c)) 
+        calc (&c, ds);
+      postcalc ();
+    }
+  ok = casegrouper_destroy (grouper);
+  ok = proc_commit (ds) && ok;
 
   free_frequencies(&cmd);
 
@@ -496,14 +513,11 @@ determine_charts (void)
 }
 
 /* Add data from case C to the frequency table. */
-static bool
-calc (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
+static void
+calc (const struct ccase *c, const struct dataset *ds)
 {
-  double weight;
+  double weight = dict_get_case_weight (dataset_dict (ds), c, NULL);
   size_t i;
-  bool bad_warn = true;
-
-  weight = dict_get_case_weight (dataset_dict (ds), c, &bad_warn);
 
   for (i = 0; i < n_variables; i++)
     {
@@ -530,7 +544,8 @@ calc (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
                  struct freq *fp = pool_alloc (gen_pool, sizeof *fp);
                   fp->count = weight;
                   fp->value = pool_clone (gen_pool,
-                                      val, MAX (MAX_SHORT_STRING, vf->width));
+                                          val,
+                                          MAX (MAX_SHORT_STRING, vf->width));
                   *fpp = fp;
                }
            }
@@ -552,24 +567,27 @@ calc (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
           NOT_REACHED ();
        }
     }
-  return true;
 }
 
 /* Prepares each variable that is the target of FREQUENCIES by setting
    up its hash table. */
 static void
-precalc (const struct ccase *first, void *aux UNUSED, const struct dataset *ds)
+precalc (struct casereader *input, struct dataset *ds)
 {
+  struct ccase c;
   size_t i;
 
-  output_split_file_values (ds, first);
+  if (!casereader_peek (input, 0, &c))
+    return;
+  output_split_file_values (ds, &c);
+  case_destroy (&c);
 
   pool_destroy (gen_pool);
   gen_pool = pool_create ();
   
   for (i = 0; i < n_variables; i++)
     {
-      struct variable *v = v_variables[i];
+      const struct variable *v = v_variables[i];
       struct freq_tab *ft = &get_var_freqs (v)->tab;
 
       if (ft->mode == FRQM_GENERAL)
@@ -590,14 +608,14 @@ precalc (const struct ccase *first, void *aux UNUSED, const struct dataset *ds)
 
 /* Finishes up with the variables after frequencies have been
    calculated.  Displays statistics, percentiles, ... */
-static bool
-postcalc (void *aux UNUSED, const struct dataset *ds  UNUSED)
+static void
+postcalc (void)
 {
   size_t i;
 
   for (i = 0; i < n_variables; i++)
     {
-      struct variable *v = v_variables[i];
+      const struct variable *v = v_variables[i];
       struct var_freqs *vf = get_var_freqs (v);
       struct freq_tab *ft = &vf->tab;
       int n_categories;
@@ -666,8 +684,6 @@ postcalc (void *aux UNUSED, const struct dataset *ds  UNUSED)
       cleanup_freq_tab (v);
 
     }
-
-  return true;
 }
 
 /* Returns the comparison function that should be used for
@@ -705,7 +721,7 @@ not_missing (const void *f_, const void *v_)
 
 /* Summarizes the frequency table data for variable V. */
 static void
-postprocess_freq_tab (struct variable *v)
+postprocess_freq_tab (const struct variable *v)
 {
   hsh_compare_func *compare;
   struct freq_tab *ft;
@@ -760,7 +776,7 @@ postprocess_freq_tab (struct variable *v)
 
 /* Frees the frequency table for variable V. */
 static void
-cleanup_freq_tab (struct variable *v)
+cleanup_freq_tab (const struct variable *v)
 {
   struct freq_tab *ft = &get_var_freqs (v)->tab;
   assert (ft->mode == FRQM_GENERAL);
@@ -784,7 +800,7 @@ frq_custom_variables (struct lexer *lexer, struct dataset *ds, struct cmd_freque
                          || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) == NULL))
     return 2;
 
-  if (!parse_variables (lexer, dataset_dict (ds), &v_variables, &n_variables,
+  if (!parse_variables_const (lexer, dataset_dict (ds), &v_variables, &n_variables,
                        PV_APPEND | PV_NO_SCRATCH))
     return 0;
 
@@ -815,7 +831,7 @@ frq_custom_variables (struct lexer *lexer, struct dataset *ds, struct cmd_freque
 
   for (i = old_n_variables; i < n_variables; i++)
     {
-      struct variable *v = v_variables[i];
+      const struct variable *v = v_variables[i];
       struct var_freqs *vf;
 
       if (var_get_aux (v) != NULL)
@@ -875,9 +891,9 @@ frq_custom_grouped (struct lexer *lexer, struct dataset *ds, struct cmd_frequenc
 
        /* Variable list. */
        size_t n;
-       struct variable **v;
+       const struct variable **v;
 
-       if (!parse_variables (lexer, dataset_dict (ds), &v, &n,
+       if (!parse_variables_const (lexer, dataset_dict (ds), &v, &n,
                               PV_NO_DUPLICATE | PV_NUMERIC))
          return 0;
        if (lex_match (lexer, '('))
@@ -1119,7 +1135,7 @@ full_dim (struct tab_table *t, struct outp_driver *d)
 
 /* Displays a full frequency table for variable V. */
 static void
-dump_full (struct variable *v)
+dump_full (const struct variable *v)
 {
   int n_categories;
   struct var_freqs *vf;
@@ -1250,7 +1266,7 @@ condensed_dim (struct tab_table *t, struct outp_driver *d)
 
 /* Display condensed frequency table for variable V. */
 static void
-dump_condensed (struct variable *v)
+dump_condensed (const struct variable *v)
 {
   int n_categories;
   struct var_freqs *vf;
@@ -1310,7 +1326,7 @@ dump_condensed (struct variable *v)
 /* Calculates all the pertinent statistics for variable V, putting
    them in array D[].  FIXME: This could be made much more optimal. */
 static void
-calc_stats (struct variable *v, double d[frq_n_stats])
+calc_stats (const struct variable *v, double d[frq_n_stats])
 {
   struct freq_tab *ft = &get_var_freqs (v)->tab;
   double W = ft->valid_cases;
@@ -1472,7 +1488,7 @@ calc_stats (struct variable *v, double d[frq_n_stats])
 
 /* Displays a table of all the statistics requested for variable V. */
 static void
-dump_statistics (struct variable *v, int show_varname)
+dump_statistics (const struct variable *v, int show_varname)
 {
   struct freq_tab *ft;
   double stat_value[frq_n_stats];