Vcsn  2.4
Be Rational
b.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 
6 #include <vcsn/core/join.hh>
7 #include <vcsn/misc/escape.hh>
8 #include <vcsn/misc/format.hh>
9 #include <vcsn/misc/functional.hh> // hash_value
10 #include <vcsn/misc/raise.hh>
11 #include <vcsn/misc/star-status.hh>
12 #include <vcsn/misc/stream.hh>
13 #include <vcsn/misc/symbol.hh>
14 #include <vcsn/weightset/fwd.hh>
16 
17 namespace vcsn
18 {
19  namespace detail
20  {
21  class b_impl
22  {
23  public:
24  using self_t = b;
25  using value_t = bool;
26 
27  static symbol sname()
28  {
29  static auto res = symbol{"b"};
30  return res;
31  }
32 
34  static b make(std::istream& is)
35  {
36  eat(is, sname());
37  return {};
38  }
39 
40  static value_t
41  zero()
42  {
43  return false;
44  }
45 
46  static value_t
47  one()
48  {
49  return true;
50  }
51 
52  static value_t
53  min()
54  {
55  return false;
56  }
57 
58  static value_t
59  max()
60  {
61  return true;
62  }
63 
64  static value_t
65  add(const value_t l, const value_t r)
66  {
67  return l || r;
68  }
69 
70  // This is highly debatable. Was introduced to allow the division
71  // of polynomials. It should rather be handled there.
72  static value_t
73  sub(const value_t l, const value_t r)
74  {
75  return l && !r;
76  }
77 
78  static value_t
79  mul(const value_t l, const value_t r)
80  {
81  return l && r;
82  }
83 
84  value_t
85  lgcd(const value_t l, const value_t r) const
86  {
87  require(!is_zero(l), *this, ": lgcd: invalid lhs: zero");
88  require(!is_zero(r), *this, ": lgcd: invalid rhs: zero");
89  return one();
90  }
91 
92  value_t
93  rgcd(const value_t l, const value_t r) const
94  {
95  return lgcd(l, r);
96  }
97 
98  value_t
99  rdivide(const value_t l, const value_t r) const
100  {
101  require(!is_zero(r), *this, ": div: division by zero");
102  return l;
103  }
104 
105  value_t
106  ldivide(const value_t l, const value_t r) const
107  {
108  return rdivide(r, l);
109  }
110 
111  static value_t
112  star(const value_t)
113  {
114  return one();
115  }
116 
117  static bool
118  equal(const value_t l, const value_t r)
119  {
120  return l == r;
121  }
122 
124  static bool less(const value_t lhs, const value_t rhs)
125  {
126  return lhs < rhs;
127  }
128 
129  constexpr static bool is_special(const value_t)
130  {
131  return false;
132  }
133 
134  static bool
136  {
137  return !v;
138  }
139 
140  static bool
141  is_one(const value_t v)
142  {
143  return v;
144  }
145 
146  static constexpr bool is_commutative() { return true; }
147  static constexpr bool is_idempotent() { return true; }
148  static constexpr bool has_lightening_weights() { return false; }
149 
150  static constexpr bool show_one() { return false; }
151  static constexpr star_status_t star_status()
152  {
154  }
155 
156  static value_t
158  {
159  return v;
160  }
161 
162  static size_t hash(const value_t v)
163  {
164  return hash_value(v);
165  }
166 
167  static value_t
169  {
170  return v;
171  }
172 
173  value_t
174  conv(std::istream& is, bool = true) const
175  {
176  int i;
177  if (is >> i)
178  {
179  require(i == 0 || i == 1,
180  *this, ": invalid value: ", i);
181  return i;
182  }
183  else
184  raise(*this, ": invalid value: ", is);
185  }
186 
187  static std::ostream&
188  print(const value_t v, std::ostream& o = std::cout,
189  format fmt = {})
190  {
191  if (fmt == format::latex)
192  o << (v ? "\\top" : "\\bot");
193  else
194  o << (v ? '1' : '0');
195  return o;
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{B}";
205  break;
206  case format::sname:
207  o << sname();
208  break;
209  case format::text:
210  o << "B";
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<b, RandomGenerator>
226  : public random_weight_base<b, RandomGenerator>
227  {
228  public:
230  using value_t = typename super_t::weight_t;
231 
232  using super_t::super_t;
233 
234  private:
236  {
237  auto dis
238  = std::uniform_int_distribution<>(super_t::min_, super_t::max_);
239  return dis(super_t::gen_) ? super_t::ws_.zero() : super_t::ws_.one();
240  }
241  };
242 
243  /*-------.
244  | join. |
245  `-------*/
246 
247  VCSN_JOIN_SIMPLE(b, b);
248  } // detail::
249 
250 }
static value_t transpose(const value_t v)
Definition: b.hh:157
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
return res
Definition: multiply.hh:398
VCSN_JOIN_SIMPLE(b, b)
Print for LaTeX.
Definition: format.hh:22
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: b.hh:199
value_t conv(std::istream &is, bool=true) const
Definition: b.hh:174
static size_t hash(const value_t v)
Definition: b.hh:162
static constexpr bool is_special(const value_t)
Definition: b.hh:129
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format fmt={})
Definition: b.hh:188
always valid.
Definition: star-status.hh:17
An input/output format for valuesets.
Definition: format.hh:13
Abstract class for random weight generation.
Definition: weightset.hh:112
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
static value_t star(const value_t)
Definition: b.hh:112
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48
static bool is_one(const value_t v)
Definition: b.hh:141
static bool is_zero(const value_t v)
Definition: b.hh:135
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t min()
Definition: b.hh:53
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
static value_t sub(const value_t l, const value_t r)
Definition: b.hh:73
value_t rgcd(const value_t l, const value_t r) const
Definition: b.hh:93
bool value_t
Definition: b.hh:25
Definition: a-star.hh:8
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
value_t lgcd(const value_t l, const value_t r) const
Definition: b.hh:85
static value_t add(const value_t l, const value_t r)
Definition: b.hh:65
static constexpr star_status_t star_status()
Definition: b.hh:151
static constexpr bool is_idempotent()
Definition: b.hh:147
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
value_t rdivide(const value_t l, const value_t r) const
Definition: b.hh:99
static constexpr bool has_lightening_weights()
Definition: b.hh:148
static value_t zero()
Definition: b.hh:41
typename super_t::weight_t value_t
Definition: b.hh:230
static value_t max()
Definition: b.hh:59
star_status_t
Definition: star-status.hh:5
static bool equal(const value_t l, const value_t r)
Definition: b.hh:118
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: b.hh:124
static constexpr bool is_commutative()
Definition: b.hh:146
static constexpr bool show_one()
Definition: b.hh:150
static b make(std::istream &is)
Build from the description in is.
Definition: b.hh:34
static value_t mul(const value_t l, const value_t r)
Definition: b.hh:79
value_t ldivide(const value_t l, const value_t r) const
Definition: b.hh:106
static value_t conv(self_t, const value_t v)
Definition: b.hh:168
static symbol sname()
Definition: b.hh:27
static value_t one()
Definition: b.hh:47