Skip to content

Commit

Permalink
Almost all test running
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-mitchell committed Nov 21, 2024
1 parent 822b758 commit a91cd01
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 236 deletions.
30 changes: 27 additions & 3 deletions include/libsemigroups/cong.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,36 @@ namespace libsemigroups {
//! use a std::shared_ptr to avoid the copy!
// template <typename T>
// TODO(0) should be a helper
Congruence(congruence_kind type, FroidurePinBase& S) : Congruence() {
init(type, S);
template <typename Node>
Congruence(congruence_kind type,
FroidurePinBase& S,
WordGraph<Node> const& wg)
: Congruence() {
init(type, S, wg);
}

// TODO doc
Congruence& init(congruence_kind type, FroidurePinBase& S);
// TODO(0) helper
template <typename Node>
Congruence& init(congruence_kind type,
FroidurePinBase& S,
WordGraph<Node> const& wg) {
if (S.is_finite() != tril::FALSE) {
S.run();
} else {
LIBSEMIGROUPS_EXCEPTION(
"the 2nd argument does not represent a finite semigroup!");
}
init(type);

// TODO(later) if necessary make a runner that tries to S.run(), then get
// the Cayley graph and use that in the ToddCoxeter, at present that'll
// happen here in the constructor
auto tc = std::make_shared<ToddCoxeter>(to_todd_coxeter(type, S, wg));
add_runner(tc);
// TODO add felsch TC also
return *this;
}

//! Construct from kind (left/right/2-sided) and Presentation.
//!
Expand Down
2 changes: 1 addition & 1 deletion include/libsemigroups/detail/race.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ namespace libsemigroups {
t.at(i).join();
}
report_elapsed_time("Race: ", tmr);
report_default("\n");
for (auto method = _runners.begin(); method < _runners.end();
++method) {
if ((*method)->finished()) {
Expand All @@ -327,7 +328,6 @@ namespace libsemigroups {
break;
}
}
// Uncommenting the following stops _winner_index from working
if (_winner != nullptr) {
for (auto rnnr : _runners) {
if (rnnr != _winner) {
Expand Down
104 changes: 43 additions & 61 deletions include/libsemigroups/knuth-bendix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
// This file contains a class KnuthBendix which implements the Knuth-Bendix
// algorithm for finitely presented monoids.

// TODO:
// TODO(1)
// * noexcept
// * separate rule container from Rules
// TODO(0)
// * fix doc
// * template Rules
// * template Order
// * Separate rule container from Rules
// * nodiscard

#ifndef LIBSEMIGROUPS_KNUTH_BENDIX_HPP_
#define LIBSEMIGROUPS_KNUTH_BENDIX_HPP_
Expand Down Expand Up @@ -65,16 +65,14 @@
#include "detail/rewriters.hpp" // for RewriteTrie
#include "detail/string.hpp" // for is_prefix, maximum_common_prefix

#include "ranges.hpp" // for iterator_range

namespace libsemigroups {
// Forward declarations
namespace detail {
template <typename KnuthBendix_>
class KBE;
} // namespace detail

// TODO update the description
// TODO(0) update the description
//! Defined in \c knuth-bendix.hpp.
//!
//! On this page we describe the functionality relating to the Knuth-Bendix
Expand Down Expand Up @@ -260,38 +258,38 @@ namespace libsemigroups {
//! rules of \p copy.
KnuthBendix(KnuthBendix const& that);

// TODO doc
// TODO(0) doc
KnuthBendix(KnuthBendix&&);

// TODO doc
// TODO(0) doc
KnuthBendix& operator=(KnuthBendix const&);

// TODO doc
// TODO(0) doc
KnuthBendix& operator=(KnuthBendix&&);

~KnuthBendix();

// TODO doc
// TODO(0) doc
KnuthBendix(congruence_kind knd, Presentation<std::string> const& p);

// TODO doc
// TODO(0) doc
KnuthBendix& init(congruence_kind knd, Presentation<std::string> const& p);

// TODO doc
// TODO(0) doc
KnuthBendix(congruence_kind knd, Presentation<std::string>&& p);

// TODO doc
// TODO(0) doc
KnuthBendix& init(congruence_kind knd, Presentation<std::string>&& p);

// TODO doc
// TODO(0) doc
// No rvalue ref version because we can't use it.
template <typename Word>
explicit KnuthBendix(congruence_kind knd, Presentation<Word> const& p)
: KnuthBendix(knd, to_presentation<std::string>(p, [](auto const& x) {
return x;
})) {}

// TODO doc
// TODO(0) doc
template <typename Word>
KnuthBendix& init(congruence_kind knd, Presentation<Word> const& p) {
init(knd,
Expand Down Expand Up @@ -351,14 +349,10 @@ namespace libsemigroups {
Iterator2 last1,
Iterator3 first2,
Iterator4 last2) {
using iterator_points_at = decltype(*std::declval<Iterator1>());
static_assert(
std::is_convertible_v<iterator_points_at, native_letter_type>);
// TODO(0) impl for non-native letter types
if (std::equal(first1, last1, first2, last2)) {
return tril::TRUE;
}
// TODO(0) remove allocations here
// TODO(1) remove allocations here
std::string w1, w2;
reduce_no_run_no_checks(std::back_inserter(w1), first1, last1);
reduce_no_run_no_checks(std::back_inserter(w2), first2, last2);
Expand Down Expand Up @@ -420,7 +414,7 @@ namespace libsemigroups {
OutputIterator reduce_no_run_no_checks(OutputIterator d_first,
InputIterator1 first,
InputIterator2 last) {
// TODO(0) improve this to not require _tmp_element1
// TODO(1) improve this to not require _tmp_element1
if constexpr (std::is_same_v<InputIterator1, char const*>) {
static_assert(std::is_same_v<InputIterator2, char const*>);
_tmp_element1.assign(first, std::distance(first, last));
Expand All @@ -432,6 +426,7 @@ namespace libsemigroups {
std::begin(_tmp_element1), std::end(_tmp_element1), d_first);
}

// TODO(0) should be const
template <typename OutputIterator,
typename InputIterator1,
typename InputIterator2>
Expand Down Expand Up @@ -462,7 +457,7 @@ namespace libsemigroups {
return reduce_no_run(d_first, first, last);
}

// TODO(0) implement reduce_inplace x4 if possible.
// TODO(1) implement reduce_inplace x4 if possible.

//////////////////////////////////////////////////////////////////////////
// KnuthBendix - setters for optional parameters - public
Expand Down Expand Up @@ -692,7 +687,6 @@ namespace libsemigroups {
// KnuthBendix - member functions for rules and rewriting - public
//////////////////////////////////////////////////////////////////////////

// TODO can this be done in a better ways?
//! \brief Check if every letter of a word is in the presentation's
//! alphabet.
//!
Expand Down Expand Up @@ -735,8 +729,7 @@ namespace libsemigroups {
return _presentation;
}

// TODO add note about empty active rules after
// init and non-const-ness
// TODO(0) add note about empty active rules after init and non-const-ness
//! \brief Return the current number of active
//! rules in the KnuthBendix instance.
//!
Expand Down Expand Up @@ -798,22 +791,21 @@ namespace libsemigroups {
return _rewriter.stats().total_rules;
}

// TODO What do we do about doc-ing this?
// TODO(0) What do we do about doc-ing this?
//! Type of the rules in the system.
using rule_type = std::pair<std::string, std::string>;
// TODO update the doc, now returns a Range
// TODO add note about empty active rules after

// TODO(0) update the doc, now returns a Range
// TODO(0) add note about empty active rules after
// init
//! \brief Return a copy of the active rules.
//!
//! This member function returns a vector
//! consisting of the pairs of strings which
//! represent the rules of the KnuthBendix
//! instance. The \c first entry in every such
//! pair is greater than the \c second according
//! to the reduction ordering of the KnuthBendix
//! instance. The rules are sorted according to
//! the reduction ordering used by the rewriting
//! system, on the first entry.
//! This member function returns a vector consisting of the pairs of
//! strings which represent the rules of the KnuthBendix instance. The \c
//! first entry in every such pair is greater than the \c second according
//! to the reduction ordering of the KnuthBendix instance. The rules are
//! sorted according to the reduction ordering used by the rewriting system,
//! on the first entry.
//!
//! \returns
//! A copy of the currently active rules, a
Expand All @@ -832,7 +824,7 @@ namespace libsemigroups {
}
return iterator_range(_rewriter.begin(), _rewriter.end())
| transform([this](auto const& rule) {
// TODO remove allocation
// TODO(1) remove allocation
detail::internal_string_type lhs
= detail::internal_string_type(*rule->lhs());
detail::internal_string_type rhs
Expand All @@ -844,8 +836,7 @@ namespace libsemigroups {
}

private:
// TODO(0) move this to a more appropriate location in this file

// TODO(0) recycle this doc or delete
// TODO add note about empty active rules after
// init and non-const-ness
//! \brief Rewrite a word in-place.
Expand All @@ -862,6 +853,7 @@ namespace libsemigroups {
// TODO update doc
void rewrite_inplace(std::string& w);

// TODO(0) recycle this doc or delete
// TODO add note about empty active rules after
// init and non-const-ness
//! \brief Rewrite a word.
Expand Down Expand Up @@ -946,7 +938,7 @@ namespace libsemigroups {
//////////////////////////////////////////////////////////////////////////
// KnuthBendix - attributes - public
//////////////////////////////////////////////////////////////////////////

// TODO(0) recycle or delete
//! \brief Check if two inputs are equivalent with respect to the system
//!
//! By first testing \c string equivalence, then by rewriting the inputs,
Expand All @@ -964,12 +956,8 @@ namespace libsemigroups {
//! terminates when the rewriting system is confluent, which may be never.
//!
//! \sa run.
// TODO rename contains
// TODO do a no check version of this
// TODO version taking rvalue refs?

// REVIEW Why does equal to take strings, but
// this take words?
// TODO(0) recycle or delete
//! \brief Check containment
//!
//! Check if the pair of words \p u and \p v is
Expand All @@ -984,16 +972,9 @@ namespace libsemigroups {
//! congruence, and \c false otherwise.
//!
//! \sa \ref equal_to.
// TODO add perf warning

// REVIEW can this be copied given the params
// are a different type?
//! \copydoc contains

// No in-place version just use rewrite instead,
// this only exists so that run is called.
// TODO required?
// [[nodiscard]] std::string normal_form(std::string const& w);

private:
void report_presentation(Presentation<std::string> const&) const;
Expand Down Expand Up @@ -1035,8 +1016,7 @@ namespace libsemigroups {
}

//////////////////////////////////////////////////////////////////////////
// Runner - pure virtual member functions -
// private
// Runner - pure virtual member functions - private
//////////////////////////////////////////////////////////////////////////

void run_impl() override;
Expand Down Expand Up @@ -1071,7 +1051,7 @@ namespace libsemigroups {
using congruence_interface::currently_contains;
using congruence_interface::currently_contains_no_checks;

// TODO(0) version of this goes to congruence_interface??
// TODO(JDM) version of this goes to congruence_interface??
template <typename Rewriter, typename ReductionOrder>
[[nodiscard]] bool contains(KnuthBendix<Rewriter, ReductionOrder>& kb,
std::string_view u,
Expand All @@ -1086,13 +1066,14 @@ namespace libsemigroups {
char const* v) {
return kb.contains(u, u + std::strlen(u), v, v + std::strlen(v));
}
// TODO(0) 6x more contains functions for string_views + char const*

// TODO(JDM) 6x more contains functions for string_views + char const*

////////////////////////////////////////////////////////////////////////
// Interface helpers - reduce
////////////////////////////////////////////////////////////////////////

// TODO(0) version of this goes to congruence_interface??
// TODO(JDM) version of this goes to congruence_interface??
// TODO(0) doc
template <typename Rewriter,
typename ReductionOrder,
Expand Down Expand Up @@ -1167,7 +1148,7 @@ namespace libsemigroups {
// Interface helpers - to_human_readable_repr
////////////////////////////////////////////////////////////////////////

// TODO What should the \param be?
// TODO(0) What should the \param be?
//! \brief Return a string representation of a KnuthBendix instance
//!
//! Return a string representation of a KnuthBendix instance, specifying the
Expand All @@ -1190,6 +1171,7 @@ namespace libsemigroups {
str alphabet_size = std::to_string(kb.presentation().alphabet().size());
str n_rules = std::to_string(kb.number_of_active_rules());

// TODO(JE) use fmt::format instead
return str("<") + conf + " on " + alphabet_size + " letters with "
+ n_rules + " active rules>";
}
Expand Down Expand Up @@ -1254,7 +1236,7 @@ namespace libsemigroups {
if (!kb.presentation().contains_empty_word()) {
paths.next();
}
// TODO update to return strings
// TODO(0) update to return strings
return paths;
}

Expand Down
8 changes: 3 additions & 5 deletions include/libsemigroups/knuth-bendix.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,11 @@ namespace libsemigroups {
for (auto it = pairs.cbegin(); it != pairs.cend(); ++it) {
// it points at a word_type
p.rules.emplace_back(it->cbegin(), it->cend());
auto& lhs = p.rules.back();
add_octo(lhs);
add_octo(p.rules.back());
++it;
p.rules.emplace_back(it->cbegin(), it->cend());
auto& rhs = p.rules.back();
add_octo(rhs);
add_rule_impl(lhs, rhs);
add_octo(p.rules.back());
add_rule_impl(p.rules.cend()[-2], p.rules.cend()[-1]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/libsemigroups/sims.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2868,7 +2868,7 @@ namespace libsemigroups {
// TODO avoid the copy here
copy.induced_subgraph_no_checks(static_cast<Node>(0),
wg.number_of_active_nodes());
tc.init(tc.kind(), p, copy).add_pair(wx, wy);
todd_coxeter::add_pair(tc.init(tc.kind(), p, copy), wx, wy);
LIBSEMIGROUPS_ASSERT(tc.word_graph().number_of_nodes()
== wg.number_of_active_nodes());
// fmt::print("x = {}, y = {}\n", x, y);
Expand Down
Loading

0 comments on commit a91cd01

Please sign in to comment.