@@ -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