Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
eval.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_EVAL_HH
2 # define VCSN_ALGOS_EVAL_HH
3 
4 # include <algorithm>
5 # include <vector>
6 
7 # include <vcsn/ctx/traits.hh>
8 # include <vcsn/dyn/fwd.hh>
9 # include <vcsn/dyn/automaton.hh>
10 # include <vcsn/dyn/label.hh>
11 # include <vcsn/dyn/weight.hh>
12 
13 namespace vcsn
14 {
15  namespace detail
16  {
17  template <typename 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  : a_(a)
35  , ws_(*a_->weightset())
36  {}
37 
38  weight_t operator()(const word_t& word) const
39  {
40  // Initialization.
41  const weight_t zero = ws_.zero();
42  // FIXME: a perfect job for a sparse array: most of the states
43  // will be not visited, nevertheless, because we iterate on
44  // all the states, they are costly at each iteration.
45 
47  const auto& states = a_->states();
48  size_t last_state = *std::max_element(std::begin(states),
49  std::end(states));
50  // Do not use braces (v1{size, zero}): the type of zero might
51  // result in the compiler believing we are building a vector
52  // with two values: a_->num_all_states() and zero.
53  weights_t v1(last_state + 1, zero);
54  v1[a_->pre()] = ws_.one();
55  weights_t v2{v1};
56 
57  // Computation.
58  auto ls = *a_->labelset();
59  for (auto l : ls.letters_of(ls.delimit(word)))
60  {
61  v2.assign(v2.size(), zero);
62  for (size_t s = 0; s < v1.size(); ++s)
63  if (!ws_.is_zero(v1[s])) // delete if bench >
64  for (auto t : a_->out(s, l))
65  // Introducing a reference to v2[a_->dst_of(tr)] is
66  // tempting, but won't work for std::vector<bool>.
67  // FIXME: Specialize for Boolean?
68  v2[a_->dst_of(t)] =
69  ws_.add(v2[a_->dst_of(t)],
70  ws_.mul(v1[s], a_->weight_of(t)));
71  std::swap(v1, v2);
72  }
73  return v1[a_->post()];
74  }
75  private:
76  const automaton_t& a_;
77  const weightset_t& ws_;
78  };
79 
80  } // namespace detail
81 
82  template <typename Aut>
83  inline
84  auto
85  eval(const Aut& a, const typename labelset_t_of<Aut>::word_t& w)
87  {
89  return e(w);
90  }
91 
92  namespace dyn
93  {
94  namespace detail
95  {
97  template <typename Aut, typename LabelSet>
98  auto
99  eval(const automaton& aut, const label& lbl)
100  -> weight
101  {
102  const auto& a = aut->as<Aut>();
103  const auto& l = lbl->as<LabelSet>().label();
104  auto res = ::vcsn::eval(a, l);
105  const auto& ctx = a->context();
106  return make_weight(*ctx.weightset(), res);
107  }
108 
110  (const automaton& aut, const label& s) -> weight);
111  }
112  }
113 
114 
115 } // namespace vcsn
116 
117 #endif // !VCSN_ALGOS_EVAL_HH
std::vector< weight_t > weights_t
Definition: eval.hh:30
typename weightset_t::value_t weight_t
Definition: eval.hh:27
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
typename labelset_t_of< automaton_t >::word_t word_t
Definition: eval.hh:25
state_t_of< automaton_t > state_t
Definition: eval.hh:24
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:82
const automaton_t & a_
Definition: eval.hh:76
weightset_t_of< automaton_t > weightset_t
Definition: eval.hh:26
auto eval(const automaton &aut, const label &lbl) -> weight
Bridge.
Definition: eval.hh:99
weight_t operator()(const word_t &word) const
Definition: eval.hh:38
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
std::shared_ptr< const detail::label_base > label
Definition: fwd.hh:46
weight make_weight(const WeightSet &ws, const typename WeightSet::value_t &w)
Definition: weight.hh:82
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
evaluator(const automaton_t &a)
Definition: eval.hh:33
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
auto eval(const Aut &a, const typename labelset_t_of< Aut >::word_t &w) -> weight_t_of< Aut >
Definition: eval.hh:85
const weightset_t & ws_
Definition: eval.hh:77