treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / data / caseinit.c
index 572f9160292e670f1a60ae99c226b2ad13b064de..c93de348728c08ab096ae8f41fae0b543769114a 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2009, 2010, 2011 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/caseinit.h>
+#include "data/caseinit.h"
 
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <data/case.h>
-#include <data/dictionary.h>
-#include <data/value.h>
-#include <data/variable.h>
-#include <libpspp/array.h>
-#include <libpspp/assertion.h>
-#include <libpspp/compiler.h>
+#include "data/case.h"
+#include "data/dictionary.h"
+#include "data/value.h"
+#include "data/variable.h"
+#include "libpspp/array.h"
+#include "libpspp/assertion.h"
+#include "libpspp/compiler.h"
 
-#include "xalloc.h"
+#include "gl/xalloc.h"
 \f
 /* Initializer list: a set of values to write to locations within
    a case. */
@@ -53,7 +53,7 @@ struct init_list
 /* A bitmap of the "left" status of variables. */
 enum leave_class
   {
-    LEAVE_REINIT = 0x001,       /* Reinitalize for every case. */
+    LEAVE_REINIT = 0x001,       /* Reinitialize for every case. */
     LEAVE_LEFT = 0x002          /* Keep the value from one case to the next. */
   };
 
@@ -65,6 +65,22 @@ init_list_create (struct init_list *list)
   list->cnt = 0;
 }
 
+/* Initializes NEW as a copy of OLD. */
+static void
+init_list_clone (struct init_list *new, const struct init_list *old)
+{
+  size_t i;
+
+  new->values = xmemdup (old->values, old->cnt * sizeof *old->values);
+  new->cnt = old->cnt;
+
+  for (i = 0; i < new->cnt; i++)
+    {
+      struct init_value *iv = &new->values[i];
+      value_clone (&iv->value, &iv->value, iv->width);
+    }
+}
+
 /* Frees the storage associated with LIST. */
 static void
 init_list_destroy (struct init_list *list)
@@ -112,13 +128,12 @@ static void
 init_list_mark (struct init_list *list, const struct init_list *exclude,
                 enum leave_class include, const struct dictionary *d)
 {
-  size_t var_cnt = dict_get_var_cnt (d);
-  size_t i;
+  size_t n_vars = dict_get_n_vars (d);
 
   assert (list != exclude);
-  list->values = xnrealloc (list->values, list->cnt + dict_get_var_cnt (d),
+  list->values = xnrealloc (list->values, list->cnt + dict_get_n_vars (d),
                             sizeof *list->values);
-  for (i = 0; i < var_cnt; i++)
+  for (size_t i = 0; i < n_vars; i++)
     {
       struct variable *v = dict_get_var (d, i);
       size_t case_index = var_get_case_index (v);
@@ -198,6 +213,17 @@ caseinit_create (void)
   return ci;
 }
 
+/* Creates and returns a copy of OLD. */
+struct caseinit *
+caseinit_clone (struct caseinit *old)
+{
+  struct caseinit *new = xmalloc (sizeof *new);
+  init_list_clone (&new->preinited_values, &old->preinited_values);
+  init_list_clone (&new->reinit_values, &old->reinit_values);
+  init_list_clone (&new->left_values, &old->left_values);
+  return new;
+}
+
 /* Clears the contents of case initializer CI. */
 void
 caseinit_clear (struct caseinit *ci)