Vcsn  2.4
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>
16 
17 namespace vcsn
18 {
19  namespace detail
20  {
21  class z_impl
22  {
23  public:
24  using self_t = z;
25 
26  static symbol sname()
27  {
28  static auto res = symbol{"z"};
29  return res;
30  }
31 
33  static z make(std::istream& is)
34  {
35  eat(is, sname());
36  return {};
37  }
38 
39  using value_t = int;
40 
41  static value_t
42  zero()
43  {
44  return 0;
45  }
46 
47  static value_t
48  one()
49  {
50  return 1;
51  }
52 
53  static value_t
54  min()
55  {
57  }
58 
59  static value_t
60  max()
61  {
62  return std::numeric_limits<value_t>::max();
63  }
64 
65  static value_t
66  add(const value_t l, const value_t r)
67  {
68  return l + r;
69  }
70 
71  static value_t
72  sub(const value_t l, const value_t r)
73  {
74  return l - r;
75  }
76 
77  static value_t
78  mul(const value_t l, const value_t r)
79  {
80  return l * r;
81  }
82 
83  value_t
84  lgcd(const value_t l, const value_t r) const
85  {
86  require(!is_zero(l), *this, ": lgcd: invalid lhs: zero");
87  require(!is_zero(r), *this, ": lgcd: invalid rhs: zero");
88  return detail::gcd(l, r);
89  }
90 
91  value_t
92  rgcd(const value_t l, const value_t r) const
93  {
94  return lgcd(l, r);
95  }
96 
97  value_t
98  rdivide(const value_t l, const value_t r) const
99  {
100  require(!is_zero(r), *this, ": div: division by zero");
101  require(!(l % r),
102  *this, ": div: invalid division: ", l, '/', r);
103  return l / r;
104  }
105 
106  value_t
107  ldivide(const value_t l, const value_t r) const
108  {
109  return rdivide(r, l);
110  }
111 
112  value_t
113  star(const value_t v) const
114  {
115  if (is_zero(v))
116  return one();
117  else
118  raise_not_starrable(*this, v);
119  }
120 
121  constexpr static bool is_special(value_t)
122  {
123  return false;
124  }
125 
126  static bool
128  {
129  return v == 0;
130  }
131 
132  static bool
133  is_one(const value_t v)
134  {
135  return v == 1;
136  }
137 
138  static bool
139  equal(const value_t l, const value_t r)
140  {
141  return l == r;
142  }
143 
145  static bool less(value_t lhs, value_t rhs)
146  {
147  return lhs < rhs;
148  }
149 
150  static constexpr bool is_commutative() { return true; }
151  static constexpr bool is_idempotent() { return false; }
152  static constexpr bool has_lightening_weights() { return true; }
153 
154  static constexpr bool show_one() { return false; }
156 
157  static value_t
159  {
160  return v;
161  }
162 
163  static size_t hash(value_t v)
164  {
165  return hash_value(v);
166  }
167 
168  static value_t
170  {
171  return v;
172  }
173 
174  static value_t
176  {
177  // Conversion from bool to int.
178  return v;
179  }
180 
181  value_t
182  conv(std::istream& is, bool = true) const
183  {
184  int res;
185  if (is >> res)
186  return res;
187  else
188  vcsn::fail_reading(is, *this, ": invalid value");
189  }
190 
191  static std::ostream&
192  print(const value_t v, std::ostream& o = std::cout,
193  format = {})
194  {
195  return o << v;
196  }
197 
198  std::ostream&
199  print_set(std::ostream& o, format fmt = {}) const
200  {
201  switch (fmt.kind())
202  {
203  case format::latex:
204  o << "\\mathbb{Z}";
205  break;
206  case format::sname:
207  o << sname();
208  break;
209  case format::text:
210  o << "Z";
211  break;
212  case format::utf8:
213  o << "ℤ";
214  break;
215  case format::raw:
216  assert(0);
217  break;
218  }
219  return o;
220  }
221  };
222 
224  template <typename RandomGenerator>
225  class random_weight<z, RandomGenerator>
226  : public random_weight_base<z, RandomGenerator>
227  {
228  public:
230  using value_t = typename super_t::weight_t;
231 
232  using super_t::super_t;
233 
234  private:
235  value_t pick_value_() const override
236  {
237  auto dis
238  = std::uniform_int_distribution<>(super_t::min_, super_t::max_);
239  return dis(super_t::gen_);
240  }
241  };
242 
243  /*-------.
244  | join. |
245  `-------*/
246 
247  VCSN_JOIN_SIMPLE(b, z);
248  VCSN_JOIN_SIMPLE(z, z);
249  }
250 
251 }
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
Print as a parsable type string.
Definition: format.hh:26
Generic declaration of the class which is specialized in each weightset.
Definition: weightset.hh:205
value_t star(const value_t v) const
Definition: z.hh:113
static value_t conv(self_t, value_t v)
Definition: z.hh:169
static symbol sname()
Definition: z.hh:26
static bool equal(const value_t l, const value_t r)
Definition: z.hh:139
static value_t add(const value_t l, const value_t r)
Definition: z.hh:66
static value_t min()
Definition: z.hh:54
return res
Definition: multiply.hh:398
VCSN_JOIN_SIMPLE(b, b)
static constexpr bool has_lightening_weights()
Definition: z.hh:152
Print for LaTeX.
Definition: format.hh:22
static value_t sub(const value_t l, const value_t r)
Definition: z.hh:72
ATTRIBUTE_PURE unsigned int gcd(unsigned int a, unsigned int b)
Greatest common divisor.
Definition: math.hh:13
An input/output format for valuesets.
Definition: format.hh:13
Abstract class for random weight generation.
Definition: weightset.hh:112
static value_t zero()
Definition: z.hh:42
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static bool is_one(const value_t v)
Definition: z.hh:133
static constexpr bool is_idempotent()
Definition: z.hh:151
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
static value_t max()
Definition: z.hh:60
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: z.hh:199
static value_t mul(const value_t l, const value_t r)
Definition: z.hh:78
return exp min
Definition: multiply.hh:361
Definition: a-star.hh:8
value_t rdivide(const value_t l, const value_t r) const
Definition: z.hh:98
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static value_t one()
Definition: z.hh:48
static value_t transpose(const value_t v)
Definition: z.hh:158
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
value_t conv(std::istream &is, bool=true) const
Definition: z.hh:182
value_t lgcd(const value_t l, const value_t r) const
Definition: z.hh:84
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: z.hh:145
static constexpr bool is_commutative()
Definition: z.hh:150
static constexpr bool is_special(value_t)
Definition: z.hh:121
value_t pick_value_() const override
Definition: z.hh:235
static z make(std::istream &is)
Build from the description in is.
Definition: z.hh:33
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&...args)
Throw an exception after failing to read from is.
Definition: stream.hh:68
static constexpr star_status_t star_status()
Definition: z.hh:155
weightset_mixin< detail::z_impl > z
Definition: fwd.hh:56
static size_t hash(value_t v)
Definition: z.hh:163
star_status_t
Definition: star-status.hh:5
value_t rgcd(const value_t l, const value_t r) const
Definition: z.hh:92
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 bool is_zero(const value_t v)
Definition: z.hh:127
static constexpr bool show_one()
Definition: z.hh:154
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format={})
Definition: z.hh:192
typename super_t::weight_t value_t
Definition: z.hh:230
static value_t conv(b, b::value_t v)
Definition: z.hh:175
value_t ldivide(const value_t l, const value_t r) const
Definition: z.hh:107