Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
copy.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_COPY_HH
2 # define VCSN_ALGOS_COPY_HH
3 
4 # include <unordered_map>
5 
6 # include <vcsn/algos/fwd.hh> // blind_automaton.
7 # include <vcsn/core/fwd.hh>
8 # include <vcsn/core/rat/copy.hh>
10 # include <vcsn/dyn/automaton.hh>
11 # include <vcsn/dyn/context.hh>
12 # include <vcsn/dyn/ratexp.hh>
13 # include <vcsn/dyn/ratexpset.hh>
14 # include <vcsn/misc/attributes.hh>
15 # include <vcsn/misc/set.hh>
17 
18 namespace vcsn
19 {
20 
21  /*------------------.
22  | copy(automaton). |
23  `------------------*/
24 
25  namespace detail
26  {
29  template <typename AutIn, typename AutOut>
30  struct copier
31  {
34 
35  copier(const AutIn& in, AutOut& out)
36  : in_(in)
37  , out_(out)
38  {}
39 
40  template <typename Pred>
41  void operator()(Pred keep_state)
42  {
43  // Copy the states. We cannot iterate on the transitions
44  // only, as we would lose the states without transitions.
45  out_state[in_->pre()] = out_->pre();
46  out_state[in_->post()] = out_->post();
47  for (auto s: in_->states())
48  if (keep_state(s))
49  out_state[s] = out_->new_state();
50 
51  for (auto t : in_->all_transitions())
52  {
53  auto src = out_state.find(in_->src_of(t));
54  auto dst = out_state.find(in_->dst_of(t));
55  if (src != out_state.end() && dst != out_state.end())
56  out_->new_transition_copy(src->second, dst->second,
57  in_, t);
58  }
59  }
60 
62  using origins_t = std::map<out_state_t, in_state_t>;
63  origins_t
64  origins() const
65  {
66  origins_t res;
67  for (const auto& p: out_state)
68  res[p.second] = p.first;
69  return res;
70  }
71 
73  static
74  std::ostream&
75  print(const origins_t& orig, std::ostream& o)
76  {
77  o << "/* Origins.\n"
78  << " node [shape = box, style = rounded]\n";
79  for (auto p : orig)
80  if (2 <= p.first)
81  o << " " << p.first - 2
82  << " [label = \"" << p.second - 2 << "\"]\n";
83  o << "*/\n";
84  return o;
85  }
86 
88  const AutIn& in_;
90  AutOut& out_;
92  std::unordered_map<in_state_t, out_state_t> out_state;
93  };
94  }
95 
98  template <typename AutIn, typename AutOut, typename Pred>
99  inline
100  void
101  copy_into(const AutIn& in, AutOut& out, Pred keep_state)
102  {
104  return copy(keep_state);
105  }
106 
107  template <typename AutIn, typename AutOut>
108  inline
109  void
110  copy_into(const AutIn& in, AutOut& out)
111  {
112  return copy_into(in, out, [](state_t_of<AutIn>) { return true; });
113  }
114 
115  namespace detail
116  {
117  template <typename Aut>
119 
120  template <typename Aut>
121  auto
122  real_context(const Aut& aut)
123  -> decltype(real_context_impl<Aut>::context(aut));
124 
125  template <typename Aut>
126  struct real_context_impl
127  {
128  static auto context(const Aut& aut)
129  -> decltype(aut->context())
130  {
131  return aut->context();
132  }
133  };
134 
135  template <std::size_t Tape, typename Aut>
137  {
138  static auto context(const blind_automaton<Tape, Aut>& aut)
139  -> decltype(aut->full_context())
140  {
141  return aut->full_context();
142  }
143  };
144 
145  template <typename Aut>
147  {
148  static auto context(const permutation_automaton<Aut>& aut)
149  -> decltype(real_context(aut->strip()))
150  {
151  return real_context(aut->strip());
152  }
153  };
154 
155  template <typename Aut>
156  auto
157  real_context(const Aut& aut)
158  -> decltype(real_context_impl<Aut>::context(aut))
159  {
161  }
162  }
163 
166  template <typename AutIn,
167  typename AutOut = typename AutIn::element_type::automaton_nocv_t,
168  typename Pred>
169  inline
170  AutOut
171  copy(const AutIn& input, Pred keep_state)
172  {
173  // FIXME: here, we need a means to convert the given input context
174  // (e.g. letter -> B) into the destination one (e.g., letter ->
175  // Q). The automaton constructor wants the exact context type.
176  auto res = make_shared_ptr<AutOut>(detail::real_context(input));
177  ::vcsn::copy_into(input, res, keep_state);
178  return res;
179  }
180 
182  template <typename AutIn,
183  typename AutOut = typename AutIn::element_type::automaton_nocv_t>
184  inline
185  AutOut
186  copy(const AutIn& input)
187  {
188  return ::vcsn::copy<AutIn, AutOut>(input,
189  [](state_t_of<AutIn>) { return true; });
190  }
191 
194  template <typename AutIn,
195  typename AutOut = typename AutIn::element_type::automaton_nocv_t>
196  inline
197  AutOut
198  copy(const AutIn& input, const std::set<state_t_of<AutIn>>& keep)
199  {
200  return ::vcsn::copy<AutIn, AutOut>
201  (input,
202  [&keep](state_t_of<AutIn> s) { return has(keep, s); });
203  }
204 
207  template <typename AutIn,
208  typename AutOut = typename AutIn::element_type::automaton_nocv_t>
209  inline
210  AutOut
211  copy(const AutIn& input, const std::unordered_set<state_t_of<AutIn>>& keep)
212  {
213  return ::vcsn::copy<AutIn, AutOut>
214  (input,
215  [&keep](state_t_of<AutIn> s) { return has(keep, s); });
216  }
217 
218  namespace dyn
219  {
220  namespace detail
221  {
223  template <typename Aut, typename Ctx>
224  inline
225  automaton
226  copy_convert(const automaton& aut, const context& ctx)
227  {
228  const auto& a = aut->as<Aut>();
229  const auto& c = ctx->as<Ctx>();
230  auto res = make_mutable_automaton(c);
231  ::vcsn::copy_into(a, res);
232  return make_automaton(res);
233  }
234 
236  (const automaton&, const context&) -> automaton);
237 
239  template <typename Aut>
240  inline
241  automaton
242  copy(const automaton& aut)
243  {
244  const auto& a = aut->as<Aut>();
245  return make_automaton(::vcsn::copy(a));
246  }
247 
249  (const automaton&) -> automaton);
250  }
251  }
252 
253  /*---------------.
254  | copy(ratexp). |
255  `---------------*/
256 
257  namespace dyn
258  {
259  namespace detail
260  {
262  template <typename InRatExpSet, typename OutRatExpSet = InRatExpSet>
263  inline
264  ratexp
265  copy_ratexp(const ratexp& exp, const ratexpset& out_rs)
266  {
267  const auto& r = exp->as<InRatExpSet>();
268  const auto& ors = out_rs->as<OutRatExpSet>().ratexpset();
269 
270  return make_ratexp(ors,
271  ::vcsn::rat::copy(r.ratexpset(), ors,
272  r.ratexp()));
273  }
274 
276  (const ratexp& exp, const ratexpset& out_rs) -> ratexp);
277  }
278  }
279 
280 
281 
282 } // namespace vcsn
283 
284 #endif // !VCSN_ALGOS_COPY_HH
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
Definition: ratexp.hh:85
automaton copy_convert(const automaton &aut, const context &ctx)
Bridge.
Definition: copy.hh:226
state_t_of< AutIn > in_state_t
Definition: copy.hh:32
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
static auto context(const blind_automaton< Tape, Aut > &aut) -> decltype(aut->full_context())
Definition: copy.hh:138
OutRatExpSet::value_t copy(const InRatExpSet &in_rs, const OutRatExpSet &out_rs, const typename InRatExpSet::value_t &v)
Definition: copy.hh:131
AutOut & out_
Output automaton.
Definition: copy.hh:90
static std::ostream & print(const origins_t &orig, std::ostream &o)
Print the origins.
Definition: copy.hh:75
const AutIn & in_
Input automaton.
Definition: copy.hh:88
ratexp copy_ratexp(const ratexp &exp, const ratexpset &out_rs)
Bridge.
Definition: copy.hh:265
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
copier(const AutIn &in, AutOut &out)
Definition: copy.hh:35
std::shared_ptr< detail::blind_automaton_impl< Tape, Aut >> blind_automaton
A blind automaton as a shared pointer.
Definition: fwd.hh:24
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
std::shared_ptr< detail::permutation_automaton_impl< Aut >> permutation_automaton
A permutation automaton as a shared pointer.
Definition: fwd.hh:38
static auto context(const Aut &aut) -> decltype(aut->context())
Definition: copy.hh:128
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:171
std::shared_ptr< const detail::ratexpset_base > ratexpset
Definition: fwd.hh:73
void operator()(Pred keep_state)
Definition: copy.hh:41
automaton copy(const automaton &aut)
Bridge.
Definition: copy.hh:242
void copy_into(const AutIn &in, AutOut &out)
Definition: copy.hh:110
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
std::shared_ptr< const detail::context_base > context
Definition: context.hh:71
auto real_context(const Aut &aut) -> decltype(real_context_impl< Aut >::context(aut))
Definition: copy.hh:157
std::map< out_state_t, in_state_t > origins_t
A map from result state to original state.
Definition: copy.hh:62
static auto context(const permutation_automaton< Aut > &aut) -> decltype(real_context(aut->strip()))
Definition: copy.hh:148
state_t_of< AutOut > out_state_t
Definition: copy.hh:33
std::unordered_map< in_state_t, out_state_t > out_state
input state -> output state.
Definition: copy.hh:92
void copy_into(const AutIn &in, AutOut &out, Pred keep_state)
Copy an automaton.
Definition: copy.hh:101
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
Copy an automaton.
Definition: copy.hh:30
bool has(const std::map< Key, Value, Compare, Alloc > &s, const Key &e)
Definition: map.hh:35
origins_t origins() const
Definition: copy.hh:64