Vcsn  2.8
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 
118  static bool
119  equal(const value_t l, const value_t r)
120  {
121  return l == r;
122  }
123 
125  static int compare(const value_t l, const value_t r)
126  {
127  return int(l) - int(r);
128  }
129 
131  static bool less(const value_t lhs, const value_t rhs)
132  {
133  return lhs < rhs;
134  }
135 
136  constexpr static bool is_special(const value_t)
137  {
138  return false;
139  }
140 
141  static bool
143  {
144  return !v;
145  }
146 
147  static bool
148  is_one(const value_t v)
149  {
150  return v;
151  }
152 
153  static constexpr bool is_commutative() { return true; }
154  static constexpr bool is_idempotent() { return true; }
155  static constexpr bool has_lightening_weights() { return false; }
156 
157  static constexpr bool show_one() { return false; }
158  static constexpr star_status_t star_status()
159  {
161  }
162 
163  static value_t
165  {
166  return v;
167  }
168 
169  static size_t hash(const value_t v)
170  {
171  return hash_value(v);
172  }
173 
174  static value_t
176  {
177  return v;
178  }
179 
180  value_t
181  conv(std::istream& is, bool = true) const
182  {
183  int i;
184  if (is >> i)
185  {
186  if (i != 0 && i != 1)
187  raise_invalid_value(*this, i);
188  return i;
189  }
190  else
191  raise_invalid_value(*this, is);
192  }
193 
194  static std::ostream&
195  print(const value_t v, std::ostream& o = std::cout,
196  format fmt = {})
197  {
198  if (fmt == format::latex)
199  o << (v ? "\\top" : "\\bot");
200  else
201  o << (v ? '1' : '0');
202  return o;
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{B}";
212  break;
213  case format::sname:
214  o << sname();
215  break;
216  case format::text:
217  o << "B";
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<b, RandomGenerator>
233  : public random_weight_base<b, RandomGenerator>
234  {
235  public:
237  using value_t = typename super_t::weight_t;
238 
239  using super_t::super_t;
240 
241  private:
243  {
244  auto dis
245  = std::uniform_int_distribution<>(super_t::min_, super_t::max_);
246  return dis(super_t::gen_) ? super_t::ws_.zero() : super_t::ws_.one();
247  }
248  };
249 
250  /*-------.
251  | join. |
252  `-------*/
253 
254  VCSN_JOIN_SIMPLE(b, b);
255  } // detail::
256 
257 }
bool value_t
Definition: b.hh:25
static constexpr bool show_one()
Definition: b.hh:157
Print as a parsable type string.
Definition: format.hh:26
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static int compare(const value_t l, const value_t r)
Three-way comparison between l and r.
Definition: b.hh:125
static value_t zero()
Definition: b.hh:41
static constexpr bool is_idempotent()
Definition: b.hh:154
ATTRIBUTE_NORETURN void raise_invalid_value(const ValueSet &vs, Args &&... args)
Cannot make a value from this.
Definition: weightset.hh:227
static value_t sub(const value_t l, const value_t r)
Definition: b.hh:73
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:147
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: b.hh:131
value_t lgcd(const value_t l, const value_t r) const
Definition: b.hh:85
static value_t min()
Definition: b.hh:53
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format fmt={})
Definition: b.hh:195
static bool is_one(const value_t v)
Definition: b.hh:148
static constexpr bool is_commutative()
Definition: b.hh:153
always valid.
Definition: star-status.hh:17
static size_t hash(const value_t v)
Definition: b.hh:169
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
static value_t max()
Definition: b.hh:59
static constexpr bool has_lightening_weights()
Definition: b.hh:155
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
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:45
typename super_t::weight_t value_t
Definition: b.hh:237
Definition: a-star.hh:8
static symbol sname()
Definition: b.hh:27
star_status_t
Definition: star-status.hh:5
static bool equal(const value_t l, const value_t r)
Whether l == r.
Definition: b.hh:119
static value_t conv(self_t, const value_t v)
Definition: b.hh:175
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: b.hh:206
static b make(std::istream &is)
Build from the description in is.
Definition: b.hh:34
static constexpr bool is_special(const value_t)
Definition: b.hh:136
static value_t star(const value_t)
Definition: b.hh:112
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
value_t ldivide(const value_t l, const value_t r) const
Definition: b.hh:106
static value_t transpose(const value_t v)
Definition: b.hh:164
value_t conv(std::istream &is, bool=true) const
Definition: b.hh:181
Print as is. For instance, don&#39;t try to escape labels.
Definition: format.hh:24
static value_t one()
Definition: b.hh:47
Print for LaTeX.
Definition: format.hh:22
VCSN_JOIN_SIMPLE(b, b)
Generic declaration of the class which is specialized in each weightset.
Definition: weightset.hh:208
static bool is_zero(const value_t v)
Definition: b.hh:142
value_t rdivide(const value_t l, const value_t r) const
Definition: b.hh:99
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:87
static value_t mul(const value_t l, const value_t r)
Definition: b.hh:79
value_t rgcd(const value_t l, const value_t r) const
Definition: b.hh:93
static value_t add(const value_t l, const value_t r)
Definition: b.hh:65
return res
Definition: multiply.hh:399
static constexpr star_status_t star_status()
Definition: b.hh:158
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48