Skip to content

Commit

Permalink
Get alphabet from presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph-Edwards committed Nov 14, 2023
1 parent 2cca72e commit d353aae
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
71 changes: 51 additions & 20 deletions include/libsemigroups/knuth-bendix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,23 +376,52 @@ namespace libsemigroups {
};

class Rewriter : public Rules {
mutable std::atomic<bool> _confluent;
mutable std::atomic<bool> _confluence_known;
std::stack<Rule*> _stack;
std::unordered_set<internal_char_type> _alphabet;
mutable std::atomic<bool> _confluent;
mutable std::atomic<bool> _confluence_known;
std::atomic<bool> _requires_alphabet;
std::stack<Rule*> _stack;

using alphabet_citerator
= std::unordered_set<internal_char_type>::const_iterator;

public:
Rewriter() = default;
Rewriter& init();

Rewriter(bool requires_alphabet) : Rewriter() {
_requires_alphabet = requires_alphabet;
}

~Rewriter();

Rewriter& operator=(Rewriter const& that) {
Rules::operator=(that);
_confluent = that._confluent.load();
_confluence_known = that._confluence_known.load();
_confluent = that._confluent.load();
_confluence_known = that._confluence_known.load();
_requires_alphabet = that._requires_alphabet.load();
if (_requires_alphabet) {
_alphabet = that._alphabet;
}
return *this;
}

bool requires_alphabet() const {
return _requires_alphabet;
}

decltype(_alphabet) alphabet() const {
return _alphabet;
}

alphabet_citerator alphabet_cbegin() const {
return _alphabet.cbegin();
}

alphabet_citerator alphabet_cend() const {
return _alphabet.cend();
}

// TODO remove?
// TODO to cpp if keeping it
void confluent(tril val) const {
Expand Down Expand Up @@ -444,6 +473,10 @@ namespace libsemigroups {
}
return false;
}

void add_to_alphabet(internal_char_type letter) {
_alphabet.emplace(letter);
}
};

class RewriteFromLeft : public Rewriter {
Expand Down Expand Up @@ -493,16 +526,15 @@ namespace libsemigroups {
class RewriteTrie : public Rewriter {
using index_type = AhoCorasick::index_type;

std::unordered_map<index_type, Rule*> _rules;
std::unordered_set<internal_char_type> _alphabet;
AhoCorasick _trie;
std::unordered_map<index_type, Rule*> _rules;
AhoCorasick _trie;

public:
using Rewriter::confluent;
using Rules::stats;
using iterator = internal_string_type::iterator;

RewriteTrie() = default;
RewriteTrie() : Rewriter(true), _rules(), _trie() {}

RewriteTrie(const RewriteTrie& that);
RewriteTrie& operator=(RewriteTrie const& that) {
Expand All @@ -521,7 +553,6 @@ namespace libsemigroups {
Rewriter::init();
_trie.init();
_rules.clear();
_alphabet.clear();
return *this;
}

Expand Down Expand Up @@ -669,7 +700,7 @@ namespace libsemigroups {
return true;
}

for (auto x = _alphabet.cbegin(); x != _alphabet.cend(); ++x) {
for (auto x = alphabet_cbegin(); x != alphabet_cend(); ++x) {
if (!backtrack_confluence(
rule1,
_trie.traverse_from(current_node,
Expand All @@ -691,15 +722,15 @@ namespace libsemigroups {
void add_rule_to_trie(Rule* rule) {
index_type node = _trie.add_word_no_checks(rule->lhs()->cbegin(),
rule->lhs()->cend());
_rules[node] = rule;
for (auto it = rule->lhs()->cbegin(); it != rule->lhs()->cend(); ++it) {
#ifdef LIBSEMIGROUPS_DEBUG
_alphabet.emplace(*it);
LIBSEMIGROUPS_ASSERT(_alphabet.count(*it) == 1);
#else
_alphabet.emplace(*it);
#endif
}
_rules.emplace(node, rule);
// for (auto it = rule->lhs()->cbegin(); it !=
// rule->lhs()->cend(); ++it) {
// #ifdef LIBSEMIGROUPS_DEBUG _alphabet.emplace(*it);
// LIBSEMIGROUPS_ASSERT(_alphabet.count(*it) == 1);
// #else
// _alphabet.emplace(*it);
// #endif
// }
}

// TODO Make use of trie
Expand Down
16 changes: 16 additions & 0 deletions src/knuth-bendix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ namespace libsemigroups {

KnuthBendix::Rewriter& KnuthBendix::Rewriter::init() {
Rules::init();
if (_requires_alphabet) {
_alphabet.clear();
}
// Put all active rules and those rules in the stack into the
// inactive_rules list
while (!_stack.empty()) {
Expand Down Expand Up @@ -1157,6 +1160,19 @@ namespace libsemigroups {
break;
}
}

if (_rewriter.requires_alphabet()) {
if (_internal_is_same_as_external) {
for (auto x = p.alphabet().begin(); x != p.alphabet().end(); ++x) {
_rewriter.add_to_alphabet(*x);
}
} else {
for (auto x = p.alphabet().begin(); x != p.alphabet().end(); ++x) {
_rewriter.add_to_alphabet(external_to_internal_char(*x));
}
}
}

auto const first = p.rules.cbegin();
auto const last = p.rules.cend();
for (auto it = first; it != last; it += 2) {
Expand Down
1 change: 1 addition & 0 deletions tests/test-rewriting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace libsemigroups {
REQUIRE(rt.number_of_active_rules() == 0);
rt.add_rule("ba"s, "a"s);
REQUIRE(rt.number_of_active_rules() == 1);
REQUIRE(rt.requires_alphabet());
}

LIBSEMIGROUPS_TEST_CASE("RewriteTrie", "001", "simple test", "[quick]") {
Expand Down

0 comments on commit d353aae

Please sign in to comment.