Vcsn  2.1
Be Rational
universal.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/copy.hh> // make_fresh_automaton
6 #include <vcsn/weightset/fwd.hh> // b
7 
8 namespace vcsn
9 {
10  namespace detail
11  {
13  template <typename Aut>
15  {
16  public:
17  static_assert(labelset_t_of<Aut>::is_free(),
18  "universal: requires free labelset");
19  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
20  "universal: requires Boolean weights");
21 
22  using automaton_t = Aut;
24  using state_set_t = std::set<state_t>;
25  using pstate_t = std::set<state_set_t>;
26  using map_t = std::map<state_t, state_set_t>;
27 
30  {
31  if (!is_deterministic(automaton))
32  return work_(determinize(automaton)->strip());
33  else if (!is_complete(automaton))
34  return work_(complete(automaton));
35  else
36  return work_(automaton);
37  }
38 
39  private:
44  {
45  // The initial state of automaton.
46  state_t i = aut->dst_of(aut->initial_transitions().front());
47 
48  // compute the co-determinized of the minimal automaton
49  // and retrieve the origin of each state.
50  const auto transposed = transpose(aut);
51  auto codet = determinize(transposed);
52  map_t origin = codet->origins();
53  origin.erase(codet->pre());
54 
55  // the 'origin' is a map from co_det's state_t to
56  // minimal's state_set_t.
57  // let 'transp_states' be the image of 'origin'.
58  pstate_t transp_states = image(origin);
59 
60  // the universal automaton's state set is its intersection closure.
61  pstate_t univers_states(intersection_closure(transp_states));
62 
63  // The universal automaton.
65 
66  // The final states of aut.
67  std::set<state_t> automaton_finals;
68  for (auto t: aut->final_transitions())
69  automaton_finals.insert(aut->src_of(t));
70 
71  // we have to save the state set associated to each automaton.
72  map_t subset_label;
73 
74  // X = univers_states \ {}.
75  for (const auto s: univers_states)
76  if (!s.empty())
77  {
78  state_t new_s = res->new_state();
79  subset_label[new_s] = s;
80  // J = { X | i in X }
81  if (has(s, i))
82  res->set_initial(new_s);
83  // U = { X | X \subset T }
84  if (subset(s, automaton_finals))
85  res->set_final(new_s);
86  }
87 
88  // Finally, the transition set.
89  for (const auto x: res->states())
90  for (const auto y: res->states())
91  for (const auto a: *res->labelset())
92  {
93  bool cont = false;
94  state_set_t delta_ret;
95  for (auto s: subset_label[x])
96  {
97  bool empty = true;
98  for (auto t: aut->out(s, a))
99  {
100  empty = false;
101  delta_ret.insert(aut->dst_of(t));
102  }
103  if (empty)
104  {
105  cont = true;
106  break;
107  }
108  }
109  // case 1: \exists p \in X, p.a = {}
110  if (cont)
111  continue;
112  // case 2: X.a \subset Y?
113  if (subset(delta_ret, subset_label[y]))
114  res->new_transition(x, y, a);
115  }
116  return res;
117  }
118  };
119  }
120 
121  template <typename Aut>
122  inline
123  Aut
124  universal(const Aut& a)
125  {
127  return universal(a);
128  }
129 
130  namespace dyn
131  {
132  namespace detail
133  {
135  template <typename Aut>
136  automaton
137  universal(const automaton& aut)
138  {
139  const auto& a = aut->as<Aut>();
140  return make_automaton(::vcsn::universal(a));
141  }
142  }
143  }
144 }
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.
Definition: set.hxx:31
automaton universal(const automaton &aut)
Bridge.
Definition: universal.hh:137
Functor for universal.
Definition: universal.hh:14
bool subset(const Container &set1, const Container &set2) ATTRIBUTE_PURE
Whether set1 ⊆ set2.
Definition: set.hxx:78
AutOut make_fresh_automaton(const AutIn &model)
Create an empty, mutable, automaton, based on another one.
Definition: copy.hh:239
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:48
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:51
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: map.hh:46
auto strip(const Aut &aut, int) -> decltype(aut->strip())
Definition: strip.hh:16
Aut transpose(const transpose_automaton< Aut > &aut)
Definition: transpose.hh:235
Aut universal(const Aut &a)
Definition: universal.hh:124
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
automaton_t work_(const automaton_t &aut)
Work on aut, which is complete and deterministic.
Definition: universal.hh:43
bool is_complete(const Aut &aut)
Whether aut is complete.
Definition: is-complete.hh:13
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
std::map< state_t, state_set_t > map_t
Definition: universal.hh:26
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:47
automaton_t operator()(const Aut &automaton)
The universal automaton of automaton.
Definition: universal.hh:29
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
ATTRIBUTE_PURE bool has(const std::deque< T, Allocator > &s, const T &e)
Whether e is member of s.
Definition: deque.hh:13
state_t_of< automaton_t > state_t
Definition: universal.hh:23
auto determinize(const Aut &a) -> determinized_automaton< Aut >
Definition: determinize.hh:251
std::set< state_t > state_set_t
Definition: universal.hh:24
std::set< state_set_t > pstate_t
Definition: universal.hh:25
auto complete(const Aut &aut) -> decltype(::vcsn::copy(aut))
Definition: complete.hh:61