Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
is-valid.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_IS_VALID_HH
2 # define VCSN_ALGOS_IS_VALID_HH
3 
4 # include <stdexcept>
5 # include <type_traits>
6 # include <utility>
7 
8 # include <vcsn/algos/copy.hh>
10 # include <vcsn/algos/is-proper.hh>
11 # include <vcsn/algos/proper.hh>
12 # include <vcsn/algos/strip.hh>
13 # include <vcsn/core/kind.hh>
14 # include <vcsn/misc/star_status.hh>
15 # include <vcsn/misc/direction.hh>
16 
17 namespace vcsn
18 {
19 
20  /*----------------.
21  | is_valid(aut). |
22  `----------------*/
23 
24  namespace detail
25  {
26 
29  template <typename Aut>
30  typename Aut::element_type::automaton_nocv_t
31  absval(const Aut& aut)
32  {
33  auto res = copy(aut);
34  // Apply absolute value to the weight of each transition.
35  const auto& ws = *aut->weightset();
36  for (auto t: res->transitions())
37  res->set_weight(t, ws.abs(res->weight_of(t)));
38  return res;
39  }
40 
43  template <typename Aut>
44  bool is_properable(Aut&& aut)
45  {
46  return in_situ_remover(aut);
47  }
48 
49 
50  template <typename Aut, bool has_one = context_t_of<Aut>::has_one()>
51  class is_valider
52  {
53  public:
54  using automaton_t = typename std::remove_cv<Aut>::type;
56 
75  static bool is_valid(const automaton_t& aut)
76  {
77  return is_valid_<weightset_t::star_status()>(aut);
78  }
79 
80  private:
81  template <star_status_t Status>
82  static
83  typename std::enable_if<Status == star_status_t::TOPS,
84  bool>::type
85  is_valid_(const automaton_t& aut)
86  {
87  return (is_proper(aut)
88  || is_eps_acyclic(aut)
89  || is_properable(copy(aut)));
90  }
91 
92  template <star_status_t Status>
93  static
94  typename std::enable_if<Status == star_status_t::ABSVAL,
95  bool>::type
96  is_valid_(const automaton_t& aut)
97  {
98  return (is_proper(aut)
99  || is_eps_acyclic(aut)
100  || is_properable(absval(aut)));
101  }
102 
103  template <star_status_t Status>
104  static
105  typename std::enable_if<Status == star_status_t::STARRABLE,
106  bool>::type
108  {
109  return true;
110  }
111 
112  template <star_status_t Status>
113  static
114  typename std::enable_if<Status == star_status_t::NON_STARRABLE,
115  bool>::type
116  is_valid_(const automaton_t& aut)
117  {
118  return is_proper(aut) || is_eps_acyclic(aut);
119  }
120  };
121 
122  template <typename Aut>
123  class is_valider<Aut, false>
124  {
125  using automaton_t = typename std::remove_cv<Aut>::type;
126  public:
127  static constexpr bool is_valid(const automaton_t&)
128  {
129  return true;
130  }
131  };
132 
133  }
134 
135 
136  template <typename Aut>
137  inline
138  bool is_valid(const Aut& aut)
139  {
141  }
142 
143  namespace dyn
144  {
145  namespace detail
146  {
148  template <typename Aut>
149  bool is_valid(const automaton& aut)
150  {
151  const auto& a = aut->as<Aut>();
152  return is_valid(strip(a));
153  }
154 
156  (const automaton& aut) -> bool);
157  }
158  }
159 } // namespace vcsn
160 
161 #endif // !VCSN_ALGOS_IS_VALID_HH
automaton strip(const automaton &aut)
Bridge.
Definition: strip.hh:52
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
bool is_properable(Aut &&aut)
Whether proper_here(aut) succeeds.
Definition: is-valid.hh:44
static std::enable_if< Status==star_status_t::TOPS, bool >::type is_valid_(const automaton_t &aut)
Definition: is-valid.hh:85
typename std::remove_cv< Aut >::type automaton_t
Definition: is-valid.hh:54
static std::enable_if< Status==star_status_t::NON_STARRABLE, bool >::type is_valid_(const automaton_t &aut)
Definition: is-valid.hh:116
bool is_valid(const Aut &aut)
Definition: is-valid.hh:138
typename std::remove_cv< Aut >::type automaton_t
Definition: is-valid.hh:125
bool in_situ_remover(Aut &aut, bool prune=true)
Blindly eliminate epsilon transitions without checking for the validity of the automaton.
Definition: proper.hh:542
static std::enable_if< Status==star_status_t::ABSVAL, bool >::type is_valid_(const automaton_t &aut)
Definition: is-valid.hh:96
bool is_proper(const Aut &aut)
Test whether an automaton is proper.
Definition: is-proper.hh:49
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:171
static bool is_valid(const automaton_t &aut)
Whether an automaton is valid.
Definition: is-valid.hh:75
bool is_valid(const automaton &aut)
Bridge.
Definition: is-valid.hh:149
static constexpr bool is_valid(const automaton_t &)
Definition: is-valid.hh:127
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
ATTRIBUTE_CONST bool is_eps_acyclic(const Aut &aut)
Aut::element_type::automaton_nocv_t absval(const Aut &aut)
Copy of aut, with absolute values.
Definition: is-valid.hh:31
weightset_t_of< automaton_t > weightset_t
Definition: is-valid.hh:55
static std::enable_if< Status==star_status_t::STARRABLE, bool >::type is_valid_(const automaton_t &)
Definition: is-valid.hh:107