Vcsn  2.2a
Be Rational
transition-map.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
5 #include <vcsn/misc/map.hh> // vcsn::less
6 
7 namespace vcsn
8 {
9  namespace detail
10  {
11 
28  template <Automaton Aut,
29  typename WeightSet = weightset_t_of<Aut>,
30  bool Deterministic = false,
31  bool AllOut = false,
32  bool KeepTransitions = false>
34  {
35  public:
38  using weightset_t = WeightSet;
39  using weight_t = typename weightset_t::value_t;
40 
42  template <typename Weight = weight_t,
43  bool KeepTransitions_ = false, typename Dummy = void>
44  struct transition_
45  {
47  : wgt(w)
48  , dst(d)
49  {}
52  weight_t weight() const { return wgt; }
54  };
55 
57  template <typename Dummy>
58  struct transition_<bool, false, Dummy>
59  {
61  : dst(d)
62  {}
63  static constexpr weight_t weight() { return true; }
65  };
66 
69  template <typename Weight, typename Dummy>
70  struct transition_<Weight, true, Dummy>
71  {
73  : wgt(w), dst(d), transition(t)
74  {}
77  weight_t weight() const { return wgt; }
80  };
81 
84  template <typename Dummy>
85  struct transition_<bool, true, Dummy>
86  {
88  : dst(d), transition(t)
89  {}
90  static constexpr bool weight() { return true; }
93  };
94 
97  using transition = transition_<weight_t, KeepTransitions>;
98 
99  using transitions_t
100  = std::conditional_t<Deterministic,
101  transition,
102  std::vector<transition>>;
103  using map_t = std::map<label_t_of<Aut>, transitions_t,
105 
106  transition_map(const Aut& aut, const weightset_t& ws)
107  : aut_(aut)
108  , ws_(ws)
109  {}
110 
111  transition_map(const Aut& aut)
112  : transition_map(aut, *aut->weightset())
113  {}
114 
117  {
118  auto lb = maps_.lower_bound(s);
119  if (lb == maps_.end() || maps_.key_comp()(s, lb->first))
120  return build_map_(lb, s);
121  else
122  return lb->second;
123  }
124 
125  private:
126  using maps_t = std::map<state_t, map_t>;
127 
129  template <bool Deterministic_>
130  void
132  label_t_of<Aut> l, transition t,
133  std::enable_if_t<Deterministic_>* = nullptr)
134  {
135  map.emplace(l, t);
136  }
137 
139  template <bool Deterministic_>
140  void
142  label_t_of<Aut> l, transition t,
143  std::enable_if_t<!Deterministic_>* = nullptr)
144  {
145  map[l].emplace_back(t);
146  }
147 
150  map_t&
151  build_map_(typename maps_t::iterator lb, state_t s)
152  {
153  auto& res = maps_.emplace_hint(lb, s, map_t{})->second;
154  for (auto t: all_out(aut_, s))
155  if (AllOut || !aut_->labelset()->is_special(aut_->label_of(t)))
156  {
157  auto w = ws_.conv(*aut_->weightset(), aut_->weight_of(t));
158  insert_<Deterministic>(res,
159  aut_->label_of(t),
160  transition{w, aut_->dst_of(t), t});
161  }
162  return res;
163  }
164 
168  Aut aut_;
170  const weightset_t& ws_;
171  };
172  }
173 }
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:54
std::conditional_t< Deterministic, transition, std::vector< transition >> transitions_t
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:57
void insert_(map_t &map, label_t_of< Aut > l, transition t, std::enable_if_t< Deterministic_ > *=nullptr)
Insert l -> t in map.
weight_t wgt
The (converted) weight.
Cache the outgoing transitions of an automaton as efficient maps label -> vector<(weight, dst)>.
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:37
void insert_(map_t &map, label_t_of< Aut > l, transition t, std::enable_if_t<!Deterministic_ > *=nullptr)
Insert l -> t in map.
const weightset_t & ws_
The result weightset.
Outgoing signature: weight, destination.
#define Automaton
Definition: automaton.hh:24
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
Definition: tuple.hh:160
map_t & operator[](state_t s)
Outgoing transitions of state s, sorted by label.
transition_< weight_t, false > transition
Outgoing signature: weight, destination, and possibly transition identifier.
transition_(weight_t w, state_t d, transition_t)
transition_map(const Aut &aut, const weightset_t &ws)
std::map< label_t_of< Aut >, transitions_t, less< labelset_t_of< Aut >>> map_t
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
Aut aut_
The automaton whose transitions are cached.
map_t & build_map_(typename maps_t::iterator lb, state_t s)
Build and return the transition map for state s, store at res.
Functor to compare Values of ValueSets.
Definition: functional.hh:78
Definition: a-star.hh:8