Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
f2.hh
Go to the documentation of this file.
1 #ifndef VCSN_WEIGHTSET_F2_HH
2 # define VCSN_WEIGHTSET_F2_HH
3 
4 # include <cassert>
5 # include <ostream>
6 
7 # include <vcsn/core/join.hh>
8 # include <vcsn/misc/escape.hh>
9 # include <vcsn/misc/hash.hh>
10 # include <vcsn/misc/raise.hh>
11 # include <vcsn/misc/star_status.hh>
12 # include <vcsn/misc/stream.hh>
14 
15 namespace vcsn
16 {
17  namespace detail
18  {
19  class f2_impl
20  {
21  public:
22  using self_type = f2;
23 
24  static std::string sname()
25  {
26  return "f2";
27  }
28 
29  std::string vname(bool = true) const
30  {
31  return sname();
32  }
33 
35  static f2 make(std::istream& is)
36  {
37  eat(is, sname());
38  return {};
39  }
40 
41  using value_t = bool;
42 
43  static value_t
44  zero()
45  {
46  return false;
47  }
48 
49  static value_t
50  one()
51  {
52  return true;
53  }
54 
55  static value_t
56  add(const value_t l, const value_t r)
57  {
58  return l ^ r;
59  }
60 
61  static value_t
62  sub(const value_t l, const value_t r)
63  {
64  return l ^ r;
65  }
66 
67  static value_t
68  mul(const value_t l, const value_t r)
69  {
70  return l && r;
71  }
72 
73  static value_t
74  rdiv(const value_t l, const value_t r)
75  {
76  require(!is_zero(r), "div: division by zero");
77  return l;
78  }
79 
80  static value_t
81  ldiv(const value_t l, const value_t r)
82  {
83  return rdiv(r, l);
84  }
85 
86  static value_t
87  star(const value_t v)
88  {
89  require(v != 1, "f2: star: invalid value: 1");
90  return true;
91  }
92 
93  static bool
94  equals(const value_t l, const value_t r)
95  {
96  return l == r;
97  }
98 
100  static bool less_than(value_t lhs, value_t rhs)
101  {
102  return lhs < rhs;
103  }
104 
105  constexpr static bool is_special(value_t)
106  {
107  return false;
108  }
109 
110  static bool
111  is_zero(const value_t v)
112  {
113  return !v;
114  }
115 
116  static bool
117  is_one(const value_t v)
118  {
119  return v;
120  }
121 
122  static constexpr bool is_commutative() { return true; }
123  static constexpr bool is_idempotent() { 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  static value_t
148  conv(std::istream& stream)
149  {
150  int i;
151  if ((stream >> i) && (i == 0 || i == 1))
152  return i;
153  else
154  vcsn::fail_reading(stream, sname() + ": invalid value");
155  }
156 
157  static std::ostream&
158  print(const value_t v, std::ostream& o,
159  const std::string& = "text")
160  {
161  return o << (v ? '1' : '0');
162  }
163 
164  std::ostream&
165  print_set(std::ostream& o, symbol format = symbol{"text"}) const
166  {
167  if (format == "latex")
168  o << "\\mathbb{F}_2";
169  else if (format == "text")
170  o << vname();
171  else
172  raise("invalid format: ", format);
173  return o;
174  }
175  };
176 
177  /*-------.
178  | join. |
179  `-------*/
180 
182  }// detail::
183 }
184 
185 #endif // !VCSN_WEIGHTSET_F2_HH
static bool is_zero(const value_t v)
Definition: f2.hh:111
static value_t conv(std::istream &stream)
Definition: f2.hh:148
static value_t mul(const value_t l, const value_t r)
Definition: f2.hh:68
std::ostream & print_set(std::ostream &o, symbol format=symbol{"text"}) const
Definition: f2.hh:165
static constexpr star_status_t star_status()
Definition: f2.hh:128
boost::flyweight< std::string, boost::flyweights::no_tracking > symbol
An internalized string.
Definition: symbol.hh:24
static value_t rdiv(const value_t l, const value_t r)
Definition: f2.hh:74
static constexpr bool show_one()
Definition: f2.hh:125
static f2 make(std::istream &is)
Build from the description in is.
Definition: f2.hh:35
std::size_t hash_value(const T &v)
Definition: hash.hh:61
static std::string sname()
Definition: f2.hh:24
static bool equals(const value_t l, const value_t r)
Definition: f2.hh:94
static value_t zero()
Definition: f2.hh:44
static value_t one()
Definition: f2.hh:50
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: f2.hh:100
static value_t star(const value_t v)
Definition: f2.hh:87
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
static value_t sub(const value_t l, const value_t r)
Definition: f2.hh:62
static size_t hash(value_t v)
Definition: f2.hh:136
static bool is_one(const value_t v)
Definition: f2.hh:117
star_status_t
Definition: star_status.hh:6
static constexpr bool is_special(value_t)
Definition: f2.hh:105
static value_t ldiv(const value_t l, const value_t r)
Definition: f2.hh:81
static constexpr bool is_commutative()
Definition: f2.hh:122
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
variadic_mul_mixin< detail::f2_impl > f2
Definition: fwd.hh:39
VCSN_JOIN_SIMPLE(b, b)
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static value_t add(const value_t l, const value_t r)
Definition: f2.hh:56
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.cc:107
static std::ostream & print(const value_t v, std::ostream &o, const std::string &="text")
Definition: f2.hh:158
std::string vname(bool=true) const
Definition: f2.hh:29
static value_t transpose(const value_t v)
Definition: f2.hh:131
static constexpr bool is_idempotent()
Definition: f2.hh:123
variadic_mul_mixin< detail::r_impl > r
Definition: fwd.hh:42
static value_t conv(self_type, value_t v)
Definition: f2.hh:142
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39