Vcsn  2.2
Be Rational
expression-automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stack>
4 
5 #include <boost/bimap.hpp>
6 #include <boost/bimap/set_of.hpp>
7 #include <boost/bimap/unordered_set_of.hpp>
8 
10 #include <vcsn/core/fwd.hh> // expression_automaton
12 #include <vcsn/misc/bimap.hh>
13 #include <vcsn/misc/symbol.hh>
14 
15 namespace vcsn
16 {
17  namespace detail
18  {
20  template <Automaton Aut>
22  : public automaton_decorator<Aut>
23  {
24  public:
25  using automaton_t = Aut;
33 
36 
38  : super_t(rs.context())
39  , rs_(rs)
40  {}
41 
43  static symbol sname()
44  {
45  static auto res = symbol{"expression_automaton<"
47  return res;
48  }
49 
50  std::ostream& print_set(std::ostream& o, format fmt = {}) const
51  {
52  o << "expression_automaton<";
53  super_t::print_set(o, fmt);
54  return o << '>';
55  }
56 
58  using left_t
59  = boost::bimaps::unordered_set_of<expression_t,
60  vcsn::hash<expressionset_t>,
61  vcsn::equal_to<expressionset_t>>;
63  using right_t = boost::bimaps::set_of<state_t>;
65  using bimap_t = boost::bimap<left_t, right_t>;
66 
69  state_t state(const expression_t& r)
70  {
71  state_t res;
72  const auto& map_ = bimap_.left;
73  auto i = map_.find(r);
74  if (i == std::end(map_))
75  {
76  res = super_t::new_state();
77  bimap_.insert({r, res});
78  todo_.emplace(res, r);
79  super_t::set_lazy(res);
80  }
81  else
82  res = i->second;
83  return res;
84  }
85 
86  using super_t::add_transition;
87  void
88  add_transition(state_t src, const expression_t& dst,
89  label_t l, const weight_t& w)
90  {
91  super_t::add_transition(src, state(dst), l, w);
92  }
93 
94  using super_t::new_transition;
95  void
96  new_transition(state_t src, const expression_t& dst,
97  label_t l, const weight_t& w)
98  {
99  super_t::new_transition(src, state(dst), l, w);
100  }
101 
102  using super_t::set_initial;
103  void
104  set_initial(const expression_t& s, const weight_t& w)
105  {
106  super_t::set_initial(state(s), w);
107  }
108 
109  bool state_has_name(state_t s) const
110  {
111  return has(origins(), s);
112  }
113 
114  std::ostream&
115  print_state_name(state_t s, std::ostream& o,
116  format fmt = {},
117  bool = false) const
118  {
119  auto i = origins().find(s);
120  if (i == std::end(origins()))
121  this->print_state(s, o);
122  else
123  rs_.print(i->second, o, fmt);
124  return o;
125  }
126 
128  using origins_t = typename bimap_t::right_map;
129  const origins_t& origins() const
130  {
131  return bimap_.right;
132  }
133 
134  // private:
136  expressionset_t rs_;
138  std::stack<std::pair<state_t, const state_name_t>> todo_;
140  bimap_t bimap_;
141  };
142  }
143 }
expressionset_t rs_
The expression's set.
expression_automaton_impl(const expressionset_t &rs)
Definition: a-star.hh:8
auto print_set(Args &&...args) const -> decltype(aut_-> print_set(std::forward< Args >(args)...))
An input/output format for valuesets.
Definition: format.hh:11
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:58
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
typename expressionset_t::value_t expression_t
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
Aggregate an automaton, and forward calls to it.
auto rs
Definition: lift.hh:151
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
expression_t state_name_t
State are named by expressions.
std::ostream & print_set(std::ostream &o, format fmt={}) const
An incremental automaton whose states are expressions.