Vcsn  2.2
Be Rational
tags.hh
Go to the documentation of this file.
1 #pragma once
2 
4 #include <vcsn/algos/standard.hh>
6 #include <vcsn/misc/static-if.hh>
7 #include <vcsn/misc/symbol.hh>
8 
9 namespace vcsn
10 {
11  /*------------------.
12  | tags definitions |
13  `------------------*/
14 
16  struct auto_tag {};
17 
19  struct deterministic_tag {};
20 
22  struct general_tag {};
23 
25  struct standard_tag {};
26 
27  namespace detail
28  {
30  template <Automaton... Aut, typename Operation>
31  auto
32  dispatch_tags(std::string algo, Operation op, Aut&&... auts)
33  {
34  // Whether all the labelsets are free. Requirement to call
35  // determinize/is_deterministic.
36  constexpr bool are_free
37  = all_<labelset_t_of<decltype(auts)>::is_free()...>();
38 
39  if (algo == "auto")
40  {
41  if (all(is_standard(std::forward<Aut>(auts))...))
42  algo = "standard";
43  else if (static_if<are_free>
44  ([](auto&&... as){ return all(is_deterministic(as)...); },
45 #if (defined __clang__ && __clang_major__ == 3 && __clang_minor__ < 6 \
46  || defined __GNUC__ && !defined __clang__ && __GNUC__ < 5)
47  // Clang 3.5 and GCC 4.9 require that we name the argument.
48  [](auto&&... as){ return false; }
49 #else
50  [](auto&&...){ return false; }
51 #endif
52  )
53  (std::forward<Aut>(auts)...))
54  algo = "deterministic";
55  else
56  algo = "general";
57  }
58 
59  if (algo == "general")
60  return op(general_tag{});
61  else if (algo == "standard")
62  return op(standard_tag{});
63  else
64  {
65  using res_t = decltype(op(standard_tag{}));
66  res_t res = nullptr;
67  if (algo == "deterministic")
68  static_if<are_free>([&res](auto&& op)
69  { res = op(deterministic_tag{}); })(op);
70  require(res, "invalid algorithm: ", str_escape(algo));
71  return res;
72  }
73  }
74 
76  template <Automaton... Auts>
77  auto
79  {
80  return join_automata(std::forward<Auts>(auts)...);
81  }
82 
85  template <Automaton... Auts>
86  auto
88  // SFINAE
89  -> decltype(nullable_join_automata(std::forward<Auts>(auts)...))
90  {
91  return nullable_join_automata(std::forward<Auts>(auts)...);
92  }
93 
95  template <Automaton... Auts>
96  auto
98  {
99  return join_automata(std::forward<Auts>(auts)...);
100  }
101  }
102 
105  struct boolean_tag
106  {
107  static symbol sname()
108  {
109  static auto res = symbol{"boolean_tag"};
110  return res;
111  }
112  };
113 
117  struct dijkstra_tag {};
118 
123  {
124  static symbol sname()
125  {
126  static auto res = symbol{"weighted_tag"};
127  return res;
128  }
129  };
130 }
Tag for operations on all automata.
Definition: tags.hh:22
bool all(Bool &&...values)
Definition: tuple.hh:410
Definition: a-star.hh:8
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:54
Request the Boolean specialization for determinization (B and F2).
Definition: tags.hh:105
Tag to request the most appropriate version of an algorithm.
Definition: tags.hh:16
auto make_join_automaton(deterministic_tag, Auts &&...auts)
Make an empty automaton which is a supertype of others.
Definition: tags.hh:78
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
auto nullable_join_automata(Auts &&...auts) -> decltype(make_mutable_automaton(nullable_join_context(auts...)))
An automaton whose type is the nullable join between those of auts.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static symbol sname()
Definition: tags.hh:107
static symbol sname()
Definition: tags.hh:124
Tag for operations on deterministic automata.
Definition: tags.hh:19
auto join_automata(Auts &&...auts) -> decltype(make_mutable_automaton(join(auts->context()...)))
An automaton whose type is the join between those of auts.
Dijkstra implementation.
Definition: tags.hh:117
#define Automaton
Definition: automaton.hh:24
bool is_standard(const Aut &a)
Whether a is standard.
Definition: standard.hh:28
Request for the weighted version of an algorithm.
Definition: tags.hh:122
Tag for operations on standard automata.
Definition: tags.hh:25
auto dispatch_tags(std::string algo, Operation op, Aut &&...auts)
Dispatch an operation between automata depending on their nature.
Definition: tags.hh:32