IAP GITLAB

Skip to content
Snippets Groups Projects
Commit bc87cf28 authored by Antonio Augusto Alves Junior's avatar Antonio Augusto Alves Junior
Browse files

hopefully, final solution for issue #4

parent 15d31c12
No related branches found
No related tags found
No related merge requests found
......@@ -234,7 +234,7 @@ public:
*
* @return Copy of the object's engine
*/
inline engine_type getEngine() const {
inline engine_type const& getEngine() const {
return engine_;
}
......
/*----------------------------------------------------------------------------
*
* Copyright (C) 2021 Antonio Augusto Alves Junior
*
* This file is part of RandomIterator.
*
* RandomIterator 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.
*
* RandomIterator 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 RandomIterator. If not, see <http://www.gnu.org/licenses/>.
*
*---------------------------------------------------------------------------*/
/*
* DistributionTraits.hpp
*
* Created on: 29 de jul. de 2022
* Author: Antonio Augusto Alves Junior
*/
#pragma once
#include <random>
namespace random_iterator {
namespace detail {
template<typename Engine>
struct distribution_traits
{
enum{stride = 1};
};
template<typename FloatType>
struct distribution_traits<std::normal_distribution<FloatType>>
{
enum{stride = 3};
};
} // namespace detail
} // namespace random_iterator
......@@ -126,7 +126,7 @@ public:
seed_ = seed;
}
inline state_type getState() const {
inline state_type const& getState() const {
return state_;
}
......
......@@ -28,6 +28,7 @@
#pragma once
#include <stdint.h>
#include "random_iterator/detail/DistributionTraits.hpp"
namespace random_iterator {
......@@ -45,6 +46,8 @@ struct EngineCaller
typedef DistibutionType distribution_type;
typedef typename distribution_type::result_type result_type;
enum{stride = distribution_traits<DistibutionType>::stride};
EngineCaller() = delete;
EngineCaller(distribution_type const& dist , seed_type seed, uint32_t stream ):
......@@ -59,7 +62,10 @@ struct EngineCaller
distribution_(other.distribution_),
seed_(other.seed_),
stream_(other.stream_)
{ }
{
//distribution_.reset();
}
EngineCaller<DistibutionType, EngineType>&
operator=( EngineCaller<DistibutionType, EngineType> const& other ){
......@@ -69,14 +75,15 @@ struct EngineCaller
distribution_ = other.distribution_ ;
seed_ = other.seed_ ;
stream_ = other.stream_;
//distribution_.reset();
return *this;
}
inline result_type operator()(advance_type n) const {
EngineType eng(seed_, stream_ );
eng.discard(n);
eng.discard(n*stride);
//static_cast<distribution_type>(distribution_).reset();
return static_cast<distribution_type>(distribution_)(eng);
}
......
......@@ -348,8 +348,8 @@ public:
inline uint128_t operator-() const;
// Get private values
inline uint64_t upper() const;
inline uint64_t lower() const;
inline uint64_t const& upper() const;
inline uint64_t const& lower() const;
......@@ -362,6 +362,21 @@ public:
// Get string representation of value
inline std::string str(uint8_t base = 10, const unsigned int & len = 0) const;
friend inline std::ostream& operator<<(std::ostream& stream, const uint128_t & rhs){
if (stream.flags() & stream.oct){
stream << rhs.str(8);
}
else if (stream.flags() & stream.dec){
stream << rhs.str(10);
}
else if (stream.flags() & stream.hex){
stream << rhs.str(16);
}
return stream ;
}
private:
std::pair <uint128_t, uint128_t> divmod(const uint128_t & lhs, const uint128_t & rhs) const;
......@@ -427,6 +442,8 @@ inline uint128_t operator^(const T & lhs, const uint128_t & rhs){
template <typename T, typename = typename std::enable_if<std::is_integral<T>::value, T>::type >
inline T & operator^=(T & lhs, const uint128_t & rhs){
return lhs = static_cast <T> (rhs ^ lhs);
}
// Bitshift operators
......@@ -554,7 +571,7 @@ inline T & operator%=(T & lhs, const uint128_t & rhs){
}
// IO Operator
inline std::ostream & operator<<(std::ostream & stream, const uint128_t & rhs);
//inline std::ostream & operator<<(std::ostream & stream, const uint128_t & rhs);
} // namespace random_iterator
......
......@@ -427,11 +427,11 @@ inline uint128_t uint128_t::operator-() const{
return ~*this + uint128_1;
}
inline uint64_t uint128_t::upper() const{
inline uint64_t const& uint128_t::upper() const{
return UPPER;
}
inline uint64_t uint128_t::lower() const{
inline uint64_t const& uint128_t::lower() const{
return LOWER;
}
......@@ -465,7 +465,7 @@ inline std::string uint128_t::str(uint8_t base, const unsigned int & len) const{
throw std::invalid_argument("Base must be in the range [2, 16]");
}
std::string out = "";
if (!(*this)){
if ( LOWER ==0 && UPPER ==0){
out = "0";
}
else{
......@@ -552,7 +552,7 @@ inline uint128_t operator>>(const int32_t & lhs, const uint128_t & rhs){
inline uint128_t operator>>(const int64_t & lhs, const uint128_t & rhs){
return uint128_t(lhs) >> rhs;
}
/*
inline std::ostream & operator<<(std::ostream & stream, const uint128_t & rhs){
if (stream.flags() & stream.oct){
stream << rhs.str(8);
......@@ -565,5 +565,5 @@ inline std::ostream & operator<<(std::ostream & stream, const uint128_t & rhs){
}
return stream;
}
*/
} // namespace random_iterator
......@@ -114,4 +114,15 @@ project(tests)
target_link_libraries( plot_stream PUBLIC matplot -lpthread -lm )
add_dependencies(tests plot_stream )
#----------------------------
message(STATUS "Adding target to tests. Executable file name: issue_4.cpp")
add_executable( issue_4 issue_4.cpp)
target_link_libraries( issue_4 )
add_dependencies(tests issue_4)
......@@ -40,7 +40,7 @@
//set a global seed
static const uint64_t default_seed= 0x548c9decbce65295 ;
#define NCHECKS 1024
#define NCHECKS 30
TEST_CASE("Distribution sampling and access testing") {
......@@ -49,7 +49,7 @@ SECTION( "[ random_iterator::squares3_128 ] Comparing function-like interface wi
{
INFO("Checking streams");
typedef random_iterator::squares3_128 engine_type;
typedef random_iterator::squares3_128 engine_type;
//typedef std::uniform_real_distribution<double> distribution_type;
typedef std::normal_distribution<double> distribution_type;
......@@ -62,10 +62,12 @@ SECTION( "[ random_iterator::squares3_128 ] Comparing function-like interface wi
for(size_t j=0; j<NCHECKS; ++j ) //loop sequence
{
INFO("#i" << " | stream() | stream[#] | *(it + i)");
INFO("#" << j << " | " << stream[j] << " | "<< *(it +j) );
//it+=3;
INFO("#i" << " | stream[#] | *(it + i)");
INFO("#" << j << " | " << stream[j] << " | "<< *(it ) );
CHECK( (stream[j] == *(it+j) ) );
CHECK( (stream[j] == *(it) ) );
it++;
}
......
/*----------------------------------------------------------------------------
*
* Copyright (C) 2021 Antonio Augusto Alves Junior
*
* This file is part of RandomIterator.
*
* RandomIterator 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.
*
* RandomIterator 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 RandomIterator. If not, see <http://www.gnu.org/licenses/>.
*
*---------------------------------------------------------------------------*/
/*
* issue_4.cpp
*
* Created on: 28 de jul. de 2022
* Author: Antonio Augusto Alves Junior
*/
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <random>
#include <random_iterator/Stream.hpp>
int main() {
random_iterator::philox rng(0);
std::normal_distribution<double> dist(0.0, 1.0);
std::cout << "===== fill via iterator =====" << std::endl;
auto rng_stream = random_iterator::make_stream(dist, rng, 0);
auto it = rng_stream.begin();
std::vector<double> samples(rng_stream.begin(), rng_stream.begin()+30);//(it, std::next(it, 30)); // generate 30 samples
std::copy(samples.cbegin(), samples.cend(), std::ostream_iterator<double>{std::cout, "\n"}); // print
std::cout << "===== fill functor-style =====" << std::endl;
auto rng_stream2 = random_iterator::make_stream(dist, rng, 1);
std::vector<double> samples2;
for (int i=0; i<30;++i) { // generate 30 samples
samples2.emplace_back(rng_stream2());
}
std::copy(samples2.cbegin(), samples2.cend(), std::ostream_iterator<double>{std::cout, "\n"}); //print
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment