Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
transition-map.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_TRANSITION_MAP_HH
2 # define VCSN_CORE_TRANSITION_MAP_HH
3 
4 # include <map>
5 # include <type_traits>
6 
7 namespace vcsn
8 {
9  namespace detail
10  {
11 
28  template <typename Aut,
29  typename WeightSet = weightset_t_of<Aut>,
30  bool Deterministic = false,
31  bool AllOut = false,
32  bool KeepTransitions = false>
34  {
35  public:
39  using weight_t = typename weightset_t::value_t;
40 
42  template <bool KeepTransitions_ = false, typename Dummy = void>
43  struct transition_
44  {
46  : wgt(w)
47  , dst(d)
48  {}
52  };
53 
56  template <typename Dummy>
57  struct transition_<true, Dummy>
58  {
63  };
64 
67  using transition = transition_<KeepTransitions>;
68 
69  using transitions_t
70  = typename std::conditional<Deterministic,
71  transition,
72  std::vector<transition>>::type;
73  using map_t = std::map<label_t_of<Aut>, transitions_t>;
74 
75  transition_map(const Aut& aut, const weightset_t& ws)
76  : aut_(aut)
77  , ws_(ws)
78  {}
79 
80  transition_map(const Aut& aut)
81  : transition_map(aut, *aut->weightset())
82  {}
83 
86  {
87  auto lb = maps_.lower_bound(s);
88  if (lb == maps_.end() || maps_.key_comp()(s, lb->first))
89  return build_map_(lb, s);
90  else
91  return lb->second;
92  }
93 
94  private:
95  using maps_t = std::map<state_t, map_t>;
96 
98  template <bool Deterministic_>
99  void
102  typename std::enable_if<Deterministic_>::type* = nullptr)
103  {
104  map.emplace(l, t);
105  }
106 
108  template <bool Deterministic_>
109  void
112  typename std::enable_if<!Deterministic_>::type* = nullptr)
113  {
114  map[l].emplace_back(t);
115  }
116 
119  map_t&
120  build_map_(typename maps_t::iterator lb, state_t s)
121  {
122  auto& res = maps_.emplace_hint(lb, s, map_t{})->second;
123  for (auto t: aut_->all_out(s))
124  if (AllOut || !aut_->labelset()->is_special(aut_->label_of(t)))
125  {
126  auto w = ws_.conv(*aut_->weightset(), aut_->weight_of(t));
127  insert_<Deterministic>(res,
128  aut_->label_of(t),
129  transition{w, aut_->dst_of(t), t});
130  }
131  return res;
132  }
133 
137  const Aut& aut_;
139  const weightset_t& ws_;
140  };
141  }
142 }
143 
144 #endif // !VCSN_CORE_TRANSITION_MAP_HH
weight_t wgt
The (converted) weight.
transition_(weight_t w, state_t d, transition_t)
void insert_(map_t &map, label_t_of< Aut > l, transition t, typename std::enable_if<!Deterministic_ >::type *=nullptr)
Insert l -> t in map.
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:101
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:33
Outgoing signature: weight, destination.
void insert_(map_t &map, label_t_of< Aut > l, transition t, typename std::enable_if< Deterministic_ >::type *=nullptr)
Insert l -> t in map.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:36
map_t & operator[](state_t s)
Outgoing transitions of state s, sorted by label.
transition_map(const Aut &aut, const weightset_t &ws)
const weightset_t & ws_
The result weightset.
typename std::conditional< Deterministic, transition, std::vector< transition >>::type transitions_t
map_t & build_map_(typename maps_t::iterator lb, state_t s)
Build and return the transition map for state s, store at res.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
const Aut & aut_
The automaton whose transitions are cached.
transition_< KeepTransitions > transition
Outgoing signature: weight, destination, and possibly transition identifier.
Cache the outgoing transitions of an automaton as efficient maps label -> vector<(weight, dst)>.