Awali
Another Weighted Automata library
nn.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_NK_HH
18 # define AWALI_WEIGHTSET_NK_HH
19 
20 # include <cassert>
21 # include <ostream>
22 # include <string>
23 
25 #include <awali/utils/hash.hh>
26 #include <awali/sttc/misc/raise.hh>
27 #include <awali/common/enums.hh>
30 
31 namespace awali {
32  namespace sttc {
40  template <unsigned int K>
41  class nn
42  {
43  public:
44  using self_type = nn<K>;
45 
46  nn() {
47  if(K==0)
48  raise("Bounded semirings should have non zero characteristic");
49  }
50 
51  static std::string sname()
52  {
53  return "nn"+std::to_string(K);
54  }
55 
56  std::string vname(bool = true) const
57  {
58  return sname();
59  }
60 
62  static nn<K> make(std::istream& is)
63  {
64  eat(is, sname());
65  return {};
66  }
67 
68  using value_t = unsigned int;
69  using finite_t = bool;
70  static const finite_t finite=true;
71 
72  static value_t
73  zero()
74  {
75  return 0u;
76  }
77 
78  static value_t
79  one()
80  {
81  return 1u;
82  }
83 
84  static value_t
85  add(const value_t l, const value_t r)
86  {
87  return (l+r<K)?l+r:K;
88  }
89 
90  static value_t
91  mul(const value_t l, const value_t r)
92  {
93  return (l*r<K)?l*r:K;
94  }
95 
96  static value_t
97  rdiv(const value_t, const value_t)
98  {
99  raise(sname(), "No division");
100  }
101 
102  static value_t
103  ldiv(const value_t l, const value_t r)
104  {
105  raise(sname(), "No division");
106  }
107 
108  static value_t
109  star(const value_t v)
110  {
111  if ( v == 0u )
112  return 1u;
113  else
114  return K;
115  }
116 
117  static value_t
118  plus(const value_t v)
119  {
120  if ( v == 0u )
121  return 0u;
122  else
123  return K;
124  }
125 
126  static bool
127  equals(const value_t l, const value_t r)
128  {
129  return ((l<K)?l:K) == ((r<K)?r:K) ;
130  }
131 
133  static bool less_than(value_t lhs, value_t rhs)
134  {
135  return lhs < rhs && lhs <K;
136  }
137 
138  constexpr static bool is_special(value_t)
139  {
140  return false;
141  }
142 
143  static bool
144  is_zero(const value_t v)
145  {
146  return ( v == 0u );
147  }
148 
149  static bool
150  is_one(const value_t v)
151  {
152  return ( v == 1u );
153  }
154 
155  static constexpr bool is_commutative_semiring() { return true; }
156 
157  static constexpr bool show_one() { return false; }
158 
159  static constexpr
161 
162  static value_t
164  {
165  return v;
166  }
167 
168  static size_t hash(value_t v)
169  {
170  return utils::hash_value(v);
171  }
172 
173  static value_t
175  {
176  return (v<K)?v:K;
177  }
178 
179  static value_t
180  conv(std::istream& stream)
181  {
182  int i;
183  if (stream >> i) {
184  if (i> (signed) K)
185  throw parse_exception("Weight-Set "+sname()+": parsed value ("+std::to_string(i)+") is too great.");
186  return i;
187  } else
188  sttc::fail_reading(stream, sname() + ": invalid value");
189  }
190 
191  static value_t
192  parse(const std::string & s, size_t& p) {
193  size_t i=p;
194  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
195  ;
196  if(i==p)
197  throw parse_exception("Wrong "+sname()+" value");
198  std::istringstream st(s.substr(i, p-i));
199  value_t x;
200  st >> x;
201  p=i;
202  if (x> (signed)K)
203  throw parse_exception("Weight-Set "+sname()+": parsed value ("+s.substr(i, p-i)+") is too great.");
204  return x;
205  }
206 
207  static std::ostream&
208  print(value_t v, std::ostream& o,
209  const std::string& /*format*/= "text")
210  {
211  if(v>K)
212  v=K;
213  return o << v;
214  }
215 
216  std::ostream&
217  print_set(std::ostream& o, const std::string& format = "text") const
218  {
219  if (format == "latex")
220  o << "\\mathbb{N}_" << K;
221  else if (format == "text")
222  o << "N" << K;
223  else
224  raise("invalid format: ", format);
225  return o;
226  }
227 
228 
229  template<unsigned version = version::fsm_json>
230  json::node_t*
231  to_json() const
232  {
233  version::check_fsmjson<version>();
234  switch (version) {
235  case 0: /* Never occurs due to above check. */
236  case 1:
237  default:
238  json::object_t* obj = new json::object_t();
239  obj->push_back("semiring", new json::string_t("Bounded"));
240  obj->push_back("characteristic", new json::int_t(K));
241  return (obj);
242  }
243  }
244 
245  template<unsigned version = version::fsm_json>
246  json::node_t*
248  {
249  version::check_fsmjson<version>();
250  switch (version) {
251  case 0: /* Never occurs due to above check. */
252  case 1:
253  default:
254  return new json::int_t(v);
255  }
256  }
257 
258  template<unsigned version = version::fsm_json>
259  value_t
260  static value_from_json(json::node_t const* p)
261  {
262  version::check_fsmjson<version>();
263  switch (version) {
264  case 0: /* Never occurs due to above check. */
265  case 1:
266  default:
267  int a=p->to_int();
268  if(a<0)
269  throw parse_exception("[nn] No negative value in bounded semiring");
270  if(a > (int) K)
271  a=K;
272  return a;
273  }
274  }
275 
276  };
277 
278  template<unsigned K>
279  nn<K> join(const nn<K>&, const nn<K>&) { return {}; }
280 
281  }
282 }//end of ns awali::stc
283 
284 #endif // !AWALI_WEIGHTSET_NK_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 semiring of natural numbers bounded by K.
Definition: nn.hh:42
static value_t zero()
Definition: nn.hh:73
static value_t conv(self_type, value_t v)
Definition: nn.hh:174
static std::string sname()
Definition: nn.hh:51
static value_t rdiv(const value_t, const value_t)
Definition: nn.hh:97
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: nn.hh:217
static std::ostream & print(value_t v, std::ostream &o, const std::string &="text")
Definition: nn.hh:208
static bool is_zero(const value_t v)
Definition: nn.hh:144
static value_t add(const value_t l, const value_t r)
Definition: nn.hh:85
static bool equals(const value_t l, const value_t r)
Definition: nn.hh:127
constexpr static bool is_special(value_t)
Definition: nn.hh:138
nn()
Definition: nn.hh:46
static value_t parse(const std::string &s, size_t &p)
Definition: nn.hh:192
unsigned int value_t
Definition: nn.hh:68
static constexpr bool show_one()
Definition: nn.hh:157
static value_t one()
Definition: nn.hh:79
json::node_t * value_to_json(value_t v) const
Definition: nn.hh:247
bool finite_t
Definition: nn.hh:69
static value_t conv(std::istream &stream)
Definition: nn.hh:180
static value_t star(const value_t v)
Definition: nn.hh:109
static value_t transpose(const value_t v)
Definition: nn.hh:163
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: nn.hh:133
static constexpr bool is_commutative_semiring()
Definition: nn.hh:155
static nn< K > make(std::istream &is)
Build from the description in is.
Definition: nn.hh:62
static value_t value_from_json(json::node_t const *p)
Definition: nn.hh:260
json::node_t * to_json() const
Definition: nn.hh:231
static size_t hash(value_t v)
Definition: nn.hh:168
static value_t mul(const value_t l, const value_t r)
Definition: nn.hh:91
static constexpr star_status_t star_status()
Definition: nn.hh:160
static bool is_one(const value_t v)
Definition: nn.hh:150
static value_t ldiv(const value_t l, const value_t r)
Definition: nn.hh:103
static const finite_t finite
Definition: nn.hh:70
std::string vname(bool=true) const
Definition: nn.hh:56
static value_t plus(const value_t v)
Definition: nn.hh:118
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
@ STARRABLE
The star of every element exists.
Definition: enums.hh:165
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: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
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