From fbe0633f91e61879fe50ace8162d0f6700e646eb Mon Sep 17 00:00:00 2001 From: Ralf Stubner Date: Thu, 21 Sep 2023 12:15:10 +0200 Subject: [PATCH] Reorganize header files * random_64bit_generator and random_64bit_accessor w/o the constructor definition go to dqrng_types.h * this gets automatically included in dqrng_RcppExports.h. * constructor definition for random_64bit_accessor goes to dqrng.h. * boost modifications go to dqrng_distributions.h --- inst/include/dqrng.h | 28 ++++++-- inst/include/dqrng_RcppExports.h | 1 + inst/include/dqrng_distribution.h | 12 ++++ inst/include/dqrng_extgenerator.h | 63 ------------------ inst/include/dqrng_generator.h | 21 +----- inst/include/dqrng_types.h | 78 +++++++++++++++++++++++ src/RcppExports.cpp | 1 + tests/testthat/cpp/external-generator.cpp | 2 - vignettes/dqrng.Rmd | 2 - 9 files changed, 117 insertions(+), 91 deletions(-) delete mode 100644 inst/include/dqrng_extgenerator.h create mode 100644 inst/include/dqrng_types.h diff --git a/inst/include/dqrng.h b/inst/include/dqrng.h index 24c0e18..5279ad7 100644 --- a/inst/include/dqrng.h +++ b/inst/include/dqrng.h @@ -1,7 +1,27 @@ -#ifndef RCPP_dqrng_H_GEN_ -#define RCPP_dqrng_H_GEN_ +// Copyright 2023 Ralf Stubner +// Copyright 2023 Henrik Sloot +// +// This file is part of dqrng. +// +// dqrng is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// dqrng 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with dqrng. If not, see . + +#ifndef dqrng_H +#define dqrng_H -#include "dqrng_generator.h" #include "dqrng_RcppExports.h" -#endif // RCPP_dqrng_H_GEN_ +namespace dqrng { +random_64bit_accessor::random_64bit_accessor() : gen(dqrng::get_rng().get()) {} +} // namespace dqrng +#endif // dqrng_H diff --git a/inst/include/dqrng_RcppExports.h b/inst/include/dqrng_RcppExports.h index a7d969d..3654b17 100644 --- a/inst/include/dqrng_RcppExports.h +++ b/inst/include/dqrng_RcppExports.h @@ -4,6 +4,7 @@ #ifndef RCPP_dqrng_RCPPEXPORTS_H_GEN_ #define RCPP_dqrng_RCPPEXPORTS_H_GEN_ +#include "dqrng_types.h" #include namespace dqrng { diff --git a/inst/include/dqrng_distribution.h b/inst/include/dqrng_distribution.h index 3b68f9c..fad81a3 100644 --- a/inst/include/dqrng_distribution.h +++ b/inst/include/dqrng_distribution.h @@ -58,6 +58,18 @@ inline double generate_uniform_real(dqrng return dqrng::uniform01(eng()) * (max - min) + min; } +template<> +inline std::pair generate_int_float_pair(dqrng::random_64bit_accessor& eng) +{ + return generate_int_float_pair(eng); +} + +template<> +inline double generate_uniform_real(dqrng::random_64bit_accessor& eng, double min, double max) +{ + return generate_uniform_real(eng, min, max); +} + } // namespace detail } // namespace random } // namespace boost diff --git a/inst/include/dqrng_extgenerator.h b/inst/include/dqrng_extgenerator.h deleted file mode 100644 index 4810310..0000000 --- a/inst/include/dqrng_extgenerator.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef DQRNG_EXTERNAL_GENERATOR_H -#define DQRNG_EXTERNAL_GENERATOR_H 1 - -#include -#include -#include -#include - -namespace dqrng { -class random_64bit_accessor : public random_64bit_generator { -private: - dqrng::random_64bit_generator *gen; - -public: - explicit random_64bit_accessor() : gen(dqrng::get_rng().get()) {} - - virtual result_type operator() () override { - return (*gen)(); - }; - - virtual void seed(result_type seed) override { - throw std::runtime_error("Seed handling not supported for this class!"); - }; - - virtual void seed(result_type seed, result_type stream) override { - throw std::runtime_error("Seed handling not supported for this class!"); - }; - - virtual uint32_t operator() (uint32_t range) override { - return (*gen)(range); - } -#ifdef LONG_VECTOR_SUPPORT - virtual uint64_t operator() (uint64_t range) override { - return (*gen)(range); - } -#endif - -}; - -}; // dqrng - -namespace boost { -namespace random { -namespace detail { - -template<> -inline std::pair generate_int_float_pair(dqrng::random_64bit_accessor& eng) -{ - return generate_int_float_pair(eng); -} - -template<> -inline double generate_uniform_real(dqrng::random_64bit_accessor& eng, double min, double max) -{ - return generate_uniform_real(eng, min, max); -} - - -}; // detail -}; // random -}; // boost - -#endif diff --git a/inst/include/dqrng_generator.h b/inst/include/dqrng_generator.h index f078930..8f926aa 100644 --- a/inst/include/dqrng_generator.h +++ b/inst/include/dqrng_generator.h @@ -19,34 +19,15 @@ #ifndef DQRNG_GENERATOR_H #define DQRNG_GENERATOR_H 1 -#include -#include #include #include #include #include +#include namespace dqrng { using default_64bit_generator = ::dqrng::xoroshiro128plus; -class random_64bit_generator { -public: - using result_type = uint64_t; - - virtual ~random_64bit_generator() {}; - virtual result_type operator() () = 0; - virtual void seed(result_type seed) = 0; - virtual void seed(result_type seed, result_type stream) = 0; - static constexpr result_type min() {return 0;}; - static constexpr result_type max() {return UINT64_MAX;}; - virtual uint32_t operator() (uint32_t range) = 0; -#ifdef LONG_VECTOR_SUPPORT - virtual uint64_t operator() (uint64_t range) = 0; -#endif -}; - -using rng64_t = std::shared_ptr; - template class random_64bit_wrapper : public random_64bit_generator { static_assert(std::is_same::value, diff --git a/inst/include/dqrng_types.h b/inst/include/dqrng_types.h new file mode 100644 index 0000000..9e5848a --- /dev/null +++ b/inst/include/dqrng_types.h @@ -0,0 +1,78 @@ +// Copyright 2018-2019 Ralf Stubner (daqana GmbH) +// Copyright 2023 Ralf Stubner +// Copyright 2023 Henrik Sloot +// +// This file is part of dqrng. +// +// dqrng is free software: you can redistribute it and/or modify it +// under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// dqrng 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with dqrng. If not, see . + +#ifndef DQRNG_TYPES_H +#define DQRNG_TYPES_H 1 + +#include +#include +#include + +namespace dqrng { + +class random_64bit_generator { +public: + using result_type = uint64_t; + + virtual ~random_64bit_generator() {}; + virtual result_type operator() () = 0; + virtual void seed(result_type seed) = 0; + virtual void seed(result_type seed, result_type stream) = 0; + static constexpr result_type min() {return 0;}; + static constexpr result_type max() {return UINT64_MAX;}; + virtual uint32_t operator() (uint32_t range) = 0; +#ifdef LONG_VECTOR_SUPPORT + virtual uint64_t operator() (uint64_t range) = 0; +#endif +}; + +using rng64_t = std::shared_ptr; + +class random_64bit_accessor : public random_64bit_generator { +private: + dqrng::random_64bit_generator *gen; + +public: + explicit random_64bit_accessor(); + + virtual result_type operator() () override { + return (*gen)(); + }; + + virtual void seed(result_type seed) override { + throw std::runtime_error("Seed handling not supported for this class!"); + }; + + virtual void seed(result_type seed, result_type stream) override { + throw std::runtime_error("Seed handling not supported for this class!"); + }; + + virtual uint32_t operator() (uint32_t range) override { + return (*gen)(range); + } +#ifdef LONG_VECTOR_SUPPORT + virtual uint64_t operator() (uint64_t range) override { + return (*gen)(range); + } +#endif +}; + + +} // namespace dqrng +#endif // DQRNG_TYPES_H diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index d63da5f..bff9be7 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -2,6 +2,7 @@ // Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 #include "../inst/include/dqrng.h" +#include "../inst/include/dqrng_types.h" #include #include #include diff --git a/tests/testthat/cpp/external-generator.cpp b/tests/testthat/cpp/external-generator.cpp index 02bfe25..be2d128 100644 --- a/tests/testthat/cpp/external-generator.cpp +++ b/tests/testthat/cpp/external-generator.cpp @@ -2,9 +2,7 @@ #include // [[Rcpp::depends(dqrng,BH)]] #include -#include #include -#include // [[Rcpp::export(rng = false)]] Rcpp::NumericVector dqrexp_extrng(const std::size_t n, const double rate = 1.0) { diff --git a/vignettes/dqrng.Rmd b/vignettes/dqrng.Rmd index 1b8b7aa..d847697 100644 --- a/vignettes/dqrng.Rmd +++ b/vignettes/dqrng.Rmd @@ -347,8 +347,6 @@ RNG engine of `dqrng`. #include #include #include -#include -#include // [[Rcpp::plugins(cpp11)]] // aliases for the used distributions