5 #include <unordered_map>
7 #include <vcsn/algos/fwd.hh>
27 template <
typename Aut>
28 class delay_automaton_impl
29 :
public automaton_decorator<fresh_automaton_t_of<Aut>>
41 template <std::size_t... I>
51 using delay_t = std::array<size_t, number_of_tapes>;
71 static symbol res(
"delay_automaton<"
78 o <<
"delay_automaton<";
84 using smap = std::unordered_map<state_name_t, state_t>;
88 state_t state(const state_name_t& r)
90 // Benches show that the map_.emplace technique is slower, and
91 // then that operator[] is faster than emplace.
92 if (r.first == aut_->post())
95 auto i = map_.find(r);
96 if (i == std::end(map_))
98 res = super_t::new_state();
107 using super_t::new_transition;
110 new_transition(const state_name_t& src, const state_name_t& dst,
111 const label_t& l, const weight_t& w)
113 super_t::new_transition(state(src), state(dst), l, w);
116 bool state_has_name(state_t s) const
118 return has(origins(), s);
122 print_state_name(state_t s, std::ostream& o,
126 auto ns = origins().at(s);
127 aut_->print_state_name(ns.first, o, fmt, true);
130 for (int i = 0; i < a.size() - 1; i++)
133 o << a[a.size() - 1];
138 delay_t delay_of(state_t s)
140 auto i = origins().find(s);
141 if (i == std::end(origins()))
144 return i->second.second;
148 using origins_t = std::unordered_map<state_t, state_name_t>;
149 mutable origins_t origins_;
154 if (origins_.empty())
155 for (const auto& p: map_)
156 origins_[p.second] = p.first;
161 std::stack<state_name_t, std::vector<state_name_t>> todo_;
168 template <typename Aut>
169 class synchronize_checker
171 static_assert(context_t_of<Aut>::is_lat,
172 "synchronize: automaton labelset must be a tupleset");
175 using automaton_t = Aut;
176 using out_automaton_t = delay_automaton<automaton_t>;
177 using state_t = state_t_of<automaton_t>;
178 using labelset_t = labelset_t_of<out_automaton_t>;
179 using label_t = typename labelset_t::value_t;
180 using delay_t = typename out_automaton_t::element_type::delay_t;
181 using state_name_t = typename out_automaton_t::element_type::state_name_t;
184 using tape_labelset_t = typename labelset_t::template valueset_t<I>;
187 template <std::size_t... I>
188 using seq = vcsn::detail::index_sequence<I...>;
191 synchronize_checker(const automaton_t& aut)
192 : in_aut_(aut), out_aut_(make_shared_ptr<out_automaton_t>(aut))
202 bool is_synchronized()
204 // tag the states with the delays
206 for (auto s : out_aut_->states())
208 delay_t d = out_aut_->delay_of(s);
210 for (auto tr : out_aut_->out(s))
212 if (out_aut_->labelset()->is_one(out_aut_->label_of(tr)))
214 auto dst = out_aut_->dst_of(tr);
215 if (out_aut_->post() == dst)
217 delay_t dst_d = out_aut_->delay_of(dst);
218 for (size_t i = 0; i < out_aut_->number_of_tapes; i++) {
219 if (d[i] && dst_d[i] <= d[i])
228 make_delay_automaton()
237 * Compute the automaton with states tagged with their delays.
239 * If split, create the artificial states required for synchronizing.
241 void value_automaton()
243 out_aut_->todo_.emplace(in_aut_->pre(), delay_t{});
245 while (!out_aut_->todo_.empty())
247 auto val_state = std::move(out_aut_->todo_.top());
248 auto st = val_state.first;
249 delay_t delay = val_state.second;
250 out_aut_->todo_.pop();
251 for (auto t : in_aut_->all_out(st))
253 auto l = in_aut_->label_of(t);
254 auto dst = in_aut_->dst_of(t);
255 delay_t d = add_delay_(delay, l, out_aut_->indices);
256 state_name_t new_state(dst, d);
257 out_aut_->new_transition(val_state,
260 in_aut_->weight_of(t));
266 template <size_t... I>
268 add_delay_(delay_t d, const label_t& l, seq<I...>) const
271 = {(std::get<I>(d) + tape_labelset_t<I>::size(std::get<I>(l)))...};
272 size_t min = *std::min_element(begin(del), end(del));
273 return {(std::get<I>(del) - min)...};
277 out_automaton_t out_aut_;
280 template <typename Aut>
281 bool is_synchronized(const Aut& aut)
283 synchronize_checker<Aut> s(aut);
284 return s.is_synchronized();
287 template <typename Aut>
289 make_delay_automaton(const Aut& aut)
291 synchronize_checker<Aut> s(aut);
292 return s.make_delay_automaton();
297 /*------------------.
299 `------------------*/
305 template <typename Aut>
307 is_synchronized(const Aut& aut)
309 return detail::is_synchronized(aut);
317 template <typename Aut>
318 bool is_synchronized(const automaton& aut)
320 return vcsn::is_synchronized(aut->as<Aut>());
325 /*------------------.
327 `------------------*/
333 template <typename Aut>
335 make_delay_automaton(const Aut& aut)
336 -> decltype(detail::make_delay_automaton(aut))
338 return detail::make_delay_automaton(aut);
346 template <typename Aut>
347 automaton delay_automaton(const automaton& aut)
349 return make_automaton(vcsn::make_delay_automaton(aut->as<Aut>()));
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
weight_t_of< super_t > weight_t
static constexpr auto pre(Args &&...args) -> decltype(element_type::pre(std::forward< Args >(args)...))
std::pair< state_t, delay_t > state_name_t
State + delay.
static constexpr auto post(Args &&...args) -> decltype(element_type::post(std::forward< Args >(args)...))
labelset_t_of< super_t > labelset_t
static symbol sname()
Static name.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
context_t_of< super_t > context_t
Aggregate an automaton, and forward calls to it.
static constexpr index_t indices
state_t_of< super_t > state_t
auto print_set(Args &&...args) const -> decltype(aut_-> print_set(std::forward< Args >(args)...))
std::array< size_t, number_of_tapes > delay_t
The delay associated with each state.
label_t_of< super_t > label_t
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
std::ostream & print_set(std::ostream &o, format fmt={}) const
automaton_t aut_
The original automaton.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
static constexpr size_t number_of_tapes
typename labelset_t::template valueset_t< I > tape_labelset_t
delay_automaton_impl(const automaton_t &aut)
smap map_
delayed_state -> state.
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of