Vcsn  2.2a
Be Rational
eval.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <vcsn/ctx/traits.hh>
6 #include <vcsn/dyn/fwd.hh>
7 #include <vcsn/dyn/automaton.hh>
8 #include <vcsn/dyn/label.hh>
9 #include <vcsn/dyn/weight.hh>
10 #include <vcsn/algos/is-proper.hh>
11 #include <vcsn/misc/algorithm.hh>
12 
13 namespace vcsn
14 {
15  namespace detail
16  {
17  template <Automaton Aut>
18  class evaluator
19  {
20  static_assert(labelset_t_of<Aut>::is_free(),
21  "evaluate: requires free labelset");
22 
23  using automaton_t = Aut;
27  using weight_t = typename weightset_t::value_t;
28 
29  // state -> weight.
30  using weights_t = std::vector<weight_t>;
31 
32  public:
34  : aut_(a)
35  {}
36 
37  weight_t operator()(const word_t& word) const
38  {
39  // Initialization.
40  const weight_t zero = ws_.zero();
41 
42  // An array indexed by state numbers.
43  //
44  // Do not use braces (v1{size, zero}): the type of zero might
45  // result in the compiler believing we are building a vector
46  // with two values: size and zero.
47  //
48  // We start with just two states numbered 0 and 1: pre() and
49  // post().
50  weights_t v1(2, zero);
51  v1.reserve(detail::back(aut_->all_states()) + 1);
52  v1[aut_->pre()] = ws_.one();
53  weights_t v2{v1};
54  v2.reserve(detail::back(aut_->all_states()) + 1);
55 
56  // Computation.
57  auto ls = *aut_->labelset();
58  for (auto l : ls.letters_of(ls.delimit(word)))
59  {
60  v2.assign(v2.size(), zero);
61  for (size_t s = 0; s < v1.size(); ++s)
62  if (!ws_.is_zero(v1[s])) // delete if bench >
63  for (auto t : out(aut_, s, l))
64  {
65  // Make sure the vectors are large enough for dst.
66  // Exponential growth on the capacity, but keep
67  // the actual size as small as possible.
68  auto dst = aut_->dst_of(t);
69  if (v2.size() <= dst)
70  {
71  auto capacity = v2.capacity();
72  while (capacity <= dst)
73  capacity *= 2;
74  v1.reserve(capacity);
75  v2.reserve(capacity);
76  v1.resize(dst + 1, zero);
77  v2.resize(dst + 1, zero);
78  }
79  // Introducing a reference to v2[aut_->dst_of(tr)] is
80  // tempting, but won't work for std::vector<bool>.
81  // FIXME: Specialize for Boolean?
82  v2[dst] =
83  ws_.add(v2[dst],
84  ws_.mul(v1[s], aut_->weight_of(t)));
85  }
86  std::swap(v1, v2);
87  }
88  return v1[aut_->post()];
89  }
90  private:
92  const weightset_t& ws_ = *aut_->weightset();
93  };
94 
95  } // namespace detail
96 
98  template <Automaton Aut>
99  inline
100  auto
101  eval(const Aut& a, const word_t_of<Aut>& w)
102  -> std::enable_if_t<!context_t_of<Aut>::is_lao, weight_t_of<Aut>>
103  {
105  return e(w);
106  }
107 
113  template <Automaton Aut>
114  inline
115  auto
116  eval(const Aut& a)
117  -> std::enable_if_t<context_t_of<Aut>::is_lao, weight_t_of<Aut>>
118  {
119  require(is_proper(a), "eval: cannot evaluate with spontaneous transitions");
120  const auto& ws = *a->weightset();
121  auto res = ws.zero();
122  for (auto init_tr: initial_transitions(a))
123  {
124  auto s = a->dst_of(init_tr);
125  auto w = a->weight_of(init_tr);
126  for (auto out: all_out(a, s))
127  {
128  assert(a->dst_of(out) == a->post());
129  res = ws.add(res, ws.mul(w, a->weight_of(out)));
130  }
131  }
132  return res;
133  }
134 
135  namespace dyn
136  {
137  namespace detail
138  {
140  template <Automaton Aut, typename LabelSet>
141  weight
142  eval(const automaton& aut, const label& lbl)
143  {
144  const auto& a = aut->as<Aut>();
145  const auto& l = lbl->as<LabelSet>().label();
146  auto res = ::vcsn::eval(a, l);
147  const auto& ctx = a->context();
148  return make_weight(*ctx.weightset(), res);
149  }
150  }
151  }
152 } // namespace vcsn
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:137
weightset_t_of< automaton_t > weightset_t
Definition: eval.hh:26
const weightset_t & ws_
Definition: eval.hh:92
A dyn automaton.
Definition: automaton.hh:19
auto eval(const Aut &a, const word_t_of< Aut > &w) -> std::enable_if_t<!context_t_of< Aut >::is_lao, weight_t_of< Aut >>
General case of evaluation.
Definition: eval.hh:101
Container::value_type back(const Container &container)
The last member of this Container.
Definition: algorithm.hh:27
evaluator(const automaton_t &a)
Definition: eval.hh:33
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:37
typename labelset_t_of< base_t< ValueSet >>::word_t word_t_of
Definition: traits.hh:82
bool is_proper(const Aut &aut)
Test whether an automaton is proper.
Definition: is-proper.hh:48
state_t_of< automaton_t > state_t
Definition: eval.hh:24
std::vector< weight_t > weights_t
Definition: eval.hh:30
weight eval(const automaton &aut, const label &lbl)
Bridge.
Definition: eval.hh:142
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:71
weight_t operator()(const word_t &word) const
Definition: eval.hh:37
weight make_weight(const WeightSet &ws, const typename WeightSet::value_t &w)
Definition: weight.hh:79
word_t_of< automaton_t > word_t
Definition: eval.hh:25
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:59
std::shared_ptr< const detail::label_base > label
Definition: fwd.hh:44
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:56
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:82
automaton_t aut_
Definition: eval.hh:91
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:39
typename weightset_t::value_t weight_t
Definition: eval.hh:27
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:58
Definition: a-star.hh:8