Vcsn  2.4
Be Rational
size.hh
Go to the documentation of this file.
1 #pragma once
2 
5 #include <vcsn/misc/cast.hh>
6 
7 namespace vcsn
8 {
9  namespace rat
10  {
14  template <typename ExpSet>
15  class sizer
16  : public ExpSet::const_visitor
17  {
18  public:
19  using expressionset_t = ExpSet;
20  using super_t = typename expressionset_t::const_visitor;
21 
23  using weight_t = typename context_t::weightset_t::value_t;
25  using node_t = typename super_t::node_t;
27  using expression_t = typename node_t::value_t;
28 
29  using inner_t = typename super_t::inner_t;
30  template <type_t Type>
31  using unary_t = typename super_t::template unary_t<Type>;
32  template <type_t Type>
33  using variadic_t = typename super_t::template variadic_t<Type>;
34  using leaf_t = typename super_t::leaf_t;
35 
37  constexpr static const char* me() { return "size"; }
38 
40  size_t
42  {
43  size_ = 0;
44  v->accept(*this);
45  return size_;
46  }
47 
48  private:
49 
57  VCSN_RAT_VISIT(lweight, v) { ++size_; v.sub()->accept(*this); }
60  VCSN_RAT_VISIT(rweight, v) { ++size_; v.sub()->accept(*this); }
65 
66  using tuple_t = typename super_t::tuple_t;
67  template <typename Dummy = void>
68  struct visit_tuple
69  {
71  template <size_t I>
72  size_t size_(const tuple_t& v)
73  {
74  using rs_t = typename expressionset_t::template project_t<I>;
75  return size<rs_t>(std::get<I>(v.sub()));
76  }
77 
79  template <size_t... I>
81  {
82  auto res = size_t{0};
83  using swallow = int[];
84  (void) swallow
85  {
86  (res += size_<I>(v),
87  0)...
88  };
89  return res;
90  }
91 
93  size_t operator()(const tuple_t& v)
94  {
96  }
97  };
98 
99  void visit(const tuple_t& v, std::true_type) override
100  {
101  detail::static_if<context_t::is_lat>
102  ([this](auto&& v){ size_ += visit_tuple<decltype(v)>{}(v); })
103  (v);
104  }
105 
107  template <rat::exp::type_t Type>
108  void visit_(const unary_t<Type>& v)
109  {
110  ++size_;
111  v.sub()->accept(*this);
112  }
113 
115  template <rat::exp::type_t Type>
116  void visit_(const variadic_t<Type>& v)
117  {
118  // One operator bw each argument.
119  size_ += v.size() - 1;
120  for (const auto& child : v)
121  child->accept(*this);
122  }
123 
124  size_t size_;
125  };
126 
127  template <typename ExpSet>
128  size_t size(const typename ExpSet::value_t& r)
129  {
130  auto s = sizer<ExpSet>{};
131  return s(r);
132  }
133  } // namespace rat
134 } // namespace vcsn
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: size.hh:37
return res
Definition: multiply.hh:398
VCSN_RAT_VISIT(rweight, v)
Definition: size.hh:60
VCSN_RAT_VISIT(mul, v)
Definition: size.hh:58
VCSN_RAT_VISIT(ldivide, v)
Definition: size.hh:56
An inner node implementing a weight.
Definition: expression.hh:255
VCSN_RAT_VISIT(zero,)
Definition: size.hh:64
context_t_of< expressionset_t > context_t
Definition: size.hh:22
ExpSet expressionset_t
Definition: size.hh:19
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
VCSN_RAT_VISIT(star, v)
Definition: size.hh:62
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
typename super_t::node_t node_t
Actual node, without indirection.
Definition: size.hh:25
Definition: a-star.hh:8
void visit_(const unary_t< Type > &v)
Traverse unary node.
Definition: size.hh:108
size_t operator()(const tuple_t &v)
Entry point.
Definition: size.hh:93
typename context_t::weightset_t::value_t weight_t
Definition: size.hh:23
Functor to compute the size of a rational expression.
Definition: size.hh:15
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
typename super_t::tuple_t tuple_t
Definition: size.hh:66
VCSN_RAT_VISIT(complement, v)
Definition: size.hh:52
VCSN_RAT_VISIT(compose, v)
Definition: size.hh:53
VCSN_RAT_VISIT(transposition, v)
Definition: size.hh:63
void visit(const tuple_t &v, std::true_type) override
Definition: size.hh:99
VCSN_RAT_VISIT(shuffle, v)
Definition: size.hh:61
typename super_t::template unary_t< Type > unary_t
Definition: size.hh:31
typename super_t::leaf_t leaf_t
Definition: size.hh:34
VCSN_RAT_VISIT(infiltrate, v)
Definition: size.hh:55
An inner node with multiple children.
Definition: expression.hh:118
return v
Definition: multiply.hh:361
void visit_(const variadic_t< Type > &v)
Traverse variadic node.
Definition: size.hh:116
typename node_t::value_t expression_t
A shared_ptr to node_t.
Definition: size.hh:27
size_t size_(const tuple_t &v, detail::index_sequence< I... >)
Sum of sizes for all tapes.
Definition: size.hh:80
VCSN_RAT_VISIT(one,)
Definition: size.hh:59
VCSN_RAT_VISIT(conjunction, v)
Definition: size.hh:54
VCSN_RAT_VISIT(add, v)
Definition: size.hh:50
VCSN_RAT_VISIT(atom,)
Definition: size.hh:51
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename super_t::template variadic_t< Type > variadic_t
Definition: size.hh:33
size_t size_(const tuple_t &v)
Size for one tape.
Definition: size.hh:72
size_t size_
Definition: size.hh:124
typename super_t::inner_t inner_t
Definition: size.hh:29
typename expressionset_t::const_visitor super_t
Definition: size.hh:20
VCSN_RAT_VISIT(lweight, v)
Definition: size.hh:57
size_t operator()(const expression_t &v)
Entry point: return the size of v.
Definition: size.hh:41