6 #include <boost/optional.hpp>
28 template <
typename LabelSet>
32 using value_t = std::pair<typename labelset_t::value_t, bool>;
38 return value_t{labelset_t::special(),
true};
45 return value_t{labelset_t::special(),
false};
48 template <
typename Ls>
50 static std::enable_if_t<Ls::has_one(), bool>
53 return std::get<1>(l) || Ls::is_one(
get_value(l));
56 template <
typename Ls>
58 static std::enable_if_t<!Ls::has_one(), bool>
61 return std::get<1>(l);
68 return is_one_<labelset_t>(l);
71 template <
typename Value>
81 return {ls.transpose(
get_value(l)),
false};
84 template <
typename... Args>
88 return {ls.value(std::forward<Args>(args)...),
false};
92 static typename labelset_t::value_t
95 return std::get<0>(
v);
105 template <
typename GenSet>
116 return genset_t::special();
123 return genset_t::one_letter();
133 template <
typename Value>
140 template <
typename... Args>
144 return ls.
value(std::forward<Args>(args)...);
157 template <
typename LabelSet>
169 using word_t =
typename labelset_t::word_t;
186 :
nullableset{std::make_shared<const labelset_t>(ls)}
201 eat(is,
"nullableset<");
202 auto ls = labelset_t::make(is);
212 return this->ls_->open(o);
215 static constexpr
bool
221 static constexpr
bool
227 static constexpr
bool
261 return ls_->genset();
268 return ls_->generators();
286 template <
typename LabelSet_>
288 conv(
const LabelSet_& ls,
typename LabelSet_::value_t
v)
const
290 return value(ls_->conv(ls, v));
298 template <
typename... Args>
317 -> decltype(labelset_t::letters_of(
v))
319 return labelset_t::letters_of(
v);
326 -> decltype(ls_->letters_of_padded(
v, l))
328 return ls_->letters_of_padded(
v, l);
366 raise(*
this,
": mul: invalid arguments: ",
387 raise(*
this,
": ldivide: invalid arguments: ",
391 boost::optional<value_t>
412 raise(*
this,
": rdivide: invalid arguments: ",
416 boost::optional<value_t>
458 return ls_->get_letter(i, quoted);
463 conv(std::istream& i,
bool quoted =
true)
const
466 if (i.good() && i.peek() ==
'\\')
478 return value(ls_->conv(i, quoted));
490 template <
typename Fun>
491 void convs(std::istream& i, Fun&& fun)
const
494 [
this,fun](
const typename labelset_t::value_t& l)
508 ": conjunction: invalid operation (lhs and rhs are not equal): ",
529 return value(ls_->zero());
545 template <
typename Value>
560 ls_->print_set(o, fmt);
566 ls_->print_set(o, fmt);
572 ls_->print_set(o, fmt);
585 static typename labelset_t::value_t
586 get_value(const value_t& v)
589 return helper_t::get_value(v);
596 template <typename LabelSet>
597 struct letterized_traits<nullableset<LabelSet>>
599 using traits = letterized_traits<LabelSet>;
600 static constexpr bool is_letterized = traits::is_letterized;
602 using labelset_t = typename traits::labelset_t;
604 static labelset_t labelset(const nullableset<LabelSet>& ls)
606 return make_letterized(*ls.labelset());
611 template <typename LabelSet>
612 struct nullableset_traits<nullableset<LabelSet>>
614 using type = nullableset<LabelSet>;
615 static type value(const nullableset<LabelSet>& ls)
621 template <typename LabelSet>
622 struct proper_traits<nullableset<LabelSet>>
624 using type = LabelSet;
625 static type value(const nullableset<LabelSet>& ls)
627 return *ls.labelset();
632 template <typename LabelSet>
633 struct law_traits<nullableset<LabelSet>>
635 using type = law_t<LabelSet>;
636 static type value(const nullableset<LabelSet>& ls)
638 return make_wordset(*ls.labelset());
647 template <typename LS>
648 struct join_impl<oneset, LS,
649 std::enable_if_t<!LS::has_one()>>
651 using type = nullableset<LS>;
652 static type join(const oneset&, const LS& ls)
659 template <typename LS>
660 struct join_impl<oneset, LS,
661 std::enable_if_t<LS::has_one()>>
664 static type join(const oneset&, const LS& ls)
671 template <typename LS1, typename LS2>
672 struct join_impl<nullableset<LS1>, LS2>
674 using type = nullableset<join_t<LS1, LS2>>;
675 static type join(const nullableset<LS1>& ls1, const LS2& ls2)
677 return {::vcsn::join(*ls1.labelset(), ls2)};
685 template <typename LS1, typename LS2>
686 struct join_impl<nullableset<LS1>, nullableset<LS2>>
688 using type = nullableset<join_t<LS1, LS2>>;
689 static type join(const nullableset<LS1>& ls1,
690 const nullableset<LS2>& ls2)
692 return {::vcsn::join(*ls1.labelset(), *ls2.labelset())};
701 #define DEFINE(Lhs, Rhs, Res) \
702 template <typename GenSet> \
704 meet(const Lhs& lhs, const Rhs& rhs) \
706 return {set_intersection(*lhs.genset(), *rhs.genset())}; \
710 DEFINE(nullableset<letterset<GenSet>>,
711 nullableset<letterset<GenSet>>, nullableset<letterset<GenSet>>);
713 DEFINE(letterset<GenSet>,
714 nullableset<letterset<GenSet>>, nullableset<letterset<GenSet>>);
716 DEFINE(nullableset<letterset<GenSet>>,
717 letterset<GenSet>, nullableset<letterset<GenSet>>);
719 template <typename Lls, typename Rls>
720 nullableset<meet_t<Lls, Rls>>
721 meet(const nullableset<Lls>& lhs, const nullableset<Rls>& rhs)
723 return nullableset<meet_t<Lls, Rls>>{meet(*lhs.labelset(),
735 template <typename LabelSet,
736 typename RandomGenerator = std::default_random_engine>
737 typename nullableset<LabelSet>::value_t
738 random_label(const nullableset<LabelSet>& ls,
739 RandomGenerator& gen = RandomGenerator())
741 // FIXME: the proportion should be controllable.
742 auto dis = std::bernoulli_distribution(0.5);
743 if (dis(gen) || ls.generators().empty())
746 return ls.value(random_label(*ls.labelset(), gen));
char eat(std::istream &is, char c)
Check lookahead character and advance.
Print as a parsable type string.
static constexpr bool is_free()
Implementation of labels are ones: there is a single instance of label.
letter_t get_letter(std::istream &i, bool quoted=true) const
void convs(std::istream &i, Fun &&fun) const
Process a label class.
decltype(ls_->generators()) genset_t
static ATTRIBUTE_PURE std::enable_if_t< Ls::has_one(), bool > is_one_(const value_t &l)
static ATTRIBUTE_PURE constexpr value_t one()
static Value transpose(const labelset_t &ls, const Value &l)
bool is_special(const Aut &aut, transition_t_of< Aut > t)
Whether this transition is from pre or to post.
static constexpr bool is_expressionset()
value_t value(Args &&...args) const
Value constructor.
An input/output format for valuesets.
weightset_mixin< detail::r_impl > r
Provide a variadic mul on top of a binary mul(), and one().
labelset_ptr ls_
The wrapped LabelSet.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
typename labelset_t::letter_t letter_t
static size_t hash(const value_t &v)
static Value transpose(const labelset_t &ls, Value l)
nullableset(const labelset_ptr &ls)
std::shared_ptr< const labelset_t > labelset_ptr
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
Print as rich UTF-8 text, escaped.
static nullableset make(std::istream &is)
Build from the description in is.
bool open(bool o) const
Whether unknown letters should be added, or rejected.
static bool is_special(const value_t &v)
value_t conv(oneset, typename oneset::value_t) const
std::ostream & print_set(std::ostream &o, format fmt={}) const
Print labelset description.
std::enable_if_t<!is_letterized_t< labelset_t_of< Aut > >{}, bool > is_letterized(const Aut &aut)
static size_t size(const value_t &v)
static value_t value(const labelset_t &ls, Args &&...args)
Implementation of labels are nullables (letter or empty).
typename helper_t::value_t value_t
bool is_valid(value_t v) const
typename labelset_t::value_t value_t
static ATTRIBUTE_PURE bool is_one(value_t l)
static ATTRIBUTE_PURE constexpr value_t one()
std::pair< typename labelset_t::value_t, bool > value_t
static bool equal(const value_t &l, const value_t &r)
Whether l == r.
value_t rdivide(const value_t &l, const value_t &r) const
Compute l / r.
value_t conjunction(const value_t &l, const value_t &r) const
static ATTRIBUTE_PURE bool is_one(value_t l)
static ATTRIBUTE_PURE labelset_t::value_t get_value(const value_t &v)
boost::optional< value_t > maybe_ldivide(const value_t &l, const value_t &r) const
value_t value(Args &&...args) const
static constexpr bool is_letterized()
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...
static ATTRIBUTE_PURE std::enable_if_t<!Ls::has_one(), bool > is_one_(const value_t &l)
ATTRIBUTE_PURE auto transpose(Args &&...args) const -> decltype(this->genset() -> transpose(std::forward< Args >(args)...))
static ATTRIBUTE_PURE constexpr value_t special()
Implementation of labels are letters.
Value transpose(const Value &l) const
Mirror label.
typename labelset_t::word_t word_t
std::string to_string(direction d)
Conversion to string.
static ATTRIBUTE_PURE bool is_one(const value_t &l)
static bool less(const value_t &l, const value_t &r)
Whether l < r.
const labelset_ptr labelset() const
static ATTRIBUTE_PURE value_t get_value(const value_t &v)
static auto letters_of(const word_t &v) -> decltype(labelset_t::letters_of(v))
Prepare to iterate over the letters of v.
decltype(auto) generators() const
The generators. Meaningful for labelsets only.
static ATTRIBUTE_PURE constexpr value_t one()
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.
value_t lgcd(const value_t &l, const value_t &r) const
The longest common prefix.
value_t conv(self_t, value_t v) const
static labelset_t::value_t get_value(const value_t &v)
The (inner) value when it (the outer value) is not one.
word_t word(const value_t &l) const
value_t ldivide(const value_t &l, const value_t &r) const
Compute l \ r = l^{-1}r.
bool is_letter(const value_t &v) const
auto letters_of_padded(value_t v, letter_t l) const -> decltype(this->letters_of_padded(this->word(v), l))
decltype(ls_->genset()) genset_ptr
bool is_zero(const value_t &v) const
genset_ptr genset() const
std::ostream & print(const value_t &l, std::ostream &o=std::cout, format fmt={}) const
Print label to stream.
static ATTRIBUTE_PURE constexpr value_t special()
Add support for an empty word to a LabelSet that does not provide such special label to this end...
value_t mul(const value_t &l, const value_t &r) const
The concatenation.
nullableset(const labelset_t &ls={})
value_t conv(std::istream &i, bool quoted=true) const
Read a label from a stream.
static constexpr bool has_one()
static value_t value(const labelset_t &ls, Args &&...args)
law_t< LabelSet > make_wordset(const LabelSet &ls)
The wordset of a labelset.
boost::optional< value_t > maybe_rdivide(const value_t &l, const value_t &r) const