Vcsn  2.3
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  add(const value_t l, const value_t r)
54  {
55  return l ^ r;
56  }
57 
58  static value_t
59  sub(const value_t l, const value_t r)
60  {
61  return l ^ r;
62  }
63 
64  static value_t
65  mul(const value_t l, const value_t r)
66  {
67  return l && r;
68  }
69 
70  value_t
71  rdivide(const value_t l, const value_t r) const
72  {
73  require(!is_zero(r), *this, ": div: division by zero");
74  return l;
75  }
76 
77  value_t
78  ldivide(const value_t l, const value_t r) const
79  {
80  return rdivide(r, l);
81  }
82 
83  value_t
84  star(const value_t v) const
85  {
86  if (v == 0)
87  return one();
88  else
89  raise_not_starrable(*this, v);
90  }
91 
92  static bool
93  equal(const value_t l, const value_t r)
94  {
95  return l == r;
96  }
97 
99  static bool less(value_t lhs, value_t rhs)
100  {
101  return lhs < rhs;
102  }
103 
104  constexpr static bool is_special(value_t)
105  {
106  return false;
107  }
108 
109  static bool
111  {
112  return !v;
113  }
114 
115  static bool
116  is_one(const value_t v)
117  {
118  return v;
119  }
120 
121  static constexpr bool is_commutative() { return true; }
122  static constexpr bool is_idempotent() { return false; }
123  static constexpr bool has_lightening_weights() { return false; }
124 
125  static constexpr bool show_one() { return false; }
126 
127  static constexpr
129 
130  static value_t
132  {
133  return v;
134  }
135 
136  static size_t hash(value_t v)
137  {
138  return hash_value(v);
139  }
140 
141  static value_t
143  {
144  return v;
145  }
146 
147  value_t
148  conv(std::istream& is, bool = true) const
149  {
150  int i;
151  if (is >> i)
152  {
153  require(i == 0 || i == 1,
154  *this, ": invalid value: ", i);
155  return i;
156  }
157  else
158  raise(*this, ": invalid value: ", is);
159  }
160 
161  static std::ostream&
162  print(const value_t v, std::ostream& o = std::cout,
163  format = {})
164  {
165  return o << (v ? '1' : '0');
166  }
167 
168  std::ostream&
169  print_set(std::ostream& o, format fmt = {}) const
170  {
171  switch (fmt.kind())
172  {
173  case format::latex:
174  o << "\\mathbb{F}_2";
175  break;
176  case format::sname:
177  o << sname();
178  break;
179  case format::text:
180  o << "F2";
181  break;
182  case format::utf8:
183  o << "𝔽₂";
184  break;
185  case format::raw:
186  assert(0);
187  break;
188  }
189  return o;
190  }
191  };
192 
193  /*-------.
194  | join. |
195  `-------*/
196 
198  }// detail::
199 }
static bool less(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: f2.hh:99
value_t rdivide(const value_t l, const value_t r) const
Definition: f2.hh:71
static constexpr star_status_t star_status()
Definition: f2.hh:128
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static constexpr bool is_idempotent()
Definition: f2.hh:122
static value_t mul(const value_t l, const value_t r)
Definition: f2.hh:65
star_status_t
Definition: star-status.hh:5
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: f2.hh:169
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: raise.hh:100
weightset_mixin< detail::f2_impl > f2
Definition: fwd.hh:49
static constexpr bool has_lightening_weights()
Definition: f2.hh:123
Print as a parsable type string.
Definition: format.hh:26
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
static value_t zero()
Definition: f2.hh:41
return res
Definition: multiply.hh:398
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
value_t ldivide(const value_t l, const value_t r) const
Definition: f2.hh:78
static value_t conv(self_t, value_t v)
Definition: f2.hh:142
VCSN_JOIN_SIMPLE(b, b)
Definition: a-star.hh:8
static constexpr bool show_one()
Definition: f2.hh:125
static constexpr bool is_special(value_t)
Definition: f2.hh:104
An input/output format for valuesets.
Definition: format.hh:13
value_t star(const value_t v) const
Definition: f2.hh:84
static size_t hash(value_t v)
Definition: f2.hh:136
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
static symbol sname()
Definition: f2.hh:25
static value_t transpose(const value_t v)
Definition: f2.hh:131
static value_t sub(const value_t l, const value_t r)
Definition: f2.hh:59
value_t conv(std::istream &is, bool=true) const
Definition: f2.hh:148
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static bool is_one(const value_t v)
Definition: f2.hh:116
static constexpr bool is_commutative()
Definition: f2.hh:121
static bool equal(const value_t l, const value_t r)
Definition: f2.hh:93
static f2 make(std::istream &is)
Build from the description in is.
Definition: f2.hh:32
static bool is_zero(const value_t v)
Definition: f2.hh:110
Print for LaTeX.
Definition: format.hh:22
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format={})
Definition: f2.hh:162
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static value_t one()
Definition: f2.hh:47
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
static value_t add(const value_t l, const value_t r)
Definition: f2.hh:53
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54