Vcsn  2.3
Be Rational
printer.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
5 #include <boost/range/algorithm/sort.hpp>
6 
7 #include <vcsn/algos/sort.hh> // transition_less.
10 
11 namespace vcsn
12 {
13  namespace detail
14  {
15 
16  /*-----------.
17  | printer. |
18  `-----------*/
19 
23  template <Automaton Aut>
24  class printer
25  {
26  protected:
27  using automaton_t = Aut;
28 
29  public:
30  printer(const automaton_t& aut, std::ostream& out)
31  : aut_(aut)
32  , os_(out)
33  {}
34 
35  // Should not be public, but needed by GCC 4.8.1.
36  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58972
38 
39  protected:
47 
49  using states_t = std::vector<state_t>;
50 
52  virtual std::string
53  label_(const label_t& l) const
54  {
55  return ls_.is_one(l) ? "@epsilon" : to_string(ls_, l);
56  }
57 
60  virtual void print_transition_(transition_t t) const
61  {
62  aut_->print_state(aut_->src_of(t), os_);
63  os_ << ' ' << label_(aut_->label_of(t)) << ' ';
64  aut_->print_state(aut_->dst_of(t), os_);
65  }
66 
68  void print_state_(const state_t s)
69  {
70  std::vector<transition_t> ts;
71  for (auto t : out(aut_, s))
72  ts.emplace_back(t);
74  for (auto t : ts)
75  {
76  os_ << '\n';
78  }
79  }
80 
84  {
85  for (auto s: aut_->states())
86  print_state_(s);
87  }
88 
90  void list_states_(const states_t& ss)
91  {
92  for (auto s: ss)
93  {
94  os_ << ' ';
95  aut_->print_state(s, os_);
96  }
97  }
98 
101  {
102  states_t res;
103  for (auto t: initial_transitions(aut_))
104  res.emplace_back(aut_->dst_of(t));
105  boost::sort(res);
106  return res;
107  }
108 
111  {
112  states_t res;
113  for (auto t: final_transitions(aut_))
114  res.emplace_back(aut_->src_of(t));
115  boost::sort(res);
116  return res;
117  }
118 
122  std::ostream& os_;
124  const labelset_t_of<automaton_t>& ls_ = *aut_->labelset();
126  const weightset_t& ws_ = *aut_->weightset();
128  const polynomialset_t ps_{aut_->context()};
129  };
130  }
131 }
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
context_t_of< automaton_t > context_t
Definition: printer.hh:40
Factor common bits in automaton formatting.
Definition: printer.hh:24
void print_transitions_()
Output transitions, sorted lexicographically.
Definition: printer.hh:83
Compare transitions of an automaton.
Definition: sort.hh:23
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:65
const polynomialset_t ps_
Short-hand to the polynomialset used to print the entries.
Definition: printer.hh:128
const weightset_t & ws_
Short-hand to the weightset.
Definition: printer.hh:126
printer(const automaton_t &aut, std::ostream &out)
Definition: printer.hh:30
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
states_t initials_()
The list of initial states, sorted.
Definition: printer.hh:100
weight_t_of< automaton_t > weight_t
Definition: printer.hh:44
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:64
states_t finals_()
The list of final states, sorted.
Definition: printer.hh:110
return res
Definition: multiply.hh:398
virtual std::string label_(const label_t &l) const
Convert a label to its representation.
Definition: printer.hh:53
typename polynomialset_t::value_t polynomial_t
Definition: printer.hh:46
void list_states_(const states_t &ss)
List names of states in ss, preceded by ' '.
Definition: printer.hh:90
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
virtual void print_transition_(transition_t t) const
Output the transition t.
Definition: printer.hh:60
Definition: a-star.hh:8
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
transition_t_of< automaton_t > transition_t
Definition: printer.hh:42
state_t_of< automaton_t > state_t
Definition: printer.hh:37
std::ostream & os_
Output stream.
Definition: printer.hh:122
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
weightset_t_of< automaton_t > weightset_t
Definition: printer.hh:43
label_t_of< automaton_t > label_t
Definition: printer.hh:41
std::vector< state_t > states_t
A list of states.
Definition: printer.hh:49
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:145
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: printer.hh:124
auto sort(const Aut &a) -> permutation_automaton< Aut >
Definition: sort.hh:161
automaton_t aut_
The automaton we have to output.
Definition: printer.hh:120
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:156
void print_state_(const state_t s)
Output transitions, sorted lexicographically on (Label, Dest).
Definition: printer.hh:68