Vcsn  2.1
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 symbol res("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 
122  static constexpr bool show_one() { return false; }
123 
124  static constexpr
126 
127  static value_t
129  {
130  return v;
131  }
132 
133  static size_t hash(value_t v)
134  {
135  return hash_value(v);
136  }
137 
138  static value_t
140  {
141  return v;
142  }
143 
144  static value_t
145  conv(std::istream& is, bool = true)
146  {
147  int i;
148  if (is >> i)
149  {
150  require(i == 0 || i == 1,
151  sname(), ": invalid value: ", i);
152  return i;
153  }
154  else
155  raise(sname(), ": invalid value: ", is);
156  }
157 
158  static std::ostream&
159  print(const value_t v, std::ostream& o,
160  format = {})
161  {
162  return o << (v ? '1' : '0');
163  }
164 
165  std::ostream&
166  print_set(std::ostream& o, format fmt = {}) const
167  {
168  if (fmt == format::latex)
169  o << "\\mathbb{F}_2";
170  else if (fmt == format::text)
171  o << sname();
172  else
173  raise("invalid format: ", fmt);
174  return o;
175  }
176  };
177 
178  /*-------.
179  | join. |
180  `-------*/
181 
183  }// detail::
184 }
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 value_t conv(std::istream &is, bool=true)
Definition: f2.hh:145
static value_t ldiv(const value_t l, const value_t r)
Definition: f2.hh:78
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static std::ostream & print(const value_t v, std::ostream &o, format={})
Definition: f2.hh:159
static value_t rdiv(const value_t l, const value_t r)
Definition: f2.hh:71
static symbol sname()
Definition: f2.hh:25
static value_t sub(const value_t l, const value_t r)
Definition: f2.hh:59
static f2 make(std::istream &is)
Build from the description in is.
Definition: f2.hh:32
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: f2.hh:166
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static size_t hash(value_t v)
Definition: f2.hh:133
static value_t mul(const value_t l, const value_t r)
Definition: f2.hh:65
static constexpr star_status_t star_status()
Definition: f2.hh:125
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
static value_t add(const value_t l, const value_t r)
Definition: f2.hh:53
An input/output format.
Definition: format.hh:11
static value_t transpose(const value_t v)
Definition: f2.hh:128
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
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:75
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 constexpr bool is_special(value_t)
Definition: f2.hh:102
static bool is_one(const value_t v)
Definition: f2.hh:114
static value_t conv(self_t, value_t v)
Definition: f2.hh:139
static constexpr bool show_one()
Definition: f2.hh:122
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
star_status_t
Definition: star-status.hh:5
VCSN_JOIN_SIMPLE(b, b)
static value_t star(const value_t v)
Definition: f2.hh:84
static constexpr bool is_idempotent()
Definition: f2.hh:120
weightset_mixin< detail::f2_impl > f2
Definition: fwd.hh:49
static value_t one()
Definition: f2.hh:47