17 #ifndef AWALI_WEIGHTSET_Q_HH
18 #define AWALI_WEIGHTSET_Q_HH
25 #include <awali/common/qfraction.cc>
51 std::string
vname(
bool =
true)
const
65 static unsigned int abs(
int a)
67 return a < 0 ? -a : a;
123 raise(
sname(),
": star: invalid value: ",
format(*
this, v));
139 return v.
num == 1 && v.
den == 1;
212 if ((maybe_slash = i.peek()) !=
'/')
230 throw std::domain_error(
sname() +
": zero denominator");
238 parse(
const std::string & s,
size_t& p) {
245 const std::string&
format =
"text")
250 o<<
"{\"num\":" << v.
num <<
", \"den\":" << v.
den <<
'}';
258 o <<
"\\frac{" << v.
num <<
"}{" << v.
den <<
'}';
274 else if (
format ==
"text")
277 raise(
"invalid format: ",
format);
281 template<
unsigned version = version::fsm_json>
295 template<
unsigned version = version::fsm_json>
314 template<
unsigned version = version::fsm_json>
325 "[Q] value_from_json: node is of kind FLOATING and we do "
326 "not support double to qfraction conversion");
337 "[Q] value_from_json: node is of kind STRING and is not a "
338 "proper qfraction representation.");
341 std::vector<json::node_t*>
const& v = p->
array()->
values;
344 "[Q] value_from_json: node is of kind ARRAY and needs to "
345 " have two children to be interpreted as a qfraction.");
352 "[Q] value_from_json: node is of kind OBJECT and needs to "
353 "have a \"num\" field to be interpreted as a qfraction.");
359 "[Q] value_from_json:: node is of kind OBJECT, hence the "
360 "node associated with \"num\" needs to be coercible to "
361 "int. " + std::string(e.
what()));
370 "[Q] value_from_json: node is of kind NULL and cannot be "
371 "interpreted as a qfraction.");
373 throw std::runtime_error(
"json parser Q");
379 inline q join(
const q&,
const q&) {
return {}; }
381 inline q join(
const z&,
const q&) {
return {}; }
382 inline q join(
const q&,
const z&) {
return {}; }
384 inline q join(
const n&,
const q&) {
return {}; }
385 inline q join(
const q&,
const n&) {
return {}; }
387 inline q join(
const b&,
const q&) {
return {}; }
388 inline q join(
const q&,
const b&) {
return {}; }
std::vector< node_t * > const & values
Definition: node.hh:429
Exception used when trying to coerce a node to a given type.
Definition: exceptions.hh:83
virtual const char * what() const noexcept override
Definition: exceptions.hh:40
virtual node_t * at(std::string const &key)
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:319
virtual string_t const * string() const
Casts this node to string_t.
Definition: node.hh:213
virtual bool has_child(std::string const &) const
Definition: node.hh:282
node_kind_t const kind
Definition: node.hh:194
virtual unsigned arity() const
Definition: node.hh:353
virtual array_t const * array() const
Casts this node to array_t.
Definition: node.hh:205
object_t * push_back(std::string key, node_t *node)
std::string value
Definition: node.hh:528
Definition: qfraction.hh:26
den_t den
Definition: qfraction.hh:32
num_t num
Definition: qfraction.hh:31
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of Natural numbers.
Definition: n.hh:34
unsigned int value_t
Definition: n.hh:55
The semiring of rational numbers.
Definition: q.hh:42
value_t star(const value_t v) const
Definition: q.hh:116
static value_t one()
Definition: q.hh:76
json::node_t * value_to_json(value_t v) const
Definition: q.hh:296
static size_t hash(value_t v)
Definition: q.hh:170
static value_t mul(const value_t l, const value_t r)
Definition: q.hh:95
static value_t parse(const std::string &s, size_t &p)
Definition: q.hh:238
value_t value_from_json(json::node_t const *p) const
Definition: q.hh:315
static value_t conv(z, z::value_t v)
Definition: q.hh:185
static constexpr bool show_one()
Definition: q.hh:155
static value_t add(const value_t l, const value_t r)
Definition: q.hh:81
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: q.hh:148
static value_t abs(const value_t v)
Definition: q.hh:159
static value_t ldiv(const value_t l, const value_t r)
Definition: q.hh:111
static value_t transpose(const value_t v)
Definition: q.hh:165
static value_t conv(b, b::value_t v)
Definition: q.hh:197
static bool is_special(const value_t)
Definition: q.hh:126
static bool is_one(const value_t v)
Definition: q.hh:136
static value_t conv(std::istream &i)
Definition: q.hh:203
static value_t conv(n, n::value_t v)
Definition: q.hh:191
static value_t rdiv(const value_t l, const value_t r)
Definition: q.hh:101
static bool is_zero(const value_t v)
Definition: q.hh:131
static std::string sname()
Definition: q.hh:46
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: q.hh:270
json::object_t * to_json() const
Definition: q.hh:283
std::string vname(bool=true) const
Definition: q.hh:51
static value_t conv(self_type, value_t v)
Definition: q.hh:179
static q make(std::istream &is)
Build from the description in is.
Definition: q.hh:57
static value_t zero()
Definition: q.hh:71
static constexpr bool is_commutative_semiring()
Definition: q.hh:153
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: q.hh:244
static constexpr star_status_t star_status()
Definition: q.hh:156
static unsigned int abs(int a)
Definition: q.hh:65
static value_t sub(const value_t l, const value_t r)
Definition: q.hh:88
static bool equals(const value_t l, const value_t r)
Definition: q.hh:142
q_fraction_t value_t
Definition: q.hh:63
The semiring of floating Numbers.
Definition: r.hh:35
The semiring of Integers.
Definition: z.hh:35
int value_t
Definition: z.hh:56
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:161
@ ABSVAL
Definition: enums.hh:166
@ OBJECT
Definition: node.hh:90
@ INTEGER
Definition: node.hh:92
@ ARRAY
Definition: node.hh:91
@ BOOLEAN
Definition: node.hh:95
@ STRING
Definition: node.hh:94
@ FLOATING
Definition: node.hh:93
@ _NULL
Definition: node.hh:96
q_fraction_t lr_parse_qfraction(std::string const &s, size_t &p, bool allow_empty=false, q_fraction_t value_if_empty={1, 1})
Reads a q_fraction_t left of position p in string, by using as many characters as possible.
Definition: lr_parse_number.hh:353
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
ATTRIBUTE_CONST unsigned int lcm(unsigned int a, unsigned int b)
Definition: arith.hh:41
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26