Skip to content

Commit 2525ba0

Browse files
KnuthBendix: don't duplicate overlap work
1 parent 3d9e5b2 commit 2525ba0

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

include/libsemigroups/detail/knuth-bendix-impl.tpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ namespace libsemigroups {
565565
_rewriter.add_rule(u.begin(), u.end(), v.begin(), v.end());
566566
++start;
567567
}
568+
_rewriter.trie().increment_generation();
568569
} else {
569570
// _rewriter.rules() calls process_pending_rules, so can't call it
570571
// inside the rule1 loop below.

include/libsemigroups/detail/overlap-iterators.hpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace libsemigroups::detail {
8787
_suffix_descendent_index(),
8888
_overlap(),
8989
_trie(nullptr),
90-
_index_stack() {};
90+
_index_stack(){};
9191

9292
// TODO: Use an init rather than setting default values?
9393
OverlapIteratorTrie(Trie const& trie)
@@ -177,9 +177,11 @@ namespace libsemigroups::detail {
177177
// TODO better name
178178
bool traverse_to_root() {
179179
while (_suffix_index != Trie::root) {
180-
_index_stack.emplace_back(_suffix_index);
181-
if (find_next_descendent()) {
182-
return true;
180+
if (should_check_descendants(_suffix_index)) {
181+
_index_stack.emplace_back(_suffix_index);
182+
if (find_next_descendent()) {
183+
return true;
184+
}
183185
}
184186
_suffix_index = _trie->node_no_checks(_suffix_index).suffix_link();
185187
}
@@ -204,13 +206,22 @@ namespace libsemigroups::detail {
204206
for (letter_type x = 0; x < _trie->alphabet_size(); ++x) {
205207
index_type child_index
206208
= _trie->child_no_checks(_suffix_descendent_index, x);
207-
if (child_index != UNDEFINED) {
209+
if (child_index != UNDEFINED
210+
&& should_check_descendants(child_index)) {
208211
_index_stack.emplace_back(child_index);
209212
}
210213
}
211214
}
212215
return false;
213216
}
217+
218+
// The <generation> of a trie represents which iteration
219+
bool should_check_descendants(size_t index) {
220+
return _trie->node_no_checks(_word_index).last_checked()
221+
== _trie->generation()
222+
|| _trie->node_no_checks(index).last_checked()
223+
== _trie->generation();
224+
}
214225
}; // class OverlapIteratorTrie
215226
} // namespace libsemigroups::detail
216227

0 commit comments

Comments
 (0)