Awali
Another Weighted Automata library
z.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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  constexpr static bool is_special(value_t)
113  {
114  return false;
115  }
116 
117  static bool
118  is_zero(const value_t v)
119  {
120  return v == 0;
121  }
122 
123  static bool
124  is_one(const value_t v)
125  {
126  return v == 1;
127  }
128 
129  static bool
130  equals(const value_t l, const value_t r)
131  {
132  return l == r;
133  }
134 
136  static bool less_than(value_t lhs, value_t rhs)
137  {
138  return lhs < rhs;
139  }
140 
141  static constexpr bool is_commutative_semiring() { return true; }
142 
143  static constexpr bool show_one() { return false; }
145 
146  static value_t
148  {
149  return v;
150  }
151 
152  static size_t hash(value_t v)
153  {
154  return utils::hash_value(v);
155  }
156 
157  static value_t
159  {
160  return v;
161  }
162 
163  static value_t
165  {
166  // Conversion from unsigned to int.
167  return (value_t)v;
168  }
169 
170  static value_t
172  {
173  // Conversion from bool to int.
174  return v;
175  }
176 
177  static value_t
178  parse(const std::string & s, size_t& p) {
179  return internal::lr_parse_int(s,p);
180  }
181 
182  static value_t
183  conv(std::istream& stream)
184  {
185  int res;
186  if (stream >> res)
187  return res;
188  else
189  fail_reading(stream, sname() + ": invalid value");
190  }
191 
192  static std::ostream&
193  print(const value_t v, std::ostream& o,
194  const std::string& fmt = "text")
195  {
196  return o << v;
197  }
198 
199  std::ostream&
200  print_set(std::ostream& o, const std::string& format = "text") const
201  {
202  if (format == "latex")
203  o << "\\mathbb{Z}";
204  else if (format == "text")
205  o << "Z";
206  else
207  raise("invalid format: ", format);
208  return o;
209  }
210 
211  template<unsigned version = version::fsm_json>
212  value_t
214  const
215  {
216  switch (version) {
217  case 0:
218  case 1:
219  default:
220  return p->to_int();
221  }
222  }
223 
224 
225  template<unsigned version = version::fsm_json>
226  json::node_t*
228  const
229  {
230  switch (version) {
231  case 0:
232  throw parse_exception("[Z] Unsupported fsm-json version:"
233  + std::to_string(version));
234  case 1:
235  default:
236  return new json::int_t(v);
237  }
238  }
239 
240  template<unsigned version = version::fsm_json>
241  json::node_t*
242  to_json() const
243  {
244  switch (version) {
245  case 0:
246  throw parse_exception("[Z] Unsupported fsm-json version:"
247  + std::to_string(version));
248  case 1:
249  default:
250  json::object_t* obj = new json::object_t();
251  obj->push_back("semiring",new json::string_t("Z"));
252  return obj;
253  }
254  }
255 
256  };
257 
258  inline z join(const z&, const z&) { return {}; }
259  inline z join(const n&, const z&) { return {}; }
260  inline z join(const z&, const n&) { return {}; }
261  inline z join(const b&, const z&) { return {}; }
262  inline z join(const z&, const b&) { return {}; }
263 
264 }}//end of ns awali::stc
265 
266 #endif // !AWALI_WEIGHTSET_Z_HH
Definition: node.hh:483
Definition: node.hh:191
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:319
Definition: node.hh:365
object_t * push_back(std::string key, node_t *node)
Definition: node.hh:526
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:144
constexpr static bool is_special(value_t)
Definition: z.hh:112
static bool is_zero(const value_t v)
Definition: z.hh:118
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
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: z.hh:200
json::node_t * value_to_json(value_t v) const
Definition: z.hh:227
value_t value_from_json(json::node_t const *p) const
Definition: z.hh:213
value_t star(const value_t v) const
Definition: z.hh:104
static value_t conv(std::istream &stream)
Definition: z.hh:183
static value_t transpose(const value_t v)
Definition: z.hh:147
static constexpr bool show_one()
Definition: z.hh:143
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:136
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 std::ostream & print(const value_t v, std::ostream &o, const std::string &fmt="text")
Definition: z.hh:193
static constexpr bool is_commutative_semiring()
Definition: z.hh:141
static value_t parse(const std::string &s, size_t &p)
Definition: z.hh:178
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:124
static value_t conv(n, n::value_t v)
Definition: z.hh:164
int value_t
Definition: z.hh:56
json::node_t * to_json() const
Definition: z.hh:242
static size_t hash(value_t v)
Definition: z.hh:152
std::string vname(bool=true) const
Definition: z.hh:44
static bool equals(const value_t l, const value_t r)
Definition: z.hh:130
static value_t conv(self_type, value_t v)
Definition: z.hh:158
static value_t conv(b, b::value_t v)
Definition: z.hh:171
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:161
@ NON_STARRABLE
Definition: enums.hh:164
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
std::string to_string(identities i)
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:448
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
Exceptions thrown during parsing.
Definition: parse_exception.hh:26