5 #include <boost/range/algorithm/mismatch.hpp>
6 #include <boost/algorithm/string/predicate.hpp>
7 #include <boost/optional.hpp>
22 template <
typename GenSet>
23 class wordset:
public detail::genset_labelset<GenSet>
32 using word_t =
typename genset_t::word_t;
43 :
wordset{std::make_shared<const genset_t>(gs)}
46 wordset(std::initializer_list<letter_t> letters)
63 auto gs = genset_t::make(is);
73 return this->
genset()->open(o);
82 template <
typename... Args>
85 return value_t{std::forward<Args>(args)...};
119 return genset_t::less(l, r);
133 return genset_t::less(l, r);
139 return genset_t::template special<value_t>();
157 static constexpr
bool
163 static constexpr
bool
169 static constexpr
bool
178 return genset_t::empty_word();
184 return genset_t::is_empty_word(l);
205 template <
typename GenSet_>
216 *
this,
": conv: invalid label: ",
str_escape(v));
221 template <
typename LabelSet_>
234 conv(std::istream& i,
bool =
true)
const
236 return this->
genset()->get_word(i);
248 template <
typename Fun>
249 void convs(std::istream& i, Fun fun)
const
263 this->
genset()->print(l, o, fmt);
273 this->
genset()->print_set(o, fmt);
278 this->
genset()->print_set(o, fmt);
283 this->genset()->print_set(o, fmt);
294 static value_t lgcd(const value_t& w1, const value_t& w2)
296 return {w1.begin(), boost::mismatch(w1, w2).first};
301 value_t ldivide(const value_t& w1, const value_t& w2) const
303 auto res = maybe_ldivide(w1, w2);
305 *this, ": ldivide: invalid arguments: ",
306 to_string(*this, w1),
307 ", ", to_string(*this, w2));
311 boost::optional<value_t>
312 maybe_ldivide(const value_t& w1, const value_t& w2) const
314 using boost::algorithm::starts_with;
315 if (starts_with(w2, w1))
316 return value_t{begin(w2) + size(w1), end(w2)};
322 value_t& ldivide_here(const value_t& w1, value_t& w2) const
324 w2 = ldivide(w1, w2);
330 value_t rdivide(const value_t& w1, const value_t& w2) const
332 auto res = maybe_rdivide(w1, w2);
334 *this, ": rdivide: invalid arguments: ",
335 to_string(*this, w1),
336 ", ", to_string(*this, w2));
340 boost::optional<value_t>
341 maybe_rdivide(const value_t& w1, const value_t& w2) const
343 using boost::algorithm::ends_with;
344 if (ends_with(w1, w2))
345 return value_t{begin(w1), end(w1) - size(w2)};
351 value_t& rdivide_here(value_t& w1, const value_t& w2) const
353 w1 = rdivide(w1, w2);
357 const value_t& conjunction(const value_t& l, const value_t& r) const
363 ": conjunction: invalid operation (lhs and rhs are not equal): ",
364 to_string(*this, l), ", ", to_string(*this, r));
371 template <typename GenSet>
372 struct letterized_traits<wordset<GenSet>>
374 static constexpr bool is_letterized = false;
376 using labelset_t = nullableset<letterset<GenSet>>;
378 static labelset_t labelset(const wordset<GenSet>& ls)
380 return {ls.genset()};
385 template <typename GenSet>
386 struct nullableset_traits<wordset<GenSet>>
388 using type = wordset<GenSet>;
389 static type value(const wordset<GenSet>& ls)
395 template <typename GenSet>
396 struct law_traits<wordset<GenSet>>
398 using type = wordset<GenSet>;
399 static type value(const wordset<GenSet>& ls)
410 #define DEFINE(Lhs, Rhs) \
411 template <typename GenSet> \
412 struct join_impl<Lhs, Rhs> \
415 static type join(const Lhs& lhs, const Rhs& rhs) \
417 return {set_union(*lhs.genset(), *rhs.genset())}; \
422 DEFINE(letterset<GenSet>, wordset<GenSet>);
423 DEFINE(nullableset<letterset<GenSet>>, wordset<GenSet>);
424 DEFINE(wordset<GenSet>, wordset<GenSet>);
429 // FIXME: Factor in genset_labelset?
430 template <typename GenSet>
432 meet(const wordset<GenSet>& lhs, const wordset<GenSet>& rhs)
434 return {set_intersection(*lhs.genset(), *rhs.genset())};
443 template <typename GenSet,
444 typename RandomGenerator = std::default_random_engine>
445 typename wordset<GenSet>::value_t
446 random_label(const wordset<GenSet>& ls,
447 RandomGenerator& gen = RandomGenerator())
449 require(!ls.generators().empty(),
450 "random_label: the alphabet needs at least 1 letter");
451 auto dis = std::uniform_int_distribution<>(0, 5);
452 auto res_label = ls.one();
453 auto pick = make_random_selector(gen);
454 for (auto _: detail::irange(dis(gen)))
455 res_label = ls.mul(res_label, ls.value(pick(ls.generators())));
bool is_valid(const value_t &v) const
static constexpr bool has_one()
bool open(bool o) const
Whether unknown letters should be added, or rejected.
char eat(std::istream &is, char c)
Check lookahead character and advance.
Print as a parsable type string.
static constexpr bool is_free()
std::ostream & print(const value_t &l, std::ostream &o=std::cout, format fmt={}) const
static word_t letters_of_padded(const value_t &v, letter_t)
Prepare to iterate over the letters of v.
value_t conv(self_t, const value_t &v) const
static bool equal(const value_t &l, const value_t &r)
Whether l == r.
word_t word(const value_t &v) const
Convert to a word.
typename genset_t::letter_t letter_t
void convs_(std::istream &i, Fun fun) const
Read and process a class of letters.
wordset(const genset_ptr &gs)
wordset(std::initializer_list< letter_t > letters)
An input/output format for valuesets.
wordset(const genset_t &gs={})
value_t conv(const letterset< GenSet_ > &ls, typename letterset< GenSet_ >::value_t v) const
weightset_mixin< detail::r_impl > r
Provide a variadic mul on top of a binary mul(), and one().
genset_ptr genset() const
static bool is_one(const value_t &l) ATTRIBUTE_PURE
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
static size_t size(const value_t &v)
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
static size_t hash(const value_t &v)
static word_t letters_of(const value_t &v)
Prepare to iterate over the letters of v.
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Print as rich UTF-8 text, escaped.
void convs(std::istream &i, Fun fun) const
Process a label class.
This class has no modeling purpose, it only serves to factor code common to letterset and wordset...
value_t conv(std::istream &i, bool=true) const
Read a word from this stream.
value_t value(Args &&...args) const
Value constructor.
Implementation of labels are nullables (letter or empty).
typename helper_t::value_t value_t
value_t conv(const nullableset< LabelSet_ > &ls, const typename nullableset< LabelSet_ >::value_t &v) const
std::shared_ptr< const genset_t > genset_ptr
static constexpr bool is_expressionset()
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
ATTRIBUTE_PURE auto has(Args &&...args) const -> decltype(this->genset() -> has(std::forward< Args >(args)...))
static ATTRIBUTE_PURE bool is_one(value_t l)
std::ostream & print_set(std::ostream &o, format fmt={}) const
typename genset_t::word_t word_t
Implementation of labels are letters.
static wordset make(std::istream &is)
Build from the description in is.
static bool less(const value_t &l, const value_t &r)
Whether l < r.
const labelset_ptr labelset() const
static bool is_special(const value_t &v)
Implementation of labels are words.
static labelset_t::value_t get_value(const value_t &v)
The (inner) value when it (the outer value) is not one.
static bool less(const letter_t &l, const letter_t &r)
Whether l < r.
static bool is_special(value_t v) ATTRIBUTE_PURE
static constexpr bool is_letterized()