Vcsn  2.5
Be Rational
implicit-path.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/core/automaton.hh>
5 #include <vcsn/ctx/traits.hh>
6 
7 namespace vcsn
8 {
9  namespace detail
10  {
19  template <Automaton Aut>
21  {
22  using automaton_t = Aut;
26 
27  public:
28  static constexpr int null_parent_path = -1;
29 
30  implicit_path(const automaton_t& aut, transition_t sidetrack,
31  int parent_path, weight_t weight)
32  : aut_{aut}
33  , sidetrack_{sidetrack}
34  , parent_path_{parent_path}
35  , weight_{weight}
36  {}
37 
47  explicit_path(const std::vector<path<automaton_t>>& ksp,
49  state_t src)
50  {
51  auto res = path<automaton_t>(aut_);
52 
53  if (parent_path_ != null_parent_path)
54  {
55  auto& explicit_pref_path = ksp[parent_path_];
56  const auto& path = explicit_pref_path.get_path();
57 
58  // The index of the last transition of the path before the
59  // sidetrack. Using the last occurence of the sidetrack is necessary
60  // to avoid computing the same path each time in the case of loops.
61  // If the sidetrack appears twice and we rebuild our path after the
62  // first one then we lost the previous path.
63  auto i = std::find_if(path.rbegin(), path.rend(), [&] (auto curr) {
64  return aut_->dst_of(curr) == aut_->src_of(sidetrack_);
65  });
66  assert(i != path.rend());
67  // Make it forward iterator, but one iteration further (in
68  // the forward direction).
69  auto last = i.base();
70  for (auto i = path.begin(); i != last; ++i)
71  res.emplace_back(aut_->weight_of(*i), *i);
72  res.emplace_back(aut_->weight_of(sidetrack_), sidetrack_);
73  }
74 
75  auto s = parent_path_ == null_parent_path
76  ? src : aut_->dst_of(sidetrack_);
77 
78  const auto& ws = *aut_->weightset();
79  while (s != tree.get_root())
80  {
81  auto next = tree.get_parent_of(s);
82  if (s == aut_->null_state() || next == aut_->null_state())
83  return path<automaton_t>(aut_);
84  auto weight = ws.rdivide(tree[s].get_weight(),
85  tree[next].get_weight());
86  auto t = find_transition(s, next);
87  assert(t != aut_->null_transition());
88  res.emplace_back(weight, t);
89  s = next;
90  }
91 
92  return res;
93  }
94 
98  {
99  auto min_tr = aut_->null_transition();
100  for (auto tr : detail::outin(aut_, src, dst))
101  if (min_tr == aut_->null_transition()
102  || aut_->weightset()->less(aut_->weight_of(tr),
103  aut_->weight_of(min_tr)))
104  min_tr = tr;
105  return min_tr;
106  }
107 
108  bool operator<(const implicit_path& other) const
109  {
110  return aut_->weightset()->less(weight_, other.weight_);
111  }
112 
113  const weight_t& get_weight() const
114  {
115  return weight_;
116  }
117 
119  {
120  return sidetrack_;
121  }
122 
123  private:
130  };
131  }
132 }
static constexpr int null_parent_path
const path_t & get_path() const
Definition: path.hh:82
Definition: a-star.hh:8
bool operator<(const implicit_path &other) const
return res
Definition: multiply.hh:398
state_t_of< automaton_t > state_t
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
Definition: traits.hh:64
value_impl< detail::weight_tag > weight
Definition: fwd.hh:34
const weight_t & get_weight() const
path< automaton_t > explicit_path(const std::vector< path< automaton_t >> &ksp, shortest_path_tree< automaton_t > &tree, state_t src)
Create the explicit representation of the implicit path.
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
Definition: traits.hh:66
transition_t get_sidetrack() const
implicit_path(const automaton_t &aut, transition_t sidetrack, int parent_path, weight_t weight)
const automaton_t & aut_
Explicit path representation.
Definition: path.hh:16
Implicit Path representation.
typename detail::transition_t_of_impl< base_t< ValueSet > >::type transition_t_of
Definition: traits.hh:65
transition_t find_transition(state_t src, state_t dst) const
Find the lightest transition from src to dst.
transition_t_of< automaton_t > transition_t
auto outin(const Aut &aut, state_t_of< Aut > s, state_t_of< Aut > d)
Indexes of visible transitions from state s to state d.
Definition: automaton.hh:189
int parent_path_
Parent path indexes in the results array.
weight_t_of< automaton_t > weight_t