Vcsn  2.2
Be Rational
nullableset.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <set>
5 #include <sstream>
6 
7 #include <vcsn/alphabets/setalpha.hh> // intersect
8 #include <vcsn/core/kind.hh>
9 #include <vcsn/labelset/fwd.hh>
12 #include <vcsn/labelset/oneset.hh>
13 #include <vcsn/misc/escape.hh>
14 #include <vcsn/misc/functional.hh>
15 #include <vcsn/misc/raise.hh>
16 
17 namespace vcsn
18 {
19  namespace detail
20  {
27  template <typename LabelSet>
29  {
30  using labelset_t = LabelSet;
31  using value_t = std::pair<typename labelset_t::value_t, bool>;
32 
33  ATTRIBUTE_PURE
34  static constexpr value_t
35  one()
36  {
37  return value_t{labelset_t::special(), true};
38  }
39 
40  ATTRIBUTE_PURE
41  static constexpr value_t
43  {
44  return value_t{labelset_t::special(), false};
45  }
46 
47  template <typename Ls>
48  ATTRIBUTE_PURE
49  static std::enable_if_t<Ls::has_one(), bool>
50  is_one_(const value_t& l)
51  {
52  return std::get<1>(l) || Ls::is_one(get_value(l));
53  }
54 
55  template <typename Ls>
56  ATTRIBUTE_PURE
57  static std::enable_if_t<!Ls::has_one(), bool>
58  is_one_(const value_t& l)
59  {
60  return std::get<1>(l);
61  }
62 
63  ATTRIBUTE_PURE
64  static bool
65  is_one(const value_t& l)
66  {
67  return is_one_<labelset_t>(l);
68  }
69 
70  template <typename Value>
71  static Value
72  transpose(const labelset_t& ls, Value l)
73  {
74  // This is not robust enough: Value is word_t, then it's not
75  // valid to call is_one on it. Since I'm unsure of the future
76  // of nullableset, let's not waste time on this now.
77  if (is_one(l))
78  return l;
79  else
80  return {ls.transpose(get_value(l)), false};
81  }
82 
83  template <typename... Args>
84  static value_t
85  value(const labelset_t& ls, Args&&... args)
86  {
87  return {ls.value(std::forward<Args>(args)...), false};
88  }
89 
90  ATTRIBUTE_PURE
91  static typename labelset_t::value_t
93  {
94  return std::get<0>(v);
95  }
96  };
97 
104  template <typename GenSet>
105  struct nullable_helper<letterset<GenSet>>
106  {
107  using genset_t = GenSet;
109  using value_t = typename labelset_t::value_t;
110 
111  ATTRIBUTE_PURE
112  static constexpr value_t
114  {
115  return genset_t::special();
116  }
117 
118  ATTRIBUTE_PURE
119  static constexpr value_t
120  one()
121  {
122  return genset_t::one_letter();
123  }
124 
125  ATTRIBUTE_PURE
126  static bool
128  {
129  return l == one();
130  }
131 
132  template <typename Value>
133  static Value
134  transpose(const labelset_t& ls, const Value& l)
135  {
136  return ls.transpose(l);
137  }
138 
139  template <typename... Args>
140  static value_t
141  value(const labelset_t& ls, Args&&... args)
142  {
143  return ls.value(std::forward<Args>(args)...);
144  }
145 
146  ATTRIBUTE_PURE
147  static value_t
149  {
150  return v;
151  }
152  };
153  }
154 
156  template <typename LabelSet>
157  class nullableset
158  {
159  public:
160  using labelset_t = LabelSet;
161  using labelset_ptr = std::shared_ptr<const labelset_t>;
165 
166  using value_t = typename helper_t::value_t;
167  using letter_t = typename labelset_t::letter_t;
168  using word_t = typename labelset_t::word_t;
169 
170  private:
174 
175  public:
176  using genset_ptr = decltype(ls_->genset());
177  using genset_t = decltype(ls_->generators());
178 
179  public:
181  : ls_{ls}
182  {}
183 
184  nullableset(const labelset_t& ls = {})
185  : nullableset{std::make_shared<const labelset_t>(ls)}
186  {}
187 
188  static symbol sname()
189  {
190  static auto res = symbol{"nullableset<" + labelset_t::sname() + '>'};
191  return res;
192  }
193 
195  static nullableset make(std::istream& is)
196  {
197  // name: nullableset<lal_char(abc)>.
198  // ^^^^^^^^^^^^
199  // labelset
200  eat(is, "nullableset<");
201  auto ls = labelset_t::make(is);
202  eat(is, '>');
203  return {ls};
204  }
205 
209  bool open(bool o) const
210  {
211  return this->ls_->open(o);
212  }
213 
214  static constexpr bool
216  {
217  return true;
218  }
219 
220  static constexpr bool
222  {
223  return false;
224  }
225 
226  static constexpr bool
228  {
229  return labelset_t::is_letterized();
230  }
231 
232  static constexpr bool is_free()
233  {
234  return false;
235  }
236 
237  ATTRIBUTE_PURE
238  static constexpr value_t
239  one()
240  {
241  return helper_t::one();
242  }
243 
244  ATTRIBUTE_PURE
245  static bool
247  {
248  return helper_t::is_one(l);
249  }
250 
251  bool
253  {
254  return is_one(v) || ls_->is_valid(get_value(v));
255  }
256 
257  genset_ptr
258  genset() const
259  {
260  return ls_->genset();
261  }
262 
264  genset_t
265  generators() const
266  {
267  return ls_->generators();
268  }
269 
270  value_t
272  {
273  return v;
274  }
275 
276  value_t
277  conv(oneset, typename oneset::value_t) const
278  {
279  return one();
280  }
281 
285  template <typename LabelSet_>
286  value_t
287  conv(const LabelSet_& ls, typename LabelSet_::value_t v) const
288  {
289  return value(ls_->conv(ls, v));
290  }
291 
292  const labelset_ptr labelset() const
293  {
294  return ls_;
295  }
296 
297  template <typename... Args>
298  value_t
299  value(Args&&... args) const
300  {
301  return helper_t::value(*ls_, std::forward<Args>(args)...);
302  }
303 
304  word_t
305  word(const value_t& l) const
306  {
307  if (is_one(l))
308  return make_wordset(*labelset()).one();
309  else
310  return ls_->word(get_value(l));
311  }
312 
314  auto static
316  -> decltype(labelset_t::letters_of(v))
317  {
318  return labelset_t::letters_of(v);
319  }
320 
323  auto
325  -> decltype(ls_->letters_of_padded(v, l))
326  {
327  return ls_->letters_of_padded(v, l);
328  }
329 
330  auto
332  -> decltype(this->letters_of_padded(this->word(v), l))
333  {
334  return letters_of_padded(word(v), l);
335  }
336 
338  static bool equal(const value_t& l, const value_t& r)
339  {
340  if (is_one(l))
341  return is_one(r);
342  else
343  return !is_one(r) && labelset_t::equal(get_value(l), get_value(r));
344  }
345 
347  static bool less(const value_t& l, const value_t& r)
348  {
349  if (is_one(r))
350  return false;
351  else if (is_one(l))
352  return true;
353  else
354  return labelset_t::less(get_value(l), get_value(r));
355  }
356 
358  value_t lgcd(const value_t& l, const value_t& r) const
359  {
360  if (equal(l, r))
361  return l;
362  else if (is_one(l) || is_one(r))
363  return one();
364  else
365  return value(ls_->lgcd(get_value(l), get_value(r)));
366  }
367 
369  value_t ldiv(const value_t& l, const value_t& r) const
370  {
371  if (equal(l, r))
372  return one();
373  else if (is_one(l))
374  return r;
375  else if (is_one(r))
376  raise(*this, ": ldiv: invalid arguments: ",
377  to_string(*this, l), ", ", to_string(*this, r));
378  else
379  return value(ls_->ldiv(get_value(l), get_value(r)));
380  }
381 
382  static value_t
384  {
385  return helper_t::special();
386  }
387 
388  static bool
390  {
391  return !is_one(v) && labelset_t::is_special(get_value(v));
392  }
393 
394  static size_t size(const value_t& v)
395  {
396  return is_one(v) ? 0 : labelset_t::size(get_value(v));
397  }
398 
399  // FIXME: specialize for both implementation.
400  static size_t hash(const value_t& v)
401  {
402  // Do not use get_value when is_one. Let's hash one() as 0.
403  return is_one(v) ? 0 : labelset_t::hash(get_value(v));
404  }
405 
406  letter_t
407  get_letter(std::istream& i, bool quoted = true) const
408  {
409  return ls_->get_letter(i, quoted);
410  }
411 
413  value_t
414  conv(std::istream& i, bool quoted = true) const
415  {
416  // Check for '\e', otherwise pass it to the inner labelset.
417  if (i.good() && i.peek() == '\\')
418  {
419  i.ignore();
420  int c = i.peek();
421  if (c == 'e')
422  {
423  i.ignore();
424  return {};
425  }
426  else
427  i.unget();
428  }
429  return value(ls_->conv(i, quoted));
430  }
431 
441  template <typename Fun>
442  void convs(std::istream& i, Fun&& fun) const
443  {
444  ls_->convs(i,
445  [this,fun](const typename labelset_t::value_t& l)
446  {
447  fun(value(l));
448  });
449  }
450 
451  value_t conjunction(const value_t& l, const value_t& r) const
452  {
453  if (is_one(l) && is_one(r))
454  return l;
455  else if (!is_one(l) && !is_one(r))
456  return ls_->conjunction(get_value(l), get_value(r));
457  else
458  raise("conjunction: invalid operation (lhs and rhs are not equal)");
459  }
460 
462  std::ostream&
463  print(const value_t& l, std::ostream& o,
464  format fmt = {}) const
465  {
466  if (is_one(l))
467  o << (fmt == format::latex ? "\\varepsilon"
468  : fmt == format::utf8 ? "ε"
469  : "\\e");
470  else
471  ls_->print(get_value(l), o, fmt);
472  return o;
473  }
474 
475  value_t
476  zero() const
477  {
478  return value(ls_->zero());
479  }
480 
481  bool
482  is_zero(const value_t& v) const
483  {
484  return !is_one(v) && ls_->is_zero(get_value(v));
485  }
486 
487  bool
488  is_letter(const value_t& v) const
489  {
490  return !is_one(v) && ls_->is_letter(get_value(v));
491  }
492 
494  template <typename Value>
495  Value
496  transpose(const Value& l) const
497  {
498  return helper_t::transpose(*ls_, l);
499  }
500 
502  std::ostream&
503  print_set(std::ostream& o, format fmt = {}) const
504  {
505  switch (fmt.kind())
506  {
507  case format::latex:
508  o << "(";
509  ls_->print_set(o, fmt);
510  o << ")^?";
511  break;
512 
513  case format::sname:
514  o << "nullableset<";
515  ls_->print_set(o, fmt);
516  o << '>';
517  break;
518 
519  case format::text:
520  case format::utf8:
521  ls_->print_set(o, fmt);
522  o << '?';
523  break;
524 
525  case format::raw:
526  assert(0);
527  break;
528  }
529 
530  return o;
531  }
532 
534  static typename labelset_t::value_t
535  get_value(const value_t& v)
536  {
537  assert(!is_one(v));
538  return helper_t::get_value(v);
539  }
540  };
541 
542  namespace detail
543  {
545  template <typename LabelSet>
546  struct letterized_traits<nullableset<LabelSet>>
547  {
548  using traits = letterized_traits<LabelSet>;
549  static constexpr bool is_letterized = traits::is_letterized;
550 
551  using labelset_t = typename traits::labelset_t;
552 
553  static labelset_t labelset(const nullableset<LabelSet>& ls)
554  {
555  return make_letterized(*ls.labelset());
556  }
557  };
558 
560  template <typename LabelSet>
561  struct nullableset_traits<nullableset<LabelSet>>
562  {
563  using type = nullableset<LabelSet>;
564  static type value(const nullableset<LabelSet>& ls)
565  {
566  return ls;
567  }
568  };
569 
570  template <typename LabelSet>
571  struct proper_traits<nullableset<LabelSet>>
572  {
573  using type = LabelSet;
574  static type value(const nullableset<LabelSet>& ls)
575  {
576  return *ls.labelset();
577  }
578  };
579 
581  template <typename LabelSet>
582  struct law_traits<nullableset<LabelSet>>
583  {
584  using type = law_t<LabelSet>;
585  static type value(const nullableset<LabelSet>& ls)
586  {
587  return make_wordset(*ls.labelset());
588  }
589  };
590 
591  /*-------.
592  | Join. |
593  `-------*/
594 
596  template <typename LS>
597  struct join_impl<oneset, LS,
598  std::enable_if_t<!LS::has_one()>>
599  {
600  using type = nullableset<LS>;
601  static type join(const oneset&, const LS& ls)
602  {
603  return {ls};
604  }
605  };
606 
608  template <typename LS>
609  struct join_impl<oneset, LS,
610  std::enable_if_t<LS::has_one()>>
611  {
612  using type = LS;
613  static type join(const oneset&, const LS& ls)
614  {
615  return ls;
616  }
617  };
618 
620  template <typename LS1, typename LS2>
621  struct join_impl<nullableset<LS1>, LS2>
622  {
623  using type = nullableset<join_t<LS1, LS2>>;
624  static type join(const nullableset<LS1>& ls1, const LS2& ls2)
625  {
626  return {::vcsn::join(*ls1.labelset(), ls2)};
627  }
628  };
629 
634  template <typename LS1, typename LS2>
635  struct join_impl<nullableset<LS1>, nullableset<LS2>>
636  {
637  using type = nullableset<join_t<LS1, LS2>>;
638  static type join(const nullableset<LS1>& ls1,
639  const nullableset<LS2>& ls2)
640  {
641  return {::vcsn::join(*ls1.labelset(), *ls2.labelset())};
642  }
643  };
644  }
645 
646  /*-------.
647  | Meet. |
648  `-------*/
649 
650 #define DEFINE(Lhs, Rhs, Res) \
651  template <typename GenSet> \
652  Res \
653  meet(const Lhs& lhs, const Rhs& rhs) \
654  { \
655  return {set_intersection(*lhs.genset(), *rhs.genset())}; \
656  }
657 
659  DEFINE(nullableset<letterset<GenSet>>,
660  nullableset<letterset<GenSet>>, nullableset<letterset<GenSet>>);
661 
662  DEFINE(letterset<GenSet>,
663  nullableset<letterset<GenSet>>, nullableset<letterset<GenSet>>);
664 
665  DEFINE(nullableset<letterset<GenSet>>,
666  letterset<GenSet>, nullableset<letterset<GenSet>>);
667 
668  template <typename Lls, typename Rls>
669  nullableset<meet_t<Lls, Rls>>
670  meet(const nullableset<Lls>& lhs, const nullableset<Rls>& rhs)
671  {
672  return nullableset<meet_t<Lls, Rls>>{meet(*lhs.labelset(),
673  *rhs.labelset())};
674  }
675 
676 #undef DEFINE
677 
678 }
static bool equal(const value_t &l, const value_t &r)
Whether l == r.
Definition: nullableset.hh:338
std::shared_ptr< const labelset_t > labelset_ptr
Definition: nullableset.hh:161
static value_t value(const labelset_t &ls, Args &&...args)
Definition: nullableset.hh:85
static bool is_special(const value_t &v)
Definition: nullableset.hh:389
value_t conv(const LabelSet_ &ls, typename LabelSet_::value_t v) const
Conversion from another type: first by the wrapped labelset, and then by our wrappers (in case the wr...
Definition: nullableset.hh:287
genset_ptr genset() const
Definition: nullableset.hh:258
value_t conv(oneset, typename oneset::value_t) const
Definition: nullableset.hh:277
bool is_zero(const value_t &v) const
Definition: nullableset.hh:482
Print as rich UTF-8 text, escaped.
Definition: format.hh:28
void convs(std::istream &i, Fun &&fun) const
Process a label class.
Definition: nullableset.hh:442
static ATTRIBUTE_PURE labelset_t::value_t get_value(const value_t &v)
Definition: nullableset.hh:92
Implementation of labels are nullables (letter or empty).
Definition: fwd.hh:15
static ATTRIBUTE_PURE constexpr value_t one()
Definition: nullableset.hh:120
static ATTRIBUTE_PURE bool is_one(value_t l)
Definition: nullableset.hh:127
nullableset(const labelset_t &ls={})
Definition: nullableset.hh:184
genset_t generators() const
The generators. Meaningful for labelsets only.
Definition: nullableset.hh:265
static auto letters_of(const word_t &v) -> decltype(labelset_t::letters_of(v))
Prepare to iterate over the letters of v.
Definition: nullableset.hh:315
Definition: a-star.hh:8
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
value_t conv(self_t, value_t v) const
Definition: nullableset.hh:271
static nullableset make(std::istream &is)
Build from the description in is.
Definition: nullableset.hh:195
value_t value(Args &&...args) const
Definition: nullableset.hh:299
bool open(bool o) const
Whether unknown letters should be added, or rejected.
Definition: nullableset.hh:209
std::ostream & print(const value_t &l, std::ostream &o, format fmt={}) const
Print label to stream.
Definition: nullableset.hh:463
Print for LaTeX.
Definition: format.hh:20
static constexpr bool is_free()
Definition: nullableset.hh:232
ATTRIBUTE_PURE auto transpose(Args &&...args) const -> decltype(this->genset() -> transpose(std::forward< Args >(args)...))
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
value_t ldiv(const value_t &l, const value_t &r) const
Compute l \ r = l^{-1}r.
Definition: nullableset.hh:369
An input/output format for valuesets.
Definition: format.hh:11
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static size_t hash(const value_t &v)
Definition: nullableset.hh:400
static constexpr bool is_expressionset()
Definition: nullableset.hh:221
static symbol sname()
Definition: nullableset.hh:188
static constexpr bool is_letterized()
Definition: nullableset.hh:227
static ATTRIBUTE_PURE std::enable_if_t<!Ls::has_one(), bool > is_one_(const value_t &l)
Definition: nullableset.hh:58
decltype(ls_->genset()) genset_ptr
Definition: nullableset.hh:176
std::pair< typename labelset_t::value_t, bool > value_t
Definition: nullableset.hh:31
static ATTRIBUTE_PURE constexpr value_t special()
Definition: nullableset.hh:113
Value transpose(const Value &l) const
Mirror label.
Definition: nullableset.hh:496
typename labelset_t::word_t word_t
Definition: nullableset.hh:168
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
decltype(ls_->generators()) genset_t
Definition: nullableset.hh:177
static size_t size(const value_t &v)
Definition: nullableset.hh:394
typename labelset_t::letter_t letter_t
Definition: nullableset.hh:167
static value_t special()
Definition: nullableset.hh:383
static Value transpose(const labelset_t &ls, const Value &l)
Definition: nullableset.hh:134
static value_t value(const labelset_t &ls, Args &&...args)
Definition: nullableset.hh:141
static ATTRIBUTE_PURE value_t get_value(const value_t &v)
Definition: nullableset.hh:148
bool is_special(const Aut &aut, transition_t_of< Aut > t)
Whether this transition is from pre or to post.
Definition: automaton.hh:199
static ATTRIBUTE_PURE constexpr value_t special()
Definition: nullableset.hh:42
bool is_letter(const value_t &v) const
Definition: nullableset.hh:488
static ATTRIBUTE_PURE constexpr value_t one()
Definition: nullableset.hh:239
bool value_t
Definition: oneset.hh:20
static ATTRIBUTE_PURE bool is_one(value_t l)
Definition: nullableset.hh:246
auto letters_of_padded(value_t v, letter_t l) const -> decltype(this->letters_of_padded(this->word(v), l))
Definition: nullableset.hh:331
symbol sname()
Definition: name.hh:67
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380
value_t zero() const
Definition: nullableset.hh:476
law_t< LabelSet > make_wordset(const LabelSet &ls)
The wordset of a labelset.
Definition: labelset.hh:269
nullableset(const labelset_ptr &ls)
Definition: nullableset.hh:180
Print as a parsable type string.
Definition: format.hh:24
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:16
Implementation of labels are letters.
Definition: fwd.hh:11
labelset_ptr ls_
The wrapped LabelSet.
Definition: nullableset.hh:173
static constexpr bool has_one()
Definition: nullableset.hh:215
static ATTRIBUTE_PURE std::enable_if_t< Ls::has_one(), bool > is_one_(const value_t &l)
Definition: nullableset.hh:50
static bool less(const value_t &l, const value_t &r)
Whether l < r.
Definition: nullableset.hh:347
typename helper_t::value_t value_t
Definition: nullableset.hh:166
const labelset_ptr labelset() const
Definition: nullableset.hh:292
static Value transpose(const labelset_t &ls, Value l)
Definition: nullableset.hh:72
auto letters_of_padded(const word_t &v, letter_t l) const -> decltype(ls_->letters_of_padded(v, l))
Prepare to iterate over the letters of v.
Definition: nullableset.hh:324
bool is_valid(value_t v) const
Definition: nullableset.hh:252
std::enable_if_t<!is_letterized_t< labelset_t_of< Aut > >{}, bool > is_letterized(const Aut &aut)
Definition: letterize.hh:162
word_t word(const value_t &l) const
Definition: nullableset.hh:305
value_t lgcd(const value_t &l, const value_t &r) const
The longest common prefix.
Definition: nullableset.hh:358
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:535
value_t conjunction(const value_t &l, const value_t &r) const
Definition: nullableset.hh:451
value_t value(Args &&...args) const
Value constructor.
Definition: letterset.hh:82
letter_t get_letter(std::istream &i, bool quoted=true) const
Definition: nullableset.hh:407
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
letter_t value_t
Definition: letterset.hh:32
static ATTRIBUTE_PURE constexpr value_t one()
Definition: nullableset.hh:35
static ATTRIBUTE_PURE bool is_one(const value_t &l)
Definition: nullableset.hh:65
value_t conv(std::istream &i, bool quoted=true) const
Read a label from a stream.
Definition: nullableset.hh:414
std::ostream & print_set(std::ostream &o, format fmt={}) const
Print labelset description.
Definition: nullableset.hh:503
Add support for an empty word to a LabelSet that does not provide such special label to this end...
Definition: nullableset.hh:28