Added casereader_clone function.
[pspp-builds.git] / src / language / stats / autorecode.c
index 1aea061f5af3f6e45b893245aeceb9a51b418bf5..f967dd3aca217d29a7594d861707bb6b82dd47f0 100644 (file)
    02110-1301, USA. */
 
 #include <config.h>
-#include "message.h"
 #include <stdlib.h>
-#include "alloc.h"
-#include "case.h"
-#include "command.h"
-#include "compiler.h"
-#include "dictionary.h"
-#include "message.h"
-#include "hash.h"
-#include "lexer.h"
-#include "pool.h"
-#include "str.h"
-#include "variable.h"
-#include "procedure.h"
+
+#include <data/case.h>
+#include <data/dictionary.h>
+#include <data/procedure.h>
+#include <data/transformations.h>
+#include <data/variable.h>
+#include <language/command.h>
+#include <language/lexer/lexer.h>
+#include <language/lexer/variable-parser.h>
+#include <libpspp/alloc.h>
+#include <libpspp/compiler.h>
+#include <libpspp/hash.h>
+#include <libpspp/message.h>
+#include <libpspp/message.h>
+#include <libpspp/pool.h>
+#include <libpspp/str.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
 /* FIXME: Implement PRINT subcommand. */
 
+/* An AUTORECODE variable's original value. */
+union arc_value 
+  {
+    double f;                   /* Numeric. */
+    char *c;                    /* Short or long string. */
+  };
+
 /* Explains how to recode one value.  `from' must be first element.  */
 struct arc_item
   {
-    union value from;          /* Original value. */
+    union arc_value from;      /* Original value. */
     double to;                 /* Recoded value. */
   };
 
@@ -74,7 +84,7 @@ struct autorecode_pgm
     struct variable **src_vars;    /* Source variables. */
     char **dst_names;              /* Target variable names. */
     struct variable **dst_vars;    /* Target variables. */
-    struct hsh_table **src_values; /* `union value's of source vars. */
+    struct hsh_table **src_values; /* `union arc_value's of source vars. */
     size_t var_cnt;                /* Number of variables. */
     struct pool *src_values_pool;  /* Pool used by src_values. */
     enum direction direction;      /* Sort order. */
@@ -83,7 +93,7 @@ struct autorecode_pgm
 
 static trns_proc_func autorecode_trns_proc;
 static trns_free_func autorecode_trns_free;
-static bool autorecode_proc_func (struct ccase *, void *);
+static bool autorecode_proc_func (const struct ccase *, void *);
 static hsh_compare_func compare_alpha_value, compare_numeric_value;
 static hsh_hash_func hash_alpha_value, hash_numeric_value;
 
@@ -178,11 +188,8 @@ cmd_autorecode (void)
   ok = procedure (autorecode_proc_func, &arc);
 
   for (i = 0; i < arc.var_cnt; i++)
-    {
-      arc.dst_vars[i] = dict_create_var_assert (default_dict,
-                                                arc.dst_names[i], 0);
-      arc.dst_vars[i]->init = 0;
-    }
+    arc.dst_vars[i] = dict_create_var_assert (default_dict,
+                                              arc.dst_names[i], 0);
 
   recode (&arc);
   arc_free (&arc);
@@ -249,7 +256,7 @@ recode (const struct autorecode_pgm *arc)
       for (j = 0; *p; p++, j++)
        {
          struct arc_item *item = pool_alloc (trns->pool, sizeof *item);
-          union value *vp = *p;
+          union arc_value *vp = *p;
           
          if (arc->src_vars[i]->type == NUMERIC)
             item->from.f = vp->f;
@@ -265,7 +272,7 @@ recode (const struct autorecode_pgm *arc)
 
 /* Executes an AUTORECODE transformation. */
 static int
-autorecode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED)
+autorecode_trns_proc (void *trns_, struct ccase *c, casenum_t case_idx UNUSED)
 {
   struct autorecode_trns *trns = trns_;
   size_t i;
@@ -274,7 +281,7 @@ autorecode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED)
     {
       struct arc_spec *spec = &trns->specs[i];
       struct arc_item *item;
-      union value v;
+      union arc_value v;
 
       if (spec->src->type == NUMERIC)
         v.f = case_num (c, spec->src->fv);
@@ -305,8 +312,8 @@ autorecode_trns_free (void *trns_)
 static int
 compare_alpha_value (const void *a_, const void *b_, void *v_)
 {
-  const union value *a = a_;
-  const union value *b = b_;
+  const union arc_value *a = a_;
+  const union arc_value *b = b_;
   const struct variable *v = v_;
 
   return memcmp (a->c, b->c, v->width);
@@ -315,7 +322,7 @@ compare_alpha_value (const void *a_, const void *b_, void *v_)
 static unsigned
 hash_alpha_value (const void *a_, void *v_)
 {
-  const union value *a = a_;
+  const union arc_value *a = a_;
   const struct variable *v = v_;
   
   return hsh_hash_bytes (a->c, v->width);
@@ -324,8 +331,8 @@ hash_alpha_value (const void *a_, void *v_)
 static int
 compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
 {
-  const union value *a = a_;
-  const union value *b = b_;
+  const union arc_value *a = a_;
+  const union arc_value *b = b_;
 
   return a->f < b->f ? -1 : a->f > b->f;
 }
@@ -333,27 +340,27 @@ compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
 static unsigned
 hash_numeric_value (const void *a_, void *foo UNUSED)
 {
-  const union value *a = a_;
+  const union arc_value *a = a_;
 
   return hsh_hash_double (a->f);
 }
 
 static bool
-autorecode_proc_func (struct ccase *c, void *arc_)
+autorecode_proc_func (const struct ccase *c, void *arc_)
 {
   struct autorecode_pgm *arc = arc_;
   size_t i;
 
   for (i = 0; i < arc->var_cnt; i++)
     {
-      union value v, *vp, **vpp;
+      union arc_value v, *vp, **vpp;
 
       if (arc->src_vars[i]->type == NUMERIC)
         v.f = case_num (c, arc->src_vars[i]->fv);
       else
         v.c = (char *) case_str (c, arc->src_vars[i]->fv);
 
-      vpp = (union value **) hsh_probe (arc->src_values[i], &v);
+      vpp = (union arc_value **) hsh_probe (arc->src_values[i], &v);
       if (*vpp == NULL)
         {
           vp = pool_alloc (arc->src_values_pool, sizeof *vp);