Vcsn  2.1
Be Rational
proper.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdexcept>
4 #include <type_traits>
5 #include <unordered_map>
6 #include <unordered_set>
7 #include <utility>
8 #include <vector>
9 
10 #include <boost/lexical_cast.hpp>
11 #include <boost/heap/fibonacci_heap.hpp>
12 
13 #include <vcsn/algos/copy.hh>
14 #include <vcsn/algos/dot.hh>
15 #include <vcsn/algos/fwd.hh>
17 #include <vcsn/algos/is-proper.hh>
18 #include <vcsn/algos/is-valid.hh>
19 #include <vcsn/core/kind.hh>
20 #include <vcsn/labelset/labelset.hh> // make_proper_context
21 #include <vcsn/misc/attributes.hh>
22 #include <vcsn/misc/builtins.hh>
23 #include <vcsn/misc/direction.hh>
24 #include <vcsn/misc/star-status.hh>
25 #include <vcsn/misc/vector.hh> // make_vector
26 
30 
31 namespace vcsn
32 {
33  namespace detail
34  {
35  template <typename Aut>
36  class properer
37  {
43 
44  public:
59  properer(const automaton_t& aut,
60  bool prune = true,
61  const std::string& algo = "auto")
62  : aut_(aut)
63  , prune_(prune)
64  , algo_(algo)
65  {}
66 
68  {
69  return proper_star<weightset_t::star_status()>();
70  }
71 
72  private:
74  {
75  if (algo_ == "auto" || algo_ == "default" || algo_ == "inplace")
76  {
77  auto a = copy(aut_); // in place
79  return r();
80  }
81  else if (algo_ == "separate")
82  {
84  return r();
85  }
86  else if (algo_ == "distance")
87  {
89  return r();
90  }
91  else
92  raise("proper: invalid algorithm: ", algo_);
93  }
94 
95  template <star_status_t Status>
98  {
99  try
100  {
101  return remover_();
102  }
103  catch (const std::runtime_error&)
104  {
105  raise("proper: invalid automaton");
106  }
107  }
108 
109  template <star_status_t Status>
112  {
113  require(is_valid(aut_), "proper: invalid automaton");
114  return remover_();
115  }
116 
117  template <star_status_t Status>
120  {
121  return remover_();
122  }
123 
124  template <star_status_t Status>
127  {
128  require(is_valid(aut_), "proper: invalid automaton");
129  return remover_();
130  }
131 
132  automaton_t aut_;
133  bool prune_;
134  const std::string& algo_;
135  };
136 
137  template <typename Aut>
138  auto make_properer(Aut aut,
139  bool prune = true,
140  const std::string& algo = "auto")
141  {
142  return properer<Aut>(aut, prune, algo);
143  }
144  }
145 
146  /*---------.
147  | proper. |
148  `---------*/
149 
163  template <typename Aut>
164  auto
165  proper(const Aut& aut, direction dir = direction::backward,
166  bool prune = true, const std::string& algo = "auto")
167  -> fresh_automaton_t_of<Aut,
169  {
170  switch (dir)
171  {
172  case direction::backward:
173  return detail::make_properer(aut, prune, algo)();
174  case direction::forward:
175  return transpose(proper(transpose(aut),
176  direction::backward, prune, algo));
177  }
179  }
180 
181  namespace dyn
182  {
183  namespace detail
184  {
186  template <typename Aut, typename Dir, typename Bool, typename String>
187  automaton proper(const automaton& aut, direction dir, bool prune,
188  const std::string& algo)
189  {
190  const auto& a = aut->as<Aut>();
191  return make_automaton(::vcsn::proper(a, dir, prune, algo));
192  }
193  }
194  }
195 } // namespace vcsn
automaton proper(const automaton &aut, direction dir, bool prune, const std::string &algo)
Bridge.
Definition: proper.hh:187
properer(const automaton_t &aut, bool prune=true, const std::string &algo="auto")
Remove the epsilon-transitions of the input.
Definition: proper.hh:59
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
weightset_t_of< automaton_t > weightset_t
Definition: proper.hh:39
vcsn::enable_if_t< Status==star_status_t::NON_STARRABLE, aut_proper_t > proper_star()
Definition: proper.hh:126
labelset_t_of< automaton_t > labelset_t
Definition: proper.hh:40
typename std::remove_cv< Aut >::type automaton_t
Definition: proper.hh:38
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:197
Looking upstream.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:51
typename std::enable_if< Cond, T >::type enable_if_t
Definition: type_traits.hh:16
vcsn::enable_if_t< Status==star_status_t::ABSVAL, aut_proper_t > proper_star()
Definition: proper.hh:111
direction
Orientation.
Definition: direction.hh:9
vcsn::enable_if_t< Status==star_status_t::STARRABLE, aut_proper_t > proper_star()
Definition: proper.hh:119
aut_proper_t remover_()
Definition: proper.hh:73
vcsn::enable_if_t< Status==star_status_t::TOPS, aut_proper_t > proper_star()
Definition: proper.hh:97
This class contains the core of the proper algorithm.
Aut transpose(const transpose_automaton< Aut > &aut)
Definition: transpose.hh:235
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Definition: traits.hh:57
aut_proper_t operator()()
Definition: proper.hh:67
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
AutOut copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:254
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
auto proper(const Aut &aut, direction dir=direction::backward, bool prune=true, const std::string &algo="auto") -> fresh_automaton_t_of< Aut, detail::proper_context< context_t_of< Aut >>>
Eliminate spontaneous transitions.
Definition: proper.hh:165
fresh_automaton_t_of< automaton_t, detail::proper_context< context_t_of< automaton_t >>> aut_proper_t
Definition: proper.hh:42
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:47
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
bool is_valid(const Aut &aut)
Definition: is-valid.hh:141
Looking downstream.
automaton_t aut_
Definition: proper.hh:132
const std::string & algo_
Definition: proper.hh:134
This class contains the core of the proper algorithm.
auto make_properer(Aut aut, bool prune=true, const std::string &algo="auto")
Definition: proper.hh:138
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
This class contains the core of the proper algorithm.