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-2022 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 
101 #undef VISIT
102 
103  /*-------------------------------------------------------.
104  | Binary functions that compare two nodes of same type. |
105  `-------------------------------------------------------*/
106 
107  bool less_than_(const zero_t&, const zero_t&)
108  {
109  return false;
110  }
111 
112  bool less_than_(const one_t&, const one_t&)
113  {
114  return false;
115  }
116 
117  bool less_than_(const atom_t& lhs, const atom_t& rhs)
118  {
119  return labelset_t::less_than(lhs.value(), rhs.value());
120  }
121 
122  template <rat::exp::type_t Type>
123  bool less_than_(const variadic_t<Type>& lhs, const variadic_t<Type>& rhs)
124  {
125  auto ls = lhs.size();
126  auto rs = rhs.size();
127  if (ls < rs)
128  return true;
129  else if (rs < ls)
130  return false;
131  else
132  for (size_t i = 0; i < ls; ++i)
133  if (ratexpset_t::less_than(lhs[i], rhs[i]))
134  return true;
135  else if (ratexpset_t::less_than(rhs[i], lhs[i]))
136  return false;
137  return false;
138  }
139 
140  template <rat::exp::type_t Type>
141  bool less_than_(const unary_t<Type>& lhs, const unary_t<Type>& rhs)
142  {
143  return ratexpset_t::less_than(lhs.sub(), rhs.sub());
144  }
145 
146  template <rat::exp::type_t Type>
148  {
149  // Lexicographic comparison on sub-expression, and then weight.
150  if (ratexpset_t::less_than(lhs.sub(), rhs.sub()))
151  return true;
152  else if (ratexpset_t::less_than(rhs.sub(), lhs.sub()))
153  return false;
154  else
155  return weightset_t::less_than(lhs.weight(), rhs.weight());
156  }
157 
158  private:
159  ratexp_t rhs_;
160  bool res_;
161  };
162  }
163 
164  template <class RatExpSet>
165  typename RatExpSet::ratexp_t
166  less_than(const RatExpSet& rs, const typename RatExpSet::ratexp_t& v)
167  {
168  return rs.less_than(v);
169  }
170 
171 }}//end of ns awali::stc
172 
173 #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:107
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:147
typename super_type::zero_t zero_t
Definition: less_than.hh:100
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:112
bool less_than_(const variadic_t< Type > &lhs, const variadic_t< Type > &rhs)
Definition: less_than.hh:123
bool less_than_(const unary_t< Type > &lhs, const unary_t< Type > &rhs)
Definition: less_than.hh:141
bool less_than_(const atom_t &lhs, const atom_t &rhs)
Definition: less_than.hh:117
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:166
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