-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathkbe.cpp
More file actions
105 lines (87 loc) · 3.92 KB
/
Copy pathkbe.cpp
File metadata and controls
105 lines (87 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// 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 LenLexTrie = detail::RewritingSystemTrie<LenLexCmp>;
using LenLexSet = detail::RewritingSystemSet<LenLexCmp>;
using RevRPOTrie = detail::RewritingSystemTrie<RevRPOCmp>;
using RevRPOSet = detail::RewritingSystemSet<RevRPOCmp>;
// LenLex
using KBEStringLenLexTrie
= detail::KBE<KnuthBendix<std::string, LenLexTrie>>;
using KBEWordLenLexTrie = detail::KBE<KnuthBendix<word_type, LenLexTrie>>;
using KBEStringLenLexSet = detail::KBE<KnuthBendix<std::string, LenLexSet>>;
using KBEWordLenLexSet = detail::KBE<KnuthBendix<word_type, LenLexSet>>;
bind_kbe<KBEStringLenLexTrie>(m, "KBEStringLenLexTrie");
bind_kbe<KBEWordLenLexTrie>(m, "KBEWordLenLexTrie");
bind_kbe<KBEStringLenLexSet>(m, "KBEStringLenLexSet");
bind_kbe<KBEWordLenLexSet>(m, "KBEWordLenLexSet");
// RPO
using KBEStringRevRPOTrie
= detail::KBE<KnuthBendix<std::string, RevRPOTrie>>;
using KBEWordRevRPOTrie = detail::KBE<KnuthBendix<word_type, RevRPOTrie>>;
using KBEStringRevRPOSet = detail::KBE<KnuthBendix<std::string, RevRPOSet>>;
using KBEWordRevRPOSet = detail::KBE<KnuthBendix<word_type, RevRPOSet>>;
bind_kbe<KBEStringRevRPOTrie>(m, "KBEStringRevRPOTrie");
bind_kbe<KBEWordRevRPOTrie>(m, "KBEWordRevRPOTrie");
bind_kbe<KBEStringRevRPOSet>(m, "KBEStringRevRPOSet");
bind_kbe<KBEWordRevRPOSet>(m, "KBEWordRevRPOSet");
}
} // namespace libsemigroups