X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fcasewindow.c;h=0d735c4dc3ff909c41278df40b5179bdb3dbd2cd;hb=c86f5b8a45cca158b46a4fe3b48280e88ce9aba1;hp=71a7c1bd2fe87aa6d6127622e9803510df1367ed;hpb=5aa037d0c36fcaf2a1ad3995bffe5c32fd201124;p=pspp-builds.git diff --git a/src/data/casewindow.c b/src/data/casewindow.c index 71a7c1bd..0d735c4d 100644 --- a/src/data/casewindow.c +++ b/src/data/casewindow.c @@ -1,20 +1,18 @@ -/* PSPP - computes sample statistics. +/* PSPP - a program for statistical analysis. Copyright (C) 2007 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 . */ /* This casewindow implementation in terms of an class interface is undoubtedly a form of over-abstraction. However, it works @@ -35,7 +33,7 @@ #include "xalloc.h" /* A queue of cases. */ -struct casewindow +struct casewindow { /* Common data. */ size_t value_cnt; /* Number of values per case. */ @@ -43,12 +41,12 @@ struct casewindow struct taint *taint; /* Taint status. */ /* Implementation data. */ - const struct casewindow_class *class; + const struct casewindow_class *class; void *aux; }; /* Implementation of a casewindow. */ -struct casewindow_class +struct casewindow_class { void *(*create) (struct taint *, size_t value_cnt); void (*destroy) (void *aux); @@ -66,7 +64,7 @@ static const struct casewindow_class casewindow_file_class; parameters. */ static struct casewindow * do_casewindow_create (struct taint *taint, - size_t value_cnt, casenumber max_in_core_cases) + size_t value_cnt, casenumber max_in_core_cases) { struct casewindow *cw = xmalloc (sizeof *cw); cw->class = (max_in_core_cases @@ -82,9 +80,9 @@ do_casewindow_create (struct taint *taint, /* Creates and returns a new casewindow for cases with VALUE_CNT values each. 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. */ struct casewindow * -casewindow_create (size_t value_cnt, casenumber max_in_core_cases) +casewindow_create (size_t value_cnt, casenumber max_in_core_cases) { return do_casewindow_create (taint_create (), value_cnt, max_in_core_cases); } @@ -93,10 +91,10 @@ casewindow_create (size_t value_cnt, casenumber max_in_core_cases) Returns true if CW was tainted, which is caused by an I/O error or by taint propagation to the casewindow. */ bool -casewindow_destroy (struct casewindow *cw) +casewindow_destroy (struct casewindow *cw) { bool ok = true; - if (cw != NULL) + if (cw != NULL) { cw->class->destroy (cw->aux); ok = taint_destroy (cw->taint); @@ -107,7 +105,7 @@ casewindow_destroy (struct casewindow *cw) /* Swaps the contents of casewindows A and B. */ static void -casewindow_swap (struct casewindow *a, struct casewindow *b) +casewindow_swap (struct casewindow *a, struct casewindow *b) { struct casewindow tmp = *a; *a = *b; @@ -116,7 +114,7 @@ casewindow_swap (struct casewindow *a, struct casewindow *b) /* Dumps the contents of casewindow OLD to disk. */ static void -casewindow_to_disk (struct casewindow *old) +casewindow_to_disk (struct casewindow *old) { struct casewindow *new; new = do_casewindow_create (taint_clone (old->taint), old->value_cnt, 0); @@ -135,7 +133,7 @@ casewindow_to_disk (struct casewindow *old) /* Pushes case C at the head of casewindow CW. Case C becomes owned by the casewindow. */ void -casewindow_push_head (struct casewindow *cw, struct ccase *c) +casewindow_push_head (struct casewindow *cw, struct ccase *c) { if (!casewindow_error (cw)) { @@ -145,7 +143,7 @@ casewindow_push_head (struct casewindow *cw, struct ccase *c) casenumber case_cnt = cw->class->get_case_cnt (cw->aux); if (case_cnt > cw->max_in_core_cases && cw->class == &casewindow_memory_class) - casewindow_to_disk (cw); + casewindow_to_disk (cw); } } else @@ -154,9 +152,9 @@ casewindow_push_head (struct casewindow *cw, struct ccase *c) /* Deletes CASE_CNT cases at the tail of casewindow CW. */ void -casewindow_pop_tail (struct casewindow *cw, casenumber case_cnt) +casewindow_pop_tail (struct casewindow *cw, casenumber case_cnt) { - if (!casewindow_error (cw)) + if (!casewindow_error (cw)) cw->class->pop_tail (cw->aux, case_cnt); } @@ -165,14 +163,14 @@ casewindow_pop_tail (struct casewindow *cw, casenumber case_cnt) if CW is otherwise tainted. On failure, nullifies case C. */ bool casewindow_get_case (const struct casewindow *cw_, casenumber case_idx, - struct ccase *c) + struct ccase *c) { struct casewindow *cw = (struct casewindow *) cw_; assert (case_idx >= 0 && case_idx < casewindow_get_case_cnt (cw)); if (!casewindow_error (cw)) return cw->class->get_case (cw->aux, case_idx, c); - else + else { case_nullify (c); return false; @@ -181,14 +179,14 @@ casewindow_get_case (const struct casewindow *cw_, casenumber case_idx, /* Returns the number of cases in casewindow CW. */ casenumber -casewindow_get_case_cnt (const struct casewindow *cw) +casewindow_get_case_cnt (const struct casewindow *cw) { return cw->class->get_case_cnt (cw->aux); } /* Returns the number of values per case in casewindow CW. */ size_t -casewindow_get_value_cnt (const struct casewindow *cw) +casewindow_get_value_cnt (const struct casewindow *cw) { return cw->value_cnt; } @@ -197,34 +195,34 @@ casewindow_get_value_cnt (const struct casewindow *cw) A casewindow is tainted by an I/O error or by taint propagation to the casewindow. */ bool -casewindow_error (const struct casewindow *cw) +casewindow_error (const struct casewindow *cw) { return taint_is_tainted (cw->taint); } /* Marks casewindow CW tainted. */ void -casewindow_force_error (struct casewindow *cw) +casewindow_force_error (struct casewindow *cw) { taint_set_taint (cw->taint); } /* Returns casewindow CW's taint object. */ const struct taint * -casewindow_get_taint (const struct casewindow *cw) +casewindow_get_taint (const struct casewindow *cw) { return cw->taint; } /* In-memory casewindow data. */ -struct casewindow_memory +struct casewindow_memory { struct deque deque; struct ccase *cases; }; static void * -casewindow_memory_create (struct taint *taint UNUSED, size_t value_cnt UNUSED) +casewindow_memory_create (struct taint *taint UNUSED, size_t value_cnt UNUSED) { struct casewindow_memory *cwm = xmalloc (sizeof *cwm); cwm->cases = deque_init (&cwm->deque, 4, sizeof *cwm->cases); @@ -232,10 +230,10 @@ casewindow_memory_create (struct taint *taint UNUSED, size_t value_cnt UNUSED) } static void -casewindow_memory_destroy (void *cwm_) +casewindow_memory_destroy (void *cwm_) { struct casewindow_memory *cwm = cwm_; - while (!deque_is_empty (&cwm->deque)) + while (!deque_is_empty (&cwm->deque)) case_destroy (&cwm->cases[deque_pop_front (&cwm->deque)]); free (cwm->cases); free (cwm); @@ -255,12 +253,12 @@ casewindow_memory_pop_tail (void *cwm_, casenumber case_cnt) { struct casewindow_memory *cwm = cwm_; assert (deque_count (&cwm->deque) >= case_cnt); - while (case_cnt-- > 0) + while (case_cnt-- > 0) case_destroy (&cwm->cases[deque_pop_front (&cwm->deque)]); } static bool -casewindow_memory_get_case (void *cwm_, casenumber ofs, struct ccase *c) +casewindow_memory_get_case (void *cwm_, casenumber ofs, struct ccase *c) { struct casewindow_memory *cwm = cwm_; case_clone (c, &cwm->cases[deque_front (&cwm->deque, ofs)]); @@ -268,13 +266,13 @@ casewindow_memory_get_case (void *cwm_, casenumber ofs, struct ccase *c) } static casenumber -casewindow_memory_get_case_cnt (const void *cwm_) +casewindow_memory_get_case_cnt (const void *cwm_) { const struct casewindow_memory *cwm = cwm_; return deque_count (&cwm->deque); } -static const struct casewindow_class casewindow_memory_class = +static const struct casewindow_class casewindow_memory_class = { casewindow_memory_create, casewindow_memory_destroy, @@ -285,14 +283,14 @@ static const struct casewindow_class casewindow_memory_class = }; /* On-disk casewindow data. */ -struct casewindow_file +struct casewindow_file { struct case_tmpfile *file; casenumber head, tail; }; static void * -casewindow_file_create (struct taint *taint, size_t value_cnt) +casewindow_file_create (struct taint *taint, size_t value_cnt) { struct casewindow_file *cwf = xmalloc (sizeof *cwf); cwf->file = case_tmpfile_create (value_cnt); @@ -302,7 +300,7 @@ casewindow_file_create (struct taint *taint, size_t value_cnt) } static void -casewindow_file_destroy (void *cwf_) +casewindow_file_destroy (void *cwf_) { struct casewindow_file *cwf = cwf_; case_tmpfile_destroy (cwf->file); @@ -310,15 +308,15 @@ casewindow_file_destroy (void *cwf_) } static void -casewindow_file_push_head (void *cwf_, struct ccase *c) +casewindow_file_push_head (void *cwf_, struct ccase *c) { struct casewindow_file *cwf = cwf_; - if (case_tmpfile_put_case (cwf->file, cwf->head, c)) + if (case_tmpfile_put_case (cwf->file, cwf->head, c)) cwf->head++; } static void -casewindow_file_pop_tail (void *cwf_, casenumber cnt) +casewindow_file_pop_tail (void *cwf_, casenumber cnt) { struct casewindow_file *cwf = cwf_; assert (cnt <= cwf->head - cwf->tail); @@ -328,20 +326,20 @@ casewindow_file_pop_tail (void *cwf_, casenumber cnt) } static bool -casewindow_file_get_case (void *cwf_, casenumber ofs, struct ccase *c) +casewindow_file_get_case (void *cwf_, casenumber ofs, struct ccase *c) { struct casewindow_file *cwf = cwf_; return case_tmpfile_get_case (cwf->file, cwf->tail + ofs, c); } static casenumber -casewindow_file_get_case_cnt (const void *cwf_) +casewindow_file_get_case_cnt (const void *cwf_) { const struct casewindow_file *cwf = cwf_; return cwf->head - cwf->tail; } -static const struct casewindow_class casewindow_file_class = +static const struct casewindow_class casewindow_file_class = { casewindow_file_create, casewindow_file_destroy,