Awali
Another Weighted Automata library
nn.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 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 bool
118  equals(const value_t l, const value_t r)
119  {
120  return ((l<K)?l:K) == ((r<K)?r:K) ;
121  }
122 
124  static bool less_than(value_t lhs, value_t rhs)
125  {
126  return lhs < rhs && lhs <K;
127  }
128 
129  constexpr static bool is_special(value_t)
130  {
131  return false;
132  }
133 
134  static bool
135  is_zero(const value_t v)
136  {
137  return ( v == 0u );
138  }
139 
140  static bool
141  is_one(const value_t v)
142  {
143  return ( v == 1u );
144  }
145 
146  static constexpr bool is_commutative_semiring() { return true; }
147 
148  static constexpr bool show_one() { return false; }
149 
150  static constexpr
152 
153  static value_t
155  {
156  return v;
157  }
158 
159  static size_t hash(value_t v)
160  {
161  return utils::hash_value(v);
162  }
163 
164  static value_t
166  {
167  return (v<K)?v:K;
168  }
169 
170  static value_t
171  conv(std::istream& stream)
172  {
173  int i;
174  if (stream >> i) {
175  if (i> (signed) K)
176  throw parse_exception("Weight-Set "+sname()+": parsed value ("+std::to_string(i)+") is too great.");
177  return i;
178  } else
179  sttc::fail_reading(stream, sname() + ": invalid value");
180  }
181 
182  static value_t
183  parse(const std::string & s, size_t& p) {
184  size_t i=p;
185  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
186  ;
187  if(i==p)
188  throw parse_exception("Wrong "+sname()+" value");
189  std::istringstream st(s.substr(i, p-i));
190  value_t x;
191  st >> x;
192  p=i;
193  if (x> (signed)K)
194  throw parse_exception("Weight-Set "+sname()+": parsed value ("+s.substr(i, p-i)+") is too great.");
195  return x;
196  }
197 
198  static std::ostream&
199  print(value_t v, std::ostream& o,
200  const std::string& /*format*/= "text")
201  {
202  if(v>K)
203  v=K;
204  return o << v;
205  }
206 
207  std::ostream&
208  print_set(std::ostream& o, const std::string& format = "text") const
209  {
210  if (format == "latex")
211  o << "\\mathbb{N}_" << K;
212  else if (format == "text")
213  o << "N" << K;
214  else
215  raise("invalid format: ", format);
216  return o;
217  }
218 
219 
220  template<unsigned version = version::fsm_json>
221  json::node_t*
222  to_json() const
223  {
224  version::check_fsmjson<version>();
225  switch (version) {
226  case 0: /* Never occurs due to above check. */
227  case 1:
228  default:
229  json::object_t* obj = new json::object_t();
230  obj->push_back("semiring", new json::string_t("Bounded"));
231  obj->push_back("characteristic", new json::int_t(K));
232  return (obj);
233  }
234  }
235 
236  template<unsigned version = version::fsm_json>
237  json::node_t*
239  {
240  version::check_fsmjson<version>();
241  switch (version) {
242  case 0: /* Never occurs due to above check. */
243  case 1:
244  default:
245  return new json::int_t(v);
246  }
247  }
248 
249  template<unsigned version = version::fsm_json>
250  value_t
251  static value_from_json(json::node_t const* p)
252  {
253  version::check_fsmjson<version>();
254  switch (version) {
255  case 0: /* Never occurs due to above check. */
256  case 1:
257  default:
258  int a=p->to_int();
259  if(a<0)
260  throw parse_exception("[nn] No negative value in bounded semiring");
261  if(a > (int) K)
262  a=K;
263  return a;
264  }
265  }
266 
267  };
268 
269  template<unsigned K>
270  nn<K> join(const nn<K>&, const nn<K>&) { return {}; }
271 
272  }
273 }//end of ns awali::stc
274 
275 #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:165
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:208
static std::ostream & print(value_t v, std::ostream &o, const std::string &="text")
Definition: nn.hh:199
static bool is_zero(const value_t v)
Definition: nn.hh:135
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:118
constexpr static bool is_special(value_t)
Definition: nn.hh:129
nn()
Definition: nn.hh:46
static value_t parse(const std::string &s, size_t &p)
Definition: nn.hh:183
unsigned int value_t
Definition: nn.hh:68
static constexpr bool show_one()
Definition: nn.hh:148
static value_t one()
Definition: nn.hh:79
json::node_t * value_to_json(value_t v) const
Definition: nn.hh:238
bool finite_t
Definition: nn.hh:69
static value_t conv(std::istream &stream)
Definition: nn.hh:171
static value_t star(const value_t v)
Definition: nn.hh:109
static value_t transpose(const value_t v)
Definition: nn.hh:154
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: nn.hh:124
static constexpr bool is_commutative_semiring()
Definition: nn.hh:146
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:251
json::node_t * to_json() const
Definition: nn.hh:222
static size_t hash(value_t v)
Definition: nn.hh:159
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:151
static bool is_one(const value_t v)
Definition: nn.hh:141
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
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:445
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