Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
less-than.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_RAT_LESS_THAN_HH
2 # define VCSN_CORE_RAT_LESS_THAN_HH
3 
4 # include <vcsn/misc/cast.hh>
5 
6 # include <vcsn/core/rat/fwd.hh>
7 # include <vcsn/core/rat/size.hh>
8 
9 namespace vcsn
10 {
11 
12  namespace rat
13  {
14 
15  template <class RatExpSet>
16  class less_than
17  : public RatExpSet::const_visitor
18  {
19  public:
20  using ratexpset_t = RatExpSet;
25  using ratexp_t = typename ratexpset_t::value_t;
26  using super_t = typename ratexpset_t::const_visitor;
27  using node_t = typename super_t::node_t;
28  using inner_t = typename super_t::inner_t;
29  template <rat::exp::type_t Type>
30  using unary_t = typename super_t::template unary_t<Type>;
31  template <rat::exp::type_t Type>
32  using variadic_t = typename super_t::template variadic_t<Type>;
33  template <rat::exp::type_t Type>
34  using weight_node_t = typename super_t::template weight_node_t<Type>;
35 
37  bool
39  {
41  size_t lhss = sizer(lhs);
42  size_t rhss = sizer(rhs);
43 
44  if (lhss < rhss)
45  return true;
46  else if (lhss > rhss)
47  return false;
48  else if (lhs->type() < rhs->type())
49  return true;
50  else if (lhs->type() > rhs->type())
51  return false;
52  else
53  {
54  rhs_ = rhs;
55  lhs->accept(*this);
56  return res_;
57  }
58  }
59 
60  /*-----------------------------------------------------------.
61  | Unary visit functions than bounces to their binary peers. |
62  `-----------------------------------------------------------*/
63 
64 #define VISIT(Type) \
65  using Type ## _t = typename super_t::Type ## _t; \
66  virtual void \
67  visit(const Type ## _t& lhs) \
68  { \
69  res_ = less_than_(lhs, *down_pointer_cast<const Type ## _t>(rhs_)); \
70  }
71 
85 #undef VISIT
86 
87  /*-------------------------------------------------------.
88  | Binary functions that compare two nodes of same type. |
89  `-------------------------------------------------------*/
90 
91  bool less_than_(const zero_t&, const zero_t&)
92  {
93  return false;
94  }
95 
96  bool less_than_(const one_t&, const one_t&)
97  {
98  return false;
99  }
100 
101  bool less_than_(const atom_t& lhs, const atom_t& rhs)
102  {
103  return labelset_t::less_than(lhs.value(), rhs.value());
104  }
105 
106  template <rat::exp::type_t Type>
107  bool less_than_(const variadic_t<Type>& lhs, const variadic_t<Type>& rhs)
108  {
109  auto ls = lhs.size();
110  auto rs = rhs.size();
111  if (ls < rs)
112  return true;
113  else if (rs < ls)
114  return false;
115  else
116  for (size_t i = 0; i < ls; ++i)
117  if (ratexpset_t::less_than(lhs[i], rhs[i]))
118  return true;
119  else if (ratexpset_t::less_than(rhs[i], lhs[i]))
120  return false;
121  return false;
122  }
123 
124  template <rat::exp::type_t Type>
125  bool less_than_(const unary_t<Type>& lhs, const unary_t<Type>& rhs)
126  {
127  return ratexpset_t::less_than(lhs.sub(), rhs.sub());
128  }
129 
130  template <rat::exp::type_t Type>
132  {
133  // Lexicographic comparison on sub-expression, and then weight.
134  if (ratexpset_t::less_than(lhs.sub(), rhs.sub()))
135  return true;
136  else if (ratexpset_t::less_than(rhs.sub(), lhs.sub()))
137  return false;
138  else
139  return weightset_t::less_than(lhs.weight(), rhs.weight());
140  }
141 
142  private:
144  bool res_;
145  };
146  }
147 
148  template <class RatExpSet>
149  typename RatExpSet::value_t
150  less_than(const RatExpSet& rs, const typename RatExpSet::value_t& v)
151  {
152  return rs.less_than(v);
153  }
154 
155 }
156 
157 #endif // !VCSN_CORE_RAT_LESS_THAN_HH
weight_t_of< context_t > weight_t
Definition: less-than.hh:24
An inner node with multiple children.
Definition: fwd.hh:123
bool less_than_(const zero_t &, const zero_t &)
Definition: less-than.hh:91
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
typename super_t::template variadic_t< Type > variadic_t
Definition: less-than.hh:32
weightset_t_of< context_t > weightset_t
Definition: less-than.hh:23
labelset_t_of< context_t > labelset_t
Definition: less-than.hh:22
typename ratexpset_t::const_visitor super_t
Definition: less-than.hh:26
typename super_t::node_t node_t
Definition: less-than.hh:27
typename super_t::template weight_node_t< Type > weight_node_t
Definition: less-than.hh:34
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
typename ratexpset_t::value_t ratexp_t
Definition: less-than.hh:25
bool operator()(ratexp_t lhs, ratexp_t rhs)
Whether lhs < rhs.
Definition: less-than.hh:38
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
bool less_than_(const one_t &, const one_t &)
Definition: less-than.hh:96
typename super_t::atom_t atom_t
Definition: less-than.hh:72
#define VISIT(Type)
Definition: less-than.hh:64
bool less_than_(const unary_t< Type > &lhs, const unary_t< Type > &rhs)
Definition: less-than.hh:125
bool less_than_(const variadic_t< Type > &lhs, const variadic_t< Type > &rhs)
Definition: less-than.hh:107
typename super_t::zero_t zero_t
Definition: less-than.hh:84
context_t_of< ratexpset_t > context_t
Definition: less-than.hh:21
An inner node implementing a weight.
Definition: fwd.hh:145
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
RatExpSet ratexpset_t
Definition: less-than.hh:20
typename super_t::inner_t inner_t
Definition: less-than.hh:28
bool less_than_(const weight_node_t< Type > &lhs, const weight_node_t< Type > &rhs)
Definition: less-than.hh:131
RatExpSet::value_t less_than(const RatExpSet &rs, const typename RatExpSet::value_t &v)
Definition: less-than.hh:150
typename super_t::one_t one_t
Definition: less-than.hh:77
bool less_than_(const atom_t &lhs, const atom_t &rhs)
Definition: less-than.hh:101
typename super_t::template unary_t< Type > unary_t
Definition: less-than.hh:30