Vcsn  2.3
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 <bool = context_t::is_lat,
68  typename Dummy = void>
69  struct visit_tuple
70  {
72  template <size_t I>
73  size_t size_(const tuple_t& v)
74  {
75  using rs_t = typename expressionset_t::template project_t<I>;
76  return size<rs_t>(std::get<I>(v.sub()));
77  }
78 
80  template <size_t... I>
82  {
83  auto res = size_t{0};
84  using swallow = int[];
85  (void) swallow
86  {
87  (res += size_<I>(v),
88  0)...
89  };
90  return res;
91  }
92 
94  size_t operator()(const tuple_t& v)
95  {
97  }
98  };
99 
100  template <typename Dummy>
101  struct visit_tuple<false, Dummy>
102  {
103  size_t operator()(const tuple_t&)
104  {
106  }
107  };
108 
109  void visit(const tuple_t& v, std::true_type) override
110  {
111  size_ += visit_tuple<>{}(v);
112  }
113 
115  template <rat::exp::type_t Type>
116  void visit_(const unary_t<Type>& v)
117  {
118  ++size_;
119  v.sub()->accept(*this);
120  }
121 
123  template <rat::exp::type_t Type>
124  void visit_(const variadic_t<Type>& v)
125  {
126  // One operator bw each argument.
127  size_ += v.size() - 1;
128  for (const auto& child : v)
129  child->accept(*this);
130  }
131 
132  size_t size_;
133  };
134 
135  template <typename ExpSet>
136  size_t size(const typename ExpSet::value_t& r)
137  {
138  auto s = sizer<ExpSet>{};
139  return s(r);
140  }
141  } // namespace rat
142 } // namespace vcsn
typename node_t::value_t expression_t
A shared_ptr to node_t.
Definition: size.hh:27
return v
Definition: multiply.hh:361
Functor to compute the size of a rational expression.
Definition: size.hh:15
VCSN_RAT_VISIT(infiltrate, v)
Definition: size.hh:55
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
typename context_t::weightset_t::value_t weight_t
Definition: size.hh:23
VCSN_RAT_VISIT(conjunction, v)
Definition: size.hh:54
typename super_t::template variadic_t< Type > variadic_t
Definition: size.hh:33
size_t operator()(const tuple_t &)
Definition: size.hh:103
VCSN_RAT_VISIT(one,)
Definition: size.hh:59
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
size_t operator()(const expression_t &v)
Entry point: return the size of v.
Definition: size.hh:41
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename super_t::template unary_t< Type > unary_t
Definition: size.hh:31
VCSN_RAT_VISIT(star, v)
Definition: size.hh:62
size_t size_
Definition: size.hh:132
VCSN_RAT_VISIT(ldivide, v)
Definition: size.hh:56
VCSN_RAT_VISIT(shuffle, v)
Definition: size.hh:61
An inner node implementing a weight.
Definition: expression.hh:264
typename super_t::leaf_t leaf_t
Definition: size.hh:34
return res
Definition: multiply.hh:398
typename super_t::inner_t inner_t
Definition: size.hh:29
VCSN_RAT_VISIT(lweight, v)
Definition: size.hh:57
typename super_t::tuple_t tuple_t
Definition: size.hh:66
size_t size_(const tuple_t &v, detail::index_sequence< I... >)
Sum of sizes for all tapes.
Definition: size.hh:81
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: size.hh:37
size_t size_(const tuple_t &v)
Size for one tape.
Definition: size.hh:73
void visit_(const unary_t< Type > &v)
Traverse unary node.
Definition: size.hh:116
void visit_(const variadic_t< Type > &v)
Traverse variadic node.
Definition: size.hh:124
Definition: a-star.hh:8
VCSN_RAT_VISIT(rweight, v)
Definition: size.hh:60
VCSN_RAT_VISIT(compose, v)
Definition: size.hh:53
VCSN_RAT_VISIT(transposition, v)
Definition: size.hh:63
context_t_of< expressionset_t > context_t
Definition: size.hh:22
size_t operator()(const tuple_t &v)
Entry point.
Definition: size.hh:94
VCSN_RAT_VISIT(zero,)
Definition: size.hh:64
ExpSet expressionset_t
Definition: size.hh:19
typename super_t::node_t node_t
Actual node, without indirection.
Definition: size.hh:25
VCSN_RAT_VISIT(mul, v)
Definition: size.hh:58
typename expressionset_t::const_visitor super_t
Definition: size.hh:20
VCSN_RAT_VISIT(add, v)
Definition: size.hh:50
An inner node with multiple children.
Definition: expression.hh:118
VCSN_RAT_VISIT(atom,)
Definition: size.hh:51
void visit(const tuple_t &v, std::true_type) override
Definition: size.hh:109
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
VCSN_RAT_VISIT(complement, v)
Definition: size.hh:52