Vcsn  2.1
Be Rational
letterset.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 
5 #include <vcsn/alphabets/setalpha.hh> // intersection
6 #include <vcsn/core/kind.hh>
8 #include <vcsn/misc/escape.hh>
9 #include <vcsn/misc/raise.hh>
10 #include <vcsn/misc/set.hh> // intersection
14 #include <vcsn/labelset/wordset.hh>
15 
16 namespace vcsn
17 {
19  template <typename GenSet>
20  class letterset: public detail::genset_labelset<GenSet>
21  {
22  public:
23  using genset_t = GenSet;
25  using self_t = letterset;
26  using genset_ptr = std::shared_ptr<const genset_t>;
27 
28  using letter_t = typename genset_t::letter_t;
29  using word_t = typename genset_t::word_t;
30  using letters_t = std::set<letter_t, vcsn::less<self_t, letter_t>>;
31 
32  using value_t = letter_t;
33  using values_t = std::set<value_t, vcsn::less<self_t>>;
34 
36 
37  letterset(const genset_ptr& gs)
38  : super_t{gs}
39  {}
40 
41  letterset(const genset_t& gs = {})
42  : letterset(std::make_shared<const genset_t>(gs))
43  {}
44 
45  letterset(const std::initializer_list<letter_t>& letters)
46  : letterset(std::make_shared<const genset_t>(letters))
47  {}
48 
49  static symbol sname()
50  {
51  static symbol res("letterset<" + super_t::sname() + '>');
52  return res;
53  }
54 
56  static letterset make(std::istream& is)
57  {
58  // name: letterset<char_letters(abc)>.
59  // ^^^^^^^^^ ^^^^^^^^^^^^^^^^^
60  // kind genset
61  eat(is, "letterset<");
62  auto gs = genset_t::make(is);
63  eat(is, '>');
64  return gs;
65  }
66 
70  bool open(bool o) const
71  {
72  return this->genset().open(o);
73  }
74 
75  static constexpr bool is_free()
76  {
77  return true;
78  }
79 
81  template <typename... Args>
82  value_t value(Args&&... args) const
83  {
84  return value_t{std::forward<Args>(args)...};
85  }
86 
88  static word_t word(value_t v)
89  {
90  return {v};
91  }
92 
94  static word_t
96  {
97  return v;
98  }
99 
102  static word_t
104  {
105  return v;
106  }
107 
109  static word_t
111  {
112  return word(v);
113  }
114 
117  static word_t
119  {
120  return word(v);
121  }
122 
123 
124  static value_t
125  special() ATTRIBUTE_PURE
126  {
127  return genset_t::template special<value_t>();
128  }
129 
131  static bool
132  equal(const value_t l, const value_t r)
133  {
134  return genset_t::equal(l, r);
135  }
136 
138  static bool less(const value_t l, const value_t r)
139  {
140  return genset_t::less(l, r);
141  }
142 
143  static constexpr bool
145  {
146  return false;
147  }
148 
149  static constexpr bool
151  {
152  return false;
153  }
154 
155  static constexpr bool
157  {
158  return true;
159  }
160 
161  static bool
162  is_special(value_t v) ATTRIBUTE_PURE
163  {
164  return v == special();
165  }
166 
167  static constexpr bool
169  {
170  return false;
171  }
172 
173  bool
175  {
176  return this->has(v);
177  }
178 
179  static size_t size(value_t)
180  {
181  return 1;
182  }
183 
184  static size_t hash(value_t v)
185  {
186  return hash_value(v);
187  }
188 
189  value_t
191  {
192  require(is_special(v) || is_valid(v),
193  *this, ": conv: invalid label: ", str_escape(v));
194  return v;
195  }
196 
198  template <typename LabelSet_>
199  value_t
201  typename nullableset<LabelSet_>::value_t v) const
202  {
203  require(!ls.is_one(v),
204  *this, ": conv: invalid label: \\e");
205  return conv(*ls.labelset(), ls.get_value(v));
206  }
207 
209  value_t
210  conv(std::istream& i, bool quoted = true) const
211  {
212  return this->get_letter(i, quoted);
213  }
214 
221  template <typename Fun>
222  void convs(std::istream& i, Fun fun) const
223  {
224  this->convs_(i, fun);
225  }
226 
244  {
245  raise(sname(), ": lgcd: impossible operation");
246  }
247 
250  {
251  raise(sname(), ": ldiv: impossible operation");
252  }
253 
254  std::ostream&
255  print(const value_t& l, std::ostream& o,
256  format fmt = {}) const
257  {
258  return this->genset().print(l, o, fmt);
259  }
260 
261  std::ostream&
262  print_set(std::ostream& o, format fmt = {}) const
263  {
264  if (fmt == format::latex)
265  this->genset().print_set(o, fmt);
266  else if (fmt == format::text)
267  {
268  o << "letterset<";
269  this->genset().print_set(o, fmt);
270  o << '>';
271  }
272  else
273  raise("invalid format: ", fmt);
274  return o;
275  }
276  };
277 
278  namespace detail
279  {
281  template <typename GenSet>
282  struct letterized_traits<letterset<GenSet>>
283  {
284  static constexpr bool is_letterized = true;
285 
286  using labelset_t = nullableset<letterset<GenSet>>;
287 
288  static labelset_t labelset(const letterset<GenSet>& ls)
289  {
290  return {ls.genset()};
291  }
292  };
293 
295  template <typename GenSet>
296  struct nullableset_traits<letterset<GenSet>>
297  {
298  using type = nullableset<letterset<GenSet>>;
299  static type value(const letterset<GenSet>& ls)
300  {
301  return ls;
302  }
303  };
304 
306  template <typename GenSet>
307  struct law_traits<letterset<GenSet>>
308  {
309  using type = wordset<GenSet>;
310  static type value(const letterset<GenSet>& ls)
311  {
312  return ls.genset();
313  }
314  };
315 
316  /*-------.
317  | Join. |
318  `-------*/
319 
320  template <typename GenSet>
321  struct join_impl<letterset<GenSet>, letterset<GenSet>>
322  {
323  using type = letterset<GenSet>;
324  static type join(const letterset<GenSet>& lhs,
325  const letterset<GenSet>& rhs)
326  {
327  return {get_union(lhs.genset(), rhs.genset())};
328  }
329  };
330  }
331 
333  template <typename GenSet>
334  letterset<GenSet>
335  meet(const letterset<GenSet>& lhs, const letterset<GenSet>& rhs)
336  {
337  return {intersection(lhs.genset(), rhs.genset())};
338  }
339 }
STL namespace.
static word_t letters_of_padded(word_t v, letter_t)
Prepare to iterate over the letters of v.
Definition: letterset.hh:103
bool open(bool o) const
Whether unknown letters should be added, or rejected.
Definition: letterset.hh:70
std::ostream & str_escape(std::ostream &os, const std::string &str)
Output a string, escaping special characters.
Definition: escape.cc:43
Implementation of labels are letters.
Definition: fwd.hh:10
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: letterset.hh:262
static word_t letters_of(letter_t v)
Prepare to iterate over v.
Definition: letterset.hh:110
This class has no modeling purpose, it only serves to factor code common to letterset, nullableset and wordset.
static word_t letters_of(word_t v)
Prepare to iterate over the letters of v.
Definition: letterset.hh:95
static value_t lgcd(value_t, value_t)
The longest common prefix.
Definition: letterset.hh:243
ATTRIBUTE_PURE auto has(Args &&...args) const -> decltype(this->genset().has(std::forward< Args >(args)...))
typename genset_t::letters_t letters_t
void convs(std::istream &i, Fun fun) const
Read a range of labels.
Definition: letterset.hh:222
typename helper_t::value_t value_t
Definition: nullableset.hh:166
static size_t size(value_t)
Definition: letterset.hh:179
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
static word_t letters_of_padded(letter_t v, letter_t)
Prepare to iterate over v.
Definition: letterset.hh:118
static value_t special() ATTRIBUTE_PURE
Definition: letterset.hh:125
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
std::shared_ptr< const genset_t > genset_ptr
static word_t word(value_t v)
Convert to a word.
Definition: letterset.hh:88
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
std::ostream & print(const value_t &l, std::ostream &o, format fmt={}) const
Definition: letterset.hh:255
letterset(const genset_t &gs={})
Definition: letterset.hh:41
letterset(const std::initializer_list< letter_t > &letters)
Definition: letterset.hh:45
static bool less(const value_t l, const value_t r)
Whether l < r.
Definition: letterset.hh:138
std::set< value_t, vcsn::less< self_t >> values_t
Definition: letterset.hh:33
bool is_valid(value_t v) const
Definition: letterset.hh:174
static letterset make(std::istream &is)
Build from the description in is.
Definition: letterset.hh:56
const genset_t & genset() const
An input/output format.
Definition: format.hh:11
static ATTRIBUTE_PURE bool is_one(value_t l)
Definition: nullableset.hh:242
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static constexpr bool is_letterized()
Definition: letterset.hh:156
static bool is_special(value_t v) ATTRIBUTE_PURE
Definition: letterset.hh:162
static constexpr bool is_expressionset()
Definition: letterset.hh:150
static value_t ldiv(value_t, value_t)
Compute w1 \ w2 = w1^{-1}w2.
Definition: letterset.hh:249
value_t conv(const nullableset< LabelSet_ > &ls, typename nullableset< LabelSet_ >::value_t v) const
Convert from nullableset to letterset.
Definition: letterset.hh:200
typename genset_t::word_t word_t
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
const labelset_ptr labelset() const
Definition: nullableset.hh:283
value_t conv(self_t, value_t v) const
Definition: letterset.hh:190
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static size_t hash(value_t v)
Definition: letterset.hh:184
static bool equal(const value_t l, const value_t r)
Whether l == r.
Definition: letterset.hh:132
static symbol sname()
Definition: letterset.hh:49
value_t value(Args &&...args) const
Value constructor.
Definition: letterset.hh:82
letter_t value_t
Definition: letterset.hh:32
letterset(const genset_ptr &gs)
Definition: letterset.hh:37
typename genset_t::letter_t letter_t
Definition: letterset.hh:28
typename genset_t::word_t word_t
Definition: letterset.hh:29
static labelset_t::value_t get_value(const value_t &v)
The (inner) value when it (the outer value) is not one.
Definition: nullableset.hh:495
static constexpr bool has_one()
Definition: letterset.hh:144
static constexpr bool is_free()
Definition: letterset.hh:75
value_t conv(std::istream &i, bool quoted=true) const
Read one letter from i, return the corresponding label.
Definition: letterset.hh:210
letter_t get_letter(std::istream &i, bool quoted=true) const
Read one letter from i.
typename genset_t::letter_t letter_t
static constexpr bool is_one(value_t)
Definition: letterset.hh:168
void convs_(std::istream &i, Fun fun) const
Read and process a class of letters.
Implementation of labels are nullables (letter or empty).
Definition: fwd.hh:14