Awali
Another Weighted Automata library
z.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_WEIGHTSET_Z_HH
18 # define AWALI_WEIGHTSET_Z_HH
19 
20 # include <ostream>
21 # include <string>
22 # include <sstream>
23 
24 #include <awali/sttc/misc/raise.hh>
25 #include <awali/common/enums.hh>
28 #include <awali/common/parse_exception.cc>
30 
31 namespace awali {
32  namespace sttc {
34  class z
35  {
36  public:
37  using self_type = z;
38 
39  static std::string sname()
40  {
41  return "z";
42  }
43 
44  std::string vname(bool = true) const
45  {
46  return sname();
47  }
48 
50  static z make(std::istream& is)
51  {
52  eat(is, sname());
53  return {};
54  }
55 
56  using value_t = int;
57 
58  static value_t
59  zero()
60  {
61  return 0;
62  }
63 
64  static value_t
65  one()
66  {
67  return 1;
68  }
69 
70  static value_t
71  add(const value_t l, const value_t r)
72  {
73  return l + r;
74  }
75 
76  static value_t
77  sub(const value_t l, const value_t r)
78  {
79  return l - r;
80  }
81 
82  static value_t
83  mul(const value_t l, const value_t r)
84  {
85  return l * r;
86  }
87 
88  static value_t
89  rdiv(const value_t l, const value_t r)
90  {
91  require(!is_zero(r), "div: division by zero");
92  require(!(l % r),
93  "Z: div: invalid division: ", l, '/', r);
94  return l / r;
95  }
96 
97  static value_t
98  ldiv(const value_t l, const value_t r)
99  {
100  return rdiv(r, l);
101  }
102 
103  value_t
104  star(const value_t v) const
105  {
106  if (is_zero(v))
107  return one();
108  else
109  raise("Z: star: invalid value: ", format(*this, v));
110  }
111 
112  value_t
113  plus(const value_t v) const
114  {
115  if (is_zero(v))
116  return zero();
117  else
118  raise("Z: star: invalid value: ", format(*this, v));
119  }
120 
121  constexpr static bool is_special(value_t)
122  {
123  return false;
124  }
125 
126  static bool
127  is_zero(const value_t v)
128  {
129  return v == 0;
130  }
131 
132  static bool
133  is_one(const value_t v)
134  {
135  return v == 1;
136  }
137 
138  static bool
139  equals(const value_t l, const value_t r)
140  {
141  return l == r;
142  }
143 
145  static bool less_than(value_t lhs, value_t rhs)
146  {
147  return lhs < rhs;
148  }
149 
150  static constexpr bool is_commutative_semiring() { return true; }
151 
152  static constexpr bool show_one() { return false; }
154 
155  static value_t
157  {
158  return v;
159  }
160 
161  static size_t hash(value_t v)
162  {
163  return utils::hash_value(v);
164  }
165 
166  static value_t
168  {
169  return v;
170  }
171 
172  static value_t
174  {
175  // Conversion from unsigned to int.
176  return (value_t)v;
177  }
178 
179  static value_t
181  {
182  // Conversion from bool to int.
183  return v;
184  }
185 
186  static value_t
187  parse(const std::string & s, size_t& p) {
188  return internal::lr_parse_int(s,p);
189  }
190 
191  static value_t
192  conv(std::istream& stream)
193  {
194  int res;
195  if (stream >> res)
196  return res;
197  else
198  fail_reading(stream, sname() + ": invalid value");
199  }
200 
201  static std::ostream&
202  print(const value_t v, std::ostream& o,
203  const std::string& = "text")
204  {
205  return o << v;
206  }
207 
208  std::ostream&
209  print_set(std::ostream& o, const std::string& format = "text") const
210  {
211  if (format == "latex")
212  o << "\\mathbb{Z}";
213  else if (format == "text")
214  o << "Z";
215  else
216  raise("invalid format: ", format);
217  return o;
218  }
219 
220  template<unsigned version = version::fsm_json>
221  value_t
223  const
224  {
225  version::check_fsmjson<version>();
226  switch (version) {
227  case 0: /* Never occurs due to above check. */
228  case 1:
229  default:
230  return p->to_int();
231  }
232  }
233 
234 
235  template<unsigned version = version::fsm_json>
236  json::node_t*
238  const
239  {
240  version::check_fsmjson<version>();
241  switch (version) {
242  case 0: /* Never occurs due to above check. */
243  case 1:
244  default:
245  return new json::int_t(v);
246  }
247  }
248 
249  template<unsigned version = version::fsm_json>
250  json::node_t*
251  to_json() const
252  {
253  version::check_fsmjson<version>();
254  switch (version) {
255  case 0: /* Never occurs due to above check. */
256  case 1:
257  default:
258  json::object_t* obj = new json::object_t();
259  obj->push_back("semiring",new json::string_t("Z"));
260  return obj;
261  }
262  }
263 
264  };
265 
266  inline z join(const z&, const z&) { return {}; }
267  inline z join(const n&, const z&) { return {}; }
268  inline z join(const z&, const n&) { return {}; }
269  inline z join(const b&, const z&) { return {}; }
270  inline z join(const z&, const b&) { return {}; }
271 
272 }}//end of ns awali::stc
273 
274 #endif // !AWALI_WEIGHTSET_Z_HH
Definition: node.hh:488
Definition: node.hh:193
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:321
Definition: node.hh:367
object_t * push_back(std::string key, node_t *node)
Definition: node.hh:529
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of Natural numbers.
Definition: n.hh:34
unsigned int value_t
Definition: n.hh:55
The semiring of floating Numbers.
Definition: r.hh:35
The semiring of Integers.
Definition: z.hh:35
static constexpr star_status_t star_status()
Definition: z.hh:153
constexpr static bool is_special(value_t)
Definition: z.hh:121
static bool is_zero(const value_t v)
Definition: z.hh:127
static z make(std::istream &is)
Build from the description in is.
Definition: z.hh:50
static value_t sub(const value_t l, const value_t r)
Definition: z.hh:77
value_t plus(const value_t v) const
Definition: z.hh:113
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: z.hh:209
json::node_t * value_to_json(value_t v) const
Definition: z.hh:237
value_t value_from_json(json::node_t const *p) const
Definition: z.hh:222
value_t star(const value_t v) const
Definition: z.hh:104
static value_t conv(std::istream &stream)
Definition: z.hh:192
static std::ostream & print(const value_t v, std::ostream &o, const std::string &="text")
Definition: z.hh:202
static value_t transpose(const value_t v)
Definition: z.hh:156
static constexpr bool show_one()
Definition: z.hh:152
static value_t add(const value_t l, const value_t r)
Definition: z.hh:71
static value_t ldiv(const value_t l, const value_t r)
Definition: z.hh:98
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: z.hh:145
static value_t zero()
Definition: z.hh:59
static value_t one()
Definition: z.hh:65
static value_t rdiv(const value_t l, const value_t r)
Definition: z.hh:89
static constexpr bool is_commutative_semiring()
Definition: z.hh:150
static value_t parse(const std::string &s, size_t &p)
Definition: z.hh:187
static value_t mul(const value_t l, const value_t r)
Definition: z.hh:83
static std::string sname()
Definition: z.hh:39
static bool is_one(const value_t v)
Definition: z.hh:133
static value_t conv(n, n::value_t v)
Definition: z.hh:173
int value_t
Definition: z.hh:56
json::node_t * to_json() const
Definition: z.hh:251
static size_t hash(value_t v)
Definition: z.hh:161
std::string vname(bool=true) const
Definition: z.hh:44
static bool equals(const value_t l, const value_t r)
Definition: z.hh:139
static value_t conv(self_type, value_t v)
Definition: z.hh:167
static value_t conv(b, b::value_t v)
Definition: z.hh:180
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
@ NON_STARRABLE
Definition: enums.hh:166
int lr_parse_int(std::string const &s, size_t &p, bool allow_empty=false, int value_if_empty=0)
Reads an int written in decimal left of position p in string s, by using as many characters as possib...
Definition: lr_parse_number.hh:239
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:449
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
std::size_t hash_value(const T &v)
Definition: hash.hh:76
Main namespace of Awali.
Definition: ato.hh:22