Vcsn  2.3
Be Rational
polystate-automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <queue>
5 #include <set>
6 #include <type_traits>
7 
10 #include <vcsn/misc/wet.hh>
11 
12 namespace vcsn
13 {
14  namespace detail
15  {
21  template <Automaton Aut,
22  wet_kind_t Kind = detail::wet_kind<labelset_t_of<Aut>,
23  weightset_t_of<Aut>>(),
24  bool Lazy = false>
28  weightset_t_of<Aut>>, Kind>,
30  Lazy>
31  {
32  public:
33  using automaton_t = Aut;
35  template <typename Ctx = context_t>
38  using state_bimap_t
40  weightset_t_of<Aut>>, Kind>,
42  Lazy>;
43 
48 
52 
54  using state_nameset_t = typename state_bimap_t::state_nameset_t;
55  using state_name_t = typename state_bimap_t::state_name_t;
56 
60  : super_t{a->context()}
61  , input_{a}
62  {
63  // Pre.
64  {
65  auto pre = zero();
66  ns_.new_weight(pre, input_->pre(), ws_.one());
67  todo_.push(this->emplace(std::move(pre), this->pre()).first);
68  if (Lazy)
69  this->set_lazy(this->pre());
70  }
71 
72  // Post.
73  {
74  auto post = zero();
75  ns_.new_weight(post, input_->post(), ws_.one());
76  this->emplace(std::move(post), this->post());
77  }
78  }
79 
80  bool state_has_name(state_t s) const
81  {
82  return has(this->origins(), s);
83  }
84 
87  {
88  return static_if<Kind == wet_kind_t::bitset>
89  ([] (const auto& self) { return state_name_t(self.state_size_); },
90  [] (const auto& self) { return self.ns_.zero(); })
91  (*this);
92  }
93 
94  std::ostream&
95  print_state_name(state_t s, std::ostream& o,
96  format fmt = {}, bool delimit = false) const
97  {
98  const auto& origs = this->origins();
99  auto i = origs.find(s);
100  if (i == std::end(origs))
101  this->print_state(s, o);
102  else
103  {
104  if (delimit)
105  o << '{';
106  ns_.print(i->second, o, fmt, ", ");
107  if (delimit)
108  o << '}';
109  }
110  return o;
111  }
112 
116  {
117  state_t res;
118  auto i = this->find_key(n);
119  if (i == this->end_key())
120  {
121  res = this->new_state();
122  if (Lazy)
123  this->set_lazy(res);
124  todo_.push(this->emplace(std::move(n), res).first);
125  }
126  else
127  res = i->second;
128  return res;
129  }
130 
133 
135  weightset_t ws_ = *input_->weightset();
136 
138  state_nameset_t ns_ = {{stateset_t(input_), ws_}};
139 
141  using queue_t = std::queue<typename state_bimap_t::const_iterator>;
143 
147  size_t state_size_ = states_size(input_);
148  };
149 
151  template <Automaton Aut,
152  wet_kind_t Kind = wet_kind<labelset_t_of<Aut>,
153  weightset_t_of<Aut>>(),
154  bool Lazy = false>
155  using polystate_automaton
157 
158  template <Automaton Aut,
159  wet_kind_t Kind = wet_kind<labelset_t_of<Aut>,
160  weightset_t_of<Aut>>(),
161  bool Lazy = false>
162  auto
163  make_polystate_automaton(const Aut& aut)
164  {
166  return make_shared_ptr<res_t>(aut);
167  }
168  }
169 } // namespace vcsn
state_name_t zero() const
The empty polynomial of states.
#define Automaton
Definition: automaton.hh:23
fresh_automaton_t_of< Aut, Ctx > fresh_automaton_t
std::shared_ptr< polystate_automaton_impl< Aut, Kind, Lazy >> polystate_automaton
A polystate automaton as a shared pointer.
An automaton whose state names are polynomials of states.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
size_t states_size(const Aut &aut)
The largest state number, plus one.
Definition: automaton.hh:19
Aggregate an automaton, and forward calls to it.
labelset_t_of< automaton_t > labelset_t
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
state_t state_(state_name_t n)
The state for set of states n.
std::queue< typename state_bimap_t::const_iterator > queue_t
States waiting to be processed.
weightset_t_of< automaton_t > weightset_t
return res
Definition: multiply.hh:398
label_t_of< automaton_t > label_t
Labels and weights.
auto context(Args &&...args) const -> decltype(aut_-> context(std::forward< Args >(args)...))
typename state_bimap_t::state_name_t state_name_t
wet_kind_t
Different implementations of wets.
Definition: wet.hh:197
std::ostream & print_state_name(state_t s, std::ostream &o, format fmt={}, bool delimit=false) const
auto make_polystate_automaton(const Aut &aut)
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
Definition: setalpha.hh:25
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
Definition: a-star.hh:8
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Given an automaton type, the type of its copies.
Definition: traits.hh:82
An input/output format for valuesets.
Definition: format.hh:13
State labelset.
Definition: stateset.hh:14
A bidirectional map from state names to state numbers.
Definition: state-bimap.hh:23
state_t_of< automaton_t > state_t
State index.
polystate_automaton_impl(const automaton_t &a)
Build the determinizer.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
typename state_bimap_t::state_nameset_t state_nameset_t
The state name: set of (input) states.
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46