Vcsn  2.3a
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.
11 
12 namespace vcsn
13 {
14  namespace detail
15  {
17  template <typename LabelSet>
18  struct rank
19  {
20  static constexpr size_t value = 1;
21  };
22 
23  template <typename... LabelSet>
24  struct rank<tupleset<LabelSet...>>
25  {
26  static constexpr size_t value = tupleset<LabelSet...>::size();
27  };
28 
29  /*-----------.
30  | printer. |
31  `-----------*/
32 
36  template <Automaton Aut>
37  class printer
38  {
39  protected:
40  using automaton_t = Aut;
41 
42  public:
43  printer(const automaton_t& aut, std::ostream& out)
44  : aut_(aut)
45  , os_(out)
46  {}
47 
48  // Should not be public, but needed by GCC 4.8.1.
49  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58972
51 
52  protected:
60 
62  using states_t = std::vector<state_t>;
63 
65  virtual std::string
66  label_(const label_t& l) const
67  {
68  return ls_.is_one(l) ? "@epsilon" : to_string(ls_, l);
69  }
70 
73  virtual void print_transition_(transition_t t) const
74  {
75  aut_->print_state(aut_->src_of(t), os_);
76  os_ << ' ' << label_(aut_->label_of(t)) << ' ';
77  aut_->print_state(aut_->dst_of(t), os_);
78  }
79 
81  void print_state_(const state_t s)
82  {
83  std::vector<transition_t> ts;
84  for (auto t : out(aut_, s))
85  ts.emplace_back(t);
87  for (auto t : ts)
88  {
89  os_ << '\n';
91  }
92  }
93 
97  {
98  for (auto s: aut_->states())
99  print_state_(s);
100  }
101 
103  void list_states_(const states_t& ss)
104  {
105  for (auto s: ss)
106  {
107  os_ << ' ';
108  aut_->print_state(s, os_);
109  }
110  }
111 
114  {
115  states_t res;
116  for (auto t: initial_transitions(aut_))
117  res.emplace_back(aut_->dst_of(t));
118  boost::sort(res);
119  return res;
120  }
121 
124  {
125  states_t res;
126  for (auto t: final_transitions(aut_))
127  res.emplace_back(aut_->src_of(t));
128  boost::sort(res);
129  return res;
130  }
131 
135  std::ostream& os_;
137  const labelset_t_of<automaton_t>& ls_ = *aut_->labelset();
139  const weightset_t& ws_ = *aut_->weightset();
141  const polynomialset_t ps_{aut_->context()};
144  using is_transducer_t =
145  std::integral_constant<bool,
148  };
149  }
150 }
state_t_of< automaton_t > state_t
Definition: printer.hh:50
weightset_t_of< automaton_t > weightset_t
Definition: printer.hh:56
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:84
Definition: a-star.hh:8
return res
Definition: multiply.hh:398
virtual std::string label_(const label_t &l) const
Convert a label to its representation.
Definition: printer.hh:66
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
transition_t_of< automaton_t > transition_t
Definition: printer.hh:55
std::ostream & os_
Output stream.
Definition: printer.hh:135
auto sort(const Aut &a) -> permutation_automaton< Aut >
Definition: sort.hh:161
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:65
const is_transducer_t is_transducer_
Definition: printer.hh:147
typename polynomialset_t::value_t polynomial_t
Definition: printer.hh:59
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
weight_t_of< automaton_t > weight_t
Definition: printer.hh:57
void print_state_(const state_t s)
Output transitions, sorted lexicographically on (Label, Dest).
Definition: printer.hh:81
void print_transitions_()
Output transitions, sorted lexicographically.
Definition: printer.hh:96
std::vector< state_t > states_t
A list of states.
Definition: printer.hh:62
printer(const automaton_t &aut, std::ostream &out)
Definition: printer.hh:43
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
static constexpr size_t value
Definition: printer.hh:20
virtual void print_transition_(transition_t t) const
Output the transition t.
Definition: printer.hh:73
Number of tapes.
Definition: printer.hh:18
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
void list_states_(const states_t &ss)
List names of states in ss, preceded by ' '.
Definition: printer.hh:103
automaton_t aut_
The automaton we have to output.
Definition: printer.hh:133
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
std::integral_constant< bool, 2<=rank< labelset_t_of< automaton_t >>::value > is_transducer_t
Whether is a transducer (two-tape automaton) as opposed to an acceptor.
Definition: printer.hh:146
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
context_t_of< automaton_t > context_t
Definition: printer.hh:53
states_t initials_()
The list of initial states, sorted.
Definition: printer.hh:113
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:165
states_t finals_()
The list of final states, sorted.
Definition: printer.hh:123
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
Compare transitions of an automaton.
Definition: sort.hh:23
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:176
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
const weightset_t & ws_
Short-hand to the weightset.
Definition: printer.hh:139
label_t_of< automaton_t > label_t
Definition: printer.hh:54
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: printer.hh:137
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46