Vcsn  2.8
Be Rational
path.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <vcsn/ctx/traits.hh>
6 
7 namespace vcsn
8 {
9  namespace detail
10  {
15  template <Automaton Aut>
16  class path
17  {
18  using automaton_t = Aut;
22  using path_t = std::vector<transition_t>;
23  public:
24 
25  weight_t
26  path_sum(const automaton_t& aut, const std::vector<transition_t>& elts)
27  {
29  for (auto tr : elts)
30  res = ws_.mul(res, aut->weight_of(tr));
31  return res;
32  }
33 
34  path(const automaton_t& aut, const std::vector<transition_t>& elts = {})
35  : path_{elts}
36  , weight_{path_sum(aut, elts)}
37  , aut_{aut}
38  , ws_{*aut_->weightset()}
39  {}
40 
41  void
43  {
44  path_.push_back(tr);
45  weight_ = ws_.mul(weight_, weight);
46  }
47 
48  template <typename... Args>
49  void emplace_back(weight_t weight, Args&&... args)
50  {
51  path_.emplace_back(std::forward<Args>(args)...);
52  weight_ = ws_.mul(weight_, weight);
53  }
54 
55  bool
56  operator<(const path& other) const
57  {
58  return ws_.less(weight_, other.weight_);
59  }
60 
62  template <typename PolynomialSet>
63  auto
64  make_monomial(const PolynomialSet& ps) const
65  -> typename PolynomialSet::monomial_t
66  {
67  const auto& pls = *ps.labelset();
68  const auto& pws = *ps.weightset();
69  const auto& ls = *aut_->labelset();
70  auto w = pws.one();
71  auto l = pls.one();
72  for (auto t : path_)
73  {
74  w = pws.mul(w, aut_->weight_of(t));
75  auto nl = aut_->label_of(t);
76  if (!ls.is_special(nl))
77  l = pls.mul(l, nl);
78  }
79  return {l, w};
80  }
81 
82  const path_t& get_path() const
83  {
84  return path_;
85  }
86 
87  private:
90  const automaton_t& aut_;
92  };
93  }
94 }
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
Definition: traits.hh:67
typename detail::transition_t_of_impl< base_t< ValueSet > >::type transition_t_of
Definition: traits.hh:65
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
Definition: traits.hh:64
const automaton_t & aut_
Definition: path.hh:90
weight_t_of< Aut > weight_t
Definition: path.hh:19
constant< type_t::one, Context > one
Definition: fwd.hh:121
path_t path_
Definition: path.hh:88
void emplace_back(weight_t weight, Args &&... args)
Definition: path.hh:49
auto make_monomial(const PolynomialSet &ps) const -> typename PolynomialSet::monomial_t
Construct a monomial from the path.
Definition: path.hh:64
transition_t_of< automaton_t > transition_t
Definition: path.hh:20
void push_back(transition_t tr, weight_t weight)
Definition: path.hh:42
weight_t path_sum(const automaton_t &aut, const std::vector< transition_t > &elts)
Definition: path.hh:26
Explicit path representation.
Definition: path.hh:16
path(const automaton_t &aut, const std::vector< transition_t > &elts={})
Definition: path.hh:34
Definition: a-star.hh:8
const weightset_t_of< automaton_t > & ws_
Definition: path.hh:91
bool operator<(const path &other) const
Definition: path.hh:56
std::vector< transition_t > path_t
Definition: path.hh:22
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
Definition: traits.hh:66
weight_t weight_
Definition: path.hh:89
value_impl< detail::weight_tag > weight
Definition: fwd.hh:34
return res
Definition: multiply.hh:399
const path_t & get_path() const
Definition: path.hh:82
state_t_of< automaton_t > state_t
Definition: path.hh:21