Vcsn  2.4
Be Rational
f2.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <ostream>
5 
6 #include <vcsn/core/join.hh>
7 #include <vcsn/misc/escape.hh>
8 #include <vcsn/misc/format.hh>
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/fwd.hh> // f2
15 
16 namespace vcsn
17 {
18  namespace detail
19  {
20  class f2_impl
21  {
22  public:
23  using self_t = f2;
24 
25  static symbol sname()
26  {
27  static auto res = symbol{"f2"};
28  return res;
29  }
30 
32  static f2 make(std::istream& is)
33  {
34  eat(is, sname());
35  return {};
36  }
37 
38  using value_t = bool;
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  static value_t
71  sub(const value_t l, const value_t r)
72  {
73  return l ^ r;
74  }
75 
76  static value_t
77  mul(const value_t l, const value_t r)
78  {
79  return l && r;
80  }
81 
82  value_t
83  rdivide(const value_t l, const value_t r) const
84  {
85  require(!is_zero(r), *this, ": div: division by zero");
86  return l;
87  }
88 
89  value_t
90  ldivide(const value_t l, const value_t r) const
91  {
92  return rdivide(r, l);
93  }
94 
95  value_t
96  star(const value_t v) const
97  {
98  if (v == 0)
99  return one();
100  else
101  raise_not_starrable(*this, v);
102  }
103 
104  static bool
105  equal(const value_t l, const value_t r)
106  {
107  return l == r;
108  }
109 
111  static bool less(value_t lhs, value_t rhs)
112  {
113  return lhs < rhs;
114  }
115 
116  constexpr static bool is_special(value_t)
117  {
118  return false;
119  }
120 
121  static bool
123  {
124  return !v;
125  }
126 
127  static bool
128  is_one(const value_t v)
129  {
130  return v;
131  }
132 
133  static constexpr bool is_commutative() { return true; }
134  static constexpr bool is_idempotent() { return false; }
135  static constexpr bool has_lightening_weights() { return false; }
136 
137  static constexpr bool show_one() { return false; }
138 
139  static constexpr
141 
142  static value_t
144  {
145  return v;
146  }
147 
148  static size_t hash(value_t v)
149  {
150  return hash_value(v);
151  }
152 
153  static value_t
155  {
156  return v;
157  }
158 
159  value_t
160  conv(std::istream& is, bool = true) const
161  {
162  int i;
163  if (is >> i)
164  {
165  require(i == 0 || i == 1,
166  *this, ": invalid value: ", i);
167  return i;
168  }
169  else
170  raise(*this, ": invalid value: ", is);
171  }
172 
173  static std::ostream&
174  print(const value_t v, std::ostream& o = std::cout,
175  format = {})
176  {
177  return o << (v ? '1' : '0');
178  }
179 
180  std::ostream&
181  print_set(std::ostream& o, format fmt = {}) const
182  {
183  switch (fmt.kind())
184  {
185  case format::latex:
186  o << "\\mathbb{F}_2";
187  break;
188  case format::sname:
189  o << sname();
190  break;
191  case format::text:
192  o << "F2";
193  break;
194  case format::utf8:
195  o << "𝔽₂";
196  break;
197  case format::raw:
198  assert(0);
199  break;
200  }
201  return o;
202  }
203  };
204 
206  template <typename RandomGenerator>
207  class random_weight<f2, RandomGenerator>
208  : public random_weight_base<f2, RandomGenerator>
209  {
210  public:
212  using value_t = typename super_t::weight_t;
213 
214  using super_t::super_t;
215 
216  private:
218  {
219  auto dis = std::uniform_int_distribution<>(super_t::min_, super_t::max_);
220  if (dis(super_t::gen_))
221  return super_t::ws_.zero();
222  else
223  return super_t::ws_.one();
224  }
225  };
226 
227  /*-------.
228  | join. |
229  `-------*/
230 
232  }// detail::
233 }
weightset_mixin< detail::f2_impl > f2
Definition: fwd.hh:49
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
static value_t mul(const value_t l, const value_t r)
Definition: f2.hh:77
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
static bool is_one(const value_t v)
Definition: f2.hh:128
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
static constexpr bool show_one()
Definition: f2.hh:137
static bool equal(const value_t l, const value_t r)
Definition: f2.hh:105
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format={})
Definition: f2.hh:174
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: f2.hh:181
An input/output format for valuesets.
Definition: format.hh:13
static value_t min()
Definition: f2.hh:53
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
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
static symbol sname()
Definition: f2.hh:25
static value_t add(const value_t l, const value_t r)
Definition: f2.hh:65
static value_t sub(const value_t l, const value_t r)
Definition: f2.hh:71
typename super_t::weight_t value_t
Definition: f2.hh:212
Definition: a-star.hh:8
static constexpr star_status_t star_status()
Definition: f2.hh:140
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static bool is_zero(const value_t v)
Definition: f2.hh:122
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static value_t zero()
Definition: f2.hh:41
static value_t conv(self_t, value_t v)
Definition: f2.hh:154
static value_t transpose(const value_t v)
Definition: f2.hh:143
static size_t hash(value_t v)
Definition: f2.hh:148
star_status_t
Definition: star-status.hh:5
value_t rdivide(const value_t l, const value_t r) const
Definition: f2.hh:83
value_t ldivide(const value_t l, const value_t r) const
Definition: f2.hh:90
static constexpr bool is_special(value_t)
Definition: f2.hh:116
static f2 make(std::istream &is)
Build from the description in is.
Definition: f2.hh:32
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: raise.hh:100
static constexpr bool is_commutative()
Definition: f2.hh:133
static constexpr bool is_idempotent()
Definition: f2.hh:134
value_t conv(std::istream &is, bool=true) const
Definition: f2.hh:160
static value_t one()
Definition: f2.hh:47
value_t star(const value_t v) const
Definition: f2.hh:96
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: f2.hh:111
static constexpr bool has_lightening_weights()
Definition: f2.hh:135
static value_t max()
Definition: f2.hh:59