X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=inline;f=src%2Fdata%2Fcasewriter.c;h=6029fc0dbe561fae7db8870559a260da072146cc;hb=5d612c43713291cae1aa473ee3d31786ef6f5b99;hp=e277749c2e99fd6d2a10ec71946686bd1694233c;hpb=ed88deded6a59254dd55883308c4c20966efc77e;p=pspp
diff --git a/src/data/casewriter.c b/src/data/casewriter.c
index e277749c2e..6029fc0dbe 100644
--- a/src/data/casewriter.c
+++ b/src/data/casewriter.c
@@ -1,54 +1,59 @@
-/* PSPP - computes sample statistics.
- Copyright (C) 2007 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+ Copyright (C) 2007, 2009, 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 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 . */
#include
-#include
-#include
+#include "data/casewriter.h"
+#include "data/casewriter-provider.h"
#include
#include
-#include
-#include
-#include
-#include
-#include
-#include
+#include "data/casereader.h"
+#include "data/casereader-provider.h"
+#include "data/casewindow.h"
+#include "data/settings.h"
+#include "libpspp/assertion.h"
+#include "libpspp/compiler.h"
+#include "libpspp/taint.h"
-#include "xalloc.h"
+#include "gl/xalloc.h"
/* A casewriter. */
struct casewriter
{
struct taint *taint;
+ struct caseproto *proto;
casenumber case_cnt;
const struct casewriter_class *class;
void *aux;
};
-static struct casewriter *create_casewriter_window (size_t value_cnt,
+static struct casewriter *create_casewriter_window (const struct caseproto *,
casenumber max_in_core);
-/* Writes case C to WRITER. */
+/* Writes case C to WRITER. Ownership of C is transferred to
+ WRITER. */
void
casewriter_write (struct casewriter *writer, struct ccase *c)
{
+ size_t n_widths UNUSED = caseproto_get_n_widths (writer->proto);
+ assert (case_get_value_cnt (c) >= n_widths);
+ expensive_assert (caseproto_equal (case_get_proto (c), 0,
+ writer->proto, 0, n_widths));
writer->class->write (writer, writer->aux, c);
}
@@ -57,18 +62,27 @@ casewriter_write (struct casewriter *writer, struct ccase *c)
encountered on WRITER or on some object on which WRITER has a
dependency. */
bool
-casewriter_destroy (struct casewriter *writer)
+casewriter_destroy (struct casewriter *writer)
{
bool ok = true;
if (writer != NULL)
{
writer->class->destroy (writer, writer->aux);
ok = taint_destroy (writer->taint);
+ caseproto_unref (writer->proto);
free (writer);
}
return ok;
}
+/* Returns the prototype for that cases written to WRITER must
+ follow. */
+const struct caseproto *
+casewriter_get_proto (const struct casewriter *writer)
+{
+ return writer->proto;
+}
+
/* Destroys WRITER and in its place returns a casereader that can
be used to read back the data written to WRITER. WRITER must
not be used again after calling this function, even as an
@@ -83,9 +97,10 @@ casewriter_destroy (struct casewriter *writer)
struct casereader *
casewriter_make_reader (struct casewriter *writer)
{
- struct casereader *reader;
- reader = writer->class->convert_to_reader (writer, writer->aux);
+ struct casereader *reader = writer->class->convert_to_reader (writer, writer->aux);
taint_propagate (writer->taint, casereader_get_taint (reader));
+
+ caseproto_unref (writer->proto);
taint_destroy (writer->taint);
free (writer);
return reader;
@@ -107,7 +122,7 @@ casewriter_rename (struct casewriter *writer)
occurred on WRITER, a clone of WRITER, or on some object on
which WRITER's data has a dependency, false otherwise. */
bool
-casewriter_error (const struct casewriter *writer)
+casewriter_error (const struct casewriter *writer)
{
return taint_is_tainted (writer->taint);
}
@@ -121,7 +136,7 @@ casewriter_error (const struct casewriter *writer)
taint_propagate to propagate to the casewriter's taint
structure, which may be obtained via casewriter_get_taint. */
void
-casewriter_force_error (struct casewriter *writer)
+casewriter_force_error (struct casewriter *writer)
{
taint_set_taint (writer->taint);
}
@@ -129,61 +144,66 @@ casewriter_force_error (struct casewriter *writer)
/* Returns WRITER's associate taint object, for use with
taint_propagate and other taint functions. */
const struct taint *
-casewriter_get_taint (const struct casewriter *writer)
+casewriter_get_taint (const struct casewriter *writer)
{
return writer->taint;
}
/* Creates and returns a new casewriter with the given CLASS and
- auxiliary data AUX. */
+ auxiliary data AUX. The casewriter accepts cases that match
+ case prototype PROTO, of which the caller retains
+ ownership. */
struct casewriter *
-casewriter_create (const struct casewriter_class *class, void *aux)
+casewriter_create (const struct caseproto *proto,
+ const struct casewriter_class *class, void *aux)
{
struct casewriter *writer = xmalloc (sizeof *writer);
writer->taint = taint_create ();
+ writer->proto = caseproto_ref (proto);
writer->case_cnt = 0;
writer->class = class;
writer->aux = aux;
return writer;
}
-/* Returns a casewriter for cases with VALUE_CNT struct values
- per case. The cases written to the casewriter will be kept in
+/* Returns a casewriter for cases that match case prototype
+ PROTO. The cases written to the casewriter will be kept in
memory, unless the amount of memory used grows too large, in
which case they will be written to disk.
A casewriter created with this function may be passed to
- casewriter_make_reader.
+ casewriter_make_reader.
This is usually the right kind of casewriter to use. */
struct casewriter *
-autopaging_writer_create (size_t value_cnt)
+autopaging_writer_create (const struct caseproto *proto)
{
- return create_casewriter_window (value_cnt, get_workspace_cases (value_cnt));
+ return create_casewriter_window (proto,
+ settings_get_workspace_cases (proto));
}
-/* Returns a casewriter for cases with VALUE_CNT struct values
- per case. The cases written to the casewriter will be kept in
+/* Returns a casewriter for cases that match case prototype
+ PROTO. The cases written to the casewriter will be kept in
memory.
A casewriter created with this function may be passed to
casewriter_make_reader. */
struct casewriter *
-mem_writer_create (size_t value_cnt)
+mem_writer_create (const struct caseproto *proto)
{
- return create_casewriter_window (value_cnt, CASENUMBER_MAX);
+ return create_casewriter_window (proto, CASENUMBER_MAX);
}
-/* Returns a casewriter for cases with VALUE_CNT struct values
- per case. The cases written to the casewriter will be written
+/* Returns a casewriter for cases that match case prototype
+ PROTO. The cases written to the casewriter will be written
to disk.
A casewriter created with this function may be passed to
casewriter_make_reader. */
struct casewriter *
-tmpfile_writer_create (size_t value_cnt)
+tmpfile_writer_create (const struct caseproto *proto)
{
- return create_casewriter_window (value_cnt, 0);
+ return create_casewriter_window (proto, 0);
}
static const struct casewriter_class casewriter_window_class;
@@ -195,10 +215,12 @@ static const struct casereader_random_class casereader_window_class;
memory until MAX_IN_CORE_CASES have been written, at which
point they will be written to disk. */
static struct casewriter *
-create_casewriter_window (size_t value_cnt, casenumber max_in_core_cases)
+create_casewriter_window (const struct caseproto *proto,
+ casenumber max_in_core_cases)
{
- struct casewindow *window = casewindow_create (value_cnt, max_in_core_cases);
- struct casewriter *writer = casewriter_create (&casewriter_window_class,
+ struct casewindow *window = casewindow_create (proto, max_in_core_cases);
+ struct casewriter *writer = casewriter_create (proto,
+ &casewriter_window_class,
window);
taint_propagate (casewindow_get_taint (window),
casewriter_get_taint (writer));
@@ -208,7 +230,7 @@ create_casewriter_window (size_t value_cnt, casenumber max_in_core_cases)
/* Writes case C to casewindow writer WINDOW. */
static void
casewriter_window_write (struct casewriter *writer UNUSED, void *window_,
- struct ccase *c)
+ struct ccase *c)
{
struct casewindow *window = window_;
casewindow_push_head (window, c);
@@ -226,30 +248,32 @@ casewriter_window_destroy (struct casewriter *writer UNUSED, void *window_)
the casereader. */
static struct casereader *
casewriter_window_convert_to_reader (struct casewriter *writer UNUSED,
- void *window_)
+ void *window_)
{
struct casewindow *window = window_;
- struct casereader *reader;
- reader = casereader_create_random (casewindow_get_value_cnt (window),
- casewindow_get_case_cnt (window),
- &casereader_window_class, window);
+ struct casereader *reader =
+ casereader_create_random (casewindow_get_proto (window),
+ casewindow_get_case_cnt (window),
+ &casereader_window_class, window);
+
taint_propagate (casewindow_get_taint (window),
casereader_get_taint (reader));
return reader;
}
-/* Reads the case at the given 0-based OFFSET from the front of
- WINDOW into C. Returns true if successful, false if
- OFFSET is beyond the end of file or upon I/O error. */
-static bool
+/* Reads and returns the case at the given 0-based OFFSET from
+ the front of WINDOW into C. Returns a null pointer if OFFSET
+ is beyond the end of file or upon I/O error. The caller must
+ call case_unref() on the returned case when it is no longer
+ needed.*/
+static struct ccase *
casereader_window_read (struct casereader *reader UNUSED, void *window_,
- casenumber offset, struct ccase *c)
+ casenumber offset)
{
struct casewindow *window = window_;
if (offset >= casewindow_get_case_cnt (window))
- return false;
- else
- return casewindow_get_case (window, offset, c);
+ return NULL;
+ return casewindow_get_case (window, offset);
}
/* Destroys casewindow reader WINDOW. */
@@ -263,14 +287,14 @@ casereader_window_destroy (struct casereader *reader UNUSED, void *window_)
/* Discards CASE_CNT cases from the front of WINDOW. */
static void
casereader_window_advance (struct casereader *reader UNUSED, void *window_,
- casenumber case_cnt)
+ casenumber case_cnt)
{
struct casewindow *window = window_;
casewindow_pop_tail (window, case_cnt);
}
/* Class for casewindow writer. */
-static const struct casewriter_class casewriter_window_class =
+static const struct casewriter_class casewriter_window_class =
{
casewriter_window_write,
casewriter_window_destroy,
@@ -278,7 +302,7 @@ static const struct casewriter_class casewriter_window_class =
};
/* Class for casewindow reader. */
-static const struct casereader_random_class casereader_window_class =
+static const struct casereader_random_class casereader_window_class =
{
casereader_window_read,
casereader_window_destroy,