taco-db  0.1.0
Walker.h
Go to the documentation of this file.
1 #ifndef UTILS_WALKER_H
2 #define UTILS_WALKER_H
3 
4 // This implements the Walker's algorithm.
5 
6 #include "tdb.h"
7 
8 #include <random>
9 
10 namespace taco {
11 
19 class Walker {
20 public:
21  using result_type = uint32_t;
22 
23  // RandomNumberDistribution requires a param_type and a few functions that
24  // accept param_type. These are not implemented since we currently don't
25  // have use for them.
26  //using param_type = /* some struct */;
27 
31  Walker():
32  m_N(0) {}
33 
39  Walker(const std::vector<double> &prob);
40 
41  Walker(const Walker&) = default;
42 
43  Walker(Walker&& other):
44  m_N(other.m_N),
45  m_IA(std::move(other.m_IA)),
46  m_F(std::move(other.m_F)),
47  m_unif_a(other.m_unif_a.param()),
48  m_unif_b(other.m_unif_b.param()) {
49  other.m_N = 0;
50  }
51 
52  Walker& operator=(const Walker&) = default;
53 
54  Walker& operator=(Walker&& other) {
55  m_N = other.m_N;
56  m_IA = std::move(other.m_IA);
57  m_F = std::move(other.m_F);
58  m_unif_a.param(other.m_unif_a.param());
59  m_unif_b.param(other.m_unif_b.param());
60  other.m_N = 0;
61  return *this;
62  }
63 
64  template<typename RNG>
65  inline uint32_t
66  operator()(RNG &rng) {
67  uint32_t IX = m_unif_a(rng);
68  double x = m_unif_b(rng);
69  if (x >= m_F[IX]) {
70  // returns the alias
71  return m_IA[IX];
72  }
73  // otherwise, returns the index itself
74  return IX;
75  }
76 
77 private:
78  uint32_t m_N;
79  std::vector<uint32_t> m_IA;
80  std::vector<double> m_F;
81 
82  std::uniform_int_distribution<uint32_t> m_unif_a;
83  std::uniform_real_distribution<double> m_unif_b;
84 };
85 
86 /*
87 template<typename RNG>
88 inline uint32_t walker_sample(WalkerParam* param, RNG &rng) {
89 
90  uint32_t IX = param->unif_a(rng);
91  if (param->unif_b(rng) >= param->F[IX]) {
92  return param->IA[IX];
93  }
94  return IX;
95 } */
96 
97 } // namespace taco
98 
99 #endif
A discrete distribution over non-negative integer set [n] for some n over a given probability distrib...
Definition: Walker.h:19
Walker & operator=(const Walker &)=default
Walker(const Walker &)=default
std::vector< uint32_t > m_IA
Definition: Walker.h:79
std::uniform_int_distribution< uint32_t > m_unif_a
Definition: Walker.h:82
uint32_t result_type
Definition: Walker.h:21
Walker()
Default constructs walker in an invalid state.
Definition: Walker.h:31
uint32_t operator()(RNG &rng)
Definition: Walker.h:66
Walker(Walker &&other)
Definition: Walker.h:43
uint32_t m_N
Definition: Walker.h:78
std::vector< double > m_F
Definition: Walker.h:80
Walker & operator=(Walker &&other)
Definition: Walker.h:54
std::uniform_real_distribution< double > m_unif_b
Definition: Walker.h:83
Definition: Record.h:148
Definition: datum.h:28