Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
universal.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_UNIVERSAL_HH
2 # define VCSN_ALGOS_UNIVERSAL_HH
3 
4 # include <map>
5 
6 # include <vcsn/misc/set.hh>
8 # include <vcsn/algos/transpose.hh>
9 # include <vcsn/weightset/fwd.hh> // b
10 
11 namespace vcsn
12 {
13  namespace detail
14  {
16  template <typename Aut>
18  {
19  public:
20  static_assert(labelset_t_of<Aut>::is_free(),
21  "universal: requires free labelset");
22  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
23  "universal: requires Boolean weights");
24 
25  using automaton_t = Aut;
27  using state_set_t = std::set<state_t>;
28  using pstate_t = std::set<state_set_t>;
29  using map_t = std::map<state_t, state_set_t>;
30 
33  {
34  if (!is_deterministic(automaton))
35  return work_(determinize(automaton)->strip());
36  else if (!is_complete(automaton))
37  return work_(complete(automaton));
38  else
39  return work_(automaton);
40  }
41 
42  private:
47  {
48  // The initial state of automaton.
49  state_t i = aut->dst_of(aut->initial_transitions().front());
50 
51  // compute the co-determinized of the minimal automaton
52  // and retrieve the origin of each state.
53  const auto transposed = transpose(aut);
54  auto codet = determinize(transposed);
55  map_t origin = codet->origins();
56  origin.erase(codet->pre());
57 
58  // the 'origin' is a map from co_det's state_t to
59  // minimal's state_set_t.
60  // let 'transp_states' be the image of 'origin'.
61  pstate_t transp_states = image(origin);
62 
63  // the universal automaton's state set is its intersection closure.
64  pstate_t univers_states(intersection_closure(transp_states));
65 
66  // The universal automaton.
67  automaton_t res = make_shared_ptr<automaton_t>(aut->context());
68 
69  // The final states of aut.
70  std::set<state_t> automaton_finals;
71  for (auto t: aut->final_transitions())
72  automaton_finals.insert(aut->src_of(t));
73 
74  // we have to save the state set associated to each automaton.
75  map_t subset_label;
76 
77  // X = univers_states \ {}.
78  for (const auto s: univers_states)
79  if (!s.empty())
80  {
81  state_t new_s = res->new_state();
82  subset_label[new_s] = s;
83  // J = { X | i in X }
84  if (has(s, i))
85  res->set_initial(new_s);
86  // U = { X | X \subset T }
87  if (subset(s, automaton_finals))
88  res->set_final(new_s);
89  }
90 
91  // Finally, the transition set.
92  for (const auto x: res->states())
93  for (const auto y: res->states())
94  for (const auto a: *res->labelset())
95  {
96  bool cont = false;
97  state_set_t delta_ret;
98  for (auto s: subset_label[x])
99  {
100  bool empty = true;
101  for (auto t: aut->out(s, a))
102  {
103  empty = false;
104  delta_ret.insert(aut->dst_of(t));
105  }
106  if (empty)
107  {
108  cont = true;
109  break;
110  }
111  }
112  // case 1: \exists p \in X, p.a = {}
113  if (cont)
114  continue;
115  // case 2: X.a \subset Y?
116  if (subset(delta_ret, subset_label[y]))
117  res->new_transition(x, y, a);
118  }
119  return res;
120  }
121  };
122  }
123 
124  template <class Aut>
125  inline
126  Aut
127  universal(const Aut& a)
128  {
130  return universal(a);
131  }
132 
133  /*-----------------.
134  | dyn::universal. |
135  `-----------------*/
136 
137  namespace dyn
138  {
139  namespace detail
140  {
141 
142  template <typename Aut>
143  automaton
144  universal(const automaton& aut)
145  {
146  const auto& a = aut->as<Aut>();
147  return make_automaton(::vcsn::universal(a));
148  }
149 
151  (const automaton& aut) -> automaton);
152  }
153  }
154 
155 }
156 
157 #endif // !VCSN_ALGOS_UNIVERSAL_HH
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
bool subset(const Container1 &set1, const Container2 &set2) ATTRIBUTE_PURE
Whether set1 ⊆ set2.
Definition: set.hxx:88
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
std::set< std::set< T, Compare, Alloc > > intersection_closure(std::set< std::set< T, Compare, Alloc >> pset)
The set of all the intersections of the sets in pset.
automaton universal(const automaton &aut)
Definition: universal.hh:144
std::set< state_set_t > pstate_t
Definition: universal.hh:28
Aut universal(const Aut &a)
Definition: universal.hh:127
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
bool is_complete(const Aut &aut)
Whether aut is complete.
Definition: is-complete.hh:15
automaton_t work_(const automaton_t &aut)
Work on aut, which is complete and deterministic.
Definition: universal.hh:46
automaton_t operator()(const Aut &automaton)
The universal automaton of automaton.
Definition: universal.hh:32
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
auto complete(const Aut &aut) -> decltype(::vcsn::copy(aut))
Definition: complete.hh:64
std::set< state_t > state_set_t
Definition: universal.hh:27
auto determinize(const Aut &a) -> determinized_automaton< Aut >
Definition: determinize.hh:244
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
std::set< typename std::map< Key, Value, Comp, Alloc >::mapped_type > image(const std::map< Key, Value, Comp, Alloc > &m)
The set of values of a map.
Definition: set.hxx:16
Aut transpose(const transpose_automaton< Aut > &aut)
Definition: transpose.hh:230
auto strip(const Aut &aut, int) -> decltype(aut->strip())
Definition: strip.hh:17
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:35
state_t_of< automaton_t > state_t
Definition: universal.hh:26
Functor for universal.
Definition: universal.hh:17
std::map< state_t, state_set_t > map_t
Definition: universal.hh:29