Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / benchmark / generator.hpp
1 // Copyright 2019 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <array>
8 #include <random>
9
10 using uniform = std::uniform_real_distribution<>;
11 using uniform_int = std::uniform_int_distribution<>;
12 using normal = std::normal_distribution<>;
13
14 template <class Distribution, class... Ts>
15 Distribution init(Ts...);
16
17 template <>
18 uniform init<uniform>() {
19   return uniform{0.0, 1.0};
20 }
21
22 template <>
23 normal init<normal>() {
24   return normal{0.5, 0.3};
25 }
26
27 template <>
28 uniform_int init<uniform_int, int>(int n) {
29   return uniform_int{0, n};
30 }
31
32 template <class Distribution, std::size_t N = 1 << 15>
33 struct generator : std::array<double, N> {
34   using base_t = std::array<double, N>;
35
36   template <class... Ts>
37   generator(Ts... ts) {
38     std::default_random_engine rng(1);
39     auto dis = init<Distribution>(ts...);
40     std::generate(base_t::begin(), base_t::end(), [&] { return dis(rng); });
41   }
42
43   const double& operator()() {
44     ++ptr_;
45     if (ptr_ == base_t::data() + N) ptr_ = base_t::data();
46     return *ptr_;
47   }
48
49   const double* ptr_ = base_t::data() - 1;
50 };