treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / data / transformations.c
index 675e1c91d95b682b87acb05a3d477125398e88d5..cd5c6a188c8048319090083ab6c221cf870527fc 100644 (file)
@@ -1,32 +1,29 @@
-/* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 1997-9, 2000, 2006, 2009, 2011, 2013 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 the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #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 "xalloc.h"
+#include "gl/xalloc.h"
 
 /* A single transformation. */
 struct transformation
@@ -34,7 +31,7 @@ struct transformation
     /* Offset to add to EXECUTE's return value, if it returns a
        transformation index.  Normally 0 but set to the starting
        index of a spliced chain after splicing. */
-    int idx_ofs;                        
+    int idx_ofs;
     trns_finalize_func *finalize;       /* Finalize proc. */
     trns_proc_func *execute;            /* Executes the transformation. */
     trns_free_func *free;               /* Garbage collector proc. */
@@ -42,81 +39,81 @@ struct transformation
   };
 
 /* A chain of transformations. */
-struct trns_chain 
+struct trns_chain
   {
     struct transformation *trns;        /* Array of transformations. */
-    size_t trns_cnt;                    /* Number of transformations. */
-    size_t trns_cap;                    /* Allocated capacity. */
+    size_t n_trns;                      /* Number of transformations. */
+    size_t allocated_trns;              /* Allocated capacity. */
     bool finalized;                     /* Finalize functions called? */
   };
 
 /* Allocates and returns a new transformation chain. */
 struct trns_chain *
-trns_chain_create (void) 
+trns_chain_create (void)
 {
   struct trns_chain *chain = xmalloc (sizeof *chain);
   chain->trns = NULL;
-  chain->trns_cnt = 0;
-  chain->trns_cap = 0;
+  chain->n_trns = 0;
+  chain->allocated_trns = 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. */
+/* Finalizes all the un-finalized transformations in CHAIN.
+   Any given transformation is only finalized once. */
 void
-trns_chain_finalize (struct trns_chain *chain) 
+trns_chain_finalize (struct trns_chain *chain)
 {
-  if (!chain->finalized) 
+  while (!chain->finalized)
     {
       size_t i;
 
-      for (i = 0; i < chain->trns_cnt; i++) 
+      chain->finalized = true;
+      for (i = 0; i < chain->n_trns; i++)
         {
           struct transformation *trns = &chain->trns[i];
-          if (trns->finalize != NULL)
-            trns->finalize (trns->aux); 
+          trns_finalize_func *finalize = trns->finalize;
+
+          trns->finalize = NULL;
+          if (finalize != NULL)
+            finalize (trns->aux);
         }
-      chain->finalized = true;
     }
 }
 
 /* Destroys CHAIN, finalizing it in the process if it has not
    already been finalized. */
 bool
-trns_chain_destroy (struct trns_chain *chain) 
+trns_chain_destroy (struct trns_chain *chain)
 {
   bool ok = true;
 
-  if (chain != NULL) 
+  if (chain != NULL)
     {
       size_t i;
-      
+
       /* Needed to ensure that the control stack gets cleared. */
       trns_chain_finalize (chain);
-      
-      for (i = 0; i < chain->trns_cnt; i++) 
+
+      for (i = 0; i < chain->n_trns; i++)
         {
           struct transformation *trns = &chain->trns[i];
-          if (trns->free != NULL) 
+          if (trns->free != NULL)
             ok = trns->free (trns->aux) && ok;
         }
+      free (chain->trns);
       free (chain);
     }
-  
+
   return ok;
 }
 
 /* Returns true if CHAIN contains any transformations,
    false otherwise. */
 bool
-trns_chain_is_empty (const struct trns_chain *chain) 
+trns_chain_is_empty (const struct trns_chain *chain)
 {
-  return chain->trns_cnt == 0;
+  return chain->n_trns == 0;
 }
 
 /* Adds a transformation to CHAIN with finalize function
@@ -125,17 +122,17 @@ trns_chain_is_empty (const struct trns_chain *chain)
 void
 trns_chain_append (struct trns_chain *chain, trns_finalize_func *finalize,
                    trns_proc_func *execute, trns_free_func *free,
-                   void *aux) 
+                   void *aux)
 {
   struct transformation *trns;
 
-  assert (!chain->finalized);
+  chain->finalized = false;
 
-  if (chain->trns_cnt == chain->trns_cap)
-    chain->trns = x2nrealloc (chain->trns, &chain->trns_cap,
+  if (chain->n_trns == chain->allocated_trns)
+    chain->trns = x2nrealloc (chain->trns, &chain->allocated_trns,
                               sizeof *chain->trns);
 
-  trns = &chain->trns[chain->trns_cnt++];
+  trns = &chain->trns[chain->n_trns++];
   trns->idx_ofs = 0;
   trns->finalize = finalize;
   trns->execute = execute;
@@ -147,63 +144,63 @@ trns_chain_append (struct trns_chain *chain, trns_finalize_func *finalize,
    and destroys SRC.
    Both DST and SRC must already be finalized. */
 void
-trns_chain_splice (struct trns_chain *dst, struct trns_chain *src) 
+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) 
+  if (dst->n_trns + src->n_trns > dst->allocated_trns)
     {
-      dst->trns_cap = dst->trns_cnt + src->trns_cnt;
-      dst->trns = xnrealloc (dst->trns, dst->trns_cap, sizeof *dst->trns);
+      dst->allocated_trns = dst->n_trns + src->n_trns;
+      dst->trns = xnrealloc (dst->trns, dst->allocated_trns, sizeof *dst->trns);
     }
 
-  for (i = 0; i < src->trns_cnt; i++) 
+  for (i = 0; i < src->n_trns; i++)
     {
-      struct transformation *d = &dst->trns[i + dst->trns_cnt];
+      struct transformation *d = &dst->trns[i + dst->n_trns];
       const struct transformation *s = &src->trns[i];
       *d = *s;
-      d->idx_ofs += src->trns_cnt; 
+      d->idx_ofs += src->n_trns;
     }
-  dst->trns_cnt += src->trns_cnt;
+  dst->n_trns += src->n_trns;
 
+  src->n_trns = 0;
   trns_chain_destroy (src);
 }
 
 /* Returns the index that a transformation execution function may
    return to "jump" to the next transformation to be added. */
 size_t
-trns_chain_next (struct trns_chain *chain) 
+trns_chain_next (struct trns_chain *chain)
 {
-  return chain->trns_cnt;
+  return chain->n_trns;
 }
 
-/* Executes the given CHAIN of transformations on C,
-   passing *CASE_NR as the case number.
-   If a transformation modifies *CASE_NR, it will affect the case
-   number passed to following transformations.
+/* Executes the given CHAIN of transformations on *C,
+   passing CASE_NR as the case number.
+   *C may be replaced by a new case.
    Returns the result code that caused the transformations to
    terminate, or TRNS_CONTINUE if the transformations finished
    due to "falling off the end" of the set of transformations. */
 enum trns_result
-trns_chain_execute (struct trns_chain *chain, struct ccase *c,
-                    const size_t *case_nr) 
+trns_chain_execute (const struct trns_chain *chain, enum trns_result start,
+                    struct ccase **c, casenumber case_nr)
 {
   size_t i;
 
   assert (chain->finalized);
-  for (i = 0; i < chain->trns_cnt; )
+  for (i = start < 0 ? 0 : start; i < chain->n_trns;)
     {
       struct transformation *trns = &chain->trns[i];
-      int retval = trns->execute (trns->aux, c, *case_nr);
+      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
+        return retval == TRNS_END_CASE ? i + 1 : retval;
     }
 
   return TRNS_CONTINUE;