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>
29 
30 namespace awali {
31  namespace sttc {
33  class z
34  {
35  public:
36  using self_type = z;
37 
38  static std::string sname()
39  {
40  return "z";
41  }
42 
43  std::string vname(bool = true) const
44  {
45  return sname();
46  }
47 
49  static z make(std::istream& is)
50  {
51  eat(is, sname());
52  return {};
53  }
54 
55  using value_t = int;
56 
57  static value_t
58  zero()
59  {
60  return 0;
61  }
62 
63  static value_t
64  one()
65  {
66  return 1;
67  }
68 
69  static value_t
70  add(const value_t l, const value_t r)
71  {
72  return l + r;
73  }
74 
75  static value_t
76  sub(const value_t l, const value_t r)
77  {
78  return l - r;
79  }
80 
81  static value_t
82  mul(const value_t l, const value_t r)
83  {
84  return l * r;
85  }
86 
87  static value_t
88  rdiv(const value_t l, const value_t r)
89  {
90  require(!is_zero(r), "div: division by zero");
91  require(!(l % r),
92  "Z: div: invalid division: ", l, '/', r);
93  return l / r;
94  }
95 
96  static value_t
97  ldiv(const value_t l, const value_t r)
98  {
99  return rdiv(r, l);
100  }
101 
102  value_t
103  star(const value_t v) const
104  {
105  if (is_zero(v))
106  return one();
107  else
108  raise("Z: star: invalid value: ", format(*this, v));
109  }
110 
111  constexpr static bool is_special(value_t)
112  {
113  return false;
114  }
115 
116  static bool
117  is_zero(const value_t v)
118  {
119  return v == 0;
120  }
121 
122  static bool
123  is_one(const value_t v)
124  {
125  return v == 1;
126  }
127 
128  static bool
129  equals(const value_t l, const value_t r)
130  {
131  return l == r;
132  }
133 
135  static bool less_than(value_t lhs, value_t rhs)
136  {
137  return lhs < rhs;
138  }
139 
140  static constexpr bool is_commutative_semiring() { return true; }
141 
142  static constexpr bool show_one() { return false; }
144 
145  static value_t
147  {
148  return v;
149  }
150 
151  static size_t hash(value_t v)
152  {
153  return utils::hash_value(v);
154  }
155 
156  static value_t
158  {
159  return v;
160  }
161 
162  static value_t
164  {
165  // Conversion from unsigned to int.
166  return (value_t)v;
167  }
168 
169  static value_t
171  {
172  // Conversion from bool to int.
173  return v;
174  }
175 
176  static value_t
177  parse(const std::string & s, size_t& p) {
178  size_t i=p;
179  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
180  ;
181  if(i==p)
182  throw parse_exception(p,"Wrong Z value");
183  if(i>0 && (s[i-1]=='-' || s[i-1]=='+'))
184  --i;
185  std::istringstream st(s.substr(i, p-i));
186  value_t x;
187  st >> x;
188  p=i;
189  return x;
190  }
191 
192  static value_t
193  conv(std::istream& stream)
194  {
195  int res;
196  if (stream >> res)
197  return res;
198  else
199  fail_reading(stream, sname() + ": invalid value");
200  }
201 
202  static std::ostream&
203  print(const value_t v, std::ostream& o,
204  const std::string& fmt = "text")
205  {
206  return o << v;
207  }
208 
209  std::ostream&
210  print_set(std::ostream& o, const std::string& format = "text") const
211  {
212  if (format == "latex")
213  o << "\\mathbb{Z}";
214  else if (format == "text")
215  o << "Z";
216  else
217  raise("invalid format: ", format);
218  return o;
219  }
220 
221  template<unsigned version = version::fsm_json>
222  value_t
224  const
225  {
226  switch (version) {
227  case 0:
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  switch (version) {
241  case 0:
242  throw parse_exception("[Z] Unsupported fsm-json version:"
243  + std::to_string(version));
244  case 1:
245  default:
246  return new json::int_t(v);
247  }
248  }
249 
250  template<unsigned version = version::fsm_json>
251  json::node_t*
252  to_json() const
253  {
254  switch (version) {
255  case 0:
256  throw parse_exception("[Z] Unsupported fsm-json version:"
257  + std::to_string(version));
258  case 1:
259  default:
260  json::object_t* obj = new json::object_t();
261  obj->push_back("semiring",new json::string_t("Z"));
262  return obj;
263  }
264  }
265 
266  };
267 
268  inline z join(const z&, const z&) { return {}; }
269  inline z join(const n&, const z&) { return {}; }
270  inline z join(const z&, const n&) { return {}; }
271  inline z join(const b&, const z&) { return {}; }
272  inline z join(const z&, const b&) { return {}; }
273 
274 }}//end of ns awali::stc
275 
276 #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:33
unsigned int value_t
Definition: n.hh:54
The semiring of floating Numbers.
Definition: r.hh:34
The semiring of Integers.
Definition: z.hh:34
static constexpr star_status_t star_status()
Definition: z.hh:143
constexpr static bool is_special(value_t)
Definition: z.hh:111
static bool is_zero(const value_t v)
Definition: z.hh:117
static z make(std::istream &is)
Build from the description in is.
Definition: z.hh:49
static value_t sub(const value_t l, const value_t r)
Definition: z.hh:76
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: z.hh:210
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:223
value_t star(const value_t v) const
Definition: z.hh:103
static value_t conv(std::istream &stream)
Definition: z.hh:193
static value_t transpose(const value_t v)
Definition: z.hh:146
static constexpr bool show_one()
Definition: z.hh:142
static value_t add(const value_t l, const value_t r)
Definition: z.hh:70
static value_t ldiv(const value_t l, const value_t r)
Definition: z.hh:97
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: z.hh:135
static value_t zero()
Definition: z.hh:58
static value_t one()
Definition: z.hh:64
static value_t rdiv(const value_t l, const value_t r)
Definition: z.hh:88
static std::ostream & print(const value_t v, std::ostream &o, const std::string &fmt="text")
Definition: z.hh:203
static constexpr bool is_commutative_semiring()
Definition: z.hh:140
static value_t parse(const std::string &s, size_t &p)
Definition: z.hh:177
static value_t mul(const value_t l, const value_t r)
Definition: z.hh:82
static std::string sname()
Definition: z.hh:38
static bool is_one(const value_t v)
Definition: z.hh:123
static value_t conv(n, n::value_t v)
Definition: z.hh:163
int value_t
Definition: z.hh:55
json::node_t * to_json() const
Definition: z.hh:252
static size_t hash(value_t v)
Definition: z.hh:151
std::string vname(bool=true) const
Definition: z.hh:43
static bool equals(const value_t l, const value_t r)
Definition: z.hh:129
static value_t conv(self_type, value_t v)
Definition: z.hh:157
static value_t conv(b, b::value_t v)
Definition: z.hh:170
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
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