ssmkit  master-68aed98
hierarchical_detail.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 //exclude all from doxygen documantation
5 
6 #include <tao/seq/make_integer_range.hpp> //https://github.com/taocpp/sequences
7 
8 namespace ssmkit {
9 namespace process {
10 
11 namespace detail {
12 //==========GetRandom==========
13 template <size_t N, size_t NA, size_t D, class TArities>
14 struct GetRandom {
15  template <class TRV, class TP, class TA, size_t... Is>
16  static void apply(std::index_sequence<Is...>, TRV &rvs, TP &prc,
17  const TA &args) {
18  std::get<N>(rvs) =
19  std::get<N>(prc).random(std::get<N - 1>(rvs), std::get<Is>(args)...);
20  constexpr size_t arity =
21  std::tuple_element<N + 1, TArities>::type::value - 1;
22  GetRandom<N + 1, NA + arity, D, TArities>::apply(
23  tao::seq::make_index_range<NA, NA + arity>(), rvs, prc, args);
24  }
25 };
26 
27 template <size_t NA, size_t D, class TArities>
28 struct GetRandom<0, NA, D, TArities> {
29  template <class TRV, class TP, class TA, size_t... Is>
30  static void apply(std::index_sequence<Is...>, TRV &rvs, TP &prc,
31  const TA &args) {
32  std::get<0>(rvs) = std::get<0>(prc).random(std::get<Is>(args)...);
33  constexpr size_t arity = std::tuple_element<1, TArities>::type::value - 1;
34  GetRandom<1, NA + arity, D, TArities>::apply(
35  tao::seq::make_index_range<NA, NA + arity>(), rvs, prc, args);
36  }
37 };
38 
39 template <size_t NA, size_t D, class TArities>
40 struct GetRandom<D, NA, D, TArities> {
41  template <class TRV, class TP, class TA, size_t... Is>
42  static void apply(std::index_sequence<Is...>, TRV &rvs, TP &prc,
43  const TA &args) {
44  std::get<D>(rvs) =
45  std::get<D>(prc).random(std::get<D - 1>(rvs), std::get<Is>(args)...);
46  }
47 };
48 //===========GetInitialized===================
49 template <size_t N>
50 struct GetInitialized {
51  template <class TRV, class TP>
52  static void apply(TRV &rvs, TP &prc) {
53  std::get<N>(rvs) = std::get<N>(prc).initialize();
54 
55  GetInitialized<N - 1>::apply(rvs, prc);
56  }
57 };
58 
59 template <>
60 struct GetInitialized<0> {
61  template <class TRV, class TP>
62  static void apply(TRV &rvs, TP &prc) {
63  std::get<0>(rvs) = std::get<0>(prc).initialize();
64  }
65 };
66 
67 //===================GetLikelihood=============================
68 template <size_t N, size_t NA, size_t D, class TArities>
69 struct GetLikelihood {
70  template <class TRV, class TP, class TA, size_t... Is>
71  static void apply(std::index_sequence<Is...>, double &lik, const TRV &rvs,
72  TP &prc, const TA &args) {
73 
74  lik *= std::get<N>(prc).likelihood(std::get<N>(rvs), std::get<N - 1>(rvs),
75  std::get<Is>(args)...);
76 
77  constexpr size_t arity =
78  std::tuple_element<N + 1, TArities>::type::value - 1;
79  GetLikelihood<N + 1, NA + arity, D, TArities>::apply(
80  tao::seq::make_index_range<NA, NA + arity>(), lik, rvs, prc, args);
81  }
82 };
83 
84 template <size_t NA, size_t D, class TArities>
85 struct GetLikelihood<0, NA, D, TArities> {
86  template <class TRV, class TP, class TA, size_t... Is>
87  static void apply(std::index_sequence<Is...>, double &lik, const TRV &rvs,
88  TP &prc, const TA &args) {
89 
90  lik *= std::get<0>(prc).likelihood(std::get<0>(rvs), std::get<Is>(args)...);
91 
92  constexpr size_t arity = std::tuple_element<1, TArities>::type::value - 1;
93  GetLikelihood<1, NA + arity, D, TArities>::apply(
94  tao::seq::make_index_range<NA, NA + arity>(), lik, rvs, prc, args);
95  }
96 };
97 
98 template <size_t NA, size_t D, class TArities>
99 struct GetLikelihood<D, NA, D, TArities> {
100  template <class TRV, class TP, class TA, size_t... Is>
101  static void apply(std::index_sequence<Is...>, double &lik, const TRV &rvs,
102  TP &prc, const TA &args) {
103 
104  lik *= std::get<D>(prc).likelihood(std::get<D>(rvs), std::get<D - 1>(rvs),
105  std::get<Is>(args)...);
106  }
107 };
108 
109 } // namespace detail
110 } // namespace process
111 } // namespace ssmkit
112