Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lift.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_LIFT_HH
2 # define VCSN_ALGOS_LIFT_HH
3 
4 # include <map>
5 
6 # include <vcsn/ctx/context.hh>
7 # include <vcsn/labelset/oneset.hh>
8 # include <vcsn/dyn/automaton.hh>
11 # include <vcsn/core/rat/ratexp.hh>
12 
13 namespace vcsn
14 {
15 
16  namespace detail
17  {
18  /*--------------.
19  | lift(types). |
20  `--------------*/
21 
22  template <typename ValueSet>
23  struct context_of
24  {
25  using type = typename ValueSet::context_t;
26  };
27 
28  template <typename Context>
29  struct context_of<vcsn::mutable_automaton<Context>>
30  {
31  using type = Context;
32  };
33 
34  template <typename ValueSet>
36 
37  template <typename Context>
38  using lifted_context_t =
40 
41  template <typename Aut>
42  using lifted_automaton_t =
44 
45  template <typename RatExpSet>
46  using lifted_ratexpset_t =
48 
49  // lift(ctx) -> ctx
50  template <typename LabelSet, typename WeightSet>
53  {
54  auto rs_in
57  return {oneset{}, rs_in};
58  }
59 
60  // lift(ratexpset) -> ratexpset
61  template <typename Context>
62  lifted_ratexpset_t<ratexpset<Context>>
64  {
65  return {lift_context(rs.context()), rs.identities()};
66  }
67 
68  }
69 
70  /*------------------.
71  | lift(automaton). |
72  `------------------*/
73 
74  template <typename Aut>
75  inline
76  detail::lifted_automaton_t<Aut>
77  lift(const Aut& a)
78  {
79  using auto_in_t = typename Aut::element_type;
80  using ctx_in_t = context_t_of<auto_in_t>;
81  using state_in_t = state_t_of<auto_in_t>;
82 
83  // Produce RatExps of the same context as the original automaton.
84  using rs_in_t = ratexpset<ctx_in_t>;
85  rs_in_t rs_in{a->context(), rs_in_t::identities_t::trivial};
86 
87  auto ctx_out = detail::lift_context(a->context());
88  using auto_out_t = detail::lifted_automaton_t<auto_in_t>;
89  using state_out_t = state_t_of<auto_out_t>;
90  auto_out_t res = make_shared_ptr<auto_out_t>(ctx_out);
91  std::map<state_in_t, state_out_t> map;
92  map[a->pre()] = res->pre();
93  map[a->post()] = res->post();
94  for (auto s: a->states())
95  map[s] = res->new_state();
96 
97  for (auto t: a->all_transitions())
98  if (a->src_of(t) == a->pre())
99  res->add_initial(map[a->dst_of(t)],
100  rs_in.lmul(a->weight_of(t), rs_in.one()));
101  else if (a->dst_of(t) == a->post())
102  res->add_final(map[a->src_of(t)],
103  rs_in.lmul(a->weight_of(t), rs_in.one()));
104  else
105  res->add_transition
106  (map[a->src_of(t)], map[a->dst_of(t)],
107  {},
108  rs_in.lmul(a->weight_of(t), rs_in.atom(a->label_of(t))));
109  return res;
110  }
111 
112 
113  namespace dyn
114  {
115  namespace detail
116  {
117  template <typename Aut>
118  automaton
120  {
121  const auto& a = aut->as<Aut>();
122  return make_automaton(::vcsn::lift(a));
123  }
124 
126  (const automaton& aut) -> automaton);
127  }
128  }
129 
130 
131  /*---------------.
132  | lift(ratexp). |
133  `---------------*/
134 
135  namespace detail
136  {
137  template <typename Exp>
138  using lifted_ratexp_t =
140  }
141 
142  template <typename RatExpSet>
143  inline
145  lift(const RatExpSet& rs, const typename RatExpSet::value_t& e)
146  {
147  auto lrs = detail::lift_ratexpset(rs);
148  return lrs.lmul(e, lrs.one());
149  }
150 
151 
152  namespace dyn
153  {
154  namespace detail
155  {
156  /*--------------------.
157  | dyn::lift(ratexp). |
158  `--------------------*/
159 
161  template <typename RatExpSet>
162  ratexp
163  lift_ratexp(const ratexp& exp)
164  {
165  const auto& e = exp->as<RatExpSet>();
166  return make_ratexp(::vcsn::detail::lift_ratexpset(e.ratexpset()),
167  ::vcsn::lift(e.ratexpset(), e.ratexp()));
168  }
169 
171  (const ratexp& aut) -> ratexp);
172  }
173  }
174 
175 } // vcsn::
176 
177 #endif // !VCSN_ALGOS_LIFT_HH
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
Definition: ratexp.hh:85
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
Trivial identities only.
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
Definition: fwd.hh:25
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:18
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
ratexp lift_ratexp(const ratexp &exp)
Bridge.
Definition: lift.hh:163
automaton lift_automaton(const automaton &aut)
Definition: lift.hh:119
mutable_automaton< lifted_context_t< context_t_of< Aut >>> lifted_automaton_t
Definition: lift.hh:43
detail::lifted_automaton_t< Aut > lift(const Aut &a)
Definition: lift.hh:77
auto map(const std::tuple< Ts...> &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
Definition: tuple.hh:101
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
typename ValueSet::context_t type
Definition: lift.hh:25
lifted_ratexpset_t< ratexpset< Context > > lift_ratexpset(const ratexpset< Context > &rs)
Definition: lift.hh:63
typename lifted_context_t< context_t_of< Exp >>::ratexp_t lifted_ratexp_t
Definition: lift.hh:139
typename context_of< ValueSet >::type context_of_t
Definition: lift.hh:35
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
lifted_context_t< context< LabelSet, WeightSet > > lift_context(const context< LabelSet, WeightSet > &ctx)
Definition: lift.hh:52