Skip to content

Commit a67b86b

Browse files
Add inits from make (#221)
* Change make_forest to an init * Change make_word_graph to an init * Linting
1 parent e02365f commit a67b86b

File tree

8 files changed

+80
-109
lines changed

8 files changed

+80
-109
lines changed

docs/source/data-structures/word-graph/forest.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Forest
1010
======
1111

1212
This class represents the collection of spanning trees of a word graph.
13-
See also :any:`make_forest`.
1413

1514
Contents
1615
--------
@@ -37,5 +36,3 @@ Full API
3736
:members:
3837
:show-inheritance:
3938
:class-doc-from: class
40-
41-
.. autofunction:: make_forest

docs/source/data-structures/word-graph/helpers.rst

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,11 @@ manipulating word graphs.
1414
Contents
1515
--------
1616

17-
In ``libsemigroups_pybind11``:
18-
19-
.. currentmodule:: libsemigroups_pybind11
20-
21-
.. autosummary::
22-
:nosignatures:
23-
24-
make_word_graph
25-
26-
In ``libsemigroups_pybind11.word_graph``:
27-
2817
.. currentmodule:: libsemigroups_pybind11.word_graph
2918

3019
.. autosummary::
3120
:nosignatures:
32-
21+
3322
add_cycle
3423
adjacency_matrix
3524
dot
@@ -54,8 +43,6 @@ Full API
5443

5544
.. currentmodule:: libsemigroups_pybind11
5645

57-
.. autofunction:: make_word_graph
58-
5946
.. automodule:: libsemigroups_pybind11.word_graph
6047
:members:
6148
:imported-members:

libsemigroups_pybind11/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@
5555
Bipartition,
5656
Blocks,
5757
freeband_equal_to,
58-
make_forest,
5958
Meeter,
60-
make_word_graph,
6159
Joiner,
6260
Dot,
6361
PBR,

src/forest.cpp

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,6 @@ namespace py = pybind11;
3737

3838
namespace libsemigroups {
3939
void init_forest(py::module& m) {
40-
m.def(
41-
"make_forest",
42-
[](std::vector<size_t> const& parents,
43-
std::vector<size_t> const& labels) {
44-
return make<Forest>(parents, labels);
45-
},
46-
py::arg("parents"),
47-
py::arg("labels"),
48-
R"pbdoc(
49-
:sig=(parents:List[int], labels:List[int])->Forest:
50-
Construct a :any:`Forest` from list of parents and labels.
51-
52-
:param parent: the list of parents of nodes
53-
:type parent: List[int]
54-
:param labels: the list of edge labels
55-
:type labels: List[int]
56-
57-
:returns:
58-
A newly constructed Forest with parents *parents* and edge labels *labels*.
59-
:rtype:
60-
Forest
61-
62-
:raises LibsemigroupsError: if *parent* and *labels* have different sizes;
63-
:raises LibsemigroupsError:
64-
*parent* and *labels* do not have the value :any:`UNDEFINED` in the same
65-
positions (these values indicate where the roots of the trees in the forest
66-
are located and so must coincide).
67-
:raises LibsemigroupsError:
68-
:any:`set_parent_and_label` throws for ``parent[i]`` and ``edge_labels[i]`` for any value of ``i``.
69-
)pbdoc");
70-
7140
using node_type = Forest::node_type;
7241

7342
py::class_<Forest> thing(m,
@@ -89,6 +58,29 @@ Constructs a forest with *n* nodes, that is initialised so that the
8958
9059
:param n: the number of nodes, defaults to ``0``.
9160
:type n: int
61+
)pbdoc");
62+
thing.def(py::init([](std::vector<size_t> const& parents,
63+
std::vector<size_t> const& labels) {
64+
return make<Forest>(parents, labels);
65+
}),
66+
py::arg("parents"),
67+
py::arg("labels"),
68+
R"pbdoc(
69+
:sig=(self: Forest, parents:List[int], labels:List[int]) -> None:
70+
Construct a :any:`Forest` from list of *parents* and *labels*.
71+
72+
:param parent: the list of parents of nodes.
73+
:type parent: List[int]
74+
:param labels: the list of edge labels.
75+
:type labels: List[int]
76+
77+
:raises LibsemigroupsError: if *parent* and *labels* have different sizes;
78+
:raises LibsemigroupsError:
79+
*parent* and *labels* do not have the value :any:`UNDEFINED` in the same
80+
positions (these values indicate where the roots of the trees in the forest
81+
are located and so must coincide).
82+
:raises LibsemigroupsError:
83+
:any:`set_parent_and_label` throws for ``parent[i]`` and ``edge_labels[i]`` for any value of ``i``.
9284
)pbdoc");
9385
thing.def("add_nodes",
9486
&Forest::add_nodes,

src/word-graph.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ the *out-degree* of the word graph, or any of its nodes.)pbdoc");
6868
});
6969

7070
thing.def("__str__", [](WordGraph_ const& self) {
71-
return to_input_string(self, "make_word_graph(", "[]", ")");
71+
return to_input_string(self, "WordGraph(", "[]", ")");
7272
});
7373

7474
thing.def(py::self != py::self);
@@ -109,6 +109,36 @@ out-degree of any node is *n*. There are no edges in the defined word graph.
109109
:math:`O(mn)` where *m* is the number of nodes, and *n* is the
110110
out-degree of the word graph.)pbdoc");
111111

112+
thing.def(py::init([](size_t num_nodes,
113+
std::vector<std::vector<node_type>> const& targets) {
114+
return make<WordGraph_>(num_nodes, targets);
115+
}),
116+
py::arg("num_nodes"),
117+
py::arg("targets"),
118+
R"pbdoc(
119+
Construct a word graph from a number of nodes and an list of targets.
120+
121+
This function constructs a word graph from its arguments whose
122+
out-degree is specified by the length of the first item in *targets*.
123+
124+
:param num_nodes: the number of nodes in the word graph.
125+
:type num_nodes: int
126+
127+
:param targets: list of the targets.
128+
:type targets: List[List[int]]
129+
130+
131+
:raises LibsemigroupsError: if any target is specified in *targets* is greater
132+
than or equal to *num_nodes*.
133+
134+
.. doctest::
135+
136+
>>> from libsemigroups_pybind11 import WordGraph
137+
>>> WordGraph(5, [[0, 0], [1, 1], [2], [3, 3]])
138+
<WordGraph with 5 nodes, 7 edges, & out-degree 2>
139+
140+
)pbdoc");
141+
112142
thing.def("add_nodes",
113143
&WordGraph_::add_nodes,
114144
py::arg("nr"),
@@ -1027,8 +1057,8 @@ considered to be reachable from itself by default).
10271057
10281058
.. doctest::
10291059
1030-
>>> from libsemigroups_pybind11 import make_word_graph, word_graph
1031-
>>> wg = make_word_graph (5, [[0, 0], [1, 1], [2], [3, 3]])
1060+
>>> from libsemigroups_pybind11 import WordGraph, word_graph
1061+
>>> wg = WordGraph(5, [[0, 0], [1, 1], [2], [3, 3]])
10321062
>>> word_graph.is_strictly_cyclic(wg)
10331063
False)pbdoc");
10341064
m.def(
@@ -1769,35 +1799,5 @@ and *y*.
17691799
:raises LibsemigroupsError: if *x* has no nodes;
17701800
:raises LibsemigroupsError: if *y* has no nodes;
17711801
:raises LibsemigroupsError: if ``x.out_degree() != y.out_degree()``.)pbdoc");
1772-
1773-
m.def("make_word_graph",
1774-
py::overload_cast<size_t, std::vector<std::vector<node_type>> const&>(
1775-
&make<WordGraph<node_type>>),
1776-
py::arg("num_nodes"),
1777-
py::arg("targets"),
1778-
R"pbdoc(
1779-
Constructs a word graph from a number of nodes and an list of targets.
1780-
1781-
This function constructs a word graph from its arguments whose
1782-
out-degree is specified by the length of the first item in *targets*.
1783-
1784-
:param num_nodes: the number of nodes in the word graph.
1785-
:type num_nodes: int
1786-
1787-
:param targets: list of the targets.
1788-
:type targets: List[List[int]]
1789-
1790-
:returns: The constructed word graph.
1791-
:rtype: WordGraph
1792-
1793-
:raises LibsemigroupsError:
1794-
if any target is specified in *targets* is greater than or equal to
1795-
*num_nodes*.
1796-
1797-
.. doctest::
1798-
1799-
>>> from libsemigroups_pybind11 import make_word_graph
1800-
>>> make_word_graph(5, [[0, 0], [1, 1], [2], [3, 3]])
1801-
<WordGraph with 5 nodes, 7 edges, & out-degree 2>)pbdoc");
18021802
}
18031803
} // namespace libsemigroups

tests/test_dot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
Dot,
2020
LibsemigroupsError,
2121
word_graph,
22-
make_word_graph,
22+
WordGraph,
2323
)
2424

2525

2626
def test_edge():
27-
wg = make_word_graph(3, [[0, 1], [1, 0], [2, 2]])
27+
wg = WordGraph(3, [[0, 1], [1, 0], [2, 2]])
2828
d = word_graph.dot(wg)
2929
edges = d.edges()
3030
assert len(edges) == 6
@@ -38,7 +38,7 @@ def test_edge():
3838

3939

4040
def test_node():
41-
wg = make_word_graph(3, [[0, 1], [1, 0], [2, 2]])
41+
wg = WordGraph(3, [[0, 1], [1, 0], [2, 2]])
4242
d = word_graph.dot(wg)
4343
nodes = d.nodes()
4444
assert len(nodes) == 3
@@ -51,13 +51,13 @@ def test_node():
5151

5252

5353
def test_dot_copy():
54-
wg = make_word_graph(3, [[0, 1], [1, 0], [2, 2]])
54+
wg = WordGraph(3, [[0, 1], [1, 0], [2, 2]])
5555
d = word_graph.dot(wg)
5656
assert copy.copy(d) is not d
5757

5858

5959
def test_dot_attrs():
60-
wg = make_word_graph(3, [[0, 1], [1, 0], [2, 2]])
60+
wg = WordGraph(3, [[0, 1], [1, 0], [2, 2]])
6161
d = word_graph.dot(wg)
6262
d.add_attr("node [shape=circle]")
6363
assert d.attrs() == {"node [shape=circle]": ""}

tests/test_paths.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
WordGraph,
2323
ToString,
2424
POSITIVE_INFINITY,
25-
make_word_graph,
2625
)
2726

2827

@@ -95,7 +94,7 @@ def test_ToString():
9594

9695

9796
def test_paths_bug():
98-
wg = make_word_graph(4, [[0, 1], [1, 0], [2, 2]])
97+
wg = WordGraph(4, [[0, 1], [1, 0], [2, 2]])
9998
p = Paths(wg)
10099
p.source(0).target(1)
101100
assert p.source() == 0
@@ -104,7 +103,7 @@ def test_paths_bug():
104103

105104

106105
def test_paths_bug2():
107-
wg = make_word_graph(4, [[0, 1], [1, 0], [2, 2]])
106+
wg = WordGraph(4, [[0, 1], [1, 0], [2, 2]])
108107
p = Paths(wg)
109108
assert p.max() is POSITIVE_INFINITY
110109

tests/test_word_graph.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
WordGraph,
2424
word_graph,
2525
LibsemigroupsError,
26-
make_forest,
2726
UNDEFINED,
2827
Forest,
2928
Order,
3029
Meeter,
31-
make_word_graph,
3230
Joiner,
3331
Matrix,
3432
MatrixKind,
@@ -241,7 +239,7 @@ def test_number_of_nodes_reachable_from(word_graph_fixture):
241239
def test_spanning_tree(word_graph_fixture):
242240
wg1, _ = word_graph_fixture
243241

244-
assert word_graph.spanning_tree(wg1, 0) == make_forest(
242+
assert word_graph.spanning_tree(wg1, 0) == Forest(
245243
[int(UNDEFINED), 0, 1, 2, 3], [int(UNDEFINED), 0, 0, 0, 0]
246244
)
247245

@@ -273,44 +271,44 @@ def test_meeter(word_graph_fixture):
273271
wg1.remove_target(4, 0)
274272

275273
meet = Meeter()
276-
assert wg1 == make_word_graph(5, [[1], [2], [3], [4], [2**32 - 1]])
274+
assert wg1 == WordGraph(5, [[1], [2], [3], [4], [2**32 - 1]])
277275
assert meet(wg1, wg1) == wg1
278276

279-
wg2 = make_word_graph(2, [[1, 0], [1, 0]])
280-
wg3 = make_word_graph(2, [[1, 1], [1, 1]])
277+
wg2 = WordGraph(2, [[1, 0], [1, 0]])
278+
wg3 = WordGraph(2, [[1, 1], [1, 1]])
281279

282-
assert meet(wg3, wg2) == make_word_graph(3, [[1, 2], [1, 2], [1, 2]])
280+
assert meet(wg3, wg2) == WordGraph(3, [[1, 2], [1, 2], [1, 2]])
283281
meet(wg1, wg3, wg2)
284-
assert wg1 == make_word_graph(3, [[1, 2], [1, 2], [1, 2]])
282+
assert wg1 == WordGraph(3, [[1, 2], [1, 2], [1, 2]])
285283
assert meet.is_subrelation(wg1, wg3)
286284
assert meet.is_subrelation(wg1, wg2)
287285

288286

289287
def test_joiner(word_graph_fixture):
290288
wg1, _ = word_graph_fixture
291289
wg1.remove_target(0, 0)
292-
assert wg1 == make_word_graph(5, [[2**32 - 1], [2], [3], [4], [0]])
290+
assert wg1 == WordGraph(5, [[2**32 - 1], [2], [3], [4], [0]])
293291

294292
join = Joiner()
295293
# TODO(later) use UNDEFINED here instead of 2**32 -1
296-
assert join(wg1, wg1) == make_word_graph(1, [[2**32 - 1]])
294+
assert join(wg1, wg1) == WordGraph(1, [[2**32 - 1]])
297295

298-
wg3 = make_word_graph(2, [[1, 1], [1, 1]])
299-
wg2 = make_word_graph(2, [[1, 0], [1, 0]])
296+
wg3 = WordGraph(2, [[1, 1], [1, 1]])
297+
wg2 = WordGraph(2, [[1, 0], [1, 0]])
300298

301-
assert join(wg3, wg2) == make_word_graph(1, [[0, 0]])
299+
assert join(wg3, wg2) == WordGraph(1, [[0, 0]])
302300
join(wg1, wg3, wg2)
303-
assert wg1 == make_word_graph(1, [[0, 0]])
301+
assert wg1 == WordGraph(1, [[0, 0]])
304302
assert join.is_subrelation(wg3, wg1)
305303
assert join.is_subrelation(wg2, wg1)
306304

307305

308306
def test_str(word_graph_fixture):
309307
wg1, wg2 = word_graph_fixture
310-
assert str(wg1) == "make_word_graph(5, [[1], [2], [3], [4], [0]])"
308+
assert str(wg1) == "WordGraph(5, [[1], [2], [3], [4], [0]])"
311309
assert (
312310
str(wg2)
313-
== "make_word_graph(10, [[1], [2], [3], [4], [0], [6], [7], [8], [9], [5]])"
311+
== "WordGraph(10, [[1], [2], [3], [4], [0], [6], [7], [8], [9], [5]])"
314312
)
315313

316314

@@ -339,7 +337,7 @@ def test_random():
339337
assert word_graph.is_acyclic(w)
340338
assert word_graph.is_connected(w)
341339

342-
d = make_word_graph(3, [[0, 1], [1, 0], [2, 2]])
340+
d = WordGraph(3, [[0, 1], [1, 0], [2, 2]])
343341
assert (
344342
str(word_graph.dot(d))
345343
== 'digraph WordGraph {\n\n 0 [shape="box"]\n 1 [shape="box"]\n 2'
@@ -349,7 +347,7 @@ def test_random():
349347
' [color="#ff00ff"]\n}'
350348
)
351349

352-
d = make_word_graph(4, list(12 * [i] for i in range(4)))
350+
d = WordGraph(4, list(12 * [i] for i in range(4)))
353351
assert (
354352
str(word_graph.dot(d))
355353
== 'digraph WordGraph {\n\n 0 [shape="box"]\n 1 [shape="box"]\n 2 [shape="box"]\n 3 [shape="box"]\n 0 -> 0 [color="#00ff00"]\n 0 -> 0 [color="#ff00ff"]\n 0 -> 0 [color="#007fff"]\n 0 -> 0 [color="#ff7f00"]\n 0 -> 0 [color="#7fbf7f"]\n 0 -> 0 [color="#4604ac"]\n 0 -> 0 [color="#de0328"]\n 0 -> 0 [color="#19801d"]\n 0 -> 0 [color="#d881f5"]\n 0 -> 0 [color="#00ffff"]\n 0 -> 0 [color="#ffff00"]\n 0 -> 0 [color="#00ff7f"]\n 1 -> 1 [color="#00ff00"]\n 1 -> 1 [color="#ff00ff"]\n 1 -> 1 [color="#007fff"]\n 1 -> 1 [color="#ff7f00"]\n 1 -> 1 [color="#7fbf7f"]\n 1 -> 1 [color="#4604ac"]\n 1 -> 1 [color="#de0328"]\n 1 -> 1 [color="#19801d"]\n 1 -> 1 [color="#d881f5"]\n 1 -> 1 [color="#00ffff"]\n 1 -> 1 [color="#ffff00"]\n 1 -> 1 [color="#00ff7f"]\n 2 -> 2 [color="#00ff00"]\n 2 -> 2 [color="#ff00ff"]\n 2 -> 2 [color="#007fff"]\n 2 -> 2 [color="#ff7f00"]\n 2 -> 2 [color="#7fbf7f"]\n 2 -> 2 [color="#4604ac"]\n 2 -> 2 [color="#de0328"]\n 2 -> 2 [color="#19801d"]\n 2 -> 2 [color="#d881f5"]\n 2 -> 2 [color="#00ffff"]\n 2 -> 2 [color="#ffff00"]\n 2 -> 2 [color="#00ff7f"]\n 3 -> 3 [color="#00ff00"]\n 3 -> 3 [color="#ff00ff"]\n 3 -> 3 [color="#007fff"]\n 3 -> 3 [color="#ff7f00"]\n 3 -> 3 [color="#7fbf7f"]\n 3 -> 3 [color="#4604ac"]\n 3 -> 3 [color="#de0328"]\n 3 -> 3 [color="#19801d"]\n 3 -> 3 [color="#d881f5"]\n 3 -> 3 [color="#00ffff"]\n 3 -> 3 [color="#ffff00"]\n 3 -> 3 [color="#00ff7f"]\n}' # pylint: disable=line-too-long

0 commit comments

Comments
 (0)