/* PSPP - a program for statistical analysis.
- Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+ 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
#include <config.h>
-#include <data/casewindow.h>
+#include "data/casewindow.h"
#include <stdlib.h>
-#include <data/case-tmpfile.h>
-#include <libpspp/assertion.h>
-#include <libpspp/compiler.h>
-#include <libpspp/deque.h>
-#include <libpspp/taint.h>
+#include "data/case-tmpfile.h"
+#include "libpspp/assertion.h"
+#include "libpspp/compiler.h"
+#include "libpspp/deque.h"
+#include "libpspp/taint.h"
-#include "xalloc.h"
+#include "gl/xalloc.h"
/* A queue of cases. */
struct casewindow
{
/* Common data. */
- size_t value_cnt; /* Number of values per case. */
+ struct caseproto *proto; /* Prototype of cases in window. */
casenumber max_in_core_cases; /* Max cases before dumping to disk. */
struct taint *taint; /* Taint status. */
/* Implementation of a casewindow. */
struct casewindow_class
{
- void *(*create) (struct taint *, size_t value_cnt);
+ void *(*create) (struct taint *, const struct caseproto *);
void (*destroy) (void *aux);
void (*push_head) (void *aux, struct ccase *);
- void (*pop_tail) (void *aux, casenumber cnt);
+ void (*pop_tail) (void *aux, casenumber n);
struct ccase *(*get_case) (void *aux, casenumber ofs);
- casenumber (*get_case_cnt) (const void *aux);
+ casenumber (*get_n_cases) (const void *aux);
};
/* Classes. */
/* Creates and returns a new casewindow using the given
parameters. */
static struct casewindow *
-do_casewindow_create (struct taint *taint,
- size_t value_cnt, casenumber max_in_core_cases)
+do_casewindow_create (struct taint *taint, const struct caseproto *proto,
+ casenumber max_in_core_cases)
{
struct casewindow *cw = xmalloc (sizeof *cw);
cw->class = (max_in_core_cases
? &casewindow_memory_class
: &casewindow_file_class);
- cw->aux = cw->class->create (taint, value_cnt);
- cw->value_cnt = value_cnt;
+ cw->aux = cw->class->create (taint, proto);
+ cw->proto = caseproto_ref (proto);
cw->max_in_core_cases = max_in_core_cases;
cw->taint = taint;
return cw;
}
-/* Creates and returns a new casewindow for cases with VALUE_CNT
- values each. If the casewindow holds more than
+/* Creates and returns a new casewindow for cases that take the
+ form specified by PROTO. If the casewindow holds more than
MAX_IN_CORE_CASES cases at any time, its cases will be dumped
- to disk; otherwise, its cases will be held in memory. */
+ to disk; otherwise, its cases will be held in memory.
+
+ The caller retains its reference to PROTO. */
struct casewindow *
-casewindow_create (size_t value_cnt, casenumber max_in_core_cases)
+casewindow_create (const struct caseproto *proto, casenumber max_in_core_cases)
{
- return do_casewindow_create (taint_create (), value_cnt, max_in_core_cases);
+ return do_casewindow_create (taint_create (), proto, max_in_core_cases);
}
/* Destroys casewindow CW.
{
cw->class->destroy (cw->aux);
ok = taint_destroy (cw->taint);
+ caseproto_unref (cw->proto);
free (cw);
}
return ok;
casewindow_to_disk (struct casewindow *old)
{
struct casewindow *new;
- new = do_casewindow_create (taint_clone (old->taint), old->value_cnt, 0);
- while (casewindow_get_case_cnt (old) > 0 && !casewindow_error (new))
+ new = do_casewindow_create (taint_clone (old->taint), old->proto, 0);
+ while (casewindow_get_n_cases (old) > 0 && !casewindow_error (new))
{
struct ccase *c = casewindow_get_case (old, 0);
if (c == NULL)
cw->class->push_head (cw->aux, c);
if (!casewindow_error (cw))
{
- casenumber case_cnt = cw->class->get_case_cnt (cw->aux);
- if (case_cnt > cw->max_in_core_cases
+ casenumber n_cases = cw->class->get_n_cases (cw->aux);
+ if (n_cases > cw->max_in_core_cases
&& cw->class == &casewindow_memory_class)
casewindow_to_disk (cw);
}
case_unref (c);
}
-/* Deletes CASE_CNT cases at the tail of casewindow CW. */
+/* Deletes N_CASES cases at the tail of casewindow CW. */
void
-casewindow_pop_tail (struct casewindow *cw, casenumber case_cnt)
+casewindow_pop_tail (struct casewindow *cw, casenumber n_cases)
{
if (!casewindow_error (cw))
- cw->class->pop_tail (cw->aux, case_cnt);
+ cw->class->pop_tail (cw->aux, n_cases);
}
/* Returns the case that is CASE_IDX cases away from CW's tail
struct ccase *
casewindow_get_case (const struct casewindow *cw_, casenumber case_idx)
{
- struct casewindow *cw = (struct casewindow *) cw_;
+ struct casewindow *cw = CONST_CAST (struct casewindow *, cw_);
- assert (case_idx >= 0 && case_idx < casewindow_get_case_cnt (cw));
+ assert (case_idx >= 0 && case_idx < casewindow_get_n_cases (cw));
if (casewindow_error (cw))
return NULL;
return cw->class->get_case (cw->aux, case_idx);
/* Returns the number of cases in casewindow CW. */
casenumber
-casewindow_get_case_cnt (const struct casewindow *cw)
+casewindow_get_n_cases (const struct casewindow *cw)
{
- return cw->class->get_case_cnt (cw->aux);
+ return cw->class->get_n_cases (cw->aux);
}
-/* Returns the number of values per case in casewindow CW. */
-size_t
-casewindow_get_value_cnt (const struct casewindow *cw)
+/* Returns the case prototype for the cases in casewindow CW.
+ The caller must not unref the returned prototype. */
+const struct caseproto *
+casewindow_get_proto (const struct casewindow *cw)
{
- return cw->value_cnt;
+ return cw->proto;
}
/* Returns true if casewindow CW is tainted.
};
static void *
-casewindow_memory_create (struct taint *taint UNUSED, size_t value_cnt UNUSED)
+casewindow_memory_create (struct taint *taint UNUSED,
+ const struct caseproto *proto UNUSED)
{
struct casewindow_memory *cwm = xmalloc (sizeof *cwm);
cwm->cases = deque_init (&cwm->deque, 4, sizeof *cwm->cases);
}
static void
-casewindow_memory_pop_tail (void *cwm_, casenumber case_cnt)
+casewindow_memory_pop_tail (void *cwm_, casenumber n_cases)
{
struct casewindow_memory *cwm = cwm_;
- assert (deque_count (&cwm->deque) >= case_cnt);
- while (case_cnt-- > 0)
+ assert (deque_count (&cwm->deque) >= n_cases);
+ while (n_cases-- > 0)
case_unref (cwm->cases[deque_pop_front (&cwm->deque)]);
}
}
static casenumber
-casewindow_memory_get_case_cnt (const void *cwm_)
+casewindow_memory_get_n_cases (const void *cwm_)
{
const struct casewindow_memory *cwm = cwm_;
return deque_count (&cwm->deque);
casewindow_memory_push_head,
casewindow_memory_pop_tail,
casewindow_memory_get_case,
- casewindow_memory_get_case_cnt,
+ casewindow_memory_get_n_cases,
};
\f
/* On-disk casewindow data. */
};
static void *
-casewindow_file_create (struct taint *taint, size_t value_cnt)
+casewindow_file_create (struct taint *taint, const struct caseproto *proto)
{
struct casewindow_file *cwf = xmalloc (sizeof *cwf);
- cwf->file = case_tmpfile_create (value_cnt);
+ cwf->file = case_tmpfile_create (proto);
cwf->head = cwf->tail = 0;
taint_propagate (case_tmpfile_get_taint (cwf->file), taint);
return cwf;
}
static void
-casewindow_file_pop_tail (void *cwf_, casenumber cnt)
+casewindow_file_pop_tail (void *cwf_, casenumber n)
{
struct casewindow_file *cwf = cwf_;
- assert (cnt <= cwf->head - cwf->tail);
- cwf->tail += cnt;
+ assert (n <= cwf->head - cwf->tail);
+ cwf->tail += n;
if (cwf->head == cwf->tail)
cwf->head = cwf->tail = 0;
}
}
static casenumber
-casewindow_file_get_case_cnt (const void *cwf_)
+casewindow_file_get_n_cases (const void *cwf_)
{
const struct casewindow_file *cwf = cwf_;
return cwf->head - cwf->tail;
casewindow_file_push_head,
casewindow_file_pop_tail,
casewindow_file_get_case,
- casewindow_file_get_case_cnt,
+ casewindow_file_get_n_cases,
};