Awali
Another Weighted Automata library
n.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_N_HH
18 #define AWALI_WEIGHTSET_N_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 {
31  namespace sttc {
33  class n
34  {
35  public:
36  using self_type = n;
37 
38  static std::string sname()
39  {
40  return "n";
41  }
42 
43  std::string vname(bool = true) const
44  {
45  return sname();
46  }
47 
49  static n make(std::istream& is)
50  {
51  eat(is, sname());
52  return {};
53  }
54 
55  using value_t = unsigned int;
56 
57  constexpr static value_t
58  zero()
59  {
60  return 0u;
61  }
62 
63  constexpr static value_t
64  one()
65  {
66  return 1u;
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  require(l>=r, "N: sub: invalid subtraction: ", l , '-', r);
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  "N: 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("N: 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("N: 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 == 0u;
130  }
131 
132  static bool
133  is_one(const value_t v)
134  {
135  return v == 1u;
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 bool to int.
176  return v;
177  }
178 
179  static value_t
180  parse(const std::string & s, size_t& p) {
181  return internal::lr_parse_int(s,p);
182  }
183 
184  static value_t
185  conv(std::istream& stream)
186  {
187  value_t res;
188  if (stream >> res)
189  return res;
190  else
191  sttc::fail_reading(stream, sname() + ": invalid value");
192  }
193 
194  static std::ostream&
195  print(const value_t v, std::ostream& o,
196  const std::string& /*fmt*/ = "text")
197  {
198  return o << v;
199  }
200 
201  static std::ostream&
202  print_set(std::ostream& o, const std::string& format = "text")
203  {
204  if (format == "latex")
205  o << "\\mathbb{N}";
206  else if (format == "text")
207  o << "N";
208  else
209  raise("invalid format: ", format);
210  return o;
211  }
212 
213 
214 
215  template<unsigned version = version::fsm_json>
216  value_t
217  static value_from_json(json::node_t const* p)
218  {
219  version::check_fsmjson<version>();
220  switch (version) {
221  case 0: /* Never occurs due to above check. */
222  case 1:
223  default:
224  int a=p->to_int();
225  if(a<0)
226  throw parse_exception("[N] Negative integers are not allowed.");
227  return a;
228  }
229  }
230 
231 
232  template<unsigned version = version::fsm_json>
233  json::node_t*
235  {
236  version::check_fsmjson<version>();
237  switch (version) {
238  case 0: /* Never occurs due to above check. */
239  case 1:
240  default:
241  return new json::int_t(v);
242  }
243  }
244 
245  template<unsigned version = version::fsm_json>
247  to_json() const
248  {
249  version::check_fsmjson<version>();
250  switch (version) {
251  case 0: /* Never occurs due to above check. */
252  case 1:
253  default:
254  json::object_t* obj = new json::object_t();
255  obj->push_back("semiring",new json::string_t("N"));
256  return obj;
257  }
258  }
259  };
260 
261  inline n join(const n&, const n&) { return {}; }
262 
263  inline n join(const b&, const n&) { return {}; }
264  inline n join(const n&, const b&) { return {}; }
265 
266 }}//end of ns awali::stc
267 
268 #endif // !AWALI_WEIGHTSET_N_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
constexpr static value_t zero()
Definition: n.hh:58
static constexpr bool is_commutative_semiring()
Definition: n.hh:150
static value_t value_from_json(json::node_t const *p)
Definition: n.hh:217
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: n.hh:145
static value_t mul(const value_t l, const value_t r)
Definition: n.hh:83
static constexpr star_status_t star_status()
Definition: n.hh:153
static value_t conv(std::istream &stream)
Definition: n.hh:185
constexpr static bool is_special(value_t)
Definition: n.hh:121
static value_t parse(const std::string &s, size_t &p)
Definition: n.hh:180
value_t plus(const value_t v) const
Definition: n.hh:113
static value_t conv(self_type, value_t v)
Definition: n.hh:167
static std::string sname()
Definition: n.hh:38
unsigned int value_t
Definition: n.hh:55
static bool equals(const value_t l, const value_t r)
Definition: n.hh:139
static value_t rdiv(const value_t l, const value_t r)
Definition: n.hh:89
static value_t conv(b, b::value_t v)
Definition: n.hh:173
value_t star(const value_t v) const
Definition: n.hh:104
static value_t add(const value_t l, const value_t r)
Definition: n.hh:70
static value_t ldiv(const value_t l, const value_t r)
Definition: n.hh:98
static bool is_zero(const value_t v)
Definition: n.hh:127
static constexpr bool show_one()
Definition: n.hh:152
json::node_t * value_to_json(value_t v) const
Definition: n.hh:234
constexpr static value_t one()
Definition: n.hh:64
static std::ostream & print_set(std::ostream &o, const std::string &format="text")
Definition: n.hh:202
static std::ostream & print(const value_t v, std::ostream &o, const std::string &="text")
Definition: n.hh:195
json::object_t * to_json() const
Definition: n.hh:247
std::string vname(bool=true) const
Definition: n.hh:43
static size_t hash(value_t v)
Definition: n.hh:161
static bool is_one(const value_t v)
Definition: n.hh:133
static value_t sub(const value_t l, const value_t r)
Definition: n.hh:76
static value_t transpose(const value_t v)
Definition: n.hh:156
static n make(std::istream &is)
Build from the description in is.
Definition: n.hh:49
The semiring of floating Numbers.
Definition: r.hh:35
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
Exceptions thrown during parsing.
Definition: parse_exception.hh:26