Vcsn  2.5.dev
Be Rational
transition-map.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 #include <vector>
5 
7 #include <vcsn/core/automaton.hh> // states_size
8 #include <vcsn/misc/map.hh> // vcsn::less
9 
10 namespace vcsn
11 {
12  namespace detail
13  {
14 
31  template <Automaton Aut,
32  typename WeightSet = weightset_t_of<Aut>,
33  bool Deterministic = false,
34  bool AllOut = false,
35  bool KeepTransitions = false>
37  {
38  public:
43  using weightset_t = WeightSet;
44  using weight_t = typename weightset_t::value_t;
45 
47  template <typename Weight = weight_t,
48  bool KeepTransitions_ = false, typename Dummy = void>
49  struct transition_
50  {
52  : wgt(w)
53  , dst(d)
54  {}
57  weight_t weight() const { return wgt; }
59  };
60 
62  template <typename Dummy>
63  struct transition_<bool, false, Dummy>
64  {
66  : dst(d)
67  {}
68  static constexpr weight_t weight() { return true; }
70  };
71 
74  template <typename Weight, typename Dummy>
75  struct transition_<Weight, true, Dummy>
76  {
78  : wgt(w), dst(d), transition(t)
79  {}
82  weight_t weight() const { return wgt; }
85  };
86 
89  template <typename Dummy>
90  struct transition_<bool, true, Dummy>
91  {
93  : dst(d), transition(t)
94  {}
95  static constexpr bool weight() { return true; }
98  };
99 
102  using transition = transition_<weight_t, KeepTransitions>;
103 
104  using transitions_t
105  = std::conditional_t<Deterministic,
106  transition,
107  std::vector<transition>>;
108  using map_t = std::map<label_t_of<Aut>, transitions_t,
110 
111  transition_map(const Aut& aut, const weightset_t& ws)
112  : maps_(states_size(aut))
113  , aut_(aut)
114  , ws_(ws)
115  {}
116 
117  transition_map(const Aut& aut)
118  : transition_map(aut, *aut->weightset())
119  {}
120 
122  : maps_(std::move(that.maps_))
123  , aut_(std::move(that.aut_))
124  , ws_(that.ws_)
125  {}
126 
129  {
130  // We might be working on a lazy automaton, so be prepared to
131  // find states that did not exist when we created this
132  // transition map.
133  if (maps_.size() <= s)
134  {
135  auto capacity = maps_.capacity();
136  while (capacity <= s)
137  capacity *= 2;
138  maps_.reserve(capacity);
139  maps_.resize(s + 1);
140  }
141  auto& res = maps_[s];
142  if (!res)
143  {
144  res = std::make_unique<map_t>();
145  build_map_(*res, s);
146  }
147  return *res;
148  }
149 
150  private:
152  using maps_t = std::vector<std::unique_ptr<map_t>>;
153 
155  template <bool Deterministic_>
156  void
158  label_t_of<Aut> l, transition t,
159  std::enable_if_t<Deterministic_>* = nullptr)
160  {
161  map.emplace(l, t);
162  }
163 
165  template <bool Deterministic_>
166  void
168  label_t_of<Aut> l, transition t,
169  std::enable_if_t<!Deterministic_>* = nullptr)
170  {
171  map[l].emplace_back(t);
172  }
173 
175  void
177  {
178  for (auto t: all_out(aut_, s))
179  if (AllOut || !aut_->labelset()->is_special(aut_->label_of(t)))
180  {
181  auto w = ws_.conv(*aut_->weightset(), aut_->weight_of(t));
182  insert_<Deterministic>(map,
183  aut_->label_of(t),
184  transition{w, aut_->dst_of(t), t});
185  }
186  }
187 
191  Aut aut_;
193  const weightset_t& ws_;
194  };
195  }
196 }
Outgoing signature: weight, destination.
void insert_(map_t &map, label_t_of< Aut > l, transition t, std::enable_if_t<!Deterministic_ > *=nullptr)
Insert l -> t in map.
transition_map(transition_map &&that)
transition_(weight_t w, state_t d, transition_t)
std::vector< std::unique_ptr< map_t > > maps_t
For each state number, its transition map.
Aut aut_
The automaton whose transitions are cached.
typename detail::label_t_of_impl< base_t< ValueSet > >::type label_t_of
Definition: traits.hh:62
return res
Definition: multiply.hh:399
Functor to compare Values of ValueSets.
Definition: functional.hh:91
transition_map(const Aut &aut, const weightset_t &ws)
std::map< label_t_of< Aut >, transitions_t, less< labelset_t_of< Aut > >> map_t
Cache the outgoing transitions of an automaton as efficient maps label -> vector<(weight, dst)>.
#define Automaton
Definition: automaton.hh:23
std::conditional_t< Deterministic, transition, std::vector< transition > > transitions_t
Definition: a-star.hh:8
transition_< weight_t, false > transition
Outgoing signature: weight, destination, and possibly transition identifier.
void build_map_(map_t &map, state_t s)
Build the transition map for state s, store at 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:223
size_t states_size(const Aut &aut)
The largest state number, plus one.
Definition: automaton.hh:41
map_t & operator[](state_t s)
Outgoing transitions of state s, sorted by label.
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
Definition: traits.hh:64
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:67
void insert_(map_t &map, label_t_of< Aut > l, transition t, std::enable_if_t< Deterministic_ > *=nullptr)
Insert l -> t in map.
typename detail::transition_t_of_impl< base_t< ValueSet > >::type transition_t_of
Definition: traits.hh:65
STL namespace.
const weightset_t & ws_
The result weightset.
weight_t wgt
The (converted) weight.