Frequencies Dialog: Avoid initialising things more than once per dialog
[pspp] / src / data / casegrouper.c
index 86788ba7a3bc5afcbd280ad2be920685a48e76db..48560bb6a5614766edfab0d345e15e0bc6027e9e 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2007, 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
 
 #include <config.h>
 
-#include <data/casegrouper.h>
+#include "data/casegrouper.h"
 
 #include <stdlib.h>
 
-#include <data/casereader.h>
-#include <data/casewriter.h>
-#include <data/dictionary.h>
-#include <data/subcase.h>
-#include <libpspp/taint.h>
+#include "data/casereader.h"
+#include "data/casewriter.h"
+#include "data/dictionary.h"
+#include "data/subcase.h"
+#include "libpspp/taint.h"
 
-#include "xalloc.h"
+#include "gl/xalloc.h"
 
 /* A casegrouper. */
 struct casegrouper
@@ -47,6 +47,8 @@ struct casegrouper
    the casegrouper is destroyed and should free any storage
    needed by SAME_GROUP.
 
+   Takes ownerhip of READER.
+
    SAME_GROUP may be a null pointer.  If so, READER's entire
    contents is considered to be a single group. */
 struct casegrouper *
@@ -89,9 +91,10 @@ casegrouper_get_next_group (struct casegrouper *grouper,
           return false;
         }
 
-      writer = autopaging_writer_create (casereader_get_value_cnt (grouper->reader));
-      case_ref (group_case);
-      casewriter_write (writer, group_case);
+      writer = autopaging_writer_create (
+        casereader_get_proto (grouper->reader));
+
+      casewriter_write (writer, case_ref (group_case));
 
       while ((tmp = casereader_peek (grouper->reader, 0)) != NULL
              && grouper->same_group (group_case, tmp, grouper->aux))
@@ -99,6 +102,7 @@ casegrouper_get_next_group (struct casegrouper *grouper,
           case_unref (casereader_read (grouper->reader));
           casewriter_write (writer, tmp);
         }
+      case_unref (tmp);
       case_unref (group_case);
 
       *reader = casewriter_make_reader (writer);
@@ -165,7 +169,9 @@ static void casegrouper_vars_destroy (void *);
 /* Creates and returns a casegrouper that reads data from READER
    and breaks it into contiguous groups of cases that have equal
    values for the VAR_CNT variables in VARS.  If VAR_CNT is 0,
-   then all the cases will be put in a single group. */
+   then all the cases will be put in a single group.
+
+   Takes ownerhip of READER. */
 struct casegrouper *
 casegrouper_create_vars (struct casereader *reader,
                          const struct variable *const *vars,
@@ -186,7 +192,9 @@ casegrouper_create_vars (struct casereader *reader,
    and breaks it into contiguous groups of cases that have equal
    values for the SPLIT FILE variables in DICT.  If DICT has no
    SPLIT FILE variables, then all the cases will be put into a
-   single group. */
+   single group.
+
+   Takes ownerhip of READER. */
 struct casegrouper *
 casegrouper_create_splits (struct casereader *reader,
                            const struct dictionary *dict)
@@ -200,7 +208,9 @@ casegrouper_create_splits (struct casereader *reader,
    and breaks it into contiguous groups of cases that have equal
    values for the variables used for sorting in SC.  If SC is
    empty (contains no fields), then all the cases will be put
-   into a single group. */
+   into a single group.
+
+   Takes ownerhip of READER. */
 struct casegrouper *
 casegrouper_create_subcase (struct casereader *reader,
                             const struct subcase *sc)