forked from libsemigroups/libsemigroups_pybind11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforest.cpp
More file actions
347 lines (296 loc) · 9.77 KB
/
Copy pathforest.cpp
File metadata and controls
347 lines (296 loc) · 9.77 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// libsemigroups_pybind11
// Copyright (C) 2021-2024 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/>.
//
// C std headers....
#include <cstddef> // for size_t
// C++ stl headers....
#include <initializer_list> // for initializer_list
// libsemigroups....
#include <libsemigroups/forest.hpp> // for Forest
// pybind11....
#include <pybind11/operators.h> // for py::operator
#include <pybind11/pybind11.h> // for class_, init, make_iterator, module
#include <pybind11/stl.h> // for conversion of C++ to py types
// libsemigroups_pybind11....
#include "main.hpp" // for init_forest
namespace py = pybind11;
namespace libsemigroups {
void init_forest(py::module& m) {
using node_type = Forest::node_type;
py::class_<Forest> thing(m,
"Forest",
R"pbdoc(
This class represents the collection of spanning trees of the strongly
connected components of a word graph.
)pbdoc");
thing.def("__repr__",
[](Forest const& f) { return to_human_readable_repr(f); });
thing.def("__copy__", [](Forest const& f) { return Forest(f); });
thing.def(
"copy",
[](Forest const& f) { return Forest(f); },
R"pbdoc(
Copy a :any:`Forest` object.
:returns: A copy.
:rtype: Forest
)pbdoc");
thing.def(py::init<size_t>(), py::arg("n") = 0, R"pbdoc(
Constructs a forest with *n* nodes.
Constructs a forest with *n* nodes, that is initialised so that the
:any:`Forest.parent` and :any:`Forest.label` of every node is :any:`UNDEFINED`.
:param n: the number of nodes, defaults to ``0``.
:type n: int
)pbdoc");
thing.def(
py::init(
[](std::vector<std::variant<Forest::node_type, Undefined>> const&
parents,
std::vector<std::variant<Forest::node_type, Undefined>> const&
labels) {
using node_type = Forest::node_type;
std::vector<node_type> parents_as_ints;
std::vector<node_type> labels_as_ints;
for (auto const& val : parents) {
if (std::holds_alternative<node_type>(val)) {
parents_as_ints.push_back(std::get<0>(val));
} else {
parents_as_ints.push_back(
static_cast<node_type>(std::get<1>(val)));
}
}
// TODO there's something like this in transf.cpp too, we should
// avoid code dupl
for (auto const& val : labels) {
if (std::holds_alternative<node_type>(val)) {
labels_as_ints.push_back(std::get<0>(val));
} else {
labels_as_ints.push_back(
static_cast<node_type>(std::get<1>(val)));
}
}
return make<Forest>(parents_as_ints, labels_as_ints);
}),
py::arg("parents"),
py::arg("labels"),
R"pbdoc(
:sig=(self: Forest, parents:List[int | Undefined], labels:List[int | Undefined]) -> None:
Construct a :any:`Forest` from list of *parents* and *labels*.
:param parent: the list of parents of nodes.
:type parent: List[int | Undefined]
:param labels: the list of edge labels.
:type labels: List[int | Undefined]
:raises LibsemigroupsError:
if *parent* and *labels* have different sizes;
:raises LibsemigroupsError:
*parent* and *labels* do not have the value :any:`UNDEFINED` in the same
positions (these values indicate where the roots of the trees in the forest
are located and so must coincide).
:raises LibsemigroupsError:
:any:`set_parent_and_label` throws for ``parent[i]`` and ``edge_labels[i]``
for any value of ``i``.
)pbdoc");
thing.def("add_nodes",
&Forest::add_nodes,
py::arg("n"),
R"pbdoc(
Add nodes to the :any:`Forest`.
This function adds *n* nodes to the forest, but no edges.
:param n: the number of nodes to add.
:type n: int
:complexity: At most linear in ``number_of_nodes() + n``.
)pbdoc");
thing.def("empty",
&Forest::empty,
R"pbdoc(
Check if there are any nodes in the forest. This function returns
``True`` if there are ``0`` nodes in the forest, and ``False`` otherwise.
:returns:
Whether or not the forest is empty.
:rtype:
bool
:complexity:
Constant.
)pbdoc");
thing.def("init",
&Forest::init,
py::arg("n"),
R"pbdoc(
Reinitialize an existing :any:`Forest` object.
This function reinitializes an existing :any:`Forest` object so that it is in
the same state as if it had just be constructed as ``Forest(n)``.
:param n: the number of nodes, defaults to ``0``.
:type n: int
:returns: ``self``.
:rtype: Forest
)pbdoc");
thing.def(
"label",
[](Forest const& self,
Forest::node_type i) -> std::variant<Forest::node_type, Undefined> {
if (self.label(i) != UNDEFINED) {
return {self.label(i)};
}
return {UNDEFINED};
},
py::arg("i"),
R"pbdoc(
:sig=(self: Forest, i: int) -> int | Undefined:
Returns the label of the edge from a node to its parent.
:param i:
the node whose label is sought.
:type i:
init
:returns:
The label of the edge from the parent of *i* to *i*.
:rtype:
int | Undefined
:raises LibsemigroupsError:
if *i* exceeds ``number_of_nodes()``.
:complexity:
Constant.
)pbdoc");
thing.def(
"labels",
[](Forest const& self)
-> std::vector<std::variant<Forest::node_type, Undefined>> {
std::vector<std::variant<Forest::node_type, Undefined>> result;
for (auto node : self.labels()) {
if (node != UNDEFINED) {
result.emplace_back(node);
} else {
result.emplace_back(UNDEFINED);
}
}
return result;
},
R"pbdoc(
:sig=(self: Forest) -> list[int | Undefined]:
Returns the list of edge labels in the :any:`Forest`. The value
in position ``i`` of this list is the label of the edge from the
parent of node ``i`` to ``i``. If the parent equals :any:`UNDEFINED`,
then node ``i`` is a root node.
:returns:
The edge labels of the forest.
:rtype:
list[int | Undefined]
:complexity:
Constant.
)pbdoc");
thing.def("number_of_nodes",
&Forest::number_of_nodes,
R"pbdoc(
Returns the number of nodes in the forest. Returns the number of nodes
in the forest.
:returns:
The number of nodes in the forest.
:rtype:
int
:complexity:
Constant.
)pbdoc");
thing.def(py::self != py::self, py::arg("that"));
thing.def(py::self == py::self, py::arg("that"));
thing.def(
"parent",
[](Forest const& self,
Forest::node_type i) -> std::variant<Forest::node_type, Undefined> {
if (self.parent(i) != UNDEFINED) {
return {self.parent(i)};
}
return {UNDEFINED};
},
py::arg("i"),
R"pbdoc(
:sig=(self: Forest, i: int) -> int | Undefined:
Returns the parent of a node.
:param i:
the node whose parent is sought.
:type i:
int
:returns:
The parent of *i*.
:rtype:
int
:raises LibsemigroupsError:
if *i* exceeds ``number_of_nodes()``.
:complexity:
Constant
)pbdoc");
thing.def(
"parents",
[](Forest const& self)
-> std::vector<std::variant<Forest::node_type, Undefined>> {
std::vector<std::variant<Forest::node_type, Undefined>> result;
for (auto node : self.parents()) {
if (node != UNDEFINED) {
result.emplace_back(node);
} else {
result.emplace_back(UNDEFINED);
}
}
return result;
},
R"pbdoc(
:sig=(self: Forest) -> list[int | Undefined]:
Returns a list of parents in the :any:`Forest`. The value in position ``i`` of
this list is the parent of node ``i``. If the parent equals :any:`UNDEFINED`,
then node ``i`` is a root node.
:returns:
The parents of the nodes in the forest.
:rtype:
list[int | Undefined]
:complexity:
Constant.
)pbdoc");
thing.def(
"path_to_root",
[](Forest const& self, node_type i) {
return forest::path_to_root(self, i);
},
py::arg("i"),
R"pbdoc(
Returns a list containing the labels of the edges on the path from a root node
to *i*.
:param i: the node.
:type i: int
:returns: The word labelling the path from the root to *i*.
:rtype: List[int]
:raises LibsemigroupsError:
if *i* is greater than or equal to :any:`number_of_nodes`.
)pbdoc");
thing.def("set_parent_and_label",
&Forest::set_parent_and_label,
py::arg("node"),
py::arg("parent"),
py::arg("gen"),
R"pbdoc(
Set the parent and edge label for a node. This function sets the parent of
*node* to be *parent*, and the associated edge-label to be *gen*.
:param node: the node whose parent and label to set.
:type node: int
:param parent: the parent node
:type parent: int
:param gen: the label of the edge from parent to node.
:type gen: int
:returns: ``self``
:rtype: Forest
:raises LibsemigroupsError:
if *node* or *parent* exceeds :any:`number_of_nodes()`.
:complexity: Constant.
)pbdoc");
}
} // namespace libsemigroups