Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
b.hh
Go to the documentation of this file.
1 #ifndef VCSN_WEIGHTSET_B_HH
2 # define VCSN_WEIGHTSET_B_HH
3 
4 # include <cassert>
5 # include <ostream>
6 # include <string>
7 
8 # include <vcsn/core/join.hh>
9 # include <vcsn/misc/escape.hh>
10 # include <vcsn/misc/hash.hh>
11 # include <vcsn/misc/raise.hh>
12 # include <vcsn/misc/stream.hh>
13 # include <vcsn/misc/star_status.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_type = b;
25 
26  static std::string sname()
27  {
28  return "b";
29  }
30 
31  std::string vname(bool = true) const
32  {
33  return sname();
34  }
35 
37  static b make(std::istream& is)
38  {
39  eat(is, sname());
40  return {};
41  }
42 
43  using value_t = bool;
44 
45  static value_t
46  zero()
47  {
48  return false;
49  }
50 
51  static value_t
52  one()
53  {
54  return true;
55  }
56 
57  static value_t
58  add(const value_t l, const value_t r)
59  {
60  return l || r;
61  }
62 
63  static value_t
64  mul(const value_t l, const value_t r)
65  {
66  return l && r;
67  }
68 
69  static value_t
70  rdiv(const value_t l, const value_t r)
71  {
72  require(!is_zero(r), "div: division by zero");
73  return l;
74  }
75 
76  static value_t
77  ldiv(const value_t l, const value_t r)
78  {
79  return rdiv(r, l);
80  }
81 
82  static value_t
83  star(const value_t)
84  {
85  return one();
86  }
87 
88  static bool
89  equals(const value_t l, const value_t r)
90  {
91  return l == r;
92  }
93 
95  static bool less_than(value_t lhs, value_t rhs)
96  {
97  return lhs < rhs;
98  }
99 
100  constexpr static bool is_special(value_t)
101  {
102  return false;
103  }
104 
105  static bool
106  is_zero(const value_t v)
107  {
108  return !v;
109  }
110 
111  static bool
112  is_one(const value_t v)
113  {
114  return v;
115  }
116 
117  static constexpr bool is_commutative() { return true; }
118  static constexpr bool is_idempotent() { return true; }
119 
120  static constexpr bool show_one() { return false; }
121  static constexpr star_status_t star_status()
122  {
124  }
125 
126  static value_t
128  {
129  return v;
130  }
131 
132  static size_t hash(value_t v)
133  {
134  return hash_value(v);
135  }
136 
137  static value_t
139  {
140  return v;
141  }
142 
143  static value_t
144  conv(std::istream& is)
145  {
146  int i;
147  if (is >> i)
148  {
149  if (i == 0 || i == 1)
150  return i;
151  else
152  fail_reading(is,
153  sname() + ": invalid value: " + std::to_string(i));
154  }
155  else
156  fail_reading(is, sname() + ": invalid value");
157  }
158 
159  static std::ostream&
160  print(const value_t v, std::ostream& o,
161  symbol format = symbol{"text"})
162  {
163  if (format == "latex")
164  o << (v ? "\\top" : "\\bot");
165  else
166  o << (v ? '1' : '0');
167  return o;
168  }
169 
170  std::ostream&
171  print_set(std::ostream& o, symbol format = symbol{"text"}) const
172  {
173  if (format == "latex")
174  o << "\\mathbb{B}";
175  else if (format == "text")
176  o << vname();
177  else
178  raise("invalid format: ", format);
179  return o;
180  }
181  };
182 
183  /*-------.
184  | join. |
185  `-------*/
186 
187  VCSN_JOIN_SIMPLE(b, b);
188  } // detail::
189 
190 }
191 #endif // !VCSN_WEIGHTSET_B_HH
static constexpr bool is_commutative()
Definition: b.hh:117
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: b.hh:95
static std::string sname()
Definition: b.hh:26
static bool is_zero(const value_t v)
Definition: b.hh:106
static value_t one()
Definition: b.hh:52
static value_t add(const value_t l, const value_t r)
Definition: b.hh:58
static bool is_one(const value_t v)
Definition: b.hh:112
static size_t hash(value_t v)
Definition: b.hh:132
static value_t conv(self_type, value_t v)
Definition: b.hh:138
std::string to_string(identities i)
Definition: identities.cc:13
static constexpr bool show_one()
Definition: b.hh:120
static constexpr star_status_t star_status()
Definition: b.hh:121
static constexpr bool is_special(value_t)
Definition: b.hh:100
boost::flyweight< std::string, boost::flyweights::no_tracking > symbol
An internalized string.
Definition: symbol.hh:24
variadic_mul_mixin< detail::b_impl > b
Definition: fwd.hh:38
static value_t ldiv(const value_t l, const value_t r)
Definition: b.hh:77
std::size_t hash_value(const T &v)
Definition: hash.hh:61
bool value_t
Definition: b.hh:43
static bool equals(const value_t l, const value_t r)
Definition: b.hh:89
static value_t transpose(const value_t v)
Definition: b.hh:127
static value_t conv(std::istream &is)
Definition: b.hh:144
std::string vname(bool=true) const
Definition: b.hh:31
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
star_status_t
Definition: star_status.hh:6
static value_t star(const value_t)
Definition: b.hh:83
static value_t mul(const value_t l, const value_t r)
Definition: b.hh:64
static b make(std::istream &is)
Build from the description in is.
Definition: b.hh:37
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
std::ostream & print_set(std::ostream &o, symbol format=symbol{"text"}) const
Definition: b.hh:171
static constexpr bool is_idempotent()
Definition: b.hh:118
static value_t zero()
Definition: b.hh:46
VCSN_JOIN_SIMPLE(b, b)
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static std::ostream & print(const value_t v, std::ostream &o, symbol format=symbol{"text"})
Definition: b.hh:160
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.cc:107
static value_t rdiv(const value_t l, const value_t r)
Definition: b.hh:70
variadic_mul_mixin< detail::r_impl > r
Definition: fwd.hh:42
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39