Skip to content

Commit

Permalink
Reorganize header files
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rstub committed Sep 21, 2023
1 parent abe00a2 commit fbe0633
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 91 deletions.
28 changes: 24 additions & 4 deletions inst/include/dqrng.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#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
1 change: 1 addition & 0 deletions inst/include/dqrng_RcppExports.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef RCPP_dqrng_RCPPEXPORTS_H_GEN_
#define RCPP_dqrng_RCPPEXPORTS_H_GEN_

#include "dqrng_types.h"
#include <Rcpp.h>

namespace dqrng {
Expand Down
12 changes: 12 additions & 0 deletions inst/include/dqrng_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ inline double generate_uniform_real<dqrng::random_64bit_generator, double>(dqrng
return dqrng::uniform01(eng()) * (max - min) + min;
}

template<>
inline std::pair<double, int> generate_int_float_pair<double, 8, dqrng::random_64bit_accessor>(dqrng::random_64bit_accessor& eng)
{
return generate_int_float_pair<double, 8, dqrng::random_64bit_generator>(eng);
}

template<>
inline double generate_uniform_real<dqrng::random_64bit_accessor, double>(dqrng::random_64bit_accessor& eng, double min, double max)
{
return generate_uniform_real<dqrng::random_64bit_generator, double>(eng, min, max);
}

} // namespace detail
} // namespace random
} // namespace boost
Expand Down
63 changes: 0 additions & 63 deletions inst/include/dqrng_extgenerator.h

This file was deleted.

21 changes: 1 addition & 20 deletions inst/include/dqrng_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,15 @@
#ifndef DQRNG_GENERATOR_H
#define DQRNG_GENERATOR_H 1

#include <mystdint.h>
#include <memory>
#include <type_traits>
#include <stdexcept>
#include <xoshiro.h>
#include <pcg_random.hpp>
#include <dqrng_types.h>

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<random_64bit_generator>;

template<typename RNG>
class random_64bit_wrapper : public random_64bit_generator {
static_assert(std::is_same<random_64bit_generator::result_type, typename RNG::result_type>::value,
Expand Down
78 changes: 78 additions & 0 deletions inst/include/dqrng_types.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#ifndef DQRNG_TYPES_H
#define DQRNG_TYPES_H 1

#include <mystdint.h>
#include <memory>
#include <stdexcept>

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<random_64bit_generator>;

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
1 change: 1 addition & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include "../inst/include/dqrng.h"
#include "../inst/include/dqrng_types.h"
#include <Rcpp.h>
#include <string>
#include <set>
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/cpp/external-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#include <Rcpp.h>
// [[Rcpp::depends(dqrng,BH)]]
#include <dqrng.h>
#include <Rcpp.h>
#include <dqrng_distribution.h>
#include <dqrng_extgenerator.h>

// [[Rcpp::export(rng = false)]]
Rcpp::NumericVector dqrexp_extrng(const std::size_t n, const double rate = 1.0) {
Expand Down
2 changes: 0 additions & 2 deletions vignettes/dqrng.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,6 @@ RNG engine of `dqrng`.
#include <boost/random/binomial_distribution.hpp>
#include <dqrng.h>
#include <dqrng_distribution.h>
#include <xoshiro.h>
#include <dqrng_extgenerator.h>
// [[Rcpp::plugins(cpp11)]]
// aliases for the used distributions
Expand Down

0 comments on commit fbe0633

Please sign in to comment.