Work on getting rid of trns_chain_finalize().
[pspp] / src / data / transformations.c
index 2a73cade41bbcf6e4c55622c5d00b9d8b18505b6..8b246b7399d036826361412bc0b3657b702f4a93 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2009 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2009, 2011, 2013, 2015 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
 
 #include <config.h>
 
-#include <data/transformations.h>
+#include "data/transformations.h"
 
 #include <assert.h>
 #include <stdlib.h>
 
-#include <libpspp/str.h>
+#include "libpspp/str.h"
+#include "data/control-stack.h" /* XXX layering violation */
 
-#include "xalloc.h"
+#include "gl/xalloc.h"
 
 /* A single transformation. */
 struct transformation
@@ -32,7 +33,6 @@ struct transformation
        transformation index.  Normally 0 but set to the starting
        index of a spliced chain after splicing. */
     int idx_ofs;
-    trns_finalize_func *finalize;       /* Finalize proc. */
     trns_proc_func *execute;            /* Executes the transformation. */
     trns_free_func *free;               /* Garbage collector proc. */
     void *aux;                          /* Auxiliary data. */
@@ -44,7 +44,6 @@ struct trns_chain
     struct transformation *trns;        /* Array of transformations. */
     size_t trns_cnt;                    /* Number of transformations. */
     size_t trns_cap;                    /* Allocated capacity. */
-    bool finalized;                     /* Finalize functions called? */
   };
 
 /* Allocates and returns a new transformation chain. */
@@ -55,35 +54,10 @@ trns_chain_create (void)
   chain->trns = NULL;
   chain->trns_cnt = 0;
   chain->trns_cap = 0;
-  chain->finalized = false;
   return chain;
 }
 
-/* Finalizes all the transformations in CHAIN.
-   A chain is only finalized once; afterward, calling this
-   function is a no-op.
-   Finalizers may add transformations to CHAIN, but after
-   finalization the chain's contents are fixed, so that no more
-   transformations may be added afterward. */
-void
-trns_chain_finalize (struct trns_chain *chain)
-{
-  if (!chain->finalized)
-    {
-      size_t i;
-
-      for (i = 0; i < chain->trns_cnt; i++)
-        {
-          struct transformation *trns = &chain->trns[i];
-          if (trns->finalize != NULL)
-            trns->finalize (trns->aux);
-        }
-      chain->finalized = true;
-    }
-}
-
-/* Destroys CHAIN, finalizing it in the process if it has not
-   already been finalized. */
+/* Destroys CHAIN. */
 bool
 trns_chain_destroy (struct trns_chain *chain)
 {
@@ -93,9 +67,6 @@ trns_chain_destroy (struct trns_chain *chain)
     {
       size_t i;
 
-      /* Needed to ensure that the control stack gets cleared. */
-      trns_chain_finalize (chain);
-
       for (i = 0; i < chain->trns_cnt; i++)
         {
           struct transformation *trns = &chain->trns[i];
@@ -117,41 +88,31 @@ trns_chain_is_empty (const struct trns_chain *chain)
   return chain->trns_cnt == 0;
 }
 
-/* Adds a transformation to CHAIN with finalize function
-   FINALIZE, execute function EXECUTE, free function FREE, and
-   auxiliary data AUX. */
+/* Adds a transformation to CHAIN with execute function EXECUTE, free function
+   FREE, and auxiliary data AUX. */
 void
-trns_chain_append (struct trns_chain *chain, trns_finalize_func *finalize,
-                   trns_proc_func *execute, trns_free_func *free,
-                   void *aux)
+trns_chain_append (struct trns_chain *chain, trns_proc_func *execute,
+                   trns_free_func *free, void *aux)
 {
   struct transformation *trns;
 
-  assert (!chain->finalized);
-
   if (chain->trns_cnt == chain->trns_cap)
     chain->trns = x2nrealloc (chain->trns, &chain->trns_cap,
                               sizeof *chain->trns);
 
   trns = &chain->trns[chain->trns_cnt++];
   trns->idx_ofs = 0;
-  trns->finalize = finalize;
   trns->execute = execute;
   trns->free = free;
   trns->aux = aux;
 }
 
-/* Appends the transformations in SRC to those in DST,
-   and destroys SRC.
-   Both DST and SRC must already be finalized. */
+/* Appends the transformations in SRC to those in DST, and destroys SRC. */
 void
 trns_chain_splice (struct trns_chain *dst, struct trns_chain *src)
 {
   size_t i;
 
-  assert (dst->finalized);
-  assert (src->finalized);
-
   if (dst->trns_cnt + src->trns_cnt > dst->trns_cap)
     {
       dst->trns_cap = dst->trns_cnt + src->trns_cnt;
@@ -167,6 +128,7 @@ trns_chain_splice (struct trns_chain *dst, struct trns_chain *src)
     }
   dst->trns_cnt += src->trns_cnt;
 
+  src->trns_cnt = 0;
   trns_chain_destroy (src);
 }
 
@@ -188,19 +150,33 @@ enum trns_result
 trns_chain_execute (const struct trns_chain *chain, enum trns_result start,
                     struct ccase **c, casenumber case_nr)
 {
-  size_t i;
+  int i;
 
-  assert (chain->finalized);
   for (i = start < 0 ? 0 : start; i < chain->trns_cnt; )
     {
       struct transformation *trns = &chain->trns[i];
-      int retval = trns->execute (trns->aux, c, case_nr);
-      if (retval == TRNS_CONTINUE)
-        i++;
-      else if (retval >= 0)
-        i = retval + trns->idx_ofs;
-      else
-        return retval == TRNS_END_CASE ? i + 1 : retval;
+      int retval;
+
+      retval = trns->execute (trns->aux, c, case_nr);
+      switch (retval)
+        {
+        case TRNS_CONTINUE:
+          i++;
+          break;
+
+        case TRNS_END_CASE:
+          return i + 1;
+
+        case TRNS_DROP_CASE:
+        case TRNS_ERROR:
+        case TRNS_END_FILE:
+          return retval;
+
+        default:
+          i += retval;
+          assert (i <= chain->trns_cnt);
+          break;
+        }
     }
 
   return TRNS_CONTINUE;