Vcsn  2.3
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 auto res = symbol{"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  value_t
71  lgcd(const value_t l, const value_t r) const
72  {
73  require(!is_zero(l), *this, ": lgcd: invalid lhs: zero");
74  require(!is_zero(r), *this, ": lgcd: invalid rhs: zero");
75  return detail::gcd(l, r);
76  }
77 
78  value_t
79  rgcd(const value_t l, const value_t r) const
80  {
81  return lgcd(l, r);
82  }
83 
84  value_t
85  rdivide(const value_t l, const value_t r) const
86  {
87  require(!is_zero(r), *this, "div: division by zero");
88  require(!(l % r),
89  *this, "div: invalid division: ", l, '/', r);
90  return l / r;
91  }
92 
93  value_t
94  ldivide(const value_t l, const value_t r) const
95  {
96  return rdivide(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_not_starrable(*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  static constexpr bool has_lightening_weights() { return true; }
140 
141  static constexpr bool show_one() { return false; }
143 
144  static value_t
146  {
147  return v;
148  }
149 
150  static size_t hash(value_t v)
151  {
152  return hash_value(v);
153  }
154 
155  static value_t
157  {
158  return v;
159  }
160 
161  static value_t
163  {
164  // Conversion from bool to int.
165  return v;
166  }
167 
168  value_t
169  conv(std::istream& is, bool = true) const
170  {
171  int res;
172  if (is >> res)
173  return res;
174  else
175  vcsn::fail_reading(is, *this, ": invalid value");
176  }
177 
178  static std::ostream&
179  print(const value_t v, std::ostream& o = std::cout,
180  format = {})
181  {
182  return o << v;
183  }
184 
185  std::ostream&
186  print_set(std::ostream& o, format fmt = {}) const
187  {
188  switch (fmt.kind())
189  {
190  case format::latex:
191  o << "\\mathbb{Z}";
192  break;
193  case format::sname:
194  o << sname();
195  break;
196  case format::text:
197  o << "Z";
198  break;
199  case format::utf8:
200  o << "ℤ";
201  break;
202  case format::raw:
203  assert(0);
204  break;
205  }
206  return o;
207  }
208  };
209 
210  /*-------.
211  | join. |
212  `-------*/
213 
214  VCSN_JOIN_SIMPLE(b, z);
215  VCSN_JOIN_SIMPLE(z, z);
216  }
217 
218 }
static value_t sub(const value_t l, const value_t r)
Definition: z.hh:59
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
Definition: stream.hh:68
value_t star(const value_t v) const
Definition: z.hh:100
static value_t add(const value_t l, const value_t r)
Definition: z.hh:53
static value_t zero()
Definition: z.hh:41
star_status_t
Definition: star-status.hh:5
static value_t one()
Definition: z.hh:47
value_t rgcd(const value_t l, const value_t r) const
Definition: z.hh:79
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: raise.hh:100
static z make(std::istream &is)
Build from the description in is.
Definition: z.hh:32
value_t lgcd(const value_t l, const value_t r) const
Definition: z.hh:71
static constexpr star_status_t star_status()
Definition: z.hh:142
static constexpr bool show_one()
Definition: z.hh:141
Print as a parsable type string.
Definition: format.hh:26
static constexpr bool is_special(value_t)
Definition: z.hh:108
value_t ldivide(const value_t l, const value_t r) const
Definition: z.hh:94
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
static size_t hash(value_t v)
Definition: z.hh:150
static bool is_zero(const value_t v)
Definition: z.hh:114
static value_t conv(b, b::value_t v)
Definition: z.hh:162
return res
Definition: multiply.hh:398
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: z.hh:132
static constexpr bool is_commutative()
Definition: z.hh:137
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format={})
Definition: z.hh:179
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: z.hh:186
value_t conv(std::istream &is, bool=true) const
Definition: z.hh:169
VCSN_JOIN_SIMPLE(b, b)
static bool equal(const value_t l, const value_t r)
Definition: z.hh:126
Definition: a-star.hh:8
An input/output format for valuesets.
Definition: format.hh:13
static symbol sname()
Definition: z.hh:25
value_t rdivide(const value_t l, const value_t r) const
Definition: z.hh:85
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static constexpr bool has_lightening_weights()
Definition: z.hh:139
static value_t mul(const value_t l, const value_t r)
Definition: z.hh:65
weightset_mixin< detail::z_impl > z
Definition: fwd.hh:56
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48
static bool is_one(const value_t v)
Definition: z.hh:120
Print for LaTeX.
Definition: format.hh:22
ATTRIBUTE_PURE unsigned int gcd(unsigned int a, unsigned int b)
Greatest common divisor.
Definition: math.hh:13
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static value_t transpose(const value_t v)
Definition: z.hh:145
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
static constexpr bool is_idempotent()
Definition: z.hh:138
static value_t conv(self_t, value_t v)
Definition: z.hh:156
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54