Awali
Another Weighted Automata library
less_than.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_CORE_RAT_LESS_THAN_HH
18 # define AWALI_CORE_RAT_LESS_THAN_HH
19 
20 #include <awali/sttc/misc/cast.hh>
21 
24 
25 namespace awali { namespace sttc
26 {
27 
28  namespace rat
29  {
30 
31  template <class RatExpSet>
32  class less_than
33  : public RatExpSet::const_visitor
34  {
35  public:
36  using ratexpset_t = RatExpSet;
41  using ratexp_t = typename context_t::ratexp_t;
42  using super_type = typename ratexpset_t::const_visitor;
43  using node_t = typename super_type::node_t;
44  using inner_t = typename super_type::inner_t;
45  template <rat::exp::type_t Type>
46  using unary_t = typename super_type::template unary_t<Type>;
47  template <rat::exp::type_t Type>
48  using variadic_t = typename super_type::template variadic_t<Type>;
49  template <rat::exp::type_t Type>
50  using weight_node_t = typename super_type::template weight_node_t<Type>;
51 
53  bool
55  {
57  size_t lhss = sizer(lhs);
58  size_t rhss = sizer(rhs);
59 
60  if (lhss < rhss)
61  return true;
62  else if (lhss > rhss)
63  return false;
64  else if (lhs->type() < rhs->type())
65  return true;
66  else if (lhs->type() > rhs->type())
67  return false;
68  else
69  {
70  rhs_ = rhs;
71  lhs->accept(*this);
72  return res_;
73  }
74  }
75 
76  /*-----------------------------------------------------------.
77  | Unary visit functions than bounces to their binary peers. |
78  `-----------------------------------------------------------*/
79 
80 #define VISIT(Type) \
81  using Type ## _t = typename super_type::Type ## _t; \
82  virtual void \
83  visit(const Type ## _t& lhs) override \
84  { \
85  res_ = less_than_(lhs, *down_pointer_cast<const Type ## _t>(rhs_)); \
86  }
87 
103 #undef VISIT
104 
105  /*-------------------------------------------------------.
106  | Binary functions that compare two nodes of same type. |
107  `-------------------------------------------------------*/
108 
109  bool less_than_(const zero_t&, const zero_t&)
110  {
111  return false;
112  }
113 
114  bool less_than_(const one_t&, const one_t&)
115  {
116  return false;
117  }
118 
119  bool less_than_(const atom_t& lhs, const atom_t& rhs)
120  {
121  return labelset_t::less_than(lhs.value(), rhs.value());
122  }
123 
124  template <rat::exp::type_t Type>
125  bool less_than_(const variadic_t<Type>& lhs, const variadic_t<Type>& rhs)
126  {
127  auto ls = lhs.size();
128  auto rs = rhs.size();
129  if (ls < rs)
130  return true;
131  else if (rs < ls)
132  return false;
133  else
134  for (size_t i = 0; i < ls; ++i)
135  if (ratexpset_t::less_than(lhs[i], rhs[i]))
136  return true;
137  else if (ratexpset_t::less_than(rhs[i], lhs[i]))
138  return false;
139  return false;
140  }
141 
142  template <rat::exp::type_t Type>
143  bool less_than_(const unary_t<Type>& lhs, const unary_t<Type>& rhs)
144  {
145  return ratexpset_t::less_than(lhs.sub(), rhs.sub());
146  }
147 
148  template <rat::exp::type_t Type>
150  {
151  // Lexicographic comparison on sub-expression, and then weight.
152  if (ratexpset_t::less_than(lhs.sub(), rhs.sub()))
153  return true;
154  else if (ratexpset_t::less_than(rhs.sub(), lhs.sub()))
155  return false;
156  else
157  return weightset_t::less_than(lhs.weight(), rhs.weight());
158  }
159 
160  private:
161  ratexp_t rhs_;
162  bool res_;
163  };
164  }
165 
166  template <class RatExpSet>
167  typename RatExpSet::ratexp_t
168  less_than(const RatExpSet& rs, const typename RatExpSet::ratexp_t& v)
169  {
170  return rs.less_than(v);
171  }
172 
173 }}//end of ns awali::stc
174 
175 #endif // !AWALI_CORE_RAT_LESS_THAN_HH
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: less_than.hh:34
typename super_type::atom_t atom_t
Definition: less_than.hh:88
labelset_t_of< context_t > labelset_t
Definition: less_than.hh:38
typename super_type::template variadic_t< Type > variadic_t
Definition: less_than.hh:48
bool operator()(ratexp_t lhs, ratexp_t rhs)
Whether lhs < rhs.
Definition: less_than.hh:54
typename super_type::template weight_node_t< Type > weight_node_t
Definition: less_than.hh:50
typename super_type::template unary_t< Type > unary_t
Definition: less_than.hh:46
typename ratexpset_t::const_visitor super_type
Definition: less_than.hh:42
weightset_t_of< context_t > weightset_t
Definition: less_than.hh:39
RatExpSet ratexpset_t
Definition: less_than.hh:36
bool less_than_(const zero_t &, const zero_t &)
Definition: less_than.hh:109
typename context_t::ratexp_t ratexp_t
Definition: less_than.hh:41
bool less_than_(const weight_node_t< Type > &lhs, const weight_node_t< Type > &rhs)
Definition: less_than.hh:149
typename super_type::zero_t zero_t
Definition: less_than.hh:102
context_t_of< ratexpset_t > context_t
Definition: less_than.hh:37
typename super_type::one_t one_t
Definition: less_than.hh:93
bool less_than_(const one_t &, const one_t &)
Definition: less_than.hh:114
bool less_than_(const variadic_t< Type > &lhs, const variadic_t< Type > &rhs)
Definition: less_than.hh:125
bool less_than_(const unary_t< Type > &lhs, const unary_t< Type > &rhs)
Definition: less_than.hh:143
bool less_than_(const atom_t &lhs, const atom_t &rhs)
Definition: less_than.hh:119
weight_t_of< context_t > weight_t
Definition: less_than.hh:40
typename super_type::inner_t inner_t
Definition: less_than.hh:44
typename super_type::node_t node_t
Definition: less_than.hh:43
Definition: size.hh:31
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
#define VISIT(Type)
Definition: less_than.hh:80
RatExpSet::ratexp_t less_than(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: less_than.hh:168
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22