Vcsn  2.2a
Be Rational
delay-automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stack>
4 #include <vector>
5 
6 #include <boost/bimap.hpp>
7 #include <boost/bimap/unordered_set_of.hpp>
8 
9 #include <vcsn/algos/fwd.hh>
10 #include <vcsn/ctx/context.hh>
11 #include <vcsn/dyn/automaton.hh> // dyn::make_automaton
12 #include <vcsn/misc/bimap.hh>
13 #include <vcsn/misc/pair.hh> // hash
14 #include <vcsn/misc/unordered_map.hh> // has
15 
16 namespace vcsn
17 {
18  namespace detail
19  {
20 
27  template <Automaton Aut>
29  : public automaton_decorator<fresh_automaton_t_of<Aut>>
30  {
31  public:
32  using automaton_t = Aut;
39 
41  template <std::size_t... I>
43 
44  static constexpr size_t number_of_tapes = labelset_t_of<Aut>::size();
45 
47 
48  static constexpr index_t indices = {};
49 
51  using delay_t = std::array<size_t, number_of_tapes>;
52 
54  using state_name_t = std::pair<state_t, delay_t>;
55 
57  using bimap_t =
58  boost::bimap<boost::bimaps::unordered_set_of<state_name_t>,
59  boost::bimaps::unordered_set_of<state_t>>;
60  using map_t = typename bimap_t::left_map;
61  using origins_t = typename bimap_t::right_map;
62 
63  template <size_t I>
64  using tape_labelset_t = typename labelset_t::template valueset_t<I>;
65 
67  : super_t(aut->context())
68  , aut_(aut)
69  {
70  map_().insert({{this->pre(), delay_t{}}, aut->pre()});
71  map_().insert({{this->post(), delay_t{}}, aut->post()});
72  }
73 
75  static symbol sname()
76  {
77  static auto res = symbol{"delay_automaton<"
79  return res;
80  }
81 
82  std::ostream& print_set(std::ostream& o, format fmt = {}) const
83  {
84  o << "delay_automaton<";
85  super_t::print_set(o, fmt);
86  return o << '>';
87  }
88 
91  state_t state(const state_name_t& r)
92  {
93  if (r.first == aut_->post())
94  return this->post();
95  state_t res;
96  auto i = map_().find(r);
97  if (i == std::end(map_()))
98  {
99  res = super_t::new_state();
100  // Emplace is not available because of the internals of bimap
101  map_().insert({r, res});
102  todo_.push(r);
103  }
104  else
105  res = i->second;
106  return res;
107  }
108 
109  using super_t::new_transition;
110 
111  void
112  new_transition(const state_name_t& src, const state_name_t& dst,
113  const label_t& l, const weight_t& w)
114  {
115  super_t::new_transition(state(src), state(dst), l, w);
116  }
117 
118  bool state_has_name(state_t s) const
119  {
120  return has(origins(), s);
121  }
122 
123  std::ostream&
124  print_state_name(state_t s, std::ostream& o,
125  format fmt = {},
126  bool = false) const
127  {
128  auto ns = origins().at(s);
129  aut_->print_state_name(ns.first, o, fmt, true);
130  o << ":(";
131  auto a = ns.second;
132  for (int i = 0; i < a.size() - 1; i++)
133  o << a[i] << ',';
134  if (a.size())
135  o << a[a.size() - 1];
136  o << ')';
137  return o;
138  }
139 
140  delay_t delay_of(state_t s)
141  {
142  auto i = origins().find(s);
143  if (i == std::end(origins()))
144  return {};
145  else
146  return i->second.second;
147  }
148 
149  const origins_t&
150  origins() const
151  {
152  return bimap_.right;
153  }
154 
155  map_t&
156  map_()
157  {
158  return bimap_.left;
159  }
160 
162  std::stack<state_name_t, std::vector<state_name_t>> todo_;
164  bimap_t bimap_;
166  automaton_t aut_;
167  };
168 
169  template <Automaton Aut>
170  delay_automaton<Aut>
171  make_delay_automaton(const Aut& aut)
172  {
173  auto s = synchronize_checker<Aut>{aut};
174  return s.make_delay_automaton();
175  }
176  }
177 
178 
179  /*------------------.
180  | delay_automaton. |
181  `------------------*/
182 
187  template <Automaton Aut>
188  auto
189  make_delay_automaton(const Aut& aut)
190  -> decltype(detail::make_delay_automaton(aut))
191  {
192  return detail::make_delay_automaton(aut);
193  }
194 
195  namespace dyn
196  {
197  namespace detail
198  {
200  template <Automaton Aut>
201  automaton delay_automaton(const automaton& aut)
202  {
203  return make_automaton(vcsn::make_delay_automaton(aut->as<Aut>()));
204  }
205  }
206  }
207 }
typename labelset_t::template valueset_t< I > tape_labelset_t
static symbol sname()
Static name.
symbol sname()
Definition: name.hh:67
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:54
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static constexpr auto pre(Args &&...args) -> decltype(element_type::pre(std::forward< Args >(args)...))
static constexpr auto post(Args &&...args) -> decltype(element_type::post(std::forward< Args >(args)...))
auto print_set(Args &&...args) const -> decltype(aut_-> print_set(std::forward< Args >(args)...))
typename bimap_t::right_map origins_t
std::pair< state_t, delay_t > state_name_t
State + delay.
typename bimap_t::left_map map_t
labelset_t_of< super_t > labelset_t
context_t_of< super_t > context_t
boost::bimap< boost::bimaps::unordered_set_of< state_name_t >, boost::bimaps::unordered_set_of< state_t >> bimap_t
Symbolic states to state handlers.
static constexpr size_t number_of_tapes
Aggregate an automaton, and forward calls to it.
std::array< size_t, number_of_tapes > delay_t
The delay associated with each state.
delay_automaton_impl(const automaton_t &aut)
std::ostream & print_set(std::ostream &o, format fmt={}) const
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
An automaton whose states may be qualified by delays and/or prefixes.
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
automaton_t aut_
The original automaton.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:58
static constexpr index_t indices
An input/output format for valuesets.
Definition: format.hh:11
Definition: a-star.hh:8