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