Skip to content

Commit

Permalink
Fix exclude pruner
Browse files Browse the repository at this point in the history
  • Loading branch information
reiniscirpons committed Sep 27, 2024
1 parent 317b9d1 commit d7732ca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
23 changes: 19 additions & 4 deletions include/libsemigroups/sims.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ namespace libsemigroups {

private:
std::vector<word_type> _exclude;
bool _exclude_pruner_added;
size_t _exclude_pruner_index;
size_t _idle_thread_restarts;
std::vector<word_type> _include;
std::vector<word_type>::const_iterator _longs_begin;
Expand Down Expand Up @@ -685,7 +685,15 @@ namespace libsemigroups {
//! \exceptions
//! \no_libsemigroups_except
Subclass& clear_pruners() {
_pruners.clear();
if (_exclude_pruner_index == UNDEFINED) {
_pruners.clear();
} else {
LIBSEMIGROUPS_ASSERT(_exclude_pruner_index < _pruners.size());
_pruners.erase(_pruners.cbegin(),
_pruners.cbegin() + _exclude_pruner_index);
_exclude_pruner_index = 0;
_pruners.erase(_pruners.cbegin() + 1, _pruners.cend());
}
return static_cast<Subclass&>(*this);
}

Expand Down Expand Up @@ -861,6 +869,7 @@ namespace libsemigroups {
// pair? Replaces current exclude with [first, last)
template <typename Iterator>
Subclass& exclude(Iterator first, Iterator last) {
add_exclude_pruner();
return include_exclude(first, last, _exclude);
}

Expand All @@ -876,6 +885,7 @@ namespace libsemigroups {
//!
//! \sa \ref exclude
Subclass& exclude(word_type const& lhs, word_type const& rhs) {
add_exclude_pruner();
return include_exclude(lhs, rhs, _exclude);
}

Expand Down Expand Up @@ -922,8 +932,11 @@ namespace libsemigroups {
//! \exceptions
//! \no_libsemigroups_except
Subclass& clear_exclude() {
// TODO(2) remove the pruner
_exclude.clear();
if (_exclude_pruner_index != UNDEFINED) {
_exclude.clear();
_pruners.erase(_pruners.cbegin() + _exclude_pruner_index);
_exclude_pruner_index = UNDEFINED;
}
return static_cast<Subclass&>(*this);
}

Expand Down Expand Up @@ -998,6 +1011,8 @@ namespace libsemigroups {
Subclass& include_exclude(word_type const& lhs,
word_type const& rhs,
std::vector<word_type>& include_or_exclude);

size_t add_exclude_pruner();
};

////////////////////////////////////////////////////////////////////////
Expand Down
42 changes: 37 additions & 5 deletions src/sims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace libsemigroups {
template <typename Subclass>
SimsSettings<Subclass>::SimsSettings()
: _exclude(),
_exclude_pruner_added(false),
_exclude_pruner_index(UNDEFINED),
_idle_thread_restarts(64),
_include(),
_longs_begin(),
Expand All @@ -106,7 +106,7 @@ namespace libsemigroups {
template <typename Subclass>
Subclass& SimsSettings<Subclass>::init() {
_exclude.clear();
_exclude_pruner_added = false;
_exclude_pruner_index = UNDEFINED;
_idle_thread_restarts = 64;
_include.clear();
_num_threads = 1;
Expand Down Expand Up @@ -199,6 +199,32 @@ namespace libsemigroups {
return static_cast<Subclass&>(*this);
}

template <typename Subclass>
size_t SimsSettings<Subclass>::add_exclude_pruner() {
if (_exclude_pruner_index != UNDEFINED) {
return _exclude_pruner_index;
}
auto pruner = [this](auto const& wg) {
auto first = _exclude.cbegin();
auto last = _exclude.cend();
node_type root = 0;

for (auto it = first; it != last; it += 2) {
auto l = word_graph::follow_path_no_checks(wg, root, *it);
if (l != UNDEFINED) {
auto r = word_graph::follow_path_no_checks(wg, root, *(it + 1));
if (l == r) {
return false;
}
}
}
return true;
};
add_pruner(pruner);
_exclude_pruner_index = _pruners.size() - 1;
return _exclude_pruner_index;
}

template <typename Subclass>
Subclass& SimsSettings<Subclass>::include_exclude(
word_type const& lhs,
Expand Down Expand Up @@ -1455,7 +1481,11 @@ namespace libsemigroups {

result += fmt::format("object over {} with",
to_human_readable_repr(x.presentation()));
std::string comma = x.pruners().empty() && extra_text.empty() ? "" : ",";
std::string comma = (x.pruners().empty()
|| (x.pruners().size() == 1 && !x.exclude().empty()))
&& extra_text.empty()
? ""
: ",";
if ((x.include().size() > 0) && (x.exclude().size() > 0)) {
result += fmt::format(" {} include and {} exclude pairs{}",
x.include().size() / 2,
Expand All @@ -1481,9 +1511,11 @@ namespace libsemigroups {
needs_and = true;
}

if (x.pruners().size() > 0) {
if (x.pruners().size() > 1
|| (x.exclude().empty() && !x.pruners().empty())) {
result += fmt::format(" {} pruner{}",
x.pruners().size(),
x.exclude().size() == 0 ? x.pruners().size()
: x.pruners().size() - 1,
x.pruners().size() == 1 ? "" : "s");
needs_and = true;
}
Expand Down

0 comments on commit d7732ca

Please sign in to comment.