Vcsn  2.3
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  {
24  static symbol sname()
25  {
26  static auto res = symbol{"general_tag"};
27  return res;
28  }
29  };
30 
32  struct standard_tag
33  {
34  static symbol sname()
35  {
36  static auto res = symbol{"standard_tag"};
37  return res;
38  }
39  };
40 
41  namespace detail
42  {
44  template <Automaton... Aut, typename Operation>
45  auto
46  dispatch_tags(std::string algo, Operation op, Aut&&... auts)
47  {
48  // Whether all the labelsets are free. Requirement to call
49  // determinize/is_deterministic.
50  constexpr bool are_free
51  = all_<labelset_t_of<decltype(auts)>::is_free()...>();
52 
53  if (algo == "auto")
54  {
55  if (all(is_standard(std::forward<Aut>(auts))...))
56  algo = "standard";
57  else if (static_if<are_free>
58  ([](auto&&... as){ return all(is_deterministic(as)...); },
59 #if (defined __clang__ && __clang_major__ == 3 && __clang_minor__ < 6 \
60  || defined __GNUC__ && !defined __clang__ && __GNUC__ < 5)
61  // Clang 3.5 and GCC 4.9 require that we name the argument.
62  [](auto&&... as){ return false; }
63 #else
64  [](auto&&...){ return false; }
65 #endif
66  )
67  (std::forward<Aut>(auts)...))
68  algo = "deterministic";
69  else
70  algo = "general";
71  }
72 
73  if (algo == "general")
74  return op(general_tag{});
75  else if (algo == "standard")
76  return op(standard_tag{});
77  else
78  {
79  using res_t = decltype(op(standard_tag{}));
80  res_t res = nullptr;
81  if (algo == "deterministic")
82  static_if<are_free>([&res](auto&& op)
83  { res = op(deterministic_tag{}); })(op);
84  require(res, "invalid algorithm: ", str_escape(algo));
85  return res;
86  }
87  }
88 
90  template <Automaton... Auts>
91  auto
93  // SFINAE
94  -> decltype(join_automata(std::forward<Auts>(auts)...))
95  {
96  return join_automata(std::forward<Auts>(auts)...);
97  }
98 
101  template <Automaton... Auts>
102  auto
104  // SFINAE
105  -> decltype(nullable_join_automata(std::forward<Auts>(auts)...))
106  {
107  return nullable_join_automata(std::forward<Auts>(auts)...);
108  }
109 
111  template <Automaton... Auts>
112  auto
114  // SFINAE
115  -> decltype(join_automata(std::forward<Auts>(auts)...))
116  {
117  return join_automata(std::forward<Auts>(auts)...);
118  }
119  }
120 
123  struct boolean_tag
124  {
125  static symbol sname()
126  {
127  static auto res = symbol{"boolean_tag"};
128  return res;
129  }
130  };
131 
135  struct dijkstra_tag {};
136 
141  {
142  static symbol sname()
143  {
144  static auto res = symbol{"weighted_tag"};
145  return res;
146  }
147  };
148 }
Tag for operations on all automata.
Definition: tags.hh:22
auto join_automata(Auts &&...auts) -> decltype(pass(auts->null_state()...), make_mutable_automaton(join(auts->context()...)))
An automaton whose type is the join between those of auts.
#define Automaton
Definition: automaton.hh:23
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:142
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:51
Tag for operations on standard automata.
Definition: tags.hh:32
static symbol sname()
Definition: tags.hh:125
Tag to request the most appropriate version of an algorithm.
Definition: tags.hh:16
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
return res
Definition: multiply.hh:398
bool is_standard(const Aut &a)
Whether a is standard.
Definition: standard.hh:28
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
Dijkstra implementation.
Definition: tags.hh:135
Definition: a-star.hh:8
Request the Boolean specialization for determinization (B and F2).
Definition: tags.hh:123
static symbol sname()
Definition: tags.hh:34
auto make_join_automaton(deterministic_tag, Auts &&...auts) -> decltype(join_automata(std::forward< Auts >(auts)...))
Make an empty automaton which is a supertype of others.
Definition: tags.hh:92
Request for the weighted version of an algorithm.
Definition: tags.hh:140
static symbol sname()
Definition: tags.hh:24
auto nullable_join_automata(Auts &&...auts) -> decltype(pass(auts->null_state()...), make_mutable_automaton(nullable_join_context(auts...)))
An automaton whose type is the nullable join between those of auts.
bool all(Bool &&...values)
Whether all the values evaluate as true.
Definition: tuple.hh:446
auto dispatch_tags(std::string algo, Operation op, Aut &&...auts)
Dispatch an operation between automata depending on their nature.
Definition: tags.hh:46
Tag for operations on deterministic automata.
Definition: tags.hh:19