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
1,155 changes: 729 additions & 426 deletions src/froidure-pin.cpp

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions src/kbe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// libsemigroups_pybind11
// Copyright (C) 2025 James D. Mitchell
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

// libsemigroups headers
#include <libsemigroups/froidure-pin.hpp>
#include <libsemigroups/knuth-bendix.hpp>

#include <libsemigroups/detail/kbe.hpp>

// pybind11....
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

// libsemigroups_pybind11....
#include "kbe.hpp" // for ElementStateful
#include "main.hpp" // for init_kbe

namespace libsemigroups {
namespace py = pybind11;

template <typename Thing>
void bind_kbe(py::module& m, std::string_view thing_name) {
using Result = ElementStateful<FroidurePin<Thing>>;
using Word = typename FroidurePin<Thing>::element_type::native_word_type;

py::class_<Result> thing(m, thing_name.data());

thing.def("__repr__", [](Result const& self) {
return fmt::format("\"{}\"", self.element.word());
});
thing.def("__str__",
[](Result const& self) { return self.element.word(); });

thing.def("word", [](Result const& self) { return self.element.word(); });

thing.def("__mul__", [](Result const& self, Result const& other) {
Result result;
result.state_ptr = self.state_ptr;
Product<typename Result::element_type>()(
result.element, self.element, other.element, result.state_ptr, 0);
return result;
});

thing.def("__eq__", [](Result const& self, Result const& other) {
return self.element == other.element && self.state_ptr == other.state_ptr;
});

thing.def("__eq__", [](Result const& self, Word const& other) {
return self.element.word() == other;
});
thing.def("__eq__", [](Word const& other, Result const& self) {
return self.element.word() == other;
});

// This class does not have very many methods, maybe that's a good thing,
// better to convert to a "word" and use that instead.
}

void init_kbe(py::module& m) {
using KBEStringTrie
= detail::KBE<KnuthBendix<std::string, detail::RewriteTrie>>;
using KBEWordTrie
= detail::KBE<KnuthBendix<word_type, detail::RewriteTrie>>;
using KBEStringFromLeft
= detail::KBE<KnuthBendix<std::string, detail::RewriteFromLeft>>;
using KBEWordFromLeft
= detail::KBE<KnuthBendix<word_type, detail::RewriteFromLeft>>;

bind_kbe<KBEStringTrie>(m, "KBEStringTrie");
bind_kbe<KBEWordTrie>(m, "KBEWordTrie");
bind_kbe<KBEStringFromLeft>(m, "KBEStringFromLeft");
bind_kbe<KBEWordFromLeft>(m, "KBEWordFromLeft");
}
} // namespace libsemigroups
42 changes: 42 additions & 0 deletions src/kbe.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// libsemigroups_pybind11
// Copyright (C) 2025 James D. Mitchell
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

#ifndef SRC_KBE_HPP_
#define SRC_KBE_HPP_

namespace libsemigroups {
template <typename FroidurePinType>
struct ElementStateful {
using element_type = typename FroidurePinType::element_type;
using state_type = typename FroidurePinType::state_type;

ElementStateful() = default;
ElementStateful(ElementStateful const&) = default;
ElementStateful(ElementStateful&&) = default;
ElementStateful& operator=(ElementStateful const&) = default;
ElementStateful& operator=(ElementStateful&&) = default;

ElementStateful(element_type const& lmnt, state_type* sttptr)
: element(lmnt), state_ptr(sttptr) {}

element_type element;
state_type* state_ptr;
};

} // namespace libsemigroups
#endif // SRC_KBE_HPP_
18 changes: 12 additions & 6 deletions src/libsemigroups_pybind11/froidure_pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@
FroidurePinTransf1 as _FroidurePinTransf1,
FroidurePinTransf2 as _FroidurePinTransf2,
FroidurePinTransf4 as _FroidurePinTransf4,
FroidurePinKBERewriteFromLeft as _FroidurePinKBERewriteFromLeft,
FroidurePinKBERewriteTrie as _FroidurePinKBERewriteTrie,
FroidurePinKBEStringRewriteFromLeft as _FroidurePinKBEStringRewriteFromLeft,
FroidurePinKBEStringRewriteTrie as _FroidurePinKBEStringRewriteTrie,
FroidurePinKBEWordRewriteFromLeft as _FroidurePinKBEWordRewriteFromLeft,
FroidurePinKBEWordRewriteTrie as _FroidurePinKBEWordRewriteTrie,
FroidurePinKEMultiStringView as _FroidurePinKEMultiStringView,
FroidurePinKEString as _FroidurePinKEString,
FroidurePinKEWord as _FroidurePinKEWord,
FroidurePinTCE as _FroidurePinTCE,
IntMat as _IntMat,
KBEStringTrie as _KBEStringTrie,
KBEStringFromLeft as _KBEStringFromLeft,
KBEWordTrie as _KBEWordTrie,
KBEWordFromLeft as _KBEWordFromLeft,
MaxPlusMat as _MaxPlusMat,
MaxPlusTruncMat as _MaxPlusTruncMat,
MinPlusMat as _MinPlusMat,
Expand Down Expand Up @@ -120,6 +126,10 @@ class FroidurePin(_CxxWrapper): # pylint: disable=missing-class-docstring
(_Transf1,): _FroidurePinTransf1,
(_Transf2,): _FroidurePinTransf2,
(_Transf4,): _FroidurePinTransf4,
(_KBEStringTrie,): _FroidurePinKBEStringRewriteTrie,
(_KBEStringFromLeft,): _FroidurePinKBEStringRewriteFromLeft,
(_KBEWordTrie,): _FroidurePinKBEWordRewriteTrie,
(_KBEWordFromLeft,): _FroidurePinKBEWordRewriteFromLeft,
}

_cxx_type_to_py_template_params = dict(
Expand All @@ -130,8 +140,6 @@ class FroidurePin(_CxxWrapper): # pylint: disable=missing-class-docstring
)

_all_wrapped_cxx_types = {*_py_template_params_to_cxx_type.values()} | {
_FroidurePinKBERewriteFromLeft,
_FroidurePinKBERewriteTrie,
_FroidurePinKEMultiStringView,
_FroidurePinKEString,
_FroidurePinKEWord,
Expand Down Expand Up @@ -227,8 +235,6 @@ def sorted_elements( # pylint: disable=missing-function-docstring
_register_cxx_wrapped_type(_fp_type, FroidurePin)


_register_cxx_wrapped_type(_FroidurePinKBERewriteFromLeft, FroidurePin)
_register_cxx_wrapped_type(_FroidurePinKBERewriteTrie, FroidurePin)
_register_cxx_wrapped_type(_FroidurePinKEMultiStringView, FroidurePin)
_register_cxx_wrapped_type(_FroidurePinKEString, FroidurePin)
_register_cxx_wrapped_type(_FroidurePinKEWord, FroidurePin)
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace libsemigroups {
init_gabow(m);
init_imagerightaction(m);
init_kambites(m);
init_kbe(m);
init_knuth_bendix(m);
init_konieczny(m);
init_matrix(m);
Expand Down
1 change: 1 addition & 0 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace libsemigroups {
void init_imagerightaction(py::module&);
void init_inverse_present(py::module&);
void init_kambites(py::module&);
void init_kbe(py::module&);
void init_knuth_bendix(py::module&);
void init_konieczny(py::module&);
void init_matrix(py::module&);
Expand Down
Loading