Vcsn  2.4
Be Rational
daut.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
6 #include <vcsn/dyn/automaton.hh>
7 #include <vcsn/dyn/fwd.hh>
8 
9 namespace vcsn
10 {
11 
12  namespace detail
13  {
14  /*-------------------------.
15  | daut(automaton, stream). |
16  `-------------------------*/
17 
21  template <Automaton Aut>
22  class daut_impl: public printer<Aut>
23  {
24  private:
26  using typename super_t::automaton_t;
27  using typename super_t::state_t;
28  using typename super_t::polynomial_t;
29  using typename super_t::transition_t;
30 
31  using super_t::aut_;
32  using super_t::os_;
33  using super_t::ps_;
34 
35  using super_t::super_t;
36 
37 
38  public:
40  std::ostream& operator()()
41  {
44  return os_;
45  }
46 
47  private:
49  {
50  os_ << "context = ";
51  aut_->context().print_set(os_, format::sname);
52  }
53 
55  void print_transitions_(const state_t src, const state_t dst,
56  const polynomial_t& entry)
57  {
58  os_ << '\n';
59  if (src == aut_->pre())
60  os_ << '$';
61  else
62  aut_->print_state(src, os_);
63  os_ << " -> ";
64  if (dst == aut_->post())
65  os_ << '$';
66  else
67  aut_->print_state(dst, os_);
68 
69  auto e = to_string(ps_, entry, format{}, ", ");
70  if (!e.empty())
71  os_ << ' ' << e;
72  }
73 
76  {
77  // For each src state, the destinations, sorted.
78  auto dsts = std::map<state_t, polynomial_t>{};
79  for (auto src : aut_->all_states())
80  if (!aut_->is_lazy(src))
81  {
82  dsts.clear();
83  for (auto t: all_out(aut_, src))
84  // Bypass weight_of(set), because we know that the weight is
85  // nonzero, and that there is only one weight per letter.
86  ps_.new_weight(dsts[aut_->dst_of(t)],
87  aut_->label_of(t), aut_->weight_of(t));
88  for (const auto& p: dsts)
89  print_transitions_(src, p.first, p.second);
90  }
91  }
92  };
93  }
94 
99  template <Automaton Aut>
100  std::ostream&
101  daut(const Aut& aut, std::ostream& out = std::cout)
102  {
103  // Cannot use auto here.
105  return daut();
106  }
107 }
state_t_of< automaton_t > state_t
Definition: printer.hh:50
Print as a parsable type string.
Definition: format.hh:26
void print_context_()
Definition: daut.hh:48
transition_t_of< automaton_t > transition_t
Definition: printer.hh:55
An input/output format for valuesets.
Definition: format.hh:13
std::ostream & os_
Output stream.
Definition: printer.hh:135
typename polynomialset_t::value_t polynomial_t
Definition: printer.hh:59
void print_transitions_(const state_t src, const state_t dst, const polynomial_t &entry)
Print the transitions between state src and state dst.
Definition: daut.hh:55
Definition: a-star.hh:8
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:85
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:66
Factor common bits in automaton formatting.
Definition: printer.hh:37
const polynomialset_t ps_
Short-hand to the polynomialset used to print the entries.
Definition: printer.hh:141
automaton_t aut_
The automaton we have to output.
Definition: printer.hh:133
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
std::ostream & daut(const Aut &aut, std::ostream &out=std::cout)
Print an automaton in Daut format.
Definition: daut.hh:101
std::ostream & operator()()
Print the automaton on the stream.
Definition: daut.hh:40
void print_transitions_()
Print all the transitions, sorted by src state, then dst state.
Definition: daut.hh:75
Format an automaton into Daut.
Definition: daut.hh:22