Vcsn  2.3
Be Rational
partial-identity.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <vcsn/algos/copy.hh> // make_fresh_automaton
7 #include <vcsn/dyn/automaton.hh>
8 #include <vcsn/dyn/context.hh>
10 
11 namespace vcsn
12 {
13  namespace detail
14  {
15  // Helper struct to compute the context of the partial identity
16  template <Automaton Aut>
18  {
21 
24 
25  static context_t value(const Aut& aut)
26  {
27  return {labelset_t{*aut->labelset(), *aut->labelset()},
28  *aut->weightset()};
29  }
30  };
31  }
32 
33  template <Automaton Aut>
36 
38  template <Automaton Aut>
40  partial_identity(const Aut& aut)
41  {
42  using part_id_ctx = detail::partial_identity_context<Aut>;
43  using automaton_t
45  using state_t = state_t_of<automaton_t>;
46  using label_t = label_t_of<automaton_t>;
47  automaton_t res = make_shared_ptr<automaton_t>(part_id_ctx::value(aut));
48 
49 
50  // map from aut state -> res state
51  auto map = std::vector<state_t>(states_size(aut));
52 
53  map[aut->pre()] = res->pre();
54  map[aut->post()] = res->post();
55 
56  // Copy every state
57  for (auto st : aut->states())
58  map[st] = res->new_state();
59 
60  // Transform every transition into identity
61  for (auto tr : all_transitions(aut))
62  res->new_transition(map[aut->src_of(tr)],
63  map[aut->dst_of(tr)],
64  label_t{aut->label_of(tr), aut->label_of(tr)},
65  aut->weight_of(tr));
66 
67  return res;
68  }
69 
70  /*------------------------.
71  | dyn::partial_identity. |
72  `------------------------*/
73 
74  namespace dyn
75  {
76  namespace detail
77  {
79  template <Automaton Aut>
80  automaton
82  {
83  const auto& a = aut->as<Aut>();
85  }
86  }
87  }
88 }
typename detail::partial_identity_context< Aut >::context_t partial_identity_context_t_of
automaton partial_identity(const automaton &aut)
Bridge.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
size_t states_size(const Aut &aut)
The largest state number, plus one.
Definition: automaton.hh:19
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
Definition: automaton.hh:192
return res
Definition: multiply.hh:398
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
Definition: a-star.hh:8
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
A dyn automaton.
Definition: automaton.hh:17
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
static context_t value(const Aut &aut)
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:37
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
fresh_automaton_t_of< Aut, partial_identity_context_t_of< Aut > > partial_identity(const Aut &aut)
Create a partial identity transducer from aut.