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