refactoring
[pspp] / src / language / stats / means-parser.c
index 73822ecce14c2588efbe7811c4fe278936ffde7a..6bbae8938579aea56b23d1e2be216b323b3a8601 100644 (file)
 #include "data/format.h"
 #include "data/variable.h"
 
-#include "language/command.h"
 #include "language/lexer/lexer.h"
 #include "language/lexer/variable-parser.h"
 
-#include "libpspp/hmap.h"
-#include "libpspp/bt.h"
-#include "libpspp/misc.h"
 #include "libpspp/pool.h"
 
 #include "means.h"
@@ -40,8 +36,7 @@ static bool
 parse_means_table_syntax (struct lexer *lexer, const struct means *cmd,
                          struct mtable *table)
 {
-  table->n_layers = 0;
-  table->layers = NULL;
+  memset (table, 0, sizeof *table);
 
   /* Dependent variable (s) */
   if (!parse_variables_const_pool (lexer, cmd->pool, cmd->dict,
@@ -49,18 +44,16 @@ parse_means_table_syntax (struct lexer *lexer, const struct means *cmd,
                                   PV_NO_DUPLICATE | PV_NUMERIC))
     return false;
 
-
   /* Factor variable (s) */
   while (lex_match (lexer, T_BY))
     {
-      struct layer *layer = xzalloc (sizeof *layer);
-      hmap_init (&layer->instances.map);
+      struct layer *layer = pool_zalloc (cmd->pool, sizeof *layer);
 
+      table->layers =
+       pool_nrealloc (cmd->pool, table->layers, table->n_layers + 1,
+                      sizeof *table->layers);
+      table->layers[table->n_layers] = layer;
       table->n_layers++;
-      table->layers  = xrealloc (table->layers,
-                                table->n_layers * sizeof *table->layers);
-
-      table->layers[table->n_layers - 1] = layer;
 
       if (!parse_variables_const_pool
          (lexer, cmd->pool, cmd->dict,
@@ -86,13 +79,13 @@ lex_is_variable (struct lexer *lexer, const struct dictionary *dict,
 
   tstr = lex_next_tokcstr (lexer, n);
 
-  if (NULL == dict_lookup_var (dict, tstr) )
+  if (NULL == dict_lookup_var (dict, tstr))
     return false;
 
   return true;
 }
 
-static bool
+bool
 means_parse (struct lexer *lexer, struct means *means)
 {
   /*   Optional TABLES =   */
@@ -106,20 +99,21 @@ means_parse (struct lexer *lexer, struct means *means)
   /* Parse the "tables" */
   while (more_tables)
     {
-      means->n_tables ++;
-      means->table = pool_realloc (means->pool, means->table, means->n_tables * sizeof (*means->table));
+      means->table = pool_realloc (means->pool, means->table,
+                                  (means->n_tables + 1) * sizeof (*means->table));
 
       if (! parse_means_table_syntax (lexer, means,
-                                     &means->table[means->n_tables - 1]))
+                                     &means->table[means->n_tables]))
        {
          return false;
        }
+      means->n_tables ++;
 
       /* Look ahead to see if there are more tables to be parsed */
       more_tables = false;
-      if ( T_SLASH == lex_next_token (lexer, 0) )
+      if (T_SLASH == lex_next_token (lexer, 0))
        {
-         if (lex_is_variable (lexer, means->dict, 1) )
+         if (lex_is_variable (lexer, means->dict, 1))
            {
              more_tables = true;
              lex_match (lexer, T_SLASH);
@@ -147,20 +141,9 @@ means_parse (struct lexer *lexer, struct means *means)
                values in the analysis.
              */
 
-             means->exclude = MV_SYSTEM;
+             means->ctrl_exclude = MV_SYSTEM;
              means->dep_exclude = MV_SYSTEM;
            }
-         else if (lex_match_id (lexer, "TABLE"))
-           /*
-             This is the default. (I think).
-             Every case containing a complete set of variables for a given
-             table. If any variable, categorical or dependent for in a table
-             is missing (as defined by what?), then that variable will
-             be dropped FOR THAT TABLE ONLY.
-           */
-           {
-             means->listwise_exclude = true;
-           }
          else if (lex_match_id (lexer, "DEPENDENT"))
            /*
              Use the command "/MISSING=DEPENDENT" to
@@ -177,7 +160,7 @@ means_parse (struct lexer *lexer, struct means *means)
            */
            {
              means->dep_exclude = MV_ANY;
-             means->exclude = MV_SYSTEM;
+             means->ctrl_exclude = MV_SYSTEM;
            }
          else
            {
@@ -191,15 +174,17 @@ means_parse (struct lexer *lexer, struct means *means)
 
          /* The default values become overwritten */
          means->n_statistics = 0;
-         free (means->statistics);
+         pool_free (means->pool, means->statistics);
          means->statistics = 0;
          while (lex_token (lexer) != T_ENDCMD
                 && lex_token (lexer) != T_SLASH)
            {
              if (lex_match (lexer, T_ALL))
                {
-                 free (means->statistics);
-                 means->statistics = xcalloc (n_MEANS_STATISTICS, sizeof (*means->statistics));
+                 pool_free (means->pool, means->statistics);
+                 means->statistics = pool_calloc (means->pool,
+                                                  n_MEANS_STATISTICS,
+                                                  sizeof (*means->statistics));
                  means->n_statistics = n_MEANS_STATISTICS;
                  int i;
                  for (i = 0; i < n_MEANS_STATISTICS; ++i)
@@ -210,13 +195,15 @@ means_parse (struct lexer *lexer, struct means *means)
              else if (lex_match_id (lexer, "NONE"))
                {
                  means->n_statistics = 0;
-                 free (means->statistics);
+                 pool_free (means->pool, means->statistics);
                  means->statistics = 0;
                }
              else if (lex_match_id (lexer, "DEFAULT"))
                {
-                 means->n_statistics = 3;
-                 means->statistics = xcalloc (3, sizeof *means->statistics);
+                 pool_free (means->pool, means->statistics);
+                 means->statistics = pool_calloc (means->pool,
+                                                  3,
+                                                  sizeof *means->statistics);
                  means->statistics[0] = MEANS_MEAN;
                  means->statistics[1] = MEANS_N;
                  means->statistics[2] = MEANS_STDDEV;
@@ -230,9 +217,10 @@ means_parse (struct lexer *lexer, struct means *means)
                      if (lex_match_id (lexer, cs->keyword))
                        {
                          means->statistics
-                           = xrealloc (means->statistics,
-                                       (means->n_statistics + 1)
-                                       * sizeof (*means->statistics));
+                           = pool_realloc (means->pool,
+                                          means->statistics,
+                                          (means->n_statistics + 1)
+                                          * sizeof (*means->statistics));
 
                          means->statistics[means->n_statistics] = i;
                          means->n_statistics++;
@@ -256,58 +244,3 @@ means_parse (struct lexer *lexer, struct means *means)
     }
   return true;
 }
-
-int
-cmd_means (struct lexer *lexer, struct dataset *ds)
-{
-  struct means means;
-  means.pool = pool_create ();
-
-  means.exclude = MV_ANY;
-  means.dep_exclude = MV_ANY;
-  means.listwise_exclude = false;
-  means.table = NULL;
-  means.n_tables = 0;
-
-  means.dict = dataset_dict (ds);
-
-  means.n_statistics = 3;
-  means.statistics = xcalloc (3, sizeof *means.statistics);
-  means.statistics[0] = MEANS_MEAN;
-  means.statistics[1] = MEANS_N;
-  means.statistics[2] = MEANS_STDDEV;
-
-  if (! means_parse (lexer, &means))
-    goto error;
-
-  {
-    struct casegrouper *grouper;
-    struct casereader *group;
-    bool ok;
-
-    grouper = casegrouper_create_splits (proc_open (ds), means.dict);
-    while (casegrouper_get_next_group (grouper, &group))
-      {
-       run_means (&means, group, ds);
-      }
-    ok = casegrouper_destroy (grouper);
-    ok = proc_commit (ds) && ok;
-  }
-
-  for (int t = 0; t < means.n_tables; ++t)
-    {
-      const struct mtable *table  = means.table + t;
-      means_shipout (table, &means);
-    }
-
-  return CMD_SUCCESS;
-
- error:
-
-  return CMD_FAILURE;
-}
-
-
-\f
-
-