Vcsn  2.1
Be Rational
z.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ostream>
4 #include <string>
5 
6 #include <vcsn/core/join.hh>
7 #include <vcsn/misc/functional.hh> // hash_value
8 #include <vcsn/misc/math.hh> // gcd
9 #include <vcsn/misc/raise.hh>
10 #include <vcsn/misc/star-status.hh>
11 #include <vcsn/misc/stream.hh>
12 #include <vcsn/misc/symbol.hh>
13 #include <vcsn/weightset/b.hh>
14 #include <vcsn/weightset/fwd.hh>
15 
16 namespace vcsn
17 {
18  namespace detail
19  {
20  class z_impl
21  {
22  public:
23  using self_t = z;
24 
25  static symbol sname()
26  {
27  static symbol res("z");
28  return res;
29  }
30 
32  static z make(std::istream& is)
33  {
34  eat(is, sname());
35  return {};
36  }
37 
38  using value_t = int;
39 
40  static value_t
41  zero()
42  {
43  return 0;
44  }
45 
46  static value_t
47  one()
48  {
49  return 1;
50  }
51 
52  static value_t
53  add(const value_t l, const value_t r)
54  {
55  return l + r;
56  }
57 
58  static value_t
59  sub(const value_t l, const value_t r)
60  {
61  return l - r;
62  }
63 
64  static value_t
65  mul(const value_t l, const value_t r)
66  {
67  return l * r;
68  }
69 
70  static value_t
71  lgcd(const value_t l, const value_t r)
72  {
73  require(!is_zero(l), sname(), ": lgcd: invalid lhs: zero");
74  require(!is_zero(r), sname(), ": lgcd: invalid rhs: zero");
75  return detail::gcd(l, r);
76  }
77 
78  static value_t
79  rgcd(const value_t l, const value_t r)
80  {
81  return lgcd(l, r);
82  }
83 
84  static value_t
85  rdiv(const value_t l, const value_t r)
86  {
87  require(!is_zero(r), "div: division by zero");
88  require(!(l % r),
89  "z: div: invalid division: ", l, '/', r);
90  return l / r;
91  }
92 
93  static value_t
94  ldiv(const value_t l, const value_t r)
95  {
96  return rdiv(r, l);
97  }
98 
99  value_t
100  star(const value_t v) const
101  {
102  if (is_zero(v))
103  return one();
104  else
105  raise("z: star: invalid value: ", to_string(*this, v));
106  }
107 
108  constexpr static bool is_special(value_t)
109  {
110  return false;
111  }
112 
113  static bool
115  {
116  return v == 0;
117  }
118 
119  static bool
120  is_one(const value_t v)
121  {
122  return v == 1;
123  }
124 
125  static bool
126  equal(const value_t l, const value_t r)
127  {
128  return l == r;
129  }
130 
132  static bool less(value_t lhs, value_t rhs)
133  {
134  return lhs < rhs;
135  }
136 
137  static constexpr bool is_commutative() { return true; }
138  static constexpr bool is_idempotent() { return false; }
139 
140  static constexpr bool show_one() { return false; }
142 
143  static value_t
145  {
146  return v;
147  }
148 
149  static size_t hash(value_t v)
150  {
151  return hash_value(v);
152  }
153 
154  static value_t
156  {
157  return v;
158  }
159 
160  static value_t
162  {
163  // Conversion from bool to int.
164  return v;
165  }
166 
167  static value_t
168  conv(std::istream& is, bool = true)
169  {
170  int res;
171  if (is >> res)
172  return res;
173  else
174  vcsn::fail_reading(is, sname(), ": invalid value");
175  }
176 
177  static std::ostream&
178  print(const value_t v, std::ostream& o,
179  format = {})
180  {
181  return o << v;
182  }
183 
184  std::ostream&
185  print_set(std::ostream& o, format fmt = {}) const
186  {
187  if (fmt == format::latex)
188  o << "\\mathbb{Z}";
189  else if (fmt == format::text)
190  o << sname();
191  else
192  raise("invalid format: ", fmt);
193  return o;
194  }
195  };
196 
197  /*-------.
198  | join. |
199  `-------*/
200 
201  VCSN_JOIN_SIMPLE(b, z);
202  VCSN_JOIN_SIMPLE(z, z);
203  }
204 
205 }
static symbol sname()
Definition: z.hh:25
static bool is_one(const value_t v)
Definition: z.hh:120
static value_t conv(self_t, value_t v)
Definition: z.hh:155
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static size_t hash(value_t v)
Definition: z.hh:149
ATTRIBUTE_PURE unsigned int gcd(unsigned int a, unsigned int b)
Greatest common divisor.
Definition: math.hh:13
static constexpr bool is_commutative()
Definition: z.hh:137
value_t star(const value_t v) const
Definition: z.hh:100
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
static std::ostream & print(const value_t v, std::ostream &o, format={})
Definition: z.hh:178
static value_t rgcd(const value_t l, const value_t r)
Definition: z.hh:79
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static z make(std::istream &is)
Build from the description in is.
Definition: z.hh:32
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: z.hh:185
static value_t rdiv(const value_t l, const value_t r)
Definition: z.hh:85
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
static value_t add(const value_t l, const value_t r)
Definition: z.hh:53
An input/output format.
Definition: format.hh:11
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static constexpr star_status_t star_status()
Definition: z.hh:141
static bool equal(const value_t l, const value_t r)
Definition: z.hh:126
static value_t one()
Definition: z.hh:47
static value_t conv(b, b::value_t v)
Definition: z.hh:161
static bool is_zero(const value_t v)
Definition: z.hh:114
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
static value_t lgcd(const value_t l, const value_t r)
Definition: z.hh:71
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: z.hh:132
static constexpr bool is_special(value_t)
Definition: z.hh:108
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t zero()
Definition: z.hh:41
static value_t transpose(const value_t v)
Definition: z.hh:144
star_status_t
Definition: star-status.hh:5
static value_t sub(const value_t l, const value_t r)
Definition: z.hh:59
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
Definition: stream.hh:62
static value_t mul(const value_t l, const value_t r)
Definition: z.hh:65
VCSN_JOIN_SIMPLE(b, b)
static value_t conv(std::istream &is, bool=true)
Definition: z.hh:168
static constexpr bool show_one()
Definition: z.hh:140
static value_t ldiv(const value_t l, const value_t r)
Definition: z.hh:94
static constexpr bool is_idempotent()
Definition: z.hh:138
weightset_mixin< detail::z_impl > z
Definition: fwd.hh:56