Vcsn  2.3
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
5 #include <vcsn/core/automaton.hh> // out
8 #include <vcsn/ctx/context.hh>
9 #include <vcsn/ctx/traits.hh>
10 #include <vcsn/dyn/automaton.hh>
11 #include <vcsn/dyn/context.hh>
13 #include <vcsn/dyn/name.hh> // integral_constant
14 #include <vcsn/misc/tuple.hh> // make_index_range
15 
16 namespace vcsn
17 {
18 
19  /*-------------------.
20  | focus_automaton. |
21  `-------------------*/
22 
23  namespace detail
24  {
25  template <Automaton A, typename I>
27 
28  template <Automaton Aut, std::size_t... I>
29  struct hidden_label_type<Aut, index_sequence<I...>>
30  {
33  };
34 
35 
37  template <std::size_t Tape, Automaton Aut>
39  : public automaton_decorator<Aut,
40  project_context<Tape, context_t_of<Aut>>>
41  {
42  public:
44  using automaton_t = Aut;
45 
46  static_assert(context_t_of<Aut>::is_lat,
47  "focus: requires labels_are_tuples");
48  static_assert(Tape < labelset_t_of<Aut>::size(),
49  "focus: invalid tape number");
50 
55 
59  using full_labelset_t = typename full_context_t::labelset_t;
61  using full_label_t = typename full_labelset_t::value_t;
62 
65 
69  using label_t = typename labelset_t::value_t;
70 
74  using weight_t = typename weightset_t::value_t;
75 
93  template <typename = void>
94  using fresh_automaton_t
95  = focus_automaton<Tape,
97 
99  using hidden_indices_t
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 
245  bool
246  has_transition(transition_t t) const
247  {
248  return aut_->has_transition(t);
249  }
250 
252  bool
253  has_transition(state_t src, state_t dst, label_t l) const
254  {
255  return get_transition(src, dst, l) != aut_->null_transition();
256  }
257 
258  using super_t::del_transition;
260  void
261  del_transition(state_t src, state_t dst, label_t l)
262  {
263  auto t = get_transition(src, dst, l);
264  if (t != aut_->null_transition())
265  aut_->del_transition(t);
266  }
267 
269  template <Automaton A>
270  transition_t
271  new_transition_copy(state_t src, state_t dst,
272  const A& aut,
273  transition_t_of<A> t,
274  bool transpose = false)
275  {
276  return aut_->new_transition_copy(src, dst,
277  aut->strip(), t, transpose);
278  }
279 
281  template <Automaton A>
282  transition_t
283  add_transition_copy(state_t src, state_t dst,
284  const A& aut,
285  transition_t_of<A> t,
286  bool transpose = false)
287  {
288  return aut_->add_transition_copy(src, dst,
289  aut->strip(), t, transpose);
290  }
291 
292 #define DEFINE(Name, Sig) \
293  auto Name Sig \
294  { \
295  raise("focus: cannot provide " #Name); \
296  }
297 
298  DEFINE(add_transition,
299  (state_t, state_t, label_t, weight_t) -> transition_t);
300  DEFINE(add_transition,
301  (state_t, state_t, label_t) -> transition_t);
302  DEFINE(new_transition,
303  (state_t, state_t, label_t, weight_t) -> transition_t);
304  DEFINE(new_transition,
305  (state_t, state_t, label_t) -> transition_t);
306  DEFINE(set_transition,
307  (state_t, state_t, label_t, weight_t) -> transition_t);
308 #undef DEFINE
309 
310  private:
315  context_t context_ = project<Tape>(full_context());
320  labelset_ptr ls_
321  = std::make_shared<labelset_t>(aut_->labelset()->template set<Tape>());
322  };
323  }
324 
325  template <unsigned Tape, Automaton Aut>
326  focus_automaton<Tape, Aut>
327  focus(Aut aut)
328  {
329  return std::make_shared<detail::focus_automaton_impl<Tape, Aut>>(aut);
330  }
331 
332 
333  namespace dyn
334  {
335  namespace detail
336  {
338  template <Automaton Aut, typename Tape>
339  automaton
340  focus(const automaton& aut, integral_constant)
341  {
342  auto& a = aut->as<Aut>();
343  return vcsn::focus<Tape::value>(a);
344  }
345  }
346  }
347 }
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:13
concat_sequence< make_index_range_t< 0, Tape >, make_index_range_t< Tape+1, std::tuple_size< full_label_t >::value >> hidden_indices_t
Indices of the remaining tapes.
Definition: focus.hh:102
typename weightset_t::value_t weight_t
Definition: focus.hh:74
LabelSet labelset_t
Definition: context.hh:21
#define Automaton
Definition: automaton.hh:23
typename context_t::labelset_t labelset_t
Exposed labelset.
Definition: focus.hh:67
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:65
symbol sname()
Definition: name.hh:65
focus_automaton_impl(const automaton_t &aut)
Definition: focus.hh:117
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
std::shared_ptr< const labelset_t > labelset_ptr
Definition: context.hh:23
Aggregate an automaton, and forward calls to it.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
Aut automaton_t
The type of the wrapped automaton.
Definition: focus.hh:44
return res
Definition: multiply.hh:398
typename hidden_label_type< Aut, hidden_indices_t >::type res_labelset_t
Definition: focus.hh:106
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
typename full_labelset_t::value_t full_label_t
Underlying automaton label.
Definition: focus.hh:61
state_t_of< automaton_t > state_t
This automaton's state and transition types are those of the wrapped automaton.
Definition: focus.hh:53
typename full_context_t::labelset_t full_labelset_t
Underlying automaton labelset.
Definition: focus.hh:59
Definition: a-star.hh:8
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:82
An input/output format for valuesets.
Definition: format.hh:13
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:96
std::shared_ptr< detail::focus_automaton_impl< Tape, Aut >> focus_automaton
A focus automaton as a shared pointer.
Definition: fwd.hh:42
typename res_labelset_t::value_t res_label_t
Definition: focus.hh:107
transition_t_of< automaton_t > transition_t
Definition: focus.hh:54
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:41
typename concat_index_sequence< S1, S2 >::type concat_sequence
Definition: tuple.hh:100
typename context_t::weightset_ptr weightset_ptr
Definition: focus.hh:73
typename context_t::weightset_t weightset_t
Exposed weightset.
Definition: focus.hh:72
WeightSet weightset_t
Definition: context.hh:22
focus_automaton_impl(const full_context_t &ctx)
Definition: focus.hh:113
context_t_of< automaton_t > full_context_t
Underlying automaton context.
Definition: focus.hh:57
typename context_t::labelset_ptr labelset_ptr
Definition: focus.hh:68
typename labelset_t::value_t label_t
Definition: focus.hh:69
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: focus.hh:129
typename make_index_range< S, E >::type make_index_range_t
Definition: tuple.hh:88
std::shared_ptr< const weightset_t > weightset_ptr
Definition: context.hh:24
Read-write on an automaton, that hides all tapes but one.
Definition: focus.hh:38