Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions include/libsemigroups/detail/aho-corasick-impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace libsemigroups {
// Private data
////////////////////////////////////////////////////////////////////////
private:
mutable size_t _last_checked;
uint32_t _height;
index_type _link;
index_type _parent;
Expand Down Expand Up @@ -97,6 +98,10 @@ namespace libsemigroups {
// Getters - public
////////////////////////////////////////////////////////////////////////

[[nodiscard]] size_t last_checked() const noexcept {
return _last_checked;
}

[[nodiscard]] size_t height() const noexcept {
return _height;
}
Expand Down Expand Up @@ -126,12 +131,22 @@ namespace libsemigroups {
return _value;
}

////////////////////////////////////////////////////////////////////////
// Setters - public
////////////////////////////////////////////////////////////////////////

Node const& last_checked(size_t val) const noexcept {
_last_checked = val;
return *this;
}

private:
////////////////////////////////////////////////////////////////////////
// Setters - private
////////////////////////////////////////////////////////////////////////

// All setters are private to avoid corrupting the objects.
// All setters of non-mutable members are private to avoid corrupting
// the objects.

Node const& height(size_t val) noexcept {
_height = val;
Expand All @@ -148,7 +163,6 @@ namespace libsemigroups {
_value = value;
return *this;
}

}; // class Node

// TODO(1) if we store pointers here instead of Nodes, then inside the
Expand All @@ -162,6 +176,7 @@ namespace libsemigroups {
std::vector<index_type> _inactive_nodes_index;
std::vector<index_type> _node_indices_to_update;
std::unordered_set<index_type> _terminal_nodes_index;
mutable size_t _generation;

// TODO(1): it seems likely that the positions of the active nodes in
// _all_nodes will become scattered and disordered over time, and so it'd
Expand All @@ -187,6 +202,14 @@ namespace libsemigroups {

~AhoCorasickImpl();

void increment_generation() const noexcept {
++_generation;
}

size_t generation() const noexcept {
return _generation;
}

size_t alphabet_size() const noexcept {
return _children.number_of_cols();
}
Expand Down Expand Up @@ -407,7 +430,7 @@ namespace libsemigroups {
Word const& w);

} // namespace aho_corasick_impl
} // namespace detail
} // namespace detail
} // namespace libsemigroups

#include "aho-corasick-impl.tpp"
Expand Down
5 changes: 3 additions & 2 deletions include/libsemigroups/detail/aho-corasick-impl.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace libsemigroups {
LIBSEMIGROUPS_ASSERT(val != nullptr);
index_type current = root;
for (auto it = first; it != last; ++it) {
_all_nodes[current].last_checked(generation());
index_type next = _children.get(current, *it);
if (next == UNDEFINED) {
// index of next node added
Expand All @@ -40,7 +41,7 @@ namespace libsemigroups {
if (inserted) {
_all_nodes[current].value(val);
}

_all_nodes[current].last_checked(generation());
return {current, inserted};
}

Expand Down Expand Up @@ -309,5 +310,5 @@ namespace libsemigroups {
}

} // namespace aho_corasick_impl
} // namespace detail
} // namespace detail
} // namespace libsemigroups
2 changes: 1 addition & 1 deletion include/libsemigroups/detail/knuth-bendix-impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ namespace libsemigroups {

bool finished_impl() const override;
}; // class KnuthBendixImpl
} // namespace detail
} // namespace detail

////////////////////////////////////////////////////////////////////////
// global functions - to_human_readable_repr
Expand Down
1 change: 1 addition & 0 deletions include/libsemigroups/detail/knuth-bendix-impl.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ namespace libsemigroups {
_rewriter.add_rule(u.begin(), u.end(), v.begin(), v.end());
++start;
}
_rewriter.trie().increment_generation();
} else {
// _rewriter.rules() calls process_pending_rules, so can't call it
// inside the rule1 loop below.
Expand Down
10 changes: 5 additions & 5 deletions include/libsemigroups/detail/knuth-bendix-nf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace libsemigroups {
class KnuthBendixNormalFormRange : public Paths<uint32_t> {
using Paths_ = Paths<uint32_t>;

mutable Word _current;
mutable Word _current;
KnuthBendix<Word, RewritingSystem, ReductionOrder>* _kb;

public:
Expand Down Expand Up @@ -113,14 +113,14 @@ namespace libsemigroups {

template <typename Word, typename RewritingSystem, typename ReductionOrder>
KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>&
KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>::operator=(
KnuthBendixNormalFormRange const&)
KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>::
operator=(KnuthBendixNormalFormRange const&)
= default;

template <typename Word, typename RewritingSystem, typename ReductionOrder>
KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>&
KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>::operator=(
KnuthBendixNormalFormRange&&)
KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>::
operator=(KnuthBendixNormalFormRange&&)
= default;

template <typename Word, typename RewritingSystem, typename ReductionOrder>
Expand Down
25 changes: 18 additions & 7 deletions include/libsemigroups/detail/overlap-iterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ namespace libsemigroups::detail {
_suffix_descendent_index(),
_overlap(),
_trie(nullptr),
_index_stack() {};
_index_stack() {}

// TODO: Use an init rather than setting default values?
OverlapIteratorTrie(Trie const& trie)
explicit OverlapIteratorTrie(Trie const& trie)
: _current_word_iterator(trie.cbegin_terminal_nodes()),
_last_word_iterator(trie.cend_terminal_nodes()),
_word_index(Trie::root),
Expand Down Expand Up @@ -177,9 +177,11 @@ namespace libsemigroups::detail {
// TODO better name
bool traverse_to_root() {
while (_suffix_index != Trie::root) {
_index_stack.emplace_back(_suffix_index);
if (find_next_descendent()) {
return true;
if (should_check_descendants(_suffix_index)) {
_index_stack.emplace_back(_suffix_index);
if (find_next_descendent()) {
return true;
}
}
_suffix_index = _trie->node_no_checks(_suffix_index).suffix_link();
}
Expand All @@ -204,14 +206,23 @@ namespace libsemigroups::detail {
for (letter_type x = 0; x < _trie->alphabet_size(); ++x) {
index_type child_index
= _trie->child_no_checks(_suffix_descendent_index, x);
if (child_index != UNDEFINED) {
if (child_index != UNDEFINED
&& should_check_descendants(child_index)) {
_index_stack.emplace_back(child_index);
}
}
}
return false;
}

// The <generation> of a trie represents which iteration
bool should_check_descendants(size_t index) {
return _trie->node_no_checks(_word_index).last_checked()
== _trie->generation()
|| _trie->node_no_checks(index).last_checked()
== _trie->generation();
}
}; // class OverlapIteratorTrie
} // namespace libsemigroups::detail

#endif
#endif // LIBSEMIGROUPS_DETAIL_OVERLAP_ITERATORS_HPP_
5 changes: 4 additions & 1 deletion include/libsemigroups/detail/rewriters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ namespace libsemigroups {
virtual void report_reducing_rules(
std::atomic_uint64_t const&,
std::chrono::high_resolution_clock::time_point const&) const {}

}; // class RewritingSystemBase

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -386,6 +385,10 @@ namespace libsemigroups {
return _rule_trie;
}

[[nodiscard]] Trie& trie() noexcept {
return _rule_trie;
}

private:
////////////////////////////////////////////////////////////////////////
// Private member functions
Expand Down
3 changes: 2 additions & 1 deletion include/libsemigroups/detail/rules.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cstddef> // for size_t
#include <cstdint> // for uint64_t
#include <list> // for list
#include <string> // for std::string
#include <utility> // for move
#include <vector> // for vector

Expand Down Expand Up @@ -273,4 +274,4 @@ namespace libsemigroups {

} // namespace detail
} // namespace libsemigroups
#endif
#endif // LIBSEMIGROUPS_DETAIL_RULES_HPP_
37 changes: 19 additions & 18 deletions include/libsemigroups/knuth-bendix-helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ namespace libsemigroups {
//! This function triggers a full enumeration of \p kb.
//!
//! \tparam Word the type of the words contained in the output range.
//! \tparam RewritingSystem the first template parameter for \ref_knuth_bendix.
//! \tparam ReductionOrder the second template parameter for
//! \ref_knuth_bendix.
//! \tparam RewritingSystem the first template parameter for
//! \ref_knuth_bendix. \tparam ReductionOrder the second template parameter
//! for \ref_knuth_bendix.
//!
//! \param kb the \ref_knuth_bendix instance.
//!
Expand All @@ -79,8 +79,8 @@ namespace libsemigroups {
template <typename Word, typename RewritingSystem, typename ReductionOrder>
[[nodiscard]] auto
normal_forms(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb) {
return detail::KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>(
kb);
return detail::
KnuthBendixNormalFormRange<Word, RewritingSystem, ReductionOrder>(kb);
}

////////////////////////////////////////////////////////////////////////
Expand All @@ -106,9 +106,9 @@ namespace libsemigroups {
//!
//! \tparam Word the type of the words contained in the output range
//! (default: std::string).
//! \tparam RewritingSystem the first template parameter for \ref_knuth_bendix.
//! \tparam ReductionOrder the second template parameter for
//! \ref_knuth_bendix.
//! \tparam RewritingSystem the first template parameter for
//! \ref_knuth_bendix. \tparam ReductionOrder the second template parameter
//! for \ref_knuth_bendix.
//!
//! \param kb1 the first \ref_knuth_bendix instance.
//! \param kb2 the second \ref_knuth_bendix instance.
Expand All @@ -128,9 +128,9 @@ namespace libsemigroups {
//!
//! \cong_common_warn_undecidable{Knuth-Bendix}.
template <typename Word, typename RewritingSystem, typename ReductionOrder>
[[nodiscard]] std::vector<std::vector<Word>>
non_trivial_classes(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb1,
KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb2);
[[nodiscard]] std::vector<std::vector<Word>> non_trivial_classes(
KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb1,
KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb2);

} // namespace congruence_common

Expand Down Expand Up @@ -174,9 +174,9 @@ namespace libsemigroups {
//!
//! \tparam Word the type of the words in the
//! \ref KnuthBendix::presentation.
//! \tparam RewritingSystem the first template parameter for \ref_knuth_bendix.
//! \tparam ReductionOrder the second template parameter for
//! \ref_knuth_bendix.
//! \tparam RewritingSystem the first template parameter for
//! \ref_knuth_bendix. \tparam ReductionOrder the second template parameter
//! for \ref_knuth_bendix.
//!
//! \param kb the \ref_knuth_bendix instance.
//!
Expand All @@ -188,7 +188,8 @@ namespace libsemigroups {
//!
//! \sa KnuthBendix::run.
template <typename Word, typename RewritingSystem, typename ReductionOrder>
void by_overlap_length(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb);
void
by_overlap_length(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb);

//! \ingroup knuth_bendix_helpers_group
//!
Expand All @@ -198,9 +199,9 @@ namespace libsemigroups {
//!
//! \tparam Word the type of the words in the
//! \ref KnuthBendix::presentation.
//! \tparam RewritingSystem the first template parameter for \ref_knuth_bendix.
//! \tparam ReductionOrder the second template parameter for
//! \ref_knuth_bendix.
//! \tparam RewritingSystem the first template parameter for
//! \ref_knuth_bendix. \tparam ReductionOrder the second template parameter
//! for \ref_knuth_bendix.
//!
//! \param kb the \ref_knuth_bendix instance defining the rules that are to
//! be checked for being reduced.
Expand Down
4 changes: 2 additions & 2 deletions include/libsemigroups/obvinf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ namespace libsemigroups {
//! \note If this function returns \c false, it is still possible that the
//! quotient defined by the \ref_knuth_bendix object \p kb is infinite.
template <typename RewritingSystem, typename ReductionOrder>
bool
is_obviously_infinite(detail::KnuthBendixImpl<RewritingSystem, ReductionOrder>& kb) {
bool is_obviously_infinite(
detail::KnuthBendixImpl<RewritingSystem, ReductionOrder>& kb) {
if (kb.finished()) {
return !v4::word_graph::is_acyclic(kb.gilman_graph());
}
Expand Down
13 changes: 8 additions & 5 deletions include/libsemigroups/to-froidure-pin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ namespace libsemigroups {
//! \p kb.
//!
//! \tparam Thing used for SFINAE should be FroidurePin.
//! \tparam RewritingSystem the second template parameter for \ref_knuth_bendix.
//! \tparam ReductionOrder the third template parameter for \ref_knuth_bendix.
//! \tparam RewritingSystem the second template parameter for
//! \ref_knuth_bendix. \tparam ReductionOrder the third template parameter for
//! \ref_knuth_bendix.
//!
//! \param kb the \ref_knuth_bendix instance to convert.
//!
Expand All @@ -180,9 +181,11 @@ namespace libsemigroups {
typename Word,
typename RewritingSystem,
typename ReductionOrder>
auto to(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb) -> std::enable_if_t<
std::is_same_v<Thing<int>, FroidurePin<int>>,
FroidurePin<detail::KBE<KnuthBendix<Word, RewritingSystem, ReductionOrder>>>>;
auto to(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb)
-> std::enable_if_t<
std::is_same_v<Thing<int>, FroidurePin<int>>,
FroidurePin<
detail::KBE<KnuthBendix<Word, RewritingSystem, ReductionOrder>>>>;

//! \ingroup to_froidure_pin_group
//!
Expand Down
11 changes: 7 additions & 4 deletions include/libsemigroups/to-froidure-pin.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace libsemigroups {

template <typename Word, typename RewritingSystem, typename ReductionOrder>
FroidurePin(KnuthBendix<Word, RewritingSystem, ReductionOrder> const&)
-> FroidurePin<detail::KBE<KnuthBendix<Word, RewritingSystem, ReductionOrder>>>;
-> FroidurePin<
detail::KBE<KnuthBendix<Word, RewritingSystem, ReductionOrder>>>;

FroidurePin(detail::ToddCoxeterImpl const&)->FroidurePin<detail::TCE>;

Expand Down Expand Up @@ -89,9 +90,11 @@ namespace libsemigroups {
typename Word,
typename RewritingSystem,
typename ReductionOrder>
auto to(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb) -> std::enable_if_t<
std::is_same_v<Thing<int>, FroidurePin<int>>,
FroidurePin<detail::KBE<KnuthBendix<Word, RewritingSystem, ReductionOrder>>>> {
auto to(KnuthBendix<Word, RewritingSystem, ReductionOrder>& kb)
-> std::enable_if_t<
std::is_same_v<Thing<int>, FroidurePin<int>>,
FroidurePin<detail::KBE<
KnuthBendix<Word, RewritingSystem, ReductionOrder>>>> {
size_t const n = kb.presentation().alphabet().size();

if (n == 0) {
Expand Down
5 changes: 3 additions & 2 deletions include/libsemigroups/to-presentation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ namespace libsemigroups {
//! `Presentation<WordOut>` for some type `WordOut`.
//! \tparam WordIn the type of the rules in the presentation of the
//! \ref_knuth_bendix object \p kb.
//! \tparam RewritingSystem the second template parameter for \ref_knuth_bendix.
//! \tparam ReductionOrder the third template parameter for \ref_knuth_bendix.
//! \tparam RewritingSystem the second template parameter for
//! \ref_knuth_bendix. \tparam ReductionOrder the third template parameter for
//! \ref_knuth_bendix.
//!
//! \param kb the \ref_knuth_bendix object from which to obtain the rules.
//!
Expand Down
Loading
Loading