Vcsn  2.5
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 
93 #undef DEFINE
94 
95  using tuple_t = typename super_t::tuple_t;
96 
97  template <typename = void>
98  struct visit_tuple
99  {
100  using tupleset_t = typename expressionset_t::template as_tupleset_t<>;
102  int operator()(const tuple_t& lhs)
103  {
104  return operator()(lhs,
105  *down_pointer_cast<const tuple_t>(self_.rhs_));
106  }
107 
109  int operator()(const tuple_t& lhs, const tuple_t& rhs)
110  {
111  return tupleset_t::compare(lhs.sub(), rhs.sub());
112  }
113 
114  const self_t& self_;
115  };
116 
117  void visit(const tuple_t& v, std::true_type) override
118  {
119  detail::static_if<context_t::is_lat>
120  ([this](auto&& v)
121  {
122  res_ = visit_tuple<decltype(v)>{*this}(v);
123  })
124  (v);
125  }
126 
127  /*-------------------------------------------------------.
128  | Binary functions that compare two nodes of same type. |
129  `-------------------------------------------------------*/
130 
131  int cmp_(const zero_t&, const zero_t&)
132  {
133  return 0;
134  }
135 
136  int cmp_(const one_t&, const one_t&)
137  {
138  return 0;
139  }
140 
141  int cmp_(const atom_t& lhs, const atom_t& rhs)
142  {
143  return labelset_t::compare(lhs.value(), rhs.value());
144  }
145 
146  template <rat::exp::type_t Type>
147  int cmp_(const variadic_t<Type>& lhs, const variadic_t<Type>& rhs)
148  {
149  if (auto res = int(lhs.size()) - int(rhs.size()))
150  return res;
151  else
152  return lexicographical_cmp(lhs, rhs,
154  }
155 
156  template <rat::exp::type_t Type>
157  int cmp_(const unary_t<Type>& lhs, const unary_t<Type>& rhs)
158  {
159  return (*this)(lhs.sub(), rhs.sub());
160  }
161 
162  template <rat::exp::type_t Type>
163  int cmp_(const weight_node_t<Type>& lhs, const weight_node_t<Type>& rhs)
164  {
165  // Lexicographic comparison on sub-expression, and then weight.
166  if (auto res = (*this)(lhs.sub(), rhs.sub()))
167  return res;
168  else
169  return weightset_t::compare(lhs.weight(), rhs.weight());
170  }
171 
172  private:
177  int res_;
178  };
179  }
180 }
typename expressionset_t::value_t expression_t
Definition: compare.hh:28
weightset_t_of< context_t > weightset_t
Definition: compare.hh:26
int operator()(const tuple_t &lhs, const tuple_t &rhs)
Binary operator().
Definition: compare.hh:109
void visit(const tuple_t &v, std::true_type) override
Definition: compare.hh:117
int res_
The current result.
Definition: compare.hh:177
return v
Definition: multiply.hh:361
Definition: a-star.hh:8
context_t_of< expressionset_t > context_t
Definition: compare.hh:24
return res
Definition: multiply.hh:398
int cmp_(const zero_t &, const zero_t &)
Definition: compare.hh:131
Functor to three-way comparison Values of ValueSets.
Definition: functional.hh:11
labelset_t_of< context_t > labelset_t
Definition: compare.hh:25
int operator()(const tuple_t &lhs)
Entry point: down_cast rhs_ and bounce to binary operator().
Definition: compare.hh:102
expression_t rhs_
The right-hand side expression with which the current node is compared.
Definition: compare.hh:175
decltype(std::declval< LabelSet >().one()) one_t
Definition: labelset.hh:36
typename super_t::template unary_t< Type > unary_t
Definition: compare.hh:33
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
Definition: traits.hh:67
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
Definition: traits.hh:66
int cmp_(const atom_t &lhs, const atom_t &rhs)
Definition: compare.hh:141
int operator()(expression_t lhs, expression_t rhs)
Whether lhs < rhs.
Definition: compare.hh:45
#define DEFINE(Type)
Definition: compare.hh:72
typename expressionset_t::const_visitor super_t
Definition: compare.hh:29
typename expressionset_t::template as_tupleset_t<> tupleset_t
Definition: compare.hh:100
int lexicographical_cmp(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp)
Lexicographical three-way comparison between two ranges.
Definition: algorithm.hh:124
int cmp_(const variadic_t< Type > &lhs, const variadic_t< Type > &rhs)
Definition: compare.hh:147
int cmp_(const weight_node_t< Type > &lhs, const weight_node_t< Type > &rhs)
Definition: compare.hh:163
A functor for three-way comparison between two expressions.
Definition: compare.hh:18
An inner node with multiple children.
Definition: expression.hh:118
An inner node implementing a weight.
Definition: expression.hh:255
typename super_t::template variadic_t< Type > variadic_t
Definition: compare.hh:35
int cmp_(const one_t &, const one_t &)
Definition: compare.hh:136
typename super_t::inner_t inner_t
Definition: compare.hh:31
typename super_t::tuple_t tuple_t
Definition: compare.hh:95
typename super_t::node_t node_t
Definition: compare.hh:30
ExpSet expressionset_t
Definition: compare.hh:22
typename super_t::template weight_node_t< Type > weight_node_t
Definition: compare.hh:37
int compare(const Lhs &lhs, const Rhs &rhs)
Comparison between lhs and rhs.
weight_t_of< context_t > weight_t
Definition: compare.hh:27
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
int cmp_(const unary_t< Type > &lhs, const unary_t< Type > &rhs)
Definition: compare.hh:157