Vcsn  2.2
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/format.hh>
8 #include <vcsn/misc/escape.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 
26  static symbol sname()
27  {
28  static auto res = symbol{"b"};
29  return res;
30  }
31 
33  static b make(std::istream& is)
34  {
35  eat(is, sname());
36  return {};
37  }
38 
39  using value_t = bool;
40 
41  static value_t
42  zero()
43  {
44  return false;
45  }
46 
47  static value_t
48  one()
49  {
50  return true;
51  }
52 
53  static value_t
54  add(const value_t l, const value_t r)
55  {
56  return l || r;
57  }
58 
59  // This is highly debatable. Was introduced to allow the division
60  // of polynomials. It should rather be handled there.
61  static value_t
62  sub(const value_t l, const value_t r)
63  {
64  return l && !r;
65  }
66 
67  static value_t
68  mul(const value_t l, const value_t r)
69  {
70  return l && r;
71  }
72 
73  static value_t
74  lgcd(const value_t l, const value_t r)
75  {
76  require(!is_zero(l), sname(), ": lgcd: invalid lhs: zero");
77  require(!is_zero(r), sname(), ": lgcd: invalid rhs: zero");
78  return one();
79  }
80 
81  static value_t
82  rgcd(const value_t l, const value_t r)
83  {
84  return lgcd(l, r);
85  }
86 
87  static value_t
88  rdiv(const value_t l, const value_t r)
89  {
90  require(!is_zero(r), "div: division by zero");
91  return l;
92  }
93 
94  static value_t
95  ldiv(const value_t l, const value_t r)
96  {
97  return rdiv(r, l);
98  }
99 
100  static value_t
101  star(const value_t)
102  {
103  return one();
104  }
105 
106  static bool
107  equal(const value_t l, const value_t r)
108  {
109  return l == r;
110  }
111 
113  static bool less(const value_t lhs, const value_t rhs)
114  {
115  return lhs < rhs;
116  }
117 
118  constexpr static bool is_special(const value_t)
119  {
120  return false;
121  }
122 
123  static bool
125  {
126  return !v;
127  }
128 
129  static bool
130  is_one(const value_t v)
131  {
132  return v;
133  }
134 
135  static constexpr bool is_commutative() { return true; }
136  static constexpr bool is_idempotent() { return true; }
137  static constexpr bool has_lightening_weights() { return false; }
138 
139  static constexpr bool show_one() { return false; }
140  static constexpr star_status_t star_status()
141  {
143  }
144 
145  static value_t
147  {
148  return v;
149  }
150 
151  static size_t hash(const value_t v)
152  {
153  return hash_value(v);
154  }
155 
156  static value_t
158  {
159  return v;
160  }
161 
162  static value_t
163  conv(std::istream& is, bool = true)
164  {
165  int i;
166  if (is >> i)
167  {
168  require(i == 0 || i == 1,
169  sname(), ": invalid value: ", i);
170  return i;
171  }
172  else
173  raise(sname(), ": invalid value: ", is);
174  }
175 
176  static std::ostream&
177  print(const value_t v, std::ostream& o,
178  format fmt = {})
179  {
180  if (fmt == format::latex)
181  o << (v ? "\\top" : "\\bot");
182  else
183  o << (v ? '1' : '0');
184  return o;
185  }
186 
187  std::ostream&
188  print_set(std::ostream& o, format fmt = {}) const
189  {
190  switch (fmt.kind())
191  {
192  case format::latex:
193  o << "\\mathbb{B}";
194  break;
195  case format::sname:
196  o << sname();
197  break;
198  case format::text:
199  o << "B";
200  break;
201  case format::utf8:
202  o << "𝔹";
203  break;
204  case format::raw:
205  assert(0);
206  break;
207  }
208  return o;
209  }
210  };
211 
212  /*-------.
213  | join. |
214  `-------*/
215 
216  VCSN_JOIN_SIMPLE(b, b);
217  } // detail::
218 
219 }
static value_t conv(std::istream &is, bool=true)
Definition: b.hh:163
Print as rich UTF-8 text, escaped.
Definition: format.hh:28
static value_t rgcd(const value_t l, const value_t r)
Definition: b.hh:82
static value_t add(const value_t l, const value_t r)
Definition: b.hh:54
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:66
static constexpr bool is_commutative()
Definition: b.hh:135
VCSN_JOIN_SIMPLE(b, b)
Definition: a-star.hh:8
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
static bool is_one(const value_t v)
Definition: b.hh:130
static value_t zero()
Definition: b.hh:42
Print for LaTeX.
Definition: format.hh:20
Print as is. For instance, don't try to escape labels.
Definition: format.hh:22
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static value_t rdiv(const value_t l, const value_t r)
Definition: b.hh:88
static symbol sname()
Definition: b.hh:26
An input/output format for valuesets.
Definition: format.hh:11
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static value_t mul(const value_t l, const value_t r)
Definition: b.hh:68
static constexpr bool show_one()
Definition: b.hh:139
static constexpr star_status_t star_status()
Definition: b.hh:140
static value_t one()
Definition: b.hh:48
static value_t star(const value_t)
Definition: b.hh:101
static value_t lgcd(const value_t l, const value_t r)
Definition: b.hh:74
always valid.
Definition: star-status.hh:17
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t ldiv(const value_t l, const value_t r)
Definition: b.hh:95
Print as plain (ASCII) text, escaped.
Definition: format.hh:26
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: b.hh:188
static bool equal(const value_t l, const value_t r)
Definition: b.hh:107
star_status_t
Definition: star-status.hh:5
static b make(std::istream &is)
Build from the description in is.
Definition: b.hh:33
static constexpr bool has_lightening_weights()
Definition: b.hh:137
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380
bool value_t
Definition: b.hh:39
static constexpr bool is_special(const value_t)
Definition: b.hh:118
Print as a parsable type string.
Definition: format.hh:24
static value_t sub(const value_t l, const value_t r)
Definition: b.hh:62
static value_t transpose(const value_t v)
Definition: b.hh:146
static constexpr bool is_idempotent()
Definition: b.hh:136
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: b.hh:113
static std::ostream & print(const value_t v, std::ostream &o, format fmt={})
Definition: b.hh:177
static value_t conv(self_t, const value_t v)
Definition: b.hh:157
static size_t hash(const value_t v)
Definition: b.hh:151
static bool is_zero(const value_t v)
Definition: b.hh:124
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48