Vcsn  2.8
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 
139  static int compare(const value_t l, const value_t r)
140  {
141  return int(l) - int(r);
142  }
143 
145  static bool
146  equal(const value_t l, const value_t r)
147  {
148  return l == r;
149  }
150 
152  static bool less(value_t lhs, value_t rhs)
153  {
154  return lhs < rhs;
155  }
156 
157  static constexpr bool is_commutative() { return true; }
158  static constexpr bool is_idempotent() { return false; }
159  static constexpr bool has_lightening_weights() { return true; }
160 
161  static constexpr bool show_one() { return false; }
163 
164  static value_t
166  {
167  return v;
168  }
169 
170  static size_t hash(value_t v)
171  {
172  return hash_value(v);
173  }
174 
175  static value_t
177  {
178  return v;
179  }
180 
181  static value_t
183  {
184  // Conversion from bool to int.
185  return v;
186  }
187 
188  value_t
189  conv(std::istream& is, bool = true) const
190  {
191  int res;
192  if (is >> res)
193  return res;
194  else
195  raise_invalid_value(*this, is);
196  }
197 
198  static std::ostream&
199  print(const value_t v, std::ostream& o = std::cout,
200  format = {})
201  {
202  return o << v;
203  }
204 
205  std::ostream&
206  print_set(std::ostream& o, format fmt = {}) const
207  {
208  switch (fmt.kind())
209  {
210  case format::latex:
211  o << "\\mathbb{Z}";
212  break;
213  case format::sname:
214  o << sname();
215  break;
216  case format::text:
217  o << "Z";
218  break;
219  case format::utf8:
220  o << "ℤ";
221  break;
222  case format::raw:
223  assert(0);
224  break;
225  }
226  return o;
227  }
228  };
229 
231  template <typename RandomGenerator>
232  class random_weight<z, RandomGenerator>
233  : public random_weight_base<z, RandomGenerator>
234  {
235  public:
237  using value_t = typename super_t::weight_t;
238 
239  using super_t::super_t;
240 
241  private:
242  value_t pick_value_() const override
243  {
244  auto dis
245  = std::uniform_int_distribution<>(super_t::min_, super_t::max_);
246  return dis(super_t::gen_);
247  }
248  };
249 
250  /*-------.
251  | join. |
252  `-------*/
253 
254  VCSN_JOIN_SIMPLE(b, z);
255  VCSN_JOIN_SIMPLE(z, z);
256  }
257 
258 }
Print as a parsable type string.
Definition: format.hh:26
static value_t conv(b, b::value_t v)
Definition: z.hh:182
static value_t add(const value_t l, const value_t r)
Definition: z.hh:66
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static value_t max()
Definition: z.hh:60
static constexpr bool show_one()
Definition: z.hh:161
static int compare(const value_t l, const value_t r)
Three-way comparison between l and r.
Definition: z.hh:139
ATTRIBUTE_NORETURN void raise_invalid_value(const ValueSet &vs, Args &&... args)
Cannot make a value from this.
Definition: weightset.hh:227
static value_t transpose(const value_t v)
Definition: z.hh:165
static bool is_one(const value_t v)
Definition: z.hh:133
static size_t hash(value_t v)
Definition: z.hh:170
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:147
return exp min
Definition: multiply.hh:362
weightset_mixin< detail::z_impl > z
Definition: fwd.hh:56
static constexpr bool is_commutative()
Definition: z.hh:157
static bool is_zero(const value_t v)
Definition: z.hh:127
value_t rdivide(const value_t l, const value_t r) const
Definition: z.hh:98
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: z.hh:152
value_t pick_value_() const override
Definition: z.hh:242
value_t ldivide(const value_t l, const value_t r) const
Definition: z.hh:107
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
static value_t conv(self_t, value_t v)
Definition: z.hh:176
static value_t mul(const value_t l, const value_t r)
Definition: z.hh:78
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
An input/output format for valuesets.
Definition: format.hh:13
Abstract class for random weight generation.
Definition: weightset.hh:118
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static value_t min()
Definition: z.hh:54
typename super_t::weight_t value_t
Definition: z.hh:237
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:45
static z make(std::istream &is)
Build from the description in is.
Definition: z.hh:33
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format={})
Definition: z.hh:199
value_t conv(std::istream &is, bool=true) const
Definition: z.hh:189
Definition: a-star.hh:8
static constexpr bool has_lightening_weights()
Definition: z.hh:159
static value_t zero()
Definition: z.hh:42
star_status_t
Definition: star-status.hh:5
value_t rgcd(const value_t l, const value_t r) const
Definition: z.hh:92
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static symbol sname()
Definition: z.hh:26
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: weightset.hh:235
static value_t one()
Definition: z.hh:48
static constexpr bool is_idempotent()
Definition: z.hh:158
Print as is. For instance, don&#39;t try to escape labels.
Definition: format.hh:24
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
VCSN_JOIN_SIMPLE(b, b)
Generic declaration of the class which is specialized in each weightset.
Definition: weightset.hh:208
static value_t sub(const value_t l, const value_t r)
Definition: z.hh:72
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:87
static constexpr star_status_t star_status()
Definition: z.hh:162
static bool equal(const value_t l, const value_t r)
Whether l == r.
Definition: z.hh:146
value_t star(const value_t v) const
Definition: z.hh:113
value_t lgcd(const value_t l, const value_t r) const
Definition: z.hh:84
return res
Definition: multiply.hh:399
static constexpr bool is_special(value_t)
Definition: z.hh:121
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: z.hh:206