Awali
Another Weighted Automata library
n.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_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>
28 
29 namespace awali {
30  namespace sttc {
32  class n
33  {
34  public:
35  using self_type = n;
36 
37  static std::string sname()
38  {
39  return "n";
40  }
41 
42  std::string vname(bool = true) const
43  {
44  return sname();
45  }
46 
48  static n make(std::istream& is)
49  {
50  eat(is, sname());
51  return {};
52  }
53 
54  using value_t = unsigned int;
55 
56  constexpr static value_t
57  zero()
58  {
59  return 0u;
60  }
61 
62  constexpr static value_t
63  one()
64  {
65  return 1u;
66  }
67 
68  static value_t
69  add(const value_t l, const value_t r)
70  {
71  return l + r;
72  }
73 
74  static value_t
75  sub(const value_t l, const value_t r)
76  {
77  require(l>=r, "N: sub: invalid subtraction: ", l , '-', r);
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  "N: 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("N: 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 == 0u;
120  }
121 
122  static bool
123  is_one(const value_t v)
124  {
125  return v == 1u;
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 bool to int.
166  return v;
167  }
168 
169  value_t
170  parse(const std::string & s, size_t& p) const {
171  size_t i=p;
172  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
173  ;
174  if(i==p)
175  throw parse_exception("Wrong N value");
176  std::istringstream st(s.substr(i, p-i));
177  value_t x;
178  st >> x;
179  p=i;
180  return x;
181  }
182 
183  static value_t
184  conv(std::istream& stream)
185  {
186  value_t res;
187  if (stream >> res)
188  return res;
189  else
190  sttc::fail_reading(stream, sname() + ": invalid value");
191  }
192 
193  static std::ostream&
194  print(const value_t v, std::ostream& o,
195  const std::string& fmt = "text")
196  {
197  return o << v;
198  }
199 
200  std::ostream&
201  print_set(std::ostream& o, const std::string& format = "text") const
202  {
203  if (format == "latex")
204  o << "\\mathbb{N}";
205  else if (format == "text")
206  o << "N";
207  else
208  raise("invalid format: ", format);
209  return o;
210  }
211 
212 
213 
214  template<unsigned version = version::fsm_json>
215  value_t
216  static value_from_json(json::node_t const* p)
217  {
218  switch (version) {
219  case 0:
220  throw parse_exception("[N] Unsupported fsm-json version:"
221  + std::to_string(version));
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  switch (version) {
237  case 0:
238  throw parse_exception("[N] Unsupported fsm-json version:"
239  + std::to_string(version));
240  case 1:
241  default:
242  return new json::int_t(v);
243  }
244  }
245 
246  template<unsigned version = version::fsm_json>
248  to_json() const
249  {
250  switch (version) {
251  case 0:
252  throw parse_exception("[N] Unsupported fsm-json version:"
253  + std::to_string(version));
254  case 1:
255  default:
256  json::object_t* obj = new json::object_t();
257  obj->push_back("semiring",new json::string_t("N"));
258  return obj;
259  }
260  }
261  };
262 
263  inline n join(const n&, const n&) { return {}; }
264 
265  inline n join(const b&, const n&) { return {}; }
266  inline n join(const n&, const b&) { return {}; }
267 
268 }}//end of ns awali::stc
269 
270 #endif // !AWALI_WEIGHTSET_N_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
constexpr static value_t zero()
Definition: n.hh:57
static constexpr bool is_commutative_semiring()
Definition: n.hh:140
static std::ostream & print(const value_t v, std::ostream &o, const std::string &fmt="text")
Definition: n.hh:194
static value_t value_from_json(json::node_t const *p)
Definition: n.hh:216
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: n.hh:135
static value_t mul(const value_t l, const value_t r)
Definition: n.hh:82
static constexpr star_status_t star_status()
Definition: n.hh:143
value_t parse(const std::string &s, size_t &p) const
Definition: n.hh:170
static value_t conv(std::istream &stream)
Definition: n.hh:184
constexpr static bool is_special(value_t)
Definition: n.hh:111
static value_t conv(self_type, value_t v)
Definition: n.hh:157
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: n.hh:201
static std::string sname()
Definition: n.hh:37
unsigned int value_t
Definition: n.hh:54
static bool equals(const value_t l, const value_t r)
Definition: n.hh:129
static value_t rdiv(const value_t l, const value_t r)
Definition: n.hh:88
static value_t conv(b, b::value_t v)
Definition: n.hh:163
value_t star(const value_t v) const
Definition: n.hh:103
static value_t add(const value_t l, const value_t r)
Definition: n.hh:69
static value_t ldiv(const value_t l, const value_t r)
Definition: n.hh:97
static bool is_zero(const value_t v)
Definition: n.hh:117
static constexpr bool show_one()
Definition: n.hh:142
json::node_t * value_to_json(value_t v) const
Definition: n.hh:234
constexpr static value_t one()
Definition: n.hh:63
json::object_t * to_json() const
Definition: n.hh:248
std::string vname(bool=true) const
Definition: n.hh:42
static size_t hash(value_t v)
Definition: n.hh:151
static bool is_one(const value_t v)
Definition: n.hh:123
static value_t sub(const value_t l, const value_t r)
Definition: n.hh:75
static value_t transpose(const value_t v)
Definition: n.hh:146
static n make(std::istream &is)
Build from the description in is.
Definition: n.hh:48
The semiring of floating Numbers.
Definition: r.hh:34
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