Vcsn  2.2
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()}, *aut->weightset()};
28  }
29  };
30  }
31 
32  template <Automaton Aut>
34 
36  template <Automaton Aut>
38  partial_identity(const Aut& aut)
39  {
40  using part_id_ctx = detail::partial_identity_context<Aut>;
42  using state_t = state_t_of<automaton_t>;
43  using label_t = label_t_of<automaton_t>;
44  automaton_t res = make_shared_ptr<automaton_t>(part_id_ctx::value(aut));
45 
46 
47  // map from aut state -> res state
48  auto map
49  = std::vector<state_t>(detail::back(aut->all_states()) + 1);
50 
51  map[aut->pre()] = res->pre();
52  map[aut->post()] = res->post();
53 
54  // Copy every state
55  for (auto st : aut->states())
56  map[st] = res->new_state();
57 
58  // Transform every transition into identity
59  for (auto tr : all_transitions(aut))
60  res->new_transition(map[aut->src_of(tr)],
61  map[aut->dst_of(tr)],
62  label_t{aut->label_of(tr), aut->label_of(tr)},
63  aut->weight_of(tr));
64 
65  return res;
66  }
67 
68  /*------------------------.
69  | dyn::partial_identity. |
70  `------------------------*/
71 
72  namespace dyn
73  {
74  namespace detail
75  {
77  template <Automaton Aut>
78  automaton
80  {
81  const auto& a = aut->as<Aut>();
83  }
84  }
85  }
86 }
Container::value_type back(const Container &container)
The last member of this Container.
Definition: algorithm.hh:27
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
static context_t value(const Aut &aut)
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:59
Definition: a-star.hh:8
automaton partial_identity(const automaton &aut)
Bridge.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
fresh_automaton_t_of< Aut, partial_identity_context_t_of< Aut > > partial_identity(const Aut &aut)
Create a partial identity transducer from aut.
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
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:54
auto all_transitions(const Aut &aut)
All the transition indexes between all states (including pre and post).
Definition: automaton.hh:184
typename detail::partial_identity_context< Aut >::context_t partial_identity_context_t_of