vSMC
vSMC: Scalable Monte Carlo
generator_wrapper.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/generator_wrapper.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_RNG_GENERATOR_WRAPPER_HPP
33 #define VSMC_RNG_GENERATOR_WRAPPER_HPP
34 
36 
37 #define VSMC_STATIC_ASSERT_RNG_GENERATOR_WRAPPER_RESULT_TYPE(ResultType) \
38  VSMC_STATIC_ASSERT(( \
39  cxx11::is_same<ResultType, uint16_t>::value || \
40  cxx11::is_same<ResultType, uint32_t>::value || \
41  cxx11::is_same<ResultType, uint64_t>::value), \
42  USE_GeneratorWrapper_WITH_RESULT_TYPE_OTHER_THAN_uint16_t_OR_uint32_t_OR_uint64_t)
43 
44 #define VSMC_STATIC_ASSERT_RNG_GENERATOR_WRAPPER \
45  VSMC_STATIC_ASSERT_RNG_GENERATOR_WRAPPER_RESULT_TYPE(ResultType);
46 
47 namespace vsmc {
48 
49 namespace traits {
50 
53 template <typename ResultType, typename = void>
55 {
56  static VSMC_CONSTEXPR const ResultType _Min = 0;
57  static VSMC_CONSTEXPR const ResultType _Max = static_cast<ResultType>(
58  ~(static_cast<ResultType>(0)));
59 
60  static VSMC_CONSTEXPR ResultType min VSMC_MNE () {return _Min;}
61  static VSMC_CONSTEXPR ResultType max VSMC_MNE () {return _Max;}
62 }; // struct GeneratorWrapperMinMaxTrait
63 
64 } // namespace traits
65 
85 template <typename ResultType, class Generator, typename Traits =
86  traits::GeneratorWrapperMinMaxTrait<ResultType, Generator> >
87 class GeneratorWrapper : public Traits
88 {
89  public :
90 
91  typedef ResultType result_type;
92 
93  explicit GeneratorWrapper (result_type = 0)
95 
96  template <typename SeedSeq>
97  explicit GeneratorWrapper (SeedSeq &,
98  typename cxx11::enable_if<internal::is_seed_seq<SeedSeq,
100  >::value>::type * = VSMC_NULLPTR)
102 
103  void seed (result_type) {}
104 
105  template <typename SeedSeq>
106  void seed (SeedSeq &,
107  typename cxx11::enable_if<internal::is_seed_seq<SeedSeq,
109  >::value>::type * = VSMC_NULLPTR) {}
110 
111  result_type operator() ()
112  {return static_cast<result_type>(generator_.generate());}
113 
114  void discard (std::size_t nskip)
115  {
116  for (std::size_t i = 0; i != nskip; ++i)
117  operator()();
118  }
119 
120  Generator &generator () {return generator_;}
121 
122  const Generator &generator () const {return generator_;}
123 
124  friend inline bool operator== (
127  {return false;}
128 
129  friend inline bool operator!= (
132  {return true;}
133 
134  template <typename CharT, typename CharTraits>
135  friend inline std::basic_ostream<CharT, CharTraits> &operator<< (
136  std::basic_ostream<CharT, CharTraits> &os,
138  {return os;}
139 
140  template <typename CharT, typename CharTraits>
141  friend inline std::basic_istream<CharT, CharTraits> &operator>> (
142  std::basic_istream<CharT, CharTraits> &is,
144  {return is;}
145 
146  private :
147 
148  Generator generator_;
149 }; // clss GeneratorWrapper
150 
151 } // namespace vsmc
152 
153 #endif // VSMC_RNG_GENERATOR_WRAPPER_HPP
friend std::basic_istream< CharT, CharTraits > & operator>>(std::basic_istream< CharT, CharTraits > &is, GeneratorWrapper< ResultType, Generator, Traits > &)
Definition: adapter.hpp:37
#define VSMC_STATIC_ASSERT_RNG_GENERATOR_WRAPPER
void seed(SeedSeq &, typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, GeneratorWrapper< ResultType, Generator, Traits > >::value >::type *=nullptr)
#define VSMC_CONSTEXPR
constexpr
Definition: defines.hpp:55
Default traits of GeneratorWrapper.
friend bool operator!=(const GeneratorWrapper< ResultType, Generator, Traits > &, const GeneratorWrapper< ResultType, Generator, Traits > &)
#define VSMC_MNE
Avoid MSVC stupid behavior: MNE = Macro No Expansion.
Definition: defines.hpp:38
friend bool operator==(const GeneratorWrapper< ResultType, Generator, Traits > &, const GeneratorWrapper< ResultType, Generator, Traits > &)
void discard(std::size_t nskip)
A thin wrapper over any RNG Generator for use with C++11 API.
#define VSMC_NULLPTR
nullptr
Definition: defines.hpp:79
static constexpr const ResultType _Min
friend std::basic_ostream< CharT, CharTraits > & operator<<(std::basic_ostream< CharT, CharTraits > &os, const GeneratorWrapper< ResultType, Generator, Traits > &)
const Generator & generator() const
static constexpr const ResultType _Max
GeneratorWrapper(SeedSeq &, typename cxx11::enable_if< internal::is_seed_seq< SeedSeq, result_type, GeneratorWrapper< ResultType, Generator, Traits > >::value >::type *=nullptr)
GeneratorWrapper(result_type=0)