Awali
Another Weighted Automata library
nn.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_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 l, const value_t r)
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  switch (version) {
225  case 0:
226  throw parse_exception("[nn] Unsupported fsm-json version:"
227  + std::to_string(version));
228  case 1:
229  default:
230  json::object_t* obj = new json::object_t();
231  obj->push_back("semiring", new json::string_t("Bounded"));
232  obj->push_back("characteristic", new json::int_t(K));
233  return (obj);
234  }
235  }
236 
237  template<unsigned version = version::fsm_json>
238  json::node_t*
240  {
241  switch (version) {
242  case 0:
243  throw parse_exception("[nn] Unsupported fsm-json version:"
244  + std::to_string(version));
245  case 1:
246  default:
247  return new json::int_t(v);
248  }
249  }
250 
251  template<unsigned version = version::fsm_json>
252  value_t
253  static value_from_json(json::node_t const* p)
254  {
255  switch (version) {
256  case 0:
257  throw parse_exception("[nn] Unsupported fsm-json version:"
258  + std::to_string(version));
259  case 1:
260  default:
261  int a=p->to_int();
262  if(a<0)
263  throw parse_exception("[nn] No negative value in bounded semiring");
264  if(a>K)
265  a=K;
266  return a;
267  }
268  }
269 
270  };
271 
272  template<unsigned K>
273  nn<K> join(const nn<K>&, const nn<K>&) { return {}; }
274 
275  }
276 }//end of ns awali::stc
277 
278 #endif // !AWALI_WEIGHTSET_NK_HH
awali::sttc::nn::equals
static bool equals(const value_t l, const value_t r)
Definition: nn.hh:118
awali::sttc::nn::mul
static value_t mul(const value_t l, const value_t r)
Definition: nn.hh:91
awali::sttc::nn::is_one
static bool is_one(const value_t v)
Definition: nn.hh:141
awali
Main namespace of Awali.
Definition: ato.hh:22
awali::sttc::nn::finite
static const finite_t finite
Definition: nn.hh:70
awali::sttc::nn::one
static value_t one()
Definition: nn.hh:79
awali::star_status_t
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:161
awali::sttc::join
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:448
awali::sttc::nn::rdiv
static value_t rdiv(const value_t l, const value_t r)
Definition: nn.hh:97
awali::sttc::nn::show_one
static constexpr bool show_one()
Definition: nn.hh:148
awali::sttc::nn::ldiv
static value_t ldiv(const value_t l, const value_t r)
Definition: nn.hh:103
awali::sttc::nn::is_commutative_semiring
static constexpr bool is_commutative_semiring()
Definition: nn.hh:146
awali::sttc::nn::make
static nn< K > make(std::istream &is)
Build from the description in is.
Definition: nn.hh:62
awali::STARRABLE
@ STARRABLE
The star of every element exists.
Definition: enums.hh:163
awali::sttc::nn::vname
std::string vname(bool=true) const
Definition: nn.hh:56
awali::sttc::nn::value_from_json
static value_t value_from_json(json::node_t const *p)
Definition: nn.hh:253
awali::sttc::nn::star_status
static constexpr star_status_t star_status()
Definition: nn.hh:151
stream.hh
awali::sttc::nn::is_zero
static bool is_zero(const value_t v)
Definition: nn.hh:135
awali::sttc::nn::conv
static value_t conv(self_type, value_t v)
Definition: nn.hh:165
weightset.hh
awali::sttc::nn::hash
static size_t hash(value_t v)
Definition: nn.hh:159
awali::json::node_t::to_int
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:318
enums.hh
awali::sttc::nn::print
static std::ostream & print(value_t v, std::ostream &o, const std::string &format="text")
Definition: nn.hh:199
awali::sttc::nn::to_json
json::node_t * to_json() const
Definition: nn.hh:222
awali::sttc::nn::nn
nn()
Definition: nn.hh:46
awali::sttc::rat::to_string
std::string to_string(identities i)
awali::json::node_t
Definition: node.hh:191
awali::json::int_t
Definition: node.hh:482
awali::parse_exception
Exceptions thrown during parsing.
Definition: parse_exception.hh:26
awali::sttc::nn::parse
static value_t parse(const std::string &s, size_t &p)
Definition: nn.hh:183
awali::sttc::nn::conv
static value_t conv(std::istream &stream)
Definition: nn.hh:171
awali::sttc::nn::zero
static value_t zero()
Definition: nn.hh:73
awali::json::object_t::push_back
object_t * push_back(std::string key, node_t *node)
awali::sttc::nn::finite_t
bool finite_t
Definition: nn.hh:69
awali::sttc::nn::less_than
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: nn.hh:124
awali::sttc::nn::transpose
static value_t transpose(const value_t v)
Definition: nn.hh:154
awali::sttc::nn::value_t
unsigned int value_t
Definition: nn.hh:68
raise.hh
awali::json::object_t
Definition: node.hh:364
awali::sttc::format
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
awali::sttc::r
The semiring of floating Numbers.
Definition: r.hh:34
awali::sttc::nn
The semiring of natural numbers bounded by K.
Definition: nn.hh:41
awali::sttc::nn::value_to_json
json::node_t * value_to_json(value_t v) const
Definition: nn.hh:239
awali::sttc::eat
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
awali::sttc::nn::star
static value_t star(const value_t v)
Definition: nn.hh:109
escape.hh
awali::sttc::fail_reading
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
awali::json::string_t
Definition: node.hh:525
awali::sttc::nn::add
static value_t add(const value_t l, const value_t r)
Definition: nn.hh:85
awali::utils::hash_value
std::size_t hash_value(const T &v)
Definition: hash.hh:76
awali::sttc::nn::print_set
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: nn.hh:208
hash.hh
awali::sttc::nn::sname
static std::string sname()
Definition: nn.hh:51
awali::sttc::nn::is_special
constexpr static bool is_special(value_t)
Definition: nn.hh:129