From 836682e6cff19cce534966b7bab4a933833f9693 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 11 Dec 2005 07:35:50 +0000 Subject: [PATCH] Separate random numbers from other settings because of GSL dependency. --- src/ChangeLog | 19 +++++++++++++ src/Makefile.am | 2 ++ src/expressions/helpers.h | 1 + src/glob.c | 5 +++- src/random.c | 57 +++++++++++++++++++++++++++++++++++++++ src/random.h | 31 +++++++++++++++++++++ src/sample.c | 2 +- src/set.q | 1 + src/settings.c | 28 ++----------------- src/settings.h | 5 ---- 10 files changed, 118 insertions(+), 33 deletions(-) create mode 100644 src/random.c create mode 100644 src/random.h diff --git a/src/ChangeLog b/src/ChangeLog index 1fc5fef1..a94b5ef5 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,22 @@ +Sat Dec 10 23:30:19 2005 Ben Pfaff + + Separate random numbers from other settings because of GSL + dependency. + + * Makefile.am: Add random.c, random.h to sources. + + * glob.c: (init_glob) Call random_init(). + (done_glob) Call random_done(). + + * settings.c: (static var rng) Move to random.c. + (done_settings) Move freeing of RNG to random_done(). + (get_rng) Move to random.c + (set_rng) Ditto. + + * random.c: New file. + + * random.h: New file. + Sat Dec 10 18:13:36 2005 Ben Pfaff Separate settings and the SET command, for modularity. diff --git a/src/Makefile.am b/src/Makefile.am index 857dd8a5..5289af2b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -187,6 +187,8 @@ pspp_SOURCES = \ pool.h \ postscript.c \ print.c \ + random.c \ + random.h \ range-prs.c \ range-prs.h \ recode.c \ diff --git a/src/expressions/helpers.h b/src/expressions/helpers.h index 4011d843..a3fbdb02 100644 --- a/src/expressions/helpers.h +++ b/src/expressions/helpers.h @@ -17,6 +17,7 @@ #include "gsl-extras/gsl-extras.h" #include "misc.h" #include "moments.h" +#include "random.h" #include "settings.h" #include "str.h" #include "val.h" diff --git a/src/glob.c b/src/glob.c index 47db10ea..38dd3237 100644 --- a/src/glob.c +++ b/src/glob.c @@ -71,6 +71,7 @@ extern void stifle_history (); #include "lexer.h" #include "magic.h" #include "main.h" +#include "random.h" #include "settings.h" #include "str.h" #include "var.h" @@ -160,7 +161,8 @@ init_glob (int argc UNUSED, char **argv) } - init_settings(); + init_settings (); + random_init (); /* log.h */ logging = 1; @@ -176,6 +178,7 @@ done_glob(void) cancel_transformations (); dict_destroy (default_dict); free (logfn); + random_done (); done_settings (); ds_destroy (&tokstr); diff --git a/src/random.c b/src/random.c new file mode 100644 index 00000000..7420a82e --- /dev/null +++ b/src/random.c @@ -0,0 +1,57 @@ +/* PSPP - computes sample statistics. + Copyright (C) 1997-9, 2000, 2005 Free Software Foundation, Inc. + Written by Ben Pfaff . + + 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 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. */ + +#include +#include "random.h" +#include +#include "xalloc.h" + +static gsl_rng *rng; + +void +random_init (void) +{ +} + +void +random_done (void) +{ + if (rng != NULL) + gsl_rng_free (rng); +} + +/* Returns the current random number generator. */ +gsl_rng * +get_rng (void) +{ + if (rng == NULL) + set_rng (time (0)); + return rng; +} + +/* Initializes or reinitializes the random number generator with + the given SEED. */ +void +set_rng (unsigned long seed) +{ + rng = gsl_rng_alloc (gsl_rng_mt19937); + if (rng == NULL) + xalloc_die (); + gsl_rng_set (rng, seed); +} diff --git a/src/random.h b/src/random.h new file mode 100644 index 00000000..85959677 --- /dev/null +++ b/src/random.h @@ -0,0 +1,31 @@ +/* PSPP - computes sample statistics. + Copyright (C) 1997-9, 2000, 2005 Free Software Foundation, Inc. + Written by Ben Pfaff . + + 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 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. */ + +#ifndef RANDOM_H +#define RANDOM_H 1 + +#include + +void random_init (void); +void random_done (void); + +gsl_rng *get_rng (void); +void set_rng (unsigned long seed); + +#endif /* random.h */ diff --git a/src/sample.c b/src/sample.c index fceadbbb..0255ae79 100644 --- a/src/sample.c +++ b/src/sample.c @@ -26,7 +26,7 @@ #include "command.h" #include "error.h" #include "lexer.h" -#include "settings.h" +#include "random.h" #include "str.h" #include "var.h" diff --git a/src/set.q b/src/set.q index 9e045fb7..8fb3a807 100644 --- a/src/set.q +++ b/src/set.q @@ -32,6 +32,7 @@ #include "magic.h" #include "log.h" #include "output.h" +#include "random.h" #include "var.h" #include "format.h" #include "copyleft.h" diff --git a/src/settings.c b/src/settings.c index 749757bc..4961b564 100644 --- a/src/settings.c +++ b/src/settings.c @@ -20,6 +20,7 @@ #include #include "settings.h" #include +#include #include #include "format.h" #include "val.h" @@ -77,8 +78,6 @@ static struct custom_currency cc[CC_CNT] = CC_INITIALIZER, }; -static gsl_rng *rng; - static bool testing_mode = false; static int global_algorithm = ENHANCED; @@ -92,9 +91,6 @@ static void init_viewport (void); void done_settings (void) { - if (rng != NULL) - gsl_rng_free (rng); - free (prompt); free (cprompt); free (dprompt); @@ -494,7 +490,7 @@ set_endcmd (char endcmd_) /* Approximate maximum amount of memory to use for cases, in bytes. */ size_t -get_workspace(void) +get_workspace (void) { return workspace; } @@ -540,26 +536,6 @@ set_cc (int idx, const struct custom_currency *cc_) cc[idx] = *cc_; } -/* Returns the current random number generator. */ -gsl_rng * -get_rng (void) -{ - if (rng == NULL) - set_rng (time (0)); - return rng; -} - -/* Initializes or reinitializes the random number generator with - the given SEED. */ -void -set_rng (unsigned long seed) -{ - rng = gsl_rng_alloc (gsl_rng_mt19937); - if (rng == NULL) - xalloc_die (); - gsl_rng_set (rng, seed); -} - /* Are we in testing mode? (e.g. --testing-mode command line option) */ bool diff --git a/src/settings.h b/src/settings.h index 9e620925..0593f2d1 100644 --- a/src/settings.h +++ b/src/settings.h @@ -116,11 +116,6 @@ struct custom_currency const struct custom_currency *get_cc (int idx); void set_cc (int idx, const struct custom_currency *); -#include - -gsl_rng *get_rng (void); -void set_rng (unsigned long seed); - bool get_testing_mode (void); void set_testing_mode (bool); -- 2.30.2