Skip to content

Commit 6c0cc50

Browse files
Fix return self by reference
1 parent 36653c5 commit 6c0cc50

File tree

6 files changed

+89
-26
lines changed

6 files changed

+89
-26
lines changed

docs/source/main-algorithms/kambites/class.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ Contents
2020
:signatures: short
2121

2222
~Kambites
23+
Kambites.add_generating_pair
2324
Kambites.contains
25+
Kambites.copy
2426
Kambites.currently_contains
2527
Kambites.generating_pairs
2628
Kambites.init
29+
Kambites.kind
2730
Kambites.number_of_classes
31+
Kambites.number_of_generating_pairs
2832
Kambites.presentation
2933
Kambites.reduce
3034
Kambites.reduce_no_run

libsemigroups_pybind11/congruence.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from .detail.decorators import (
3636
may_return_positive_infinity as _may_return_positive_infinity,
3737
copydoc as _copydoc,
38+
returns_self as _returns_self,
3839
)
3940

4041
from .detail import cong_intf
@@ -108,7 +109,7 @@ def get(self: Self, t: type):
108109
if t is _ToddCoxeter:
109110
return self._get_todd_coxeter()
110111
if t is _Kambites:
111-
return self._get_kambites()
112+
return _Kambites(self._get_kambites())
112113
return None
113114

114115
def has(self: Self, t: type) -> bool:
@@ -136,6 +137,20 @@ def has(self: Self, t: type) -> bool:
136137
return self._has_kambites()
137138
return False
138139

140+
@_copydoc(_CongruenceWord.add_generating_pair)
141+
@_returns_self
142+
def add_generating_pair(self: Self, *args) -> Self:
143+
return _to_cxx(self).add_generating_pair(*args)
144+
145+
@_copydoc(_CongruenceWord.copy)
146+
def copy(self: Self) -> Self:
147+
return Congruence(_to_cxx(self).copy())
148+
149+
@_copydoc(_CongruenceWord.init)
150+
@_returns_self
151+
def init(self: Self, *args) -> Self:
152+
return _to_cxx(self).init(*args)
153+
139154
@_copydoc(_CongruenceWord._number_of_classes)
140155
@_may_return_positive_infinity
141156
def number_of_classes(self: Self) -> int:

libsemigroups_pybind11/detail/decorators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ def wrapper(self, *args, **kwargs):
7979
return wrapper
8080

8181

82+
def returns_self(func):
83+
"""
84+
This function is a decorator for functions/methods that return self.
85+
"""
86+
87+
@wraps(func)
88+
def wrapper(self, *args, **kwargs):
89+
func(self, *args, **kwargs)
90+
return self
91+
92+
return wrapper
93+
94+
8295
def template_params_as_kwargs(**kwargs_map):
8396
"""
8497
Usage example:

libsemigroups_pybind11/kambites.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from _libsemigroups_pybind11 import (
2323
KambitesMultiStringView as _KambitesMultiStringView,
2424
KambitesWord as _KambitesWord,
25+
KambitesString as _KambitesString,
2526
PresentationStrings as _PresentationStrings,
2627
PresentationWords as _PresentationWords,
2728
congruence_kind as _congruence_kind,
@@ -33,6 +34,7 @@
3334
from .detail.decorators import (
3435
may_return_positive_infinity as _may_return_positive_infinity,
3536
copydoc as _copydoc,
37+
returns_self as _returns_self,
3638
)
3739

3840
from .detail import cong_intf as _cong_intf
@@ -56,21 +58,48 @@ class Kambites(_CxxWrapper):
5658
def __init__( # pylint: disable=super-init-not-called
5759
self: Self, *args, **kwargs
5860
) -> None:
59-
_cong_intf.raise_if_bad_args(*args, **kwargs)
60-
if len(args) == 0:
61-
Word = kwargs["Word"]
61+
if (
62+
len(args) == 1
63+
and len(kwargs) == 0
64+
and isinstance(
65+
args[0],
66+
(_KambitesWord, _KambitesMultiStringView, _KambitesString),
67+
)
68+
):
69+
self._cxx_obj = args[0]
6270
else:
63-
assert len(args) == 2
64-
if isinstance(args[1], _PresentationStrings):
65-
Word = str
66-
elif isinstance(args[1], _PresentationWords):
67-
Word = List[int]
71+
_cong_intf.raise_if_bad_args(*args, **kwargs)
72+
if len(args) == 0:
73+
Word = kwargs["Word"]
6874
else:
69-
raise TypeError(
70-
f"expected the 2nd argument to be Presentation, but found {type(args[1])}"
71-
)
75+
assert len(args) == 2
76+
if isinstance(args[1], _PresentationStrings):
77+
Word = str
78+
elif isinstance(args[1], _PresentationWords):
79+
Word = List[int]
80+
else:
81+
raise TypeError(
82+
f"expected the 2nd argument to be Presentation, but found {type(args[1])}"
83+
)
84+
self._cxx_obj = self._py_to_cxx_type_dict[Word](*args)
85+
if isinstance(self._cxx_obj, _KambitesWord):
86+
self.Word = List[int]
87+
else:
88+
self.Word = str
89+
90+
@_copydoc(_KambitesWord.add_generating_pair)
91+
@_returns_self
92+
def add_generating_pair(self: Self, *args) -> Self:
93+
return _to_cxx(self).add_generating_pair(*args)
94+
95+
@_copydoc(_KambitesWord.copy)
96+
def copy(self: Self) -> Self:
97+
return Kambites(_to_cxx(self).copy())
7298

73-
self._cxx_obj = self._py_to_cxx_type_dict[Word](*args)
99+
@_copydoc(_KambitesWord.init)
100+
@_returns_self
101+
def init(self: Self, *args) -> Self:
102+
return _to_cxx(self).init(*args)
74103

75104
@_copydoc(_KambitesWord._number_of_classes)
76105
@_may_return_positive_infinity

src/cong.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ individual algorithms, such as :any:`Kambites`,
6464
>>> is_obviously_infinite(cong)
6565
True
6666
>>> cong.add_generating_pair([1, 1, 1], [])
67+
<Congruence over <monoid presentation with 2 letters, 1 rule, and length 2> with 4 runners>
6768
>>> cong.number_of_classes()
6869
3
6970
>>> is_obviously_infinite(cong)

src/kambites.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,10 @@ included for uniformity of interface between with
7070
:any:`KnuthBendixStringRewriteTrie`, :any:`ToddCoxeterWord`, and
7171
:any:`Congruence`.)pbdoc"sv;
7272

73-
auto extra_raises = R"pbdoc(
74-
:raises LibsemigroupsError:
75-
if :any:`small_overlap_class` is not at least :math:`4`.
76-
)pbdoc"sv;
77-
7873
def_construct_kind_presentation(
79-
thing,
80-
"Kambites",
81-
doc{.detail = extra_detail, .raises = extra_raises});
74+
thing, "Kambites", doc{.detail = extra_detail});
8275

83-
extra_raises = R"pbdoc(
76+
auto extra_raises = R"pbdoc(
8477
:raises LibsemigroupsError: if *knd* is ``congruence_kind.onesided``.
8578
)pbdoc"sv;
8679

@@ -105,7 +98,13 @@ raised.)pbdoc"sv;
10598
"Kambites",
10699
doc{.detail = extra_detail, .raises = extra_raises});
107100

108-
def_add_generating_pair(thing, "Kambites");
101+
extra_raises = R"pbdoc(
102+
:raises TypeError:
103+
if the type of the arguments is not the same as the type of the words in
104+
:any:`Kambites.presentation`.
105+
)pbdoc"sv;
106+
107+
def_add_generating_pair(thing, "Kambites", doc{.raises = extra_raises});
109108

110109
extra_raises = R"pbdoc(
111110
:raises LibsemigroupsError:
@@ -124,10 +123,11 @@ raised.)pbdoc"sv;
124123
extra_detail = R"pbdoc(If the :any:`Kambites.small_overlap_class`
125124
is not at least :math:`4`, then an exception is thrown.)pbdoc"sv;
126125

127-
def_reduce_no_run(thing, name, doc{.detail = extra_detail});
126+
def_reduce_no_run(thing, "Kambites", doc{.detail = extra_detail});
128127

129-
def_reduce(
130-
thing, name, doc{.detail = extra_detail, .raises = extra_raises});
128+
def_reduce(thing,
129+
"Kambites",
130+
doc{.detail = extra_detail, .raises = extra_raises});
131131

132132
////////////////////////////////////////////////////////////////////////
133133
// Kambites specific stuff
@@ -169,6 +169,7 @@ at least :math:`n`.
169169
thing.def("ukkonen",
170170
&Kambites_::ukkonen,
171171
R"pbdoc(
172+
:sig=(self: Kambites) -> Ukkonen:
172173
Returns the generalised suffix tree used to compute pieces.
173174
174175
This function returns the generalised suffix tree :any:`Ukkonen` object

0 commit comments

Comments
 (0)