Skip to content

Commit

Permalink
WordRange with too few leters is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph-Edwards committed Aug 19, 2024
1 parent 64ab031 commit a15f85a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/words.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ namespace libsemigroups {
LIBSEMIGROUPS_ASSERT(r != 1); // to avoid division by 0
return a * ((1 - std::pow(r, n)) / (1 - static_cast<float>(r)));
}

bool word_in_language(size_t n, word_type const& w) {
return std::all_of(
w.cbegin(), w.cend(), [&](letter_type x) { return x < n; });
}
} // namespace

uint64_t number_of_words(size_t n, size_t min, size_t max) {
Expand Down Expand Up @@ -86,7 +91,8 @@ namespace libsemigroups {
size_t upper_bound,
word_type&& first,
word_type&& last) {
if (!lexicographical_compare(
if (!word_in_language(n, first)
|| !lexicographical_compare(
first.cbegin(), first.cend(), last.cbegin(), last.cend())) {
return cend_wilo(n, upper_bound, std::move(first), std::move(last));
}
Expand Down Expand Up @@ -121,7 +127,8 @@ namespace libsemigroups {
detail::const_wislo_iterator cbegin_wislo(size_t n,
word_type&& first,
word_type&& last) {
if (!shortlex_compare(
if (!word_in_language(n, first)
|| !shortlex_compare(
first.cbegin(), first.cend(), last.cbegin(), last.cend())) {
return cend_wislo(n, std::move(first), std::move(last));
}
Expand Down
16 changes: 16 additions & 0 deletions tests/test-words.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,4 +1313,20 @@ namespace libsemigroups {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaa");
}
}

LIBSEMIGROUPS_TEST_CASE("WordRange", "041", "empty iterator", "[quick]") {
using words::pow;

WordRange words;
words.alphabet_size(1).first("01"_w).last("11"_w);
REQUIRE(words.at_end());
REQUIRE(words.count() == 0);
REQUIRE((words | to_vector()) == std::vector<word_type>({}));

words.init();
words.alphabet_size(0).min(2).max(5);
REQUIRE(words.at_end());
REQUIRE(words.count() == 0);
REQUIRE((words | to_vector()) == std::vector<word_type>({}));
}
} // namespace libsemigroups

0 comments on commit a15f85a

Please sign in to comment.