Vcsn  2.2
Be Rational
focus.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/fwd.hh>
4 #include <vcsn/algos/project.hh> // project
7 #include <vcsn/ctx/context.hh>
8 #include <vcsn/ctx/traits.hh>
9 #include <vcsn/dyn/automaton.hh>
10 #include <vcsn/dyn/context.hh>
12 #include <vcsn/misc/name.hh> // integral_constant
13 #include <vcsn/misc/tuple.hh> // make_index_range
14 
15 namespace vcsn
16 {
17 
18  /*-------------------.
19  | focus_automaton. |
20  `-------------------*/
21 
22  namespace detail
23  {
24  template <typename A, typename I>
26 
27  template <Automaton Aut, std::size_t... I>
28  struct hidden_label_type<Aut, index_sequence<I...>>
29  {
32  };
33 
34 
36  template <std::size_t Tape, Automaton Aut>
38  : public automaton_decorator<Aut,
39  project_context<Tape, context_t_of<Aut>>>
40  {
41  public:
43  using automaton_t = Aut;
44 
45  static_assert(context_t_of<Aut>::is_lat,
46  "focus: requires labels_are_tuples");
47  static_assert(Tape < labelset_t_of<Aut>::size(),
48  "focus: invalid tape number");
49 
54 
58  using full_labelset_t = typename full_context_t::labelset_t;
60  using full_label_t = typename full_labelset_t::value_t;
61 
64 
68  using label_t = typename labelset_t::value_t;
69 
73  using weight_t = typename weightset_t::value_t;
74 
92  template <typename = void>
93  using fresh_automaton_t
94  = focus_automaton<Tape,
96 
98  using hidden_indices_t
101  make_index_range_t<Tape + 1,
102  std::tuple_size<full_label_t>::value - Tape - 1>>;
103 
104  // All tapes except the exposed one.
105  using res_labelset_t
107  using res_label_t = typename res_labelset_t::value_t;
108 
110 
111  public:
112 
115  {}
116 
118  : super_t(aut)
119  {}
120 
121  static symbol sname()
122  {
123  static auto res = symbol{"focus_automaton<"
124  + std::to_string(Tape) + ", "
126  return res;
127  }
128 
129  std::ostream& print_set(std::ostream& o, format fmt = {}) const
130  {
131  o << "focus_automaton<" << Tape << ", ";
132  aut_->print_set(o, fmt);
133  return o << '>';
134  }
135 
136  full_context_t full_context() const
137  {
138  return aut_->context();
139  }
140 
141  context_t context() const
142  {
143  return context_;
144  }
145 
146  res_label_t
147  hidden_label_of(transition_t t) const
148  {
149  return hidden_label_of_(t, hidden_indices);
150  }
151 
152  res_label_t
153  hidden_one() const
154  {
155  return hidden_one_<full_labelset_t>(hidden_indices);
156  }
157 
158  res_labelset_t
159  res_labelset() const
160  {
161  return res_labelset_(hidden_indices);
162  }
163 
165  labelset_ptr
166  labelset() const
167  {
168  return ls_;
169  }
170 
171  private:
172  using super_t::aut_;
173 
174  hidden_indices_t hidden_indices{};
175 
176  static label_t hide_(full_label_t l)
177  {
178  return std::get<Tape>(l);
179  }
180 
181  template <std::size_t... I>
182  res_label_t hidden_label_of_(transition_t t, index_sequence<I...>) const
183  {
184  full_label_t l = aut_->label_of(t);
185  return std::make_tuple(std::get<I>(l)...);
186  }
187 
188  template <typename L, std::size_t... I>
189  std::enable_if_t<L::has_one(), res_label_t>
190  hidden_one_(index_sequence<I...>) const
191  {
192  full_label_t l = aut_->labelset()->one();
193  return std::make_tuple(std::get<I>(l)...);
194  }
195 
196  template <typename L, std::size_t... I>
197  std::enable_if_t<!L::has_one(), res_label_t>
198  hidden_one_(index_sequence<I...>) const
199  {
200  raise("Should not get here");
201  }
202 
203  template <std::size_t... I>
204  res_labelset_t res_labelset_(index_sequence<I...>) const
205  {
206  return res_labelset_t{std::get<I>(aut_->labelset()->sets())...};
207  }
208 
209  public:
210 
211  /*----------------------------.
212  | const methods that change. |
213  `----------------------------*/
214 
216  auto label_of(transition_t t) const
217  -> label_t
218  {
219  return hide_(aut_->label_of(t));
220  }
221 
222  // FIXME: http://llvm.org/bugs/show_bug.cgi?id=20175.
223  // using super_t::out;
224  auto
225  out(state_t s) const
226  -> decltype(out(aut_, s))
227  {
228  return out(aut_, s);
229  }
230 
231  // FIXME: Having support for predicates in
232  // mutable_automaton::get_transition would help.
233  transition_t
234  get_transition(state_t src, state_t dst, label_t l) const
235  {
236  for (auto t: out(src, l))
237  if (aut_->dst_of(t) == dst)
238  return t;
239  return aut_->null_transition();
240  }
241 
243  bool
244  has_transition(state_t src, state_t dst, label_t l) const
245  {
246  return get_transition(src, dst, l) != aut_->null_transition();
247  }
248 
249  using super_t::del_transition;
251  void
252  del_transition(state_t src, state_t dst, label_t l)
253  {
254  auto t = get_transition(src, dst, l);
255  if (t != aut_->null_transition())
256  aut_->del_transition(t);
257  }
258 
260  template <typename A>
261  transition_t
262  new_transition_copy(state_t src, state_t dst,
263  const A& aut,
264  typename A::element_type::transition_t t,
265  bool transpose = false)
266  {
267  return aut_->new_transition_copy(src, dst,
268  aut->strip(), t, transpose);
269  }
270 
272  template <typename A>
273  transition_t
274  add_transition_copy(state_t src, state_t dst,
275  const A& aut,
276  typename A::element_type::transition_t t,
277  bool transpose = false)
278  {
279  return aut_->add_transition_copy(src, dst,
280  aut->strip(), t, transpose);
281  }
282 
283 #define DEFINE(Name, Sig) \
284  auto Name Sig \
285  { \
286  raise("focus: cannot provide " #Name); \
287  }
288 
289  DEFINE(add_transition,
290  (state_t, state_t, label_t, weight_t) -> transition_t);
291  DEFINE(add_transition,
292  (state_t, state_t, label_t) -> transition_t);
293  DEFINE(new_transition,
294  (state_t, state_t, label_t, weight_t) -> transition_t);
295  DEFINE(new_transition,
296  (state_t, state_t, label_t) -> transition_t);
297  DEFINE(set_transition,
298  (state_t, state_t, label_t, weight_t) -> transition_t);
299 #undef DEFINE
300 
301  private:
306  context_t context_ = project<Tape>(full_context());
311  labelset_ptr ls_
312  = std::make_shared<labelset_t>(aut_->labelset()->template set<Tape>());
313  };
314  }
315 
316  template <unsigned Tape, Automaton Aut>
317  inline
318  focus_automaton<Tape, Aut>
319  focus(Aut aut)
320  {
321  return std::make_shared<detail::focus_automaton_impl<Tape, Aut>>(aut);
322  }
323 
324 
325  namespace dyn
326  {
327  namespace detail
328  {
330  template <Automaton Aut, typename Tape>
331  automaton
332  focus(const automaton& aut, integral_constant)
333  {
334  auto& a = aut->as<Aut>();
335  return make_automaton(vcsn::focus<Tape::value>(a));
336  }
337  }
338  }
339 }
std::shared_ptr< detail::focus_automaton_impl< Tape, Aut >> focus_automaton
A focus automaton as a shared pointer.
Definition: fwd.hh:42
typename labelset_t::value_t label_t
Definition: focus.hh:68
focus_automaton_impl(const automaton_t &aut)
Definition: focus.hh:117
Aut automaton_t
The type of the wrapped automaton.
Definition: focus.hh:43
Definition: a-star.hh:8
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
SharedPtr make_shared_ptr(Args &&...args)
Same as std::make_shared, but parameterized by the shared_ptr type, not the (pointed to) element_type...
Definition: memory.hh:14
An input/output format for valuesets.
Definition: format.hh:11
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
state_t_of< automaton_t > state_t
This automaton's state and transition types are those of the wrapped automaton.
Definition: focus.hh:52
LabelSet labelset_t
Definition: context.hh:21
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
focus_automaton< Tape, fresh_automaton_t_of< automaton_t, full_context_t >> fresh_automaton_t
The type of automata to produce this kind of automata.
Definition: focus.hh:95
typename res_labelset_t::value_t res_label_t
Definition: focus.hh:107
transition_t_of< automaton_t > transition_t
Definition: focus.hh:53
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
Aggregate an automaton, and forward calls to it.
Read-write on an automaton, that hides all tapes but one.
Definition: focus.hh:37
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Given an automaton type, the type of its copies.
Definition: traits.hh:74
typename context_t::weightset_t weightset_t
Exposed weightset.
Definition: focus.hh:71
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:57
focus_automaton_impl(const full_context_t &ctx)
Definition: focus.hh:113
WeightSet weightset_t
Definition: context.hh:22
typename hidden_label_type< Aut, hidden_indices_t >::type res_labelset_t
Definition: focus.hh:106
symbol sname()
Definition: name.hh:67
typename make_index_range< S, L >::type make_index_range_t
Definition: tuple.hh:74
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
typename full_labelset_t::value_t full_label_t
Underlying automaton label.
Definition: focus.hh:60
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:82
typename full_context_t::labelset_t full_labelset_t
Underlying automaton labelset.
Definition: focus.hh:58
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:17
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: focus.hh:129
std::shared_ptr< const weightset_t > weightset_ptr
Definition: context.hh:24
concat_sequence< make_index_range_t< 0, Tape >, make_index_range_t< Tape+1, std::tuple_size< full_label_t >::value-Tape-1 >> hidden_indices_t
Indices of the remaining tapes.
Definition: focus.hh:102
#define Automaton
Definition: automaton.hh:24
typename concat_index_sequence< S1, S2 >::type concat_sequence
Definition: tuple.hh:84
typename weightset_t::value_t weight_t
Definition: focus.hh:73
typename context_t::labelset_t labelset_t
Exposed labelset.
Definition: focus.hh:66
typename context_t::weightset_ptr weightset_ptr
Definition: focus.hh:72
std::shared_ptr< const labelset_t > labelset_ptr
Definition: context.hh:23
context_t_of< automaton_t > full_context_t
Underlying automaton context.
Definition: focus.hh:56
typename context_t::labelset_ptr labelset_ptr
Definition: focus.hh:67