subcase: Rename subcase_destroy() to subcase_uninit().
[pspp] / src / data / subcase.c
index 32056215e4ace37d4fcd9b544c3e48c493d82dec..ca4992ae6030d91bd4f5b4f38a8c0d4583075b2b 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2008, 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
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <data/subcase.h>
+
+#include "data/subcase.h"
+
 #include <stdlib.h>
-#include <data/case.h>
-#include <data/variable.h>
-#include <libpspp/assertion.h>
 
-#include "xalloc.h"
+#include "data/case.h"
+#include "data/variable.h"
+#include "libpspp/assertion.h"
+
+#include "gl/xalloc.h"
 
 static void invalidate_proto (struct subcase *sc);
 
@@ -40,18 +43,8 @@ void
 subcase_init_vars (struct subcase *sc,
                    const struct variable *const *vars, size_t n_vars)
 {
-  size_t i;
-
-  sc->fields = xnmalloc (n_vars, sizeof *sc->fields);
-  sc->n_fields = n_vars;
-  sc->proto = NULL;
-  for (i = 0; i < n_vars; i++)
-    {
-      struct subcase_field *field = &sc->fields[i];
-      field->case_index = var_get_case_index (vars[i]);
-      field->width = var_get_width (vars[i]);
-      field->direction = SC_ASCEND;
-    }
+  subcase_init_empty (sc);
+  subcase_add_vars_always (sc, vars, n_vars);
 }
 
 /* Initializes SC as a subcase with a single field extracted
@@ -93,7 +86,7 @@ subcase_clone (struct subcase *sc, const struct subcase *orig)
 
 /* Frees the memory owned by SC (but not SC itself). */
 void
-subcase_destroy (struct subcase *sc)
+subcase_uninit (struct subcase *sc)
 {
   free (sc->fields);
   caseproto_unref (sc->proto);
@@ -163,6 +156,27 @@ subcase_add_var_always (struct subcase *sc, const struct variable *var,
                              var_get_width (var), direction);
 }
 
+/* Add a field for each of the N_VARS variables in VAR to SC, regardless of
+   whether each variable already has a field in SC.  The fields are added with
+   ascending direction. */
+void
+subcase_add_vars_always (struct subcase *sc,
+                         const struct variable *const *vars, size_t n_vars)
+{
+  size_t i;
+
+  sc->fields = xnrealloc (sc->fields,
+                          sc->n_fields + n_vars, sizeof *sc->fields);
+  for (i = 0; i < n_vars; i++)
+    {
+      struct subcase_field *field = &sc->fields[sc->n_fields++];
+      field->case_index = var_get_case_index (vars[i]);
+      field->width = var_get_width (vars[i]);
+      field->direction = SC_ASCEND;
+    }
+  invalidate_proto (sc);
+}
+
 /* Add a field for CASE_INDEX, WIDTH to SC, with DIRECTION as the
    sort order, regardless of whether CASE_INDEX already has a
    field in SC. */