Patch #5672
[pspp-builds.git] / src / language / stats / autorecode.c
index d8b72ca682cb225d67a130b52c927e370d5db19f..a84528af3c1b97fd7e9c8d3a0cc8763e606388cc 100644 (file)
@@ -1,6 +1,5 @@
 /* PSPP - computes sample statistics.
    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -93,7 +92,6 @@ struct autorecode_pgm
 
 static trns_proc_func autorecode_trns_proc;
 static trns_free_func autorecode_trns_free;
-static bool autorecode_proc_func (const struct ccase *, void *, const struct dataset *);
 static hsh_compare_func compare_alpha_value, compare_numeric_value;
 static hsh_hash_func hash_alpha_value, hash_numeric_value;
 
@@ -105,6 +103,7 @@ int
 cmd_autorecode (struct lexer *lexer, struct dataset *ds)
 {
   struct autorecode_pgm arc;
+  struct ccase *c;
   size_t dst_cnt;
   size_t i;
   bool ok;
@@ -178,14 +177,37 @@ cmd_autorecode (struct lexer *lexer, struct dataset *ds)
   arc.dst_vars = xnmalloc (arc.var_cnt, sizeof *arc.dst_vars);
   arc.src_values = xnmalloc (arc.var_cnt, sizeof *arc.src_values);
   for (i = 0; i < dst_cnt; i++)
-    if (arc.src_vars[i]->type == ALPHA)
+    if (var_is_alpha (arc.src_vars[i]))
       arc.src_values[i] = hsh_create (10, compare_alpha_value,
                                       hash_alpha_value, NULL, arc.src_vars[i]);
     else
       arc.src_values[i] = hsh_create (10, compare_numeric_value,
                                       hash_numeric_value, NULL, NULL);
 
-  ok = procedure (ds, autorecode_proc_func, &arc);
+  proc_open (ds);
+  while (proc_read (ds, &c))
+    for (i = 0; i < arc.var_cnt; i++)
+      {
+        union arc_value v, *vp, **vpp;
+
+        if (var_is_numeric (arc.src_vars[i]))
+          v.f = case_num (c, arc.src_vars[i]);
+        else
+          v.c = (char *) case_str (c, arc.src_vars[i]);
+
+        vpp = (union arc_value **) hsh_probe (arc.src_values[i], &v);
+        if (*vpp == NULL)
+          {
+            vp = pool_alloc (arc.src_values_pool, sizeof *vp);
+            if (var_is_numeric (arc.src_vars[i]))
+              vp->f = v.f;
+            else
+              vp->c = pool_clone (arc.src_values_pool,
+                                  v.c, var_get_width (arc.src_vars[i]));
+            *vpp = vp;
+          }
+      }
+  ok = proc_close (ds);
 
   for (i = 0; i < arc.var_cnt; i++)
     arc.dst_vars[i] = dict_create_var_assert (dataset_dict (ds),
@@ -246,7 +268,7 @@ recode (struct dataset *ds, const struct autorecode_pgm *arc)
       spec->src = arc->src_vars[i];
       spec->dest = arc->dst_vars[i];
 
-      if (arc->src_vars[i]->type == ALPHA)
+      if (var_is_alpha (arc->src_vars[i]))
        spec->items = hsh_create (2 * count, compare_alpha_value,
                                  hash_alpha_value, NULL, arc->src_vars[i]);
       else
@@ -258,11 +280,11 @@ recode (struct dataset *ds, const struct autorecode_pgm *arc)
          struct arc_item *item = pool_alloc (trns->pool, sizeof *item);
           union arc_value *vp = *p;
           
-         if (arc->src_vars[i]->type == NUMERIC)
+         if (var_is_numeric (arc->src_vars[i]))
             item->from.f = vp->f;
           else
            item->from.c = pool_clone (trns->pool, vp->c,
-                                       arc->src_vars[i]->width);
+                                       var_get_width (arc->src_vars[i]));
          item->to = arc->direction == ASCENDING ? j + 1 : count - j;
          hsh_force_insert (spec->items, item);
        }
@@ -284,13 +306,13 @@ autorecode_trns_proc (void *trns_, struct ccase *c, casenumber case_idx UNUSED)
       struct arc_item *item;
       union arc_value v;
 
-      if (spec->src->type == NUMERIC)
-        v.f = case_num (c, spec->src->fv);
+      if (var_is_numeric (spec->src))
+        v.f = case_num (c, spec->src);
       else
-        v.c = (char *) case_str (c, spec->src->fv);
+        v.c = (char *) case_str (c, spec->src);
       item = hsh_force_find (spec->items, &v);
 
-      case_data_rw (c, spec->dest->fv)->f = item->to;
+      case_data_rw (c, spec->dest)->f = item->to;
     }
   return TRNS_CONTINUE;
 }
@@ -317,7 +339,7 @@ compare_alpha_value (const void *a_, const void *b_, const void *v_)
   const union arc_value *b = b_;
   const struct variable *v = v_;
 
-  return memcmp (a->c, b->c, v->width);
+  return memcmp (a->c, b->c, var_get_width (v));
 }
 
 static unsigned
@@ -326,7 +348,7 @@ hash_alpha_value (const void *a_, const void *v_)
   const union arc_value *a = a_;
   const struct variable *v = v_;
   
-  return hsh_hash_bytes (a->c, v->width);
+  return hsh_hash_bytes (a->c, var_get_width (v));
 }
 
 static int
@@ -345,33 +367,3 @@ hash_numeric_value (const void *a_, const void *aux UNUSED)
 
   return hsh_hash_double (a->f);
 }
-
-static bool
-autorecode_proc_func (const struct ccase *c, void *arc_, const struct dataset *ds UNUSED)
-{
-  struct autorecode_pgm *arc = arc_;
-  size_t i;
-
-  for (i = 0; i < arc->var_cnt; i++)
-    {
-      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 arc_value **) hsh_probe (arc->src_values[i], &v);
-      if (*vpp == NULL)
-        {
-          vp = pool_alloc (arc->src_values_pool, sizeof *vp);
-          if (arc->src_vars[i]->type == NUMERIC)
-            vp->f = v.f;
-          else
-            vp->c = pool_clone (arc->src_values_pool,
-                                v.c, arc->src_vars[i]->width);
-          *vpp = vp;
-        }
-    }
-  return true;
-}