Awali
Another Weighted Automata library
equal_visit.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_EQUAL_VISIT_HH
18 # define AWALI_CORE_RAT_EQUAL_VISIT_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>
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 false;
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  | Unary visit functions than bounces to their binary peers. |
74  `-----------------------------------------------------------*/
75 
76 #define VISIT(Type) \
77  using Type ## _t = typename super_type::Type ## _t; \
78  virtual void \
79  visit(const Type ## _t& lhs) override \
80  { \
81  res_ = equal_visit_(lhs, *down_pointer_cast<const Type ## _t>(rhs_)); \
82  }
83 
99 #undef VISIT
100 
101  /*-------------------------------------------------------.
102  | Binary functions that compare two nodes of same type. |
103  `-------------------------------------------------------*/
104 
105  bool equal_visit_(const zero_t&, const zero_t&)
106  {
107  return true;
108  }
109 
110  bool equal_visit_(const one_t&, const one_t&)
111  {
112  return true;
113  }
114 
115  bool equal_visit_(const atom_t& lhs, const atom_t& rhs)
116  {
117  return labelset_t::equals(lhs.value(), rhs.value());
118  }
119 
120  template <rat::exp::type_t Type>
121  bool equal_visit_(const variadic_t<Type>& lhs, const variadic_t<Type>& rhs)
122  {
123  auto ls = lhs.size();
124  auto rs = rhs.size();
125  if (ls != rs)
126  return false;
127  for (size_t i = 0; i < ls; ++i)
128  if (! ratexpset_t::equals(lhs[i], rhs[i]))
129  return false;
130  return true;
131  }
132 
133  template <rat::exp::type_t Type>
134  bool equal_visit_(const unary_t<Type>& lhs, const unary_t<Type>& rhs)
135  {
136  return ratexpset_t::equals(lhs.sub(), rhs.sub());
137  }
138 
139  template <rat::exp::type_t Type>
141  {
142  if (! weightset_t::equals(lhs.weight(), rhs.weight()))
143  return false;
144  return ratexpset_t::equals(lhs.sub(), rhs.sub());
145  }
146 
147  private:
148  ratexp_t rhs_;
149  bool res_;
150  };
151  }
152 
153  template <class RatExpSet>
154  typename RatExpSet::ratexp_t
155  equals(const RatExpSet& rs, const typename RatExpSet::ratexp_t& v)
156  {
157  return rs.equals(v);
158  }
159 
160 }}//end of ns awali::stc
161 
162 #endif // !AWALI_CORE_RAT_EQUAL_VISIT_HH
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: equal_visit.hh:34
weightset_t_of< context_t > weightset_t
Definition: equal_visit.hh:39
typename super_type::zero_t zero_t
Definition: equal_visit.hh:98
bool equal_visit_(const zero_t &, const zero_t &)
Definition: equal_visit.hh:105
bool equal_visit_(const weight_node_t< Type > &lhs, const weight_node_t< Type > &rhs)
Definition: equal_visit.hh:140
context_t_of< ratexpset_t > context_t
Definition: equal_visit.hh:37
RatExpSet ratexpset_t
Definition: equal_visit.hh:36
typename ratexpset_t::const_visitor super_type
Definition: equal_visit.hh:42
typename super_type::one_t one_t
Definition: equal_visit.hh:89
bool equal_visit_(const unary_t< Type > &lhs, const unary_t< Type > &rhs)
Definition: equal_visit.hh:134
typename super_type::template variadic_t< Type > variadic_t
Definition: equal_visit.hh:48
labelset_t_of< context_t > labelset_t
Definition: equal_visit.hh:38
bool equal_visit_(const one_t &, const one_t &)
Definition: equal_visit.hh:110
typename super_type::template unary_t< Type > unary_t
Definition: equal_visit.hh:46
typename context_t::ratexp_t ratexp_t
Definition: equal_visit.hh:41
bool equal_visit_(const atom_t &lhs, const atom_t &rhs)
Definition: equal_visit.hh:115
bool operator()(ratexp_t lhs, ratexp_t rhs)
Whether lhs == rhs.
Definition: equal_visit.hh:54
weight_t_of< context_t > weight_t
Definition: equal_visit.hh:40
typename super_type::atom_t atom_t
Definition: equal_visit.hh:84
typename super_type::node_t node_t
Definition: equal_visit.hh:43
typename super_type::template weight_node_t< Type > weight_node_t
Definition: equal_visit.hh:50
bool equal_visit_(const variadic_t< Type > &lhs, const variadic_t< Type > &rhs)
Definition: equal_visit.hh:121
typename super_type::inner_t inner_t
Definition: equal_visit.hh:44
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: equal_visit.hh:76
RatExpSet::ratexp_t equals(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: equal_visit.hh:155
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