Vcsn  2.8
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 
105  static int compare(const value_t l, const value_t r)
106  {
107  return l - r;
108  }
109 
111  static bool
112  equal(const value_t l, const value_t r)
113  {
114  return l == r;
115  }
116 
118  static bool less(value_t l, value_t r)
119  {
120  return l < r;
121  }
122 
123  constexpr static bool is_special(value_t)
124  {
125  return false;
126  }
127 
128  static bool
130  {
131  return !v;
132  }
133 
134  static bool
135  is_one(const value_t v)
136  {
137  return v;
138  }
139 
140  static constexpr bool is_commutative() { return true; }
141  static constexpr bool is_idempotent() { return false; }
142  static constexpr bool has_lightening_weights() { return false; }
143 
144  static constexpr bool show_one() { return false; }
145 
146  static constexpr
148 
149  static value_t
151  {
152  return v;
153  }
154 
155  static size_t hash(value_t v)
156  {
157  return hash_value(v);
158  }
159 
160  static value_t
162  {
163  return v;
164  }
165 
166  value_t
167  conv(std::istream& is, bool = true) const
168  {
169  int i;
170  if (is >> i)
171  {
172  if (i != 0 && i != 1)
173  raise_invalid_value(*this, i);
174  return i;
175  }
176  else
177  raise_invalid_value(*this, is);
178  }
179 
180  static std::ostream&
181  print(const value_t v, std::ostream& o = std::cout,
182  format = {})
183  {
184  return o << (v ? '1' : '0');
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{F}_2";
194  break;
195  case format::sname:
196  o << sname();
197  break;
198  case format::text:
199  o << "F2";
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 
213  template <typename RandomGenerator>
214  class random_weight<f2, RandomGenerator>
215  : public random_weight_base<f2, RandomGenerator>
216  {
217  public:
219  using value_t = typename super_t::weight_t;
220 
221  using super_t::super_t;
222 
223  private:
225  {
226  auto dis = std::uniform_int_distribution<>(super_t::min_, super_t::max_);
227  if (dis(super_t::gen_))
228  return super_t::ws_.zero();
229  else
230  return super_t::ws_.one();
231  }
232  };
233 
234  /*-------.
235  | join. |
236  `-------*/
237 
239  }// detail::
240 }
static f2 make(std::istream &is)
Build from the description in is.
Definition: f2.hh:32
Print as a parsable type string.
Definition: format.hh:26
static int compare(const value_t l, const value_t r)
Three-way comparison between l and r.
Definition: f2.hh:105
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static bool less(value_t l, value_t r)
Whether lhs < rhs.
Definition: f2.hh:118
static constexpr star_status_t star_status()
Definition: f2.hh:147
static value_t max()
Definition: f2.hh:59
static value_t mul(const value_t l, const value_t r)
Definition: f2.hh:77
typename super_t::weight_t value_t
Definition: f2.hh:219
ATTRIBUTE_NORETURN void raise_invalid_value(const ValueSet &vs, Args &&... args)
Cannot make a value from this.
Definition: weightset.hh:227
weightset_mixin< detail::f2_impl > f2
Definition: fwd.hh:49
static constexpr bool show_one()
Definition: f2.hh:144
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:147
static size_t hash(value_t v)
Definition: f2.hh:155
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
static bool is_zero(const value_t v)
Definition: f2.hh:129
static value_t sub(const value_t l, const value_t r)
Definition: f2.hh:71
value_t ldivide(const value_t l, const value_t r) const
Definition: f2.hh:90
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
static value_t transpose(const value_t v)
Definition: f2.hh:150
value_t conv(std::istream &is, bool=true) const
Definition: f2.hh:167
static value_t conv(self_t, value_t v)
Definition: f2.hh:161
static constexpr bool is_idempotent()
Definition: f2.hh:141
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:45
static value_t add(const value_t l, const value_t r)
Definition: f2.hh:65
static value_t min()
Definition: f2.hh:53
Definition: a-star.hh:8
star_status_t
Definition: star-status.hh:5
static constexpr bool is_commutative()
Definition: f2.hh:140
static bool equal(const value_t l, const value_t r)
Whether l == r.
Definition: f2.hh:112
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format={})
Definition: f2.hh:181
static value_t zero()
Definition: f2.hh:41
value_t rdivide(const value_t l, const value_t r) const
Definition: f2.hh:83
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: weightset.hh:235
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: f2.hh:188
static symbol sname()
Definition: f2.hh:25
Print as is. For instance, don&#39;t try to escape labels.
Definition: format.hh:24
Print for LaTeX.
Definition: format.hh:22
value_t star(const value_t v) const
Definition: f2.hh:96
static constexpr bool is_special(value_t)
Definition: f2.hh:123
VCSN_JOIN_SIMPLE(b, b)
Generic declaration of the class which is specialized in each weightset.
Definition: weightset.hh:208
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:87
static bool is_one(const value_t v)
Definition: f2.hh:135
static value_t one()
Definition: f2.hh:47
return res
Definition: multiply.hh:399
static constexpr bool has_lightening_weights()
Definition: f2.hh:142