Vcsn  2.1
Be Rational
expression-automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <stack>
5 #include <string>
6 #include <vector>
7 
9 #include <vcsn/core/fwd.hh> // expression_automaton
11 #include <vcsn/misc/map.hh>
12 #include <vcsn/misc/symbol.hh>
14 
15 //# define DEBUG 1
16 
17 #if DEBUG
18 # define DEBUG_IFELSE(Then, Else) Then
19 #else
20 # define DEBUG_IFELSE(Then, Else) Else
21 #endif
22 
23 #define DEBUG_IF(Then) DEBUG_IFELSE(Then,)
24 
25 namespace vcsn
26 {
27  namespace detail
28  {
30  template <typename Aut>
32  : public automaton_decorator<Aut>
33  {
34  public:
35  using automaton_t = Aut;
43 
45  : super_t(rs.context())
46  , rs_(rs)
47  {}
48 
50  static symbol sname()
51  {
52  static symbol res("expression_automaton<"
54  return res;
55  }
56 
57  std::ostream& print_set(std::ostream& o, format fmt = {}) const
58  {
59  o << "expression_automaton<";
60  super_t::print_set(o, fmt);
61  return o << '>';
62  }
63 
65  using smap = std::unordered_map<expression_t, state_t,
66  vcsn::hash<expressionset_t>,
67  vcsn::equal_to<expressionset_t>>;
68 
71  state_t state(const expression_t& r)
72  {
73  // Benches show that the map_.emplace technique is slower, and
74  // then that operator[] is faster than emplace.
75  state_t res;
76  auto i = map_.find(r);
77  if (i == std::end(map_))
78  {
79  DEBUG_IF(
80  std::cerr << "New state: ";
81  rs_.print(r, std::cerr) << '\n';
82  );
83  res = super_t::new_state();
84  map_[r] = res;
85  todo_.push(r);
86  }
87  else
88  res = i->second;
89  return res;
90  }
91 
92  using super_t::add_transition;
93  void
94  add_transition(state_t src, const expression_t& dst,
95  label_t l, const weight_t& w)
96  {
97  super_t::add_transition(src, state(dst), l, w);
98  }
99 
100  using super_t::new_transition;
101  void
102  new_transition(state_t src, const expression_t& dst,
103  label_t l, const weight_t& w)
104  {
105  super_t::new_transition(src, state(dst), l, w);
106  }
107 
108  using super_t::set_initial;
109  void
110  set_initial(const expression_t& s, const weight_t& w)
111  {
112  super_t::set_initial(state(s), w);
113  }
114 
115  bool state_has_name(state_t s) const
116  {
117  return has(origins(), s);
118  }
119 
120  std::ostream&
121  print_state_name(state_t s, std::ostream& o,
122  format fmt = {},
123  bool = false) const
124  {
125  auto i = origins().find(s);
126  if (i == std::end(origins()))
127  this->print_state(s, o);
128  else
129  rs_.print(i->second, o, fmt);
130  return o;
131  }
132 
134  using origins_t = std::map<state_t, expression_t>;
135  mutable origins_t origins_;
136  const origins_t&
137  origins() const
138  {
139  if (origins_.empty())
140  for (const auto& p: map_)
141  origins_[p.second] = p.first;
142  return origins_;
143  }
144 
146  expressionset_t rs_;
148  std::stack<expression_t, std::vector<expression_t>> todo_;
150  smap map_;
151  };
152  }
153 }
typename expressionset_t::value_t expression_t
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:46
An incremental automaton whose states are expressions.
expression_automaton_impl(const expressionset_t &rs)
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:48
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:45
Aggregate an automaton, and forward calls to it.
auto print_set(Args &&...args) const -> decltype(aut_-> print_set(std::forward< Args >(args)...))
An input/output format.
Definition: format.hh:11
expressionset_t rs_
The expression's set.
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
auto rs
Definition: lift.hh:151
symbol sname()
Definition: name.hh:67
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
std::ostream & print_set(std::ostream &o, format fmt={}) const
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:50