Mercurial > octave
changeset 30059:cc7ca74e883d
maint: use "m_" prefix for member variables in class rand.
* oct-rand.cc, oct-rand.h: use "m_" prefix for member variables in class rand.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 29 Aug 2021 19:43:04 -0700 |
parents | f2062fb507bc |
children | ae47678c76e8 |
files | liboctave/numeric/oct-rand.cc liboctave/numeric/oct-rand.h |
diffstat | 2 files changed, 83 insertions(+), 82 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/numeric/oct-rand.cc Sat Aug 28 07:11:49 2021 -0700 +++ b/liboctave/numeric/oct-rand.cc Sun Aug 29 19:43:04 2021 -0700 @@ -48,11 +48,11 @@ namespace octave { - rand *rand::instance = nullptr; + rand *rand::m_instance = nullptr; rand::rand (void) - : current_distribution (uniform_dist), use_old_generators (false), - rand_states () + : m_current_distribution (uniform_dist), m_use_old_generators (false), + m_rand_states () { initialize_ranlib_generators (); @@ -63,9 +63,9 @@ { bool retval = true; - if (! instance) + if (! m_instance) { - instance = new rand (); + m_instance = new rand (); singleton_cleanup_list::add (cleanup_instance); } @@ -110,7 +110,7 @@ void rand::do_seed (double s) { - use_old_generators = true; + m_use_old_generators = true; int i0, i1; union d2i { double d; int32_t i[2]; }; @@ -137,22 +137,22 @@ void rand::do_reset (void) { - use_old_generators = true; + m_use_old_generators = true; initialize_ranlib_generators (); } uint32NDArray rand::do_state (const std::string& d) { - return rand_states[d.empty () ? current_distribution : get_dist_id (d)]; + return m_rand_states[d.empty () ? m_current_distribution : get_dist_id (d)]; } void rand::do_state (const uint32NDArray& s, const std::string& d) { - use_old_generators = false; + m_use_old_generators = false; - int old_dist = current_distribution; + int old_dist = m_current_distribution; - int new_dist = (d.empty () ? current_distribution : get_dist_id (d)); + int new_dist = (d.empty () ? m_current_distribution : get_dist_id (d)); uint32NDArray saved_state; @@ -161,19 +161,19 @@ set_internal_state (s); - rand_states[new_dist] = get_internal_state (); + m_rand_states[new_dist] = get_internal_state (); if (old_dist != new_dist) - rand_states[old_dist] = saved_state; + m_rand_states[old_dist] = saved_state; } void rand::do_reset (const std::string& d) { - use_old_generators = false; + m_use_old_generators = false; - int old_dist = current_distribution; + int old_dist = m_current_distribution; - int new_dist = (d.empty () ? current_distribution : get_dist_id (d)); + int new_dist = (d.empty () ? m_current_distribution : get_dist_id (d)); uint32NDArray saved_state; @@ -181,17 +181,17 @@ saved_state = get_internal_state (); init_mersenne_twister (); - rand_states[new_dist] = get_internal_state (); + m_rand_states[new_dist] = get_internal_state (); if (old_dist != new_dist) - rand_states[old_dist] = saved_state; + m_rand_states[old_dist] = saved_state; } std::string rand::do_distribution (void) { std::string retval; - switch (current_distribution) + switch (m_current_distribution) { case uniform_dist: retval = "uniform"; @@ -215,7 +215,7 @@ default: (*current_liboctave_error_handler) - ("rand: invalid distribution ID = %d", current_distribution); + ("rand: invalid distribution ID = %d", m_current_distribution); break; } @@ -295,7 +295,7 @@ { double retval; - if (use_old_generators) + if (m_use_old_generators) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval); else retval = rand_uniform<double> (); @@ -308,7 +308,7 @@ { double retval; - if (use_old_generators) + if (m_use_old_generators) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval); else retval = rand_normal<double> (); @@ -321,7 +321,7 @@ { double retval; - if (use_old_generators) + if (m_use_old_generators) F77_FUNC (dgenexp, DGENEXP) (1.0, retval); else retval = rand_exponential<double> (); @@ -334,7 +334,7 @@ { double retval; - if (use_old_generators) + if (m_use_old_generators) { if (a < 0.0 || ! math::isfinite (a)) retval = numeric_limits<double>::NaN (); @@ -356,7 +356,7 @@ { double retval; - if (use_old_generators) + if (m_use_old_generators) { if (a <= 0.0 || ! math::isfinite (a)) retval = numeric_limits<double>::NaN (); @@ -374,7 +374,7 @@ { float retval; - if (use_old_generators) + if (m_use_old_generators) F77_FUNC (fgenunf, FGENUNF) (0.0f, 1.0f, retval); else retval = rand_uniform<float> (); @@ -387,7 +387,7 @@ { float retval; - if (use_old_generators) + if (m_use_old_generators) F77_FUNC (fgennor, FGENNOR) (0.0f, 1.0f, retval); else retval = rand_normal<float> (); @@ -400,7 +400,7 @@ { float retval; - if (use_old_generators) + if (m_use_old_generators) F77_FUNC (fgenexp, FGENEXP) (1.0f, retval); else retval = rand_exponential<float> (); @@ -413,7 +413,7 @@ { float retval; - if (use_old_generators) + if (m_use_old_generators) { if (a < 0.0f || ! math::isfinite (a)) retval = numeric_limits<float>::NaN (); @@ -438,7 +438,7 @@ { float retval; - if (use_old_generators) + if (m_use_old_generators) { if (a <= 0.0f || ! math::isfinite (a)) retval = numeric_limits<float>::NaN (); @@ -456,7 +456,7 @@ { T retval = 0; - switch (current_distribution) + switch (m_current_distribution) { case uniform_dist: retval = uniform<T> (); @@ -480,11 +480,11 @@ default: (*current_liboctave_error_handler) - ("rand: invalid distribution ID = %d", current_distribution); + ("rand: invalid distribution ID = %d", m_current_distribution); break; } - if (! use_old_generators) + if (! m_use_old_generators) save_state (); return retval; @@ -552,7 +552,7 @@ void rand::initialize_ranlib_generators (void) { sys::localtime tm; - int stored_distribution = current_distribution; + int stored_distribution = m_current_distribution; F77_FUNC (setcgn, SETCGN) (uniform_dist); int hour = tm.hour () + 1; @@ -575,27 +575,27 @@ init_mersenne_twister (); s = get_internal_state (); - rand_states[uniform_dist] = s; + m_rand_states[uniform_dist] = s; init_mersenne_twister (); s = get_internal_state (); - rand_states[normal_dist] = s; + m_rand_states[normal_dist] = s; init_mersenne_twister (); s = get_internal_state (); - rand_states[expon_dist] = s; + m_rand_states[expon_dist] = s; init_mersenne_twister (); s = get_internal_state (); - rand_states[poisson_dist] = s; + m_rand_states[poisson_dist] = s; init_mersenne_twister (); s = get_internal_state (); - rand_states[gamma_dist] = s; + m_rand_states[gamma_dist] = s; // All of the initializations above have messed with the internal state. // Restore the state of the currently selected distribution. - set_internal_state (rand_states[current_distribution]); + set_internal_state (m_rand_states[m_current_distribution]); } uint32NDArray rand::get_internal_state (void) @@ -609,7 +609,7 @@ void rand::save_state (void) { - rand_states[current_distribution] = get_internal_state ();; + m_rand_states[m_current_distribution] = get_internal_state ();; } int rand::get_dist_id (const std::string& d) @@ -647,11 +647,11 @@ void rand::switch_to_generator (int dist) { - if (dist != current_distribution) + if (dist != m_current_distribution) { - current_distribution = dist; + m_current_distribution = dist; - set_internal_state (rand_states[dist]); + set_internal_state (m_rand_states[dist]); } } @@ -660,31 +660,31 @@ if (len < 1) return; - switch (current_distribution) + switch (m_current_distribution) { case uniform_dist: - if (use_old_generators) + if (m_use_old_generators) std::generate_n (v, len, [](void) { double x; F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x); return x; }); else rand_uniform<double> (len, v); break; case normal_dist: - if (use_old_generators) + if (m_use_old_generators) std::generate_n (v, len, [](void) { double x; F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x); return x; }); else rand_normal<double> (len, v); break; case expon_dist: - if (use_old_generators) + if (m_use_old_generators) std::generate_n (v, len, [](void) { double x; F77_FUNC (dgenexp, DGENEXP) (1.0, x); return x; }); else rand_exponential<double> (len, v); break; case poisson_dist: - if (use_old_generators) + if (m_use_old_generators) { if (a < 0.0 || ! math::isfinite (a)) std::fill_n (v, len, numeric_limits<double>::NaN ()); @@ -701,7 +701,7 @@ break; case gamma_dist: - if (use_old_generators) + if (m_use_old_generators) { if (a <= 0.0 || ! math::isfinite (a)) std::fill_n (v, len, numeric_limits<double>::NaN ()); @@ -714,7 +714,7 @@ default: (*current_liboctave_error_handler) - ("rand: invalid distribution ID = %d", current_distribution); + ("rand: invalid distribution ID = %d", m_current_distribution); break; } @@ -728,31 +728,31 @@ if (len < 1) return; - switch (current_distribution) + switch (m_current_distribution) { case uniform_dist: - if (use_old_generators) + if (m_use_old_generators) std::generate_n (v, len, [](void) { float x; F77_FUNC (fgenunf, FGENUNF) (0.0f, 1.0f, x); return x; }); else rand_uniform<float> (len, v); break; case normal_dist: - if (use_old_generators) + if (m_use_old_generators) std::generate_n (v, len, [](void) { float x; F77_FUNC (fgennor, FGENNOR) (0.0f, 1.0f, x); return x; }); else rand_normal<float> (len, v); break; case expon_dist: - if (use_old_generators) + if (m_use_old_generators) std::generate_n (v, len, [](void) { float x; F77_FUNC (fgenexp, FGENEXP) (1.0f, x); return x; }); else rand_exponential<float> (len, v); break; case poisson_dist: - if (use_old_generators) + if (m_use_old_generators) { if (a < 0.0f || ! math::isfinite (a)) std::fill_n (v, len, numeric_limits<float>::NaN ()); @@ -769,7 +769,7 @@ break; case gamma_dist: - if (use_old_generators) + if (m_use_old_generators) { if (a <= 0.0f || ! math::isfinite (a)) std::fill_n (v, len, numeric_limits<float>::NaN ()); @@ -782,7 +782,7 @@ default: (*current_liboctave_error_handler) - ("rand: invalid distribution ID = %d", current_distribution); + ("rand: invalid distribution ID = %d", m_current_distribution); break; }
--- a/liboctave/numeric/oct-rand.h Sat Aug 28 07:11:49 2021 -0700 +++ b/liboctave/numeric/oct-rand.h Sun Aug 29 19:43:04 2021 -0700 @@ -57,27 +57,27 @@ static double seed (void) { return (instance_ok () - ? instance->do_seed () : numeric_limits<double>::NaN ()); + ? m_instance->do_seed () : numeric_limits<double>::NaN ()); } // Set the seed. static void seed (double s) { if (instance_ok ()) - instance->do_seed (s); + m_instance->do_seed (s); } // Reset the seed. static void reset (void) { if (instance_ok ()) - instance->do_reset (); + m_instance->do_reset (); } // Return the current state. static uint32NDArray state (const std::string& d = "") { - return instance_ok () ? instance->do_state (d) : uint32NDArray (); + return instance_ok () ? m_instance->do_state (d) : uint32NDArray (); } // Set the current state/ @@ -85,20 +85,20 @@ const std::string& d = "") { if (instance_ok ()) - instance->do_state (s, d); + m_instance->do_state (s, d); } // Reset the current state/ static void reset (const std::string& d) { if (instance_ok ()) - instance->do_reset (d); + m_instance->do_reset (d); } // Return the current distribution. static std::string distribution (void) { - return instance_ok () ? instance->do_distribution () : ""; + return instance_ok () ? m_instance->do_distribution () : ""; } // Set the current distribution. May be either "uniform" (the @@ -106,70 +106,70 @@ static void distribution (const std::string& d) { if (instance_ok ()) - instance->do_distribution (d); + m_instance->do_distribution (d); } static void uniform_distribution (void) { if (instance_ok ()) - instance->do_uniform_distribution (); + m_instance->do_uniform_distribution (); } static void normal_distribution (void) { if (instance_ok ()) - instance->do_normal_distribution (); + m_instance->do_normal_distribution (); } static void exponential_distribution (void) { if (instance_ok ()) - instance->do_exponential_distribution (); + m_instance->do_exponential_distribution (); } static void poisson_distribution (void) { if (instance_ok ()) - instance->do_poisson_distribution (); + m_instance->do_poisson_distribution (); } static void gamma_distribution (void) { if (instance_ok ()) - instance->do_gamma_distribution (); + m_instance->do_gamma_distribution (); } // Return the next number from the sequence. static double scalar (double a = 1.0) { return (instance_ok () - ? instance->do_scalar (a) : numeric_limits<double>::NaN ()); + ? m_instance->do_scalar (a) : numeric_limits<double>::NaN ()); } // Return the next number from the sequence. static float float_scalar (float a = 1.0) { return (instance_ok () - ? instance->do_scalar (a) : numeric_limits<float>::NaN ()); + ? m_instance->do_scalar (a) : numeric_limits<float>::NaN ()); } // Return an array of numbers from the sequence. static Array<double> vector (octave_idx_type n, double a = 1.0) { - return instance_ok () ? instance->do_vector (n, a) : Array<double> (); + return instance_ok () ? m_instance->do_vector (n, a) : Array<double> (); } // Return an array of numbers from the sequence. static Array<float> float_vector (octave_idx_type n, float a = 1.0) { - return instance_ok () ? instance->do_vector (n, a) : Array<float> (); + return instance_ok () ? m_instance->do_vector (n, a) : Array<float> (); } // Return an N-dimensional array of numbers from the sequence, // filled in column major order. static NDArray nd_array (const dim_vector& dims, double a = 1.0) { - return instance_ok () ? instance->do_nd_array (dims, a) : NDArray (); + return instance_ok () ? m_instance->do_nd_array (dims, a) : NDArray (); } // Return an N-dimensional array of numbers from the sequence, @@ -177,14 +177,15 @@ static FloatNDArray float_nd_array (const dim_vector& dims, float a = 1.0) { return (instance_ok () - ? instance->do_float_nd_array (dims, a) : FloatNDArray ()); + ? m_instance->do_float_nd_array (dims, a) : FloatNDArray ()); } private: - static rand *instance; + static rand *m_instance; - static void cleanup_instance (void) { delete instance; instance = nullptr; } + static void cleanup_instance (void) + { delete m_instance; m_instance = nullptr; } enum { @@ -197,14 +198,14 @@ }; // Current distribution of random numbers. - int current_distribution; + int m_current_distribution; // If TRUE, use old RANLIB generators. Otherwise, use Mersenne // Twister generator. - bool use_old_generators; + bool m_use_old_generators; // Saved MT states. - std::map<int, uint32NDArray> rand_states; + std::map<int, uint32NDArray> m_rand_states; // Return the current seed. OCTAVE_API double do_seed (void);