Vcsn  2.2
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;
24  using node_t = typename super_t::node_t;
25  using inner_t = typename super_t::inner_t;
26  template <type_t Type>
27  using unary_t = typename super_t::template unary_t<Type>;
28  template <type_t Type>
29  using variadic_t = typename super_t::template variadic_t<Type>;
30  using leaf_t = typename super_t::leaf_t;
31 
33  constexpr static const char* me() { return "size"; }
34 
36  size_t
38  {
39  size_ = 0;
40  v.accept(*this);
41  return size_;
42  }
43 
45  size_t
46  operator()(const std::shared_ptr<const node_t>& v)
47  {
48  return operator()(*v);
49  }
50 
51  private:
52 
58  VCSN_RAT_VISIT(lweight, v) { ++size_; v.sub()->accept(*this); }
61  VCSN_RAT_VISIT(rweight, v) { ++size_; v.sub()->accept(*this); }
67 
68  using tuple_t = typename super_t::tuple_t;
69  template <bool = context_t::is_lat,
70  typename Dummy = void>
71  struct visit_tuple
72  {
74  template <size_t I>
75  size_t size_(const tuple_t& v)
76  {
77  using rs_t = typename expressionset_t::template project_t<I>;
78  return size<rs_t>(std::get<I>(v.sub()));
79  }
80 
82  template <size_t... I>
84  {
85  auto res = size_t{0};
86  using swallow = int[];
87  (void) swallow
88  {
89  (res += size_<I>(v),
90  0)...
91  };
92  return res;
93  }
94 
96  size_t operator()(const tuple_t& v)
97  {
99  }
100  };
101 
102  template <typename Dummy>
103  struct visit_tuple<false, Dummy>
104  {
105  size_t operator()(const tuple_t&)
106  {
108  }
109  };
110 
111  void visit(const tuple_t& v, std::true_type) override
112  {
113  size_ += visit_tuple<>{}(v);
114  }
115 
117  template <rat::exp::type_t Type>
118  void visit_unary(const unary_t<Type>& v)
119  {
120  ++size_;
121  v.sub()->accept(*this);
122  }
123 
125  template <rat::exp::type_t Type>
126  void visit_variadic(const variadic_t<Type>& v)
127  {
128  // One operator bw each argument.
129  size_ += v.size() - 1;
130  for (const auto& child : v)
131  child->accept(*this);
132  }
133 
134  size_t size_;
135  };
136 
137  template <typename ExpSet>
138  size_t size(const typename ExpSet::value_t& r)
139  {
140  auto s = sizer<ExpSet>{};
141  return s(r);
142  }
143  } // namespace rat
144 } // namespace vcsn
context_t_of< expressionset_t > context_t
Definition: size.hh:22
typename super_t::template variadic_t< Type > variadic_t
Definition: size.hh:29
VCSN_RAT_VISIT(sum, v)
Definition: size.hh:64
typename super_t::inner_t inner_t
Definition: size.hh:25
VCSN_RAT_VISIT(lweight, v)
Definition: size.hh:58
ExpSet expressionset_t
Definition: size.hh:19
typename super_t::node_t node_t
Definition: size.hh:24
void visit_variadic(const variadic_t< Type > &v)
Traverse variadic node.
Definition: size.hh:126
void visit_unary(const unary_t< Type > &v)
Traverse unary node.
Definition: size.hh:118
VCSN_RAT_VISIT(star, v)
Definition: size.hh:63
Definition: a-star.hh:8
void visit(const tuple_t &v, std::true_type) override
Definition: size.hh:111
VCSN_RAT_VISIT(transposition, v)
Definition: size.hh:65
size_t operator()(const tuple_t &v)
Entry point.
Definition: size.hh:96
VCSN_RAT_VISIT(zero,)
Definition: size.hh:66
VCSN_RAT_VISIT(complement, v)
Definition: size.hh:54
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
An inner node with multiple children.
Definition: expression.hh:118
typename expressionset_t::const_visitor super_t
Definition: size.hh:20
VCSN_RAT_VISIT(atom,)
Definition: size.hh:53
VCSN_RAT_VISIT(ldiv, v)
Definition: size.hh:57
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
typename super_t::leaf_t leaf_t
Definition: size.hh:30
size_t operator()(const node_t &v)
Entry point: return the size of v.
Definition: size.hh:37
VCSN_RAT_VISIT(one,)
Definition: size.hh:59
VCSN_RAT_VISIT(prod, v)
Definition: size.hh:60
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_
Definition: size.hh:134
VCSN_RAT_VISIT(shuffle, v)
Definition: size.hh:62
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
size_t operator()(const std::shared_ptr< const node_t > &v)
Entry point: return the size of v.
Definition: size.hh:46
An inner node implementing a weight.
Definition: expression.hh:264
size_t size_(const tuple_t &v)
Size for one tape.
Definition: size.hh:75
VCSN_RAT_VISIT(infiltration, v)
Definition: size.hh:56
VCSN_RAT_VISIT(conjunction, v)
Definition: size.hh:55
VCSN_RAT_VISIT(rweight, v)
Definition: size.hh:61
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
size_t operator()(const tuple_t &)
Definition: size.hh:105
typename super_t::tuple_t tuple_t
Definition: size.hh:68
size_t size_(const tuple_t &v, detail::index_sequence< I... >)
Sum of sizes for all tapes.
Definition: size.hh:83
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: size.hh:33
typename super_t::template unary_t< Type > unary_t
Definition: size.hh:27