Skip to content

Commit

Permalink
move rewriters to detail
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph-Edwards committed May 31, 2024
1 parent 4b7a53b commit 7c597a2
Show file tree
Hide file tree
Showing 17 changed files with 1,642 additions and 1,625 deletions.
706 changes: 706 additions & 0 deletions include/libsemigroups/detail/rewriters.hpp

Large diffs are not rendered by default.

58 changes: 32 additions & 26 deletions include/libsemigroups/knuth-bendix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include "paths.hpp" // for Paths
#include "presentation.hpp" // for Presentation
#include "ranges.hpp" // for operator<<
#include "rewriters.hpp" // for RewriteTrie
#include "runner.hpp" // for Runner
#include "to-presentation.hpp" // for to_presentation
#include "types.hpp" // for word_type
Expand All @@ -63,6 +62,7 @@

#include "detail/multi-string-view.hpp" // for MultiStringView
#include "detail/report.hpp" // for Reporter, REPORT_DEFAULT, REP...
#include "detail/rewriters.hpp" // for RewriteTrie
#include "detail/string.hpp" // for is_prefix, maximum_common_prefix

#include "ranges.hpp" // for iterator_range
Expand Down Expand Up @@ -105,7 +105,7 @@ namespace libsemigroups {
//! kb.confluent(); // true
//! kb.number_of_classes(); // POSITIVE_INFINITY
//! \endcode
template <typename Rewriter = RewriteTrie,
template <typename Rewriter = detail::RewriteTrie,
typename ReductionOrder = ShortLexCompare>
class KnuthBendix : public CongruenceInterface {
// defined in detail/kbe.hpp
Expand All @@ -115,20 +115,21 @@ namespace libsemigroups {
// KnuthBendix - typedefs/aliases - private
////////////////////////////////////////////////////////////////////////

using external_string_type = std::string;
using internal_string_type = std::string;
using external_char_type = char;
using internal_char_type = char;
// using external_string_type = std::string;
// using internal_string_type = std::string;
// using external_char_type = char;
// using internal_char_type = char;

////////////////////////////////////////////////////////////////////////
// KnuthBendix - nested subclasses - private
////////////////////////////////////////////////////////////////////////

// Overlap measures
struct OverlapMeasure {
virtual size_t operator()(Rule const*,
Rule const* examples,
internal_string_type::const_iterator const&)
virtual size_t
operator()(detail::Rule const*,
detail::Rule const* examples,
detail::internal_string_type::const_iterator const&)
= 0;
virtual ~OverlapMeasure() {}
};
Expand Down Expand Up @@ -728,8 +729,10 @@ namespace libsemigroups {
return iterator_range(_rewriter.begin(), _rewriter.end())
| transform([this](auto const& rule) {
// TODO remove allocation
internal_string_type lhs = internal_string_type(*rule->lhs());
internal_string_type rhs = internal_string_type(*rule->rhs());
detail::internal_string_type lhs
= detail::internal_string_type(*rule->lhs());
detail::internal_string_type rhs
= detail::internal_string_type(*rule->rhs());
internal_to_external_string(lhs);
internal_to_external_string(rhs);
if (this->kind() == congruence_kind::left) {
Expand Down Expand Up @@ -912,28 +915,31 @@ namespace libsemigroups {
void throw_if_started() const;
void stats_check_point();

[[nodiscard]] static internal_char_type uint_to_internal_char(size_t a);
[[nodiscard]] static size_t internal_char_to_uint(internal_char_type c);
[[nodiscard]] static detail::internal_char_type
uint_to_internal_char(size_t a);
[[nodiscard]] static size_t
internal_char_to_uint(detail::internal_char_type c);

[[nodiscard]] static internal_string_type uint_to_internal_string(size_t i);
[[nodiscard]] static detail::internal_string_type
uint_to_internal_string(size_t i);

[[nodiscard]] static word_type
internal_string_to_word(internal_string_type const& s);
internal_string_to_word(detail::internal_string_type const& s);

[[nodiscard]] internal_char_type
external_to_internal_char(external_char_type c) const;
[[nodiscard]] external_char_type
internal_to_external_char(internal_char_type a) const;
[[nodiscard]] detail::internal_char_type
external_to_internal_char(detail::external_char_type c) const;
[[nodiscard]] detail::external_char_type
internal_to_external_char(detail::internal_char_type a) const;

void external_to_internal_string(external_string_type& w) const;
void internal_to_external_string(internal_string_type& w) const;
void external_to_internal_string(detail::external_string_type& w) const;
void internal_to_external_string(detail::internal_string_type& w) const;

void add_octo(external_string_type& w) const;
void rm_octo(external_string_type& w) const;
void add_octo(detail::external_string_type& w) const;
void rm_octo(detail::external_string_type& w) const;

void add_rule_impl(std::string const& p, std::string const& q);

void overlap(Rule const* u, Rule const* v);
void overlap(detail::Rule const* u, detail::Rule const* v);

[[nodiscard]] size_t max_active_word_length() const {
return _rewriter.max_active_word_length();
Expand Down Expand Up @@ -1131,8 +1137,8 @@ namespace libsemigroups {
continue;
}

if (rule.first.find(lhs) != internal_string_type::npos
|| rule.second.find(lhs) != internal_string_type::npos) {
if (rule.first.find(lhs) != detail::internal_string_type::npos
|| rule.second.find(lhs) != detail::internal_string_type::npos) {
return false;
}
}
Expand Down
71 changes: 36 additions & 35 deletions include/libsemigroups/knuth-bendix.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ namespace libsemigroups {
template <typename Rewriter, typename ReductionOrder>
struct KnuthBendix<Rewriter, ReductionOrder>::ABC
: KnuthBendix<Rewriter, ReductionOrder>::OverlapMeasure {
size_t operator()(Rule const* AB,
Rule const* BC,
internal_string_type::const_iterator const& it) {
size_t operator()(detail::Rule const* AB,
detail::Rule const* BC,
detail::internal_string_type::const_iterator const& it) {
LIBSEMIGROUPS_ASSERT(AB->active() && BC->active());
LIBSEMIGROUPS_ASSERT(AB->lhs()->cbegin() <= it);
LIBSEMIGROUPS_ASSERT(it < AB->lhs()->cend());
Expand All @@ -52,9 +52,9 @@ namespace libsemigroups {
template <typename Rewriter, typename ReductionOrder>
struct KnuthBendix<Rewriter, ReductionOrder>::AB_BC
: KnuthBendix<Rewriter, ReductionOrder>::OverlapMeasure {
size_t operator()(Rule const* AB,
Rule const* BC,
internal_string_type::const_iterator const& it) {
size_t operator()(detail::Rule const* AB,
detail::Rule const* BC,
detail::internal_string_type::const_iterator const& it) {
LIBSEMIGROUPS_ASSERT(AB->active() && BC->active());
LIBSEMIGROUPS_ASSERT(AB->lhs()->cbegin() <= it);
LIBSEMIGROUPS_ASSERT(it < AB->lhs()->cend());
Expand All @@ -67,9 +67,9 @@ namespace libsemigroups {
template <typename Rewriter, typename ReductionOrder>
struct KnuthBendix<Rewriter, ReductionOrder>::MAX_AB_BC
: KnuthBendix<Rewriter, ReductionOrder>::OverlapMeasure {
size_t operator()(Rule const* AB,
Rule const* BC,
internal_string_type::const_iterator const& it) {
size_t operator()(detail::Rule const* AB,
detail::Rule const* BC,
detail::internal_string_type::const_iterator const& it) {
LIBSEMIGROUPS_ASSERT(AB->active() && BC->active());
LIBSEMIGROUPS_ASSERT(AB->lhs()->cbegin() <= it);
LIBSEMIGROUPS_ASSERT(it < AB->lhs()->cend());
Expand Down Expand Up @@ -291,8 +291,8 @@ namespace libsemigroups {
return true;
}

external_string_type uu = u;
external_string_type vv = v;
detail::external_string_type uu = u;
detail::external_string_type vv = v;

if (kind() == congruence_kind::left) {
std::reverse(uu.begin(), uu.end());
Expand Down Expand Up @@ -454,7 +454,7 @@ namespace libsemigroups {
// some rules.
template <typename Rewriter, typename ReductionOrder>
void KnuthBendix<Rewriter, ReductionOrder>::rewrite_inplace(
external_string_type& w) {
detail::external_string_type& w) {
if (kind() == congruence_kind::left) {
std::reverse(w.begin(), w.end());
}
Expand Down Expand Up @@ -568,15 +568,15 @@ namespace libsemigroups {
// non-trivial overlaps are added and there are a no pending rules.
while (add_overlaps) {
while (first != _rewriter.end() && !stop_running()) {
Rule const* rule1 = *first;
detail::Rule const* rule1 = *first;
// It is tempting to remove rule1 and rule2 here and use *first and
// *second instead but this leads to some badness (which we didn't
// understand, but it also didn't seem super important).
second = first++;
overlap(rule1, rule1);
while (second != _rewriter.begin() && rule1->active()) {
--second;
Rule const* rule2 = *second;
detail::Rule const* rule2 = *second;
overlap(rule1, rule2);
++nr;
if (rule1->active() && rule2->active()) {
Expand Down Expand Up @@ -774,7 +774,7 @@ namespace libsemigroups {

template <typename Rewriter, typename ReductionOrder>
size_t KnuthBendix<Rewriter, ReductionOrder>::internal_char_to_uint(
internal_char_type c) {
detail::internal_char_type c) {
#ifdef LIBSEMIGROUPS_DEBUG
LIBSEMIGROUPS_ASSERT(c >= 97);
return static_cast<size_t>(c - 97);
Expand All @@ -784,57 +784,58 @@ namespace libsemigroups {
}

template <typename Rewriter, typename ReductionOrder>
typename KnuthBendix<Rewriter, ReductionOrder>::internal_char_type
typename detail::internal_char_type
KnuthBendix<Rewriter, ReductionOrder>::uint_to_internal_char(size_t a) {
LIBSEMIGROUPS_ASSERT(
a <= size_t(std::numeric_limits<internal_char_type>::max()));
a <= size_t(std::numeric_limits<detail::internal_char_type>::max()));
#ifdef LIBSEMIGROUPS_DEBUG
LIBSEMIGROUPS_ASSERT(
a <= size_t(std::numeric_limits<internal_char_type>::max() - 97));
return static_cast<internal_char_type>(a + 97);
a
<= size_t(std::numeric_limits<detail::internal_char_type>::max() - 97));
return static_cast<detail::internal_char_type>(a + 97);
#else
return static_cast<internal_char_type>(a + 1);
return static_cast<detail::internal_char_type>(a + 1);
#endif
}

template <typename Rewriter, typename ReductionOrder>
typename KnuthBendix<Rewriter, ReductionOrder>::internal_string_type
typename detail::internal_string_type
KnuthBendix<Rewriter, ReductionOrder>::uint_to_internal_string(size_t i) {
LIBSEMIGROUPS_ASSERT(
i <= size_t(std::numeric_limits<internal_char_type>::max()));
return internal_string_type({uint_to_internal_char(i)});
i <= size_t(std::numeric_limits<detail::internal_char_type>::max()));
return detail::internal_string_type({uint_to_internal_char(i)});
}

template <typename Rewriter, typename ReductionOrder>
word_type KnuthBendix<Rewriter, ReductionOrder>::internal_string_to_word(
internal_string_type const& s) {
detail::internal_string_type const& s) {
word_type w;
w.reserve(s.size());
for (internal_char_type const& c : s) {
for (detail::internal_char_type const& c : s) {
w.push_back(internal_char_to_uint(c));
}
return w;
}

template <typename Rewriter, typename ReductionOrder>
typename KnuthBendix<Rewriter, ReductionOrder>::internal_char_type
typename detail::internal_char_type
KnuthBendix<Rewriter, ReductionOrder>::external_to_internal_char(
external_char_type c) const {
detail::external_char_type c) const {
LIBSEMIGROUPS_ASSERT(!_internal_is_same_as_external);
return uint_to_internal_char(presentation().index(c));
}

template <typename Rewriter, typename ReductionOrder>
typename KnuthBendix<Rewriter, ReductionOrder>::external_char_type
typename detail::external_char_type
KnuthBendix<Rewriter, ReductionOrder>::internal_to_external_char(
internal_char_type a) const {
detail::internal_char_type a) const {
LIBSEMIGROUPS_ASSERT(!_internal_is_same_as_external);
return presentation().letter_no_checks(internal_char_to_uint(a));
}

template <typename Rewriter, typename ReductionOrder>
void KnuthBendix<Rewriter, ReductionOrder>::external_to_internal_string(
external_string_type& w) const {
detail::external_string_type& w) const {
if (_internal_is_same_as_external) {
return;
}
Expand All @@ -845,7 +846,7 @@ namespace libsemigroups {

template <typename Rewriter, typename ReductionOrder>
void KnuthBendix<Rewriter, ReductionOrder>::internal_to_external_string(
internal_string_type& w) const {
detail::internal_string_type& w) const {
if (_internal_is_same_as_external) {
return;
}
Expand All @@ -856,7 +857,7 @@ namespace libsemigroups {

template <typename Rewriter, typename ReductionOrder>
void KnuthBendix<Rewriter, ReductionOrder>::add_octo(
external_string_type& w) const {
detail::external_string_type& w) const {
if (kind() != congruence_kind::twosided
&& (generating_pairs() | rx::count()) != 0) {
w = presentation().alphabet().back() + w;
Expand All @@ -865,7 +866,7 @@ namespace libsemigroups {

template <typename Rewriter, typename ReductionOrder>
void KnuthBendix<Rewriter, ReductionOrder>::rm_octo(
external_string_type& w) const {
detail::external_string_type& w) const {
if (kind() != congruence_kind::twosided
&& (generating_pairs() | rx::count()) != 0) {
LIBSEMIGROUPS_ASSERT(w.front() == presentation().alphabet().back());
Expand Down Expand Up @@ -914,8 +915,8 @@ namespace libsemigroups {

// OVERLAP_2 from Sims, p77
template <typename Rewriter, typename ReductionOrder>
void KnuthBendix<Rewriter, ReductionOrder>::overlap(Rule const* u,
Rule const* v) {
void KnuthBendix<Rewriter, ReductionOrder>::overlap(detail::Rule const* u,
detail::Rule const* v) {
LIBSEMIGROUPS_ASSERT(u->active() && v->active());
auto const &ulhs = *(u->lhs()), vlhs = *(v->lhs());
auto const &urhs = *(u->rhs()), vrhs = *(v->rhs());
Expand Down
1 change: 0 additions & 1 deletion include/libsemigroups/libsemigroups.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
#include "pbr.hpp"
#include "presentation.hpp"
#include "ranges.hpp"
#include "rewriters.hpp"
#include "runner.hpp"
#include "schreier-sims.hpp"
#include "sims.hpp"
Expand Down
10 changes: 5 additions & 5 deletions include/libsemigroups/obvinf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@
#include <utility> // for pair
#include <vector> // for vector

#include "config.hpp" // for LIBSEMIGROUPS_EIGEN_ENABLED
#include "ranges.hpp" // for word_type etc
#include "rewriters.hpp" // for RewriteTrie
#include "types.hpp" // for word_type etc
#include "config.hpp" // for LIBSEMIGROUPS_EIGEN_ENABLED
#include "ranges.hpp" // for word_type etc
#include "types.hpp" // for word_type etc

#include "detail/eigen.hpp"
#include "detail/uf.hpp" // for Duf
#include "detail/rewriters.hpp" // for RewriteTrie
#include "detail/uf.hpp" // for Duf

namespace libsemigroups {
#ifndef DOXYGEN_SHOULD_SKIP_THIS
Expand Down
Loading

0 comments on commit 7c597a2

Please sign in to comment.