Vcsn  2.8
Be Rational
compare.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/core/rat/fwd.hh>
4 #include <vcsn/core/rat/size.hh>
6 #include <vcsn/misc/cast.hh>
7 #include <vcsn/misc/functional.hh> // vcsn::lexicographical_cmp
8 
9 namespace vcsn
10 {
11 
12  namespace rat
13  {
14 
17  template <typename ExpSet>
18  class compare
19  : public ExpSet::const_visitor
20  {
21  public:
22  using expressionset_t = ExpSet;
23  using self_t = compare;
28  using expression_t = typename expressionset_t::value_t;
29  using super_t = typename expressionset_t::const_visitor;
30  using node_t = typename super_t::node_t;
31  using inner_t = typename super_t::inner_t;
32  template <rat::exp::type_t Type>
33  using unary_t = typename super_t::template unary_t<Type>;
34  template <rat::exp::type_t Type>
35  using variadic_t = typename super_t::template variadic_t<Type>;
36  template <rat::exp::type_t Type>
37  using weight_node_t = typename super_t::template weight_node_t<Type>;
38 
46  {
47  if (lhs == rhs)
48  return 0;
49  else
50  {
51  auto lhss = int(size<ExpSet>(lhs));
52  auto rhss = int(size<ExpSet>(rhs));
53 
54  if (auto res = lhss - rhss)
55  return res;
56  else if (auto res = int(lhs->type()) - int(rhs->type()))
57  return res;
58  else
59  {
60  rhs_ = rhs;
61  lhs->accept(*this);
62  return res_;
63  }
64  }
65  }
66 
67  private:
68  /*-----------------------------------------------------------.
69  | Unary visit functions than bounces to their binary peers. |
70  `-----------------------------------------------------------*/
71 
72 #define DEFINE(Type) \
73  VCSN_RAT_VISIT(Type, lhs) \
74  { \
75  res_ = cmp_(lhs, *down_pointer_cast<const Type ## _t>(rhs_)); \
76  }
77 
94 #undef DEFINE
95 
96  using tuple_t = typename super_t::tuple_t;
97 
98  template <typename = void>
99  struct visit_tuple
100  {
101  using tupleset_t = typename expressionset_t::template as_tupleset_t<>;
103  int operator()(const tuple_t& lhs)
104  {
105  return operator()(lhs,
106  *down_pointer_cast<const tuple_t>(self_.rhs_));
107  }
108 
110  int operator()(const tuple_t& lhs, const tuple_t& rhs)
111  {
112  return tupleset_t::compare(lhs.sub(), rhs.sub());
113  }
114 
115  const self_t& self_;
116  };
117 
118  void visit(const tuple_t& v, std::true_type) override
119  {
120  detail::static_if<context_t::is_lat>
121  ([this](auto&& v)
122  {
123  res_ = visit_tuple<decltype(v)>{*this}(v);
124  })
125  (v);
126  }
127 
128  /*-------------------------------------------------------.
129  | Binary functions that compare two nodes of same type. |
130  `-------------------------------------------------------*/
131 
132  int cmp_(const zero_t&, const zero_t&)
133  {
134  return 0;
135  }
136 
137  int cmp_(const one_t&, const one_t&)
138  {
139  return 0;
140  }
141 
142  int cmp_(const atom_t& lhs, const atom_t& rhs)
143  {
144  return labelset_t::compare(lhs.value(), rhs.value());
145  }
146 
147  int cmp_(const name_t& lhs, const name_t& rhs)
148  {
149  if (auto res = (*this)(lhs.sub(), rhs.sub()))
150  return res;
151  else
152  return lhs.name_get().get().compare(rhs.name_get());
153  }
154 
155  template <rat::exp::type_t Type>
156  int cmp_(const variadic_t<Type>& lhs, const variadic_t<Type>& rhs)
157  {
158  if (auto res = int(lhs.size()) - int(rhs.size()))
159  return res;
160  else
161  return lexicographical_cmp(lhs, rhs,
163  }
164 
165  template <rat::exp::type_t Type>
166  int cmp_(const unary_t<Type>& lhs, const unary_t<Type>& rhs)
167  {
168  return (*this)(lhs.sub(), rhs.sub());
169  }
170 
171  template <rat::exp::type_t Type>
172  int cmp_(const weight_node_t<Type>& lhs, const weight_node_t<Type>& rhs)
173  {
174  // Lexicographic comparison on sub-expression, and then weight.
175  if (auto res = (*this)(lhs.sub(), rhs.sub()))
176  return res;
177  else
178  return weightset_t::compare(lhs.weight(), rhs.weight());
179  }
180 
181  private:
186  int res_;
187  };
188  }
189 }
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
Definition: traits.hh:67
int lexicographical_cmp(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp)
Lexicographical three-way comparison between two ranges.
Definition: algorithm.hh:128
weightset_t_of< context_t > weightset_t
Definition: compare.hh:26
int cmp_(const zero_t &, const zero_t &)
Definition: compare.hh:132
int operator()(expression_t lhs, expression_t rhs)
Whether lhs < rhs.
Definition: compare.hh:45
ExpSet expressionset_t
Definition: compare.hh:22
An inner node with multiple children.
Definition: expression.hh:119
void visit(const tuple_t &v, std::true_type) override
Definition: compare.hh:118
typename super_t::inner_t inner_t
Definition: compare.hh:31
typename super_t::template unary_t< Type > unary_t
Definition: compare.hh:33
An inner node to name the subexpression.
Definition: expression.hh:290
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
int operator()(const tuple_t &lhs, const tuple_t &rhs)
Binary operator().
Definition: compare.hh:110
decltype(std::declval< LabelSet >().one()) one_t
Definition: labelset.hh:35
int cmp_(const weight_node_t< Type > &lhs, const weight_node_t< Type > &rhs)
Definition: compare.hh:172
weight_t_of< context_t > weight_t
Definition: compare.hh:27
int cmp_(const name_t &lhs, const name_t &rhs)
Definition: compare.hh:147
A functor for three-way comparison between two expressions.
Definition: compare.hh:18
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
typename expressionset_t::value_t expression_t
Definition: compare.hh:28
Definition: a-star.hh:8
int operator()(const tuple_t &lhs)
Entry point: down_cast rhs_ and bounce to binary operator().
Definition: compare.hh:103
typename super_t::node_t node_t
Definition: compare.hh:30
context_t_of< expressionset_t > context_t
Definition: compare.hh:24
typename super_t::template weight_node_t< Type > weight_node_t
Definition: compare.hh:37
int cmp_(const one_t &, const one_t &)
Definition: compare.hh:137
int cmp_(const atom_t &lhs, const atom_t &rhs)
Definition: compare.hh:142
An inner node implementing a weight.
Definition: expression.hh:256
int cmp_(const variadic_t< Type > &lhs, const variadic_t< Type > &rhs)
Definition: compare.hh:156
typename expressionset_t::const_visitor super_t
Definition: compare.hh:29
return v
Definition: multiply.hh:362
labelset_t_of< context_t > labelset_t
Definition: compare.hh:25
expression_t rhs_
The right-hand side expression with which the current node is compared.
Definition: compare.hh:184
int res_
The current result.
Definition: compare.hh:186
Functor to three-way comparison Values of ValueSets.
Definition: functional.hh:11
#define DEFINE(Type)
Definition: compare.hh:72
typename super_t::template variadic_t< Type > variadic_t
Definition: compare.hh:35
int cmp_(const unary_t< Type > &lhs, const unary_t< Type > &rhs)
Definition: compare.hh:166
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
Definition: traits.hh:66
int compare(const Lhs &lhs, const Rhs &rhs)
Comparison between lhs and rhs.
return res
Definition: multiply.hh:399
typename expressionset_t::template as_tupleset_t<> tupleset_t
Definition: compare.hh:101
typename super_t::tuple_t tuple_t
Definition: compare.hh:96