Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
blind.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_BLIND_HH
2 # define VCSN_ALGOS_BLIND_HH
3 
4 # include <vcsn/algos/fwd.hh>
6 # include <vcsn/ctx/context.hh>
7 # include <vcsn/ctx/traits.hh>
8 # include <vcsn/dyn/automaton.hh>
10 # include <vcsn/misc/name.hh> // integral_parameter
11 # include <vcsn/misc/tuple.hh> // make_index_range
12 
13 namespace vcsn
14 {
15  namespace detail
16  {
17  template <typename A, typename I>
19 
20  template <typename Aut, std::size_t... I>
21  struct hidden_label_type<Aut, index_sequence<I...>>
22  {
23  template <std::size_t J>
24  using elem = typename std::tuple_element<J,
26 
27  using type = tupleset<elem<I>...>;
28  };
29 
30 
31  template <size_t Tape,
32  typename LabelSet, typename WeightSet>
35  {
36  return {ctx.labelset()->template set<Tape>(), *ctx.weightset()};
37  }
38 
39  template <typename Context, size_t Tape>
40  using blind_context_t = decltype(blind_context<Tape>(std::declval<Context>()));
41 
42  /*------------------.
43  | blind_automaton. |
44  `------------------*/
45 
47  template <std::size_t Tape, typename Aut>
49  : public automaton_decorator<Aut,
50  blind_context_t<context_t_of<Aut>, Tape>>
51  {
52  public:
54  using automaton_t = Aut;
55 
56  static_assert(context_t_of<Aut>::is_lat,
57  "blind: requires labels_are_tuples");
58  static_assert(Tape < labelset_t_of<Aut>::size(),
59  "blind: invalid tape number");
60 
66  using automaton_nocv_t
67  = blind_automaton<Tape,
68  typename automaton_t::element_type::automaton_nocv_t>;
69 
74 
78  using full_labelset_t = typename full_context_t::labelset_t;
80  using full_label_t = typename full_labelset_t::value_t;
81 
84 
86  using labelset_t = typename context_t::labelset_t;
87  using labelset_ptr = typename context_t::labelset_ptr;
88  using label_t = typename labelset_t::value_t;
89 
91  using weightset_t = typename context_t::weightset_t;
92  using weightset_ptr = typename context_t::weightset_ptr;
93  using weight_t = typename weightset_t::value_t;
94 
96  using hidden_indices_t
99  typename make_index_range<Tape + 1,
100  std::tuple_size<full_label_t>::value - Tape - 1>::type>;
101 
102  // All tapes except the exposed one.
104  using res_label_t = typename res_labelset_t::value_t;
105 
107 
108  public:
109 
112  {}
113 
115  : super_t(aut)
116  {}
117 
118  static std::string sname()
119  {
120  return ("blind_automaton<" + std::to_string(Tape) + ", "
122  }
123 
124  std::string vname(bool full = true) const
125  {
126  return ("blind_automaton<" + std::to_string(Tape) + ", "
127  + aut_->vname(full) + ">");
128  }
129 
131  {
132  return aut_->context();
133  }
134 
136  {
137  return context_;
138  }
139 
142  {
143  return hidden_label_of_(t, hidden_indices);
144  }
145 
147  hidden_one() const
148  {
149  return hidden_one_<full_labelset_t>(hidden_indices);
150  }
151 
153  res_labelset() const
154  {
156  }
157 
159  std::shared_ptr<labelset_t>
160  labelset() const
161  {
162  return std::make_shared<labelset_t>(aut_->labelset()->template set<Tape>());
163  }
164 
165  private:
166  using super_t::aut_;
167 
169 
171  {
172  return std::get<Tape>(l);
173  }
174 
175  template <std::size_t... I>
177  {
178  full_label_t l = aut_->label_of(t);
179  return std::make_tuple(std::get<I>(l)...);
180  }
181 
182  template <typename L, std::size_t... I>
183  typename std::enable_if<L::has_one(), res_label_t>::type
185  {
186  full_label_t l = aut_->labelset()->one();
187  return std::make_tuple(std::get<I>(l)...);
188  }
189 
190  template <typename L, std::size_t... I>
191  typename std::enable_if<!L::has_one(), res_label_t>::type
193  {
194  raise("Should not get here");
195  }
196 
197  template <std::size_t... I>
199  {
200  return res_labelset_t{std::get<I>(aut_->labelset()->sets())...};
201  }
202 
203  public:
204 
205  /*----------------------------.
206  | const methods that change. |
207  `----------------------------*/
208 
210  auto label_of(transition_t t) const
211  -> label_t
212  {
213  return hide_(aut_->label_of(t));
214  }
215 
216  // FIXME: http://llvm.org/bugs/show_bug.cgi?id=20175.
217  // using super_t::out;
218  auto
219  out(state_t s) const
220  -> decltype(aut_->out(s))
221  {
222  return aut_->out(s);
223  }
224 
229  std::vector<transition_t>
230  out(state_t s, const label_t& l) const
231  {
232  std::vector<transition_t> res;
233  for (auto t: aut_->all_out(s))
234  if (labelset()->equals(label_of(t), l))
235  res.emplace_back(t);
236  return res;
237  }
238 
243  using super_t::in;
244  std::vector<transition_t>
245  in(state_t s, const label_t& l) const
246  {
247  std::vector<transition_t> res;
248  for (auto t: aut_->all_in(s))
249  if (this->labelset()->equals(label_of(t), l))
250  res.emplace_back(t);
251  return res;
252  }
253 
254  // FIXME: Having support for predicates in
255  // mutable_automaton::get_transition would help.
258  {
259  for (auto t: out(src, l))
260  if (aut_->dst_of(t) == dst)
261  return t;
262  return aut_->null_transition();
263  }
264 
266  bool
268  {
269  return get_transition(src, dst, l) != aut_->null_transition();
270  }
271 
274  void
276  {
277  auto t = get_transition(src, dst, l);
278  if (t != aut_->null_transition())
279  aut_->del_transition(t);
280  }
281 
283  template <typename A>
286  const A& aut,
287  typename A::element_type::transition_t t,
288  bool transpose = false)
289  {
290  return aut_->new_transition_copy(src, dst,
291  aut->strip(), t, transpose);
292  }
293 
295  template <typename A>
296  weight_t
298  const A& aut,
299  typename A::element_type::transition_t t,
300  bool transpose = false)
301  {
302  return aut_->add_transition_copy(src, dst,
303  aut->strip(), t, transpose);
304  }
305 
306 #define DEFINE(Name, Sig) \
307  auto Name Sig \
308  { \
309  raise("blind: cannot provide " #Name); \
310  }
311 
322 #undef DEFINE
323 
324  private:
329  context_t context_ = blind_context<Tape>(full_context());
330  };
331  }
332 
333  template <unsigned Tape, typename Aut>
334  inline
336  blind(Aut aut)
337  {
338  return std::make_shared<detail::blind_automaton_impl<Tape, Aut>>(aut);
339  }
340 
341 
342  namespace dyn
343  {
344  namespace detail
345  {
347  template <typename Aut, typename Tape>
348  automaton
350  {
351  auto& a = aut->as<Aut>();
352  return make_automaton(vcsn::blind<Tape::value>(a));
353  }
354 
356  (automaton& aut, integral_constant tape) -> automaton);
357  }
358  }
359 
360 }
361 
362 #endif // !VCSN_ALGOS_BLIND_HH
void del_transition(state_t src, state_t dst, label_t l)
Apparent label of a transition.
Definition: blind.hh:275
typename context_t::weightset_ptr weightset_ptr
Definition: blind.hh:92
context_t_of< automaton_t > full_context_t
Underlying automaton context.
Definition: blind.hh:76
typename labelset_t::value_t label_t
Definition: blind.hh:88
blind_automaton_impl(const automaton_t &aut)
Definition: blind.hh:114
hidden_indices_t hidden_indices
Definition: blind.hh:168
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
blind_automaton< Tape, Aut > blind(Aut aut)
Definition: blind.hh:336
typename context_t::labelset_ptr labelset_ptr
Definition: blind.hh:87
context_t context_
Our apparent context.
Definition: blind.hh:329
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
A ValueSet which is a Cartesian product of ValueSets.
Definition: fwd.hh:20
typename weightset_t::value_t weight_t
Definition: blind.hh:93
transition_t new_transition_copy(state_t src, state_t dst, const A &aut, typename A::element_type::transition_t t, bool transpose=false)
Copy the full wrapped transition.
Definition: blind.hh:285
typename concat_index_sequence< S1, S2 >::type concat_sequence
Definition: tuple.hh:71
std::string to_string(identities i)
Definition: identities.cc:13
typename context_t::weightset_t weightset_t
Exposed weightset.
Definition: blind.hh:91
transition_t_of< automaton_t > transition_t
Definition: blind.hh:73
auto set_transition(state_t, state_t, label_t, weight_t) -> transition_t
Definition: blind.hh:321
decltype(blind_context< Tape >(std::declval< Context >())) blind_context_t
Definition: blind.hh:40
std::enable_if<!L::has_one(), res_label_t >::type hidden_one_(index_sequence< I...>) const
Definition: blind.hh:192
auto out(state_t s) const -> decltype(aut_->out(s))
Definition: blind.hh:219
res_labelset_t res_labelset() const
Definition: blind.hh:153
std::string sname()
Definition: name.hh:31
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
weight_t add_transition_copy(state_t src, state_t dst, const A &aut, typename A::element_type::transition_t t, bool transpose=false)
Copy the full wrapped transition.
Definition: blind.hh:297
auto new_transition(state_t, state_t, label_t, weight_t) -> transition_t
Definition: blind.hh:317
std::string vname(bool full=true) const
Definition: blind.hh:124
std::shared_ptr< detail::blind_automaton_impl< Tape, Aut >> blind_automaton
A blind automaton as a shared pointer.
Definition: fwd.hh:24
auto add_transition(state_t, state_t, label_t, weight_t) -> weight_t
Definition: blind.hh:313
#define DEFINE(Name, Sig)
Definition: blind.hh:306
res_labelset_t res_labelset_(index_sequence< I...>) const
Definition: blind.hh:198
static std::string sname()
Definition: blind.hh:118
typename res_labelset_t::value_t res_label_t
Definition: blind.hh:104
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
full_context_t full_context() const
Definition: blind.hh:130
Aggregate an automaton, and forward calls to it.
#define REGISTER_DECLARE(Name, Signature)
Definition: fwd.hh:104
A simple placeholder for integral constants.
Definition: name.hh:155
blind_automaton< Tape, typename automaton_t::element_type::automaton_nocv_t > automaton_nocv_t
The type of automata to produce this kind of automata.
Definition: blind.hh:68
auto blind_context(const context< LabelSet, WeightSet > &ctx) -> context< typename LabelSet::template valueset_t< Tape >, WeightSet >
Definition: blind.hh:33
std::vector< transition_t > out(state_t s, const label_t &l) const
Indexes of all transitions leaving state s on label l.
Definition: blind.hh:230
std::vector< transition_t > in(state_t s, const label_t &l) const
Definition: blind.hh:245
static label_t hide_(full_label_t l)
Definition: blind.hh:170
concat_sequence< typename make_index_range< 0, Tape >::type, typename make_index_range< Tape+1, std::tuple_size< full_label_t >::value-Tape-1 >::type > hidden_indices_t
Indices of the remaining tapes.
Definition: blind.hh:100
Read-write on an automaton, that hides all tapes but one.
Definition: blind.hh:48
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:36
typename hidden_label_type< Aut, hidden_indices_t >::type res_labelset_t
Definition: blind.hh:103
blind_automaton_impl(const full_context_t &ctx)
Definition: blind.hh:110
typename context_t::labelset_t labelset_t
Exposed labelset.
Definition: blind.hh:86
state_t_of< automaton_t > state_t
This automaton's state and transition types are those of the wrapped automaton.
Definition: blind.hh:72
typename full_context_t::labelset_t full_labelset_t
Underlying automaton labelset.
Definition: blind.hh:78
typename std::tuple_element< J, typename labelset_t_of< Aut >::valuesets_t >::type elem
Definition: blind.hh:25
res_label_t hidden_label_of(transition_t t) const
Definition: blind.hh:141
bool has_transition(state_t src, state_t dst, label_t l) const
Apparent label of a transition.
Definition: blind.hh:267
res_label_t hidden_label_of_(transition_t t, index_sequence< I...>) const
Definition: blind.hh:176
context_t context() const
Definition: blind.hh:135
std::shared_ptr< labelset_t > labelset() const
Apparent labelset.
Definition: blind.hh:160
transition_t get_transition(state_t src, state_t dst, label_t l) const
Definition: blind.hh:257
Aut transpose(const transpose_automaton< Aut > &aut)
Definition: transpose.hh:230
std::enable_if< L::has_one(), res_label_t >::type hidden_one_(index_sequence< I...>) const
Definition: blind.hh:184
res_label_t hidden_one() const
Definition: blind.hh:147
blind_context_t< full_context_t, Tape > context_t
Exposed context.
Definition: blind.hh:83
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
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:15
auto label_of(transition_t t) const -> label_t
Apparent label of a transition.
Definition: blind.hh:210
auto in(Args &&...args) const -> decltype(aut_-> in(std::forward< Args >(args)...))
typename full_labelset_t::value_t full_label_t
Underlying automaton label.
Definition: blind.hh:80
auto del_transition(Args &&...args) -> decltype(aut_-> del_transition(std::forward< Args >(args)...))
Aut automaton_t
The type of the wrapped automaton.
Definition: blind.hh:54