Awali
Another Weighted Automata library
z_old.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 VAUCR_WEIGHTSET_Z_HH
18 # define VAUCR_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>
29 
30 namespace awali { namespace sttc {
31 
32  namespace internal
33  {
34  class z_impl
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 bool to int.
167  return v;
168  }
169 
170  value_t
171  parse(const std::string & s, size_t& p) const {
172  size_t i=p;
173  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
174  ;
175  if(i==p)
176  throw parse_exception("Wrong Z value");
177  if(i>0 && (s[i-1]=='-' || s[i-1]=='+'))
178  --i;
179  std::istringstream st(s.substr(i, p-i));
180  value_t x;
181  st >> x;
182  p=i;
183  return x;
184  }
185 
186  static value_t
187  conv(std::istream& stream)
188  {
189  int res;
190  if (stream >> res)
191  return res;
192  else
193  sttc::fail_reading(stream, sname() + ": invalid value");
194  }
195 
196  static std::ostream&
197  print(const value_t v, std::ostream& o,
198  const std::string& fmt = "text")
199  {
200  if(fmt == "json")
201  return o<< '"' << v << '"';
202  return o << v;
203  }
204 
205  std::ostream&
206  print_set(std::ostream& o, const std::string& format = "text") const
207  {
208  if (format == "latex")
209  o << "\\mathbb{Z}";
210  else if (format == "text")
211  o << "Z";
212  else
213  raise("invalid format: ", format);
214  return o;
215  }
216 
217  std::ostream&
218  js_print(std::ostream& o) const
219  {
220  o << "{\"Semiring\":\"Z\"}";
221  return o;
222  }
223 
224  value_t
225  js_parse(std::istream& i) const
226  {
227  char c;
228  i >> c;
229  if(c != '"')
230  throw std::runtime_error("json parser Z");
231  value_t r;
232  i >> r;
233  i >> c;
234  if(c != '"')
235  throw std::runtime_error("json parser Z");
236  return r;
237  }
238  };
239  }
240 
242 
245 
246 
247 }}//end of ns awali::stc
248 
249 #endif // !VAUCR_WEIGHTSET_Z_HH
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of complex numbers.
Definition: c.hh:44
Definition: z_old.hh:35
std::string vname(bool=true) const
Definition: z_old.hh:44
value_t star(const value_t v) const
Definition: z_old.hh:104
static z make(std::istream &is)
Build from the description in is.
Definition: z_old.hh:50
static value_t conv(self_type, value_t v)
Definition: z_old.hh:158
static constexpr bool show_one()
Definition: z_old.hh:143
static value_t conv(std::istream &stream)
Definition: z_old.hh:187
static constexpr bool is_commutative_semiring()
Definition: z_old.hh:141
std::ostream & js_print(std::ostream &o) const
Definition: z_old.hh:218
static value_t ldiv(const value_t l, const value_t r)
Definition: z_old.hh:98
static value_t zero()
Definition: z_old.hh:59
int value_t
Definition: z_old.hh:56
static value_t conv(b, b::value_t v)
Definition: z_old.hh:164
static value_t mul(const value_t l, const value_t r)
Definition: z_old.hh:83
static bool is_zero(const value_t v)
Definition: z_old.hh:118
static value_t sub(const value_t l, const value_t r)
Definition: z_old.hh:77
value_t parse(const std::string &s, size_t &p) const
Definition: z_old.hh:171
static size_t hash(value_t v)
Definition: z_old.hh:152
static value_t add(const value_t l, const value_t r)
Definition: z_old.hh:71
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: z_old.hh:206
static value_t rdiv(const value_t l, const value_t r)
Definition: z_old.hh:89
static bool equals(const value_t l, const value_t r)
Definition: z_old.hh:130
static constexpr star_status_t star_status()
Definition: z_old.hh:144
static value_t transpose(const value_t v)
Definition: z_old.hh:147
static std::string sname()
Definition: z_old.hh:39
constexpr static bool is_special(value_t)
Definition: z_old.hh:112
static std::ostream & print(const value_t v, std::ostream &o, const std::string &fmt="text")
Definition: z_old.hh:197
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: z_old.hh:136
static bool is_one(const value_t v)
Definition: z_old.hh:124
value_t js_parse(std::istream &i) const
Definition: z_old.hh:225
static value_t one()
Definition: z_old.hh:65
The semiring of floating Numbers.
Definition: r.hh:35
The semiring of Integers.
Definition: z.hh:35
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
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
VAUCR_WEIGHTS_BINARY(z, z, z)
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