Vcsn  2.4
Be Rational
less.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/range/algorithm/lexicographical_compare.hpp>
4 
5 #include <vcsn/core/rat/fwd.hh>
6 #include <vcsn/core/rat/size.hh>
8 #include <vcsn/misc/cast.hh>
9 #include <vcsn/misc/functional.hh> // vcsn::less
10 
11 namespace vcsn
12 {
13 
14  namespace rat
15  {
16 
20  template <typename ExpSet>
21  class less
22  : public ExpSet::const_visitor
23  {
24  public:
25  using expressionset_t = ExpSet;
30  using expression_t = typename expressionset_t::value_t;
31  using super_t = typename expressionset_t::const_visitor;
32  using node_t = typename super_t::node_t;
33  using inner_t = typename super_t::inner_t;
34  template <rat::exp::type_t Type>
35  using unary_t = typename super_t::template unary_t<Type>;
36  template <rat::exp::type_t Type>
37  using variadic_t = typename super_t::template variadic_t<Type>;
38  template <rat::exp::type_t Type>
39  using weight_node_t = typename super_t::template weight_node_t<Type>;
40 
48  {
49  if (lhs == rhs)
50  return false;
51  else
52  {
53  size_t lhss = size<ExpSet>(lhs);
54  size_t rhss = size<ExpSet>(rhs);
55 
56  if (lhss < rhss)
57  return true;
58  else if (lhss > rhss)
59  return false;
60  else if (lhs->type() < rhs->type())
61  return true;
62  else if (lhs->type() > rhs->type())
63  return false;
64  else
65  {
66  rhs_ = rhs;
67  lhs->accept(*this);
68  return res_;
69  }
70  }
71  }
72 
73  private:
74  /*-----------------------------------------------------------.
75  | Unary visit functions than bounces to their binary peers. |
76  `-----------------------------------------------------------*/
77 
78 #define DEFINE(Type) \
79  VCSN_RAT_VISIT(Type, lhs) \
80  { \
81  res_ = less_(lhs, *down_pointer_cast<const Type ## _t>(rhs_)); \
82  }
83 
99 #undef DEFINE
100 
101  using tuple_t = typename super_t::tuple_t;
102 
103  template <typename = void>
104  struct visit_tuple
105  {
106  using tupleset_t = typename expressionset_t::template as_tupleset_t<>;
108  bool operator()(const tuple_t& lhs)
109  {
110  return operator()(lhs,
111  *down_pointer_cast<const tuple_t>(visitor_.rhs_));
112  }
113 
115  bool operator()(const tuple_t& lhs, const tuple_t& rhs)
116  {
117  return tupleset_t::less(lhs.sub(), rhs.sub());
118  }
119 
120  const less& visitor_;
121  };
122 
123  void visit(const tuple_t& v, std::true_type) override
124  {
125  detail::static_if<context_t::is_lat>
126  ([this](auto&& v)
127  {
128  res_ = visit_tuple<decltype(v)>{*this}(v);
129  })
130  (v);
131  }
132 
133  /*-------------------------------------------------------.
134  | Binary functions that compare two nodes of same type. |
135  `-------------------------------------------------------*/
136 
137  bool less_(const zero_t&, const zero_t&)
138  {
139  return false;
140  }
141 
142  bool less_(const one_t&, const one_t&)
143  {
144  return false;
145  }
146 
147  bool less_(const atom_t& lhs, const atom_t& rhs)
148  {
149  return labelset_t::less(lhs.value(), rhs.value());
150  }
151 
152  template <rat::exp::type_t Type>
153  bool less_(const variadic_t<Type>& lhs, const variadic_t<Type>& rhs)
154  {
155  using boost::range::lexicographical_compare;
156  auto ls = lhs.size();
157  auto rs = rhs.size();
158  if (ls < rs)
159  return true;
160  else if (rs < ls)
161  return false;
162  else
163  return lexicographical_compare(lhs, rhs,
165  }
166 
167  template <rat::exp::type_t Type>
168  bool less_(const unary_t<Type>& lhs, const unary_t<Type>& rhs)
169  {
170  return expressionset_t::less(lhs.sub(), rhs.sub());
171  }
172 
173  template <rat::exp::type_t Type>
174  bool less_(const weight_node_t<Type>& lhs, const weight_node_t<Type>& rhs)
175  {
176  // Lexicographic comparison on sub-expression, and then weight.
177  if (expressionset_t::less(lhs.sub(), rhs.sub()))
178  return true;
179  else if (expressionset_t::less(rhs.sub(), lhs.sub()))
180  return false;
181  else
182  return weightset_t::less(lhs.weight(), rhs.weight());
183  }
184 
185  private:
190  bool res_;
191  };
192  }
193 }
labelset_t_of< context_t > labelset_t
Definition: less.hh:27
bool less_(const atom_t &lhs, const atom_t &rhs)
Definition: less.hh:147
bool operator()(const tuple_t &lhs)
Entry point: down_cast rhs_ and bounce to binary operator().
Definition: less.hh:108
typename super_t::template unary_t< Type > unary_t
Definition: less.hh:35
auto rs
Definition: lift.hh:152
typename super_t::template variadic_t< Type > variadic_t
Definition: less.hh:37
weight_t_of< context_t > weight_t
Definition: less.hh:29
const less & visitor_
Definition: less.hh:120
Functor to compare Values of ValueSets.
Definition: functional.hh:76
An inner node implementing a weight.
Definition: expression.hh:255
typename super_t::inner_t inner_t
Definition: less.hh:33
#define DEFINE(Type)
Definition: less.hh:78
bool less_(const weight_node_t< Type > &lhs, const weight_node_t< Type > &rhs)
Definition: less.hh:174
context_t_of< expressionset_t > context_t
Definition: less.hh:26
typename expressionset_t::value_t expression_t
Definition: less.hh:30
decltype(std::declval< LabelSet >().one()) one_t
Definition: labelset.hh:36
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
typename expressionset_t::template as_tupleset_t<> tupleset_t
Definition: less.hh:106
typename super_t::template weight_node_t< Type > weight_node_t
Definition: less.hh:39
Definition: a-star.hh:8
bool operator()(expression_t lhs, expression_t rhs)
Whether lhs < rhs.
Definition: less.hh:47
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
bool less_(const one_t &, const one_t &)
Definition: less.hh:142
bool less_(const unary_t< Type > &lhs, const unary_t< Type > &rhs)
Definition: less.hh:168
bool less_(const variadic_t< Type > &lhs, const variadic_t< Type > &rhs)
Definition: less.hh:153
typename super_t::node_t node_t
Definition: less.hh:32
An inner node with multiple children.
Definition: expression.hh:118
weightset_t_of< context_t > weightset_t
Definition: less.hh:28
return v
Definition: multiply.hh:361
bool res_
The current result.
Definition: less.hh:190
expression_t rhs_
The right-hand side expression with which the current node is compared.
Definition: less.hh:188
bool less_(const zero_t &, const zero_t &)
Definition: less.hh:137
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
typename expressionset_t::const_visitor super_t
Definition: less.hh:31
A functor to check whether one rational expression is (strictly) less than another one...
Definition: less.hh:21
ExpSet expressionset_t
Definition: less.hh:25
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename super_t::tuple_t tuple_t
Definition: less.hh:101
bool operator()(const tuple_t &lhs, const tuple_t &rhs)
Binary operator().
Definition: less.hh:115
void visit(const tuple_t &v, std::true_type) override
Definition: less.hh:123