Skip to content

Commit 089ff29

Browse files
Use UNDEFINED and not 2**32-1
1 parent 062776b commit 089ff29

6 files changed

Lines changed: 214 additions & 215 deletions

File tree

src/forest.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,7 @@ Constructs a forest with *n* nodes, that is initialised so that the
7474
parents,
7575
std::vector<std::variant<Forest::node_type, Undefined>> const&
7676
labels) {
77-
using node_type = Forest::node_type;
78-
std::vector<node_type> parents_as_ints;
79-
std::vector<node_type> labels_as_ints;
80-
for (auto const& val : parents) {
81-
if (std::holds_alternative<node_type>(val)) {
82-
parents_as_ints.push_back(std::get<0>(val));
83-
} else {
84-
parents_as_ints.push_back(
85-
static_cast<node_type>(std::get<1>(val)));
86-
}
87-
}
88-
// TODO there's something like this in transf.cpp too, we should
89-
// avoid code dupl
90-
for (auto const& val : labels) {
91-
if (std::holds_alternative<node_type>(val)) {
92-
labels_as_ints.push_back(std::get<0>(val));
93-
} else {
94-
labels_as_ints.push_back(
95-
static_cast<node_type>(std::get<1>(val)));
96-
}
97-
}
98-
return make<Forest>(parents_as_ints, labels_as_ints);
77+
return make<Forest>(to_ints(parents), to_ints(labels));
9978
}),
10079
py::arg("parents"),
10180
py::arg("labels"),

src/main.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ namespace libsemigroups {
7070
void init_word_graph(py::module&);
7171
void init_words(py::module&);
7272

73+
template <typename Int>
74+
std::vector<Int>
75+
to_ints(std::vector<std::variant<Int, Undefined>> const& vec) {
76+
std::vector<Int> vec_as_ints;
77+
for (auto const& val : vec) {
78+
if (std::holds_alternative<Int>(val)) {
79+
vec_as_ints.push_back(std::get<0>(val));
80+
} else {
81+
vec_as_ints.push_back(static_cast<Int>(std::get<1>(val)));
82+
}
83+
}
84+
return vec_as_ints;
85+
}
86+
87+
template <typename Int>
88+
std::vector<std::vector<Int>>
89+
to_ints(std::vector<std::vector<std::variant<Int, Undefined>>> const& vec) {
90+
std::vector<std::vector<Int>> vec_as_ints;
91+
for (auto const& val : vec) {
92+
vec_as_ints.push_back(to_ints(val));
93+
}
94+
return vec_as_ints;
95+
}
96+
7397
} // namespace libsemigroups
7498

7599
#endif // SRC_MAIN_HPP_

src/transf.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -524,16 +524,7 @@ among the points where :math:`f` is defined).
524524
thing.def(
525525
py::init(
526526
[](std::vector<std::variant<Scalar, Undefined>> const& imgs) {
527-
std::vector<Scalar> imgs_as_ints;
528-
for (auto const& val : imgs) {
529-
if (std::holds_alternative<Scalar>(val)) {
530-
imgs_as_ints.push_back(std::get<0>(val));
531-
} else {
532-
imgs_as_ints.push_back(
533-
static_cast<Scalar>(std::get<1>(val)));
534-
}
535-
}
536-
return make<PPerm_>(std::move(imgs_as_ints));
527+
return make<PPerm_>(to_ints(imgs));
537528
}),
538529
py::arg("imgs"),
539530
R"pbdoc(

src/word-graph.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ out-degree of any node is *n*. There are no edges in the defined word graph.
111111
:math:`O(mn)` where *m* is the number of nodes, and *n* is the
112112
out-degree of the word graph.)pbdoc");
113113

114-
thing.def(py::init([](size_t num_nodes,
115-
std::vector<std::vector<node_type>> const& targets) {
116-
return make<WordGraph_>(num_nodes, targets);
117-
}),
118-
py::arg("num_nodes"),
119-
py::arg("targets"),
120-
R"pbdoc(
114+
thing.def(
115+
py::init([](size_t num_nodes,
116+
std::vector<std::vector<
117+
std::variant<node_type, Undefined>>> const& targets) {
118+
return make<WordGraph_>(num_nodes, to_ints(targets));
119+
}),
120+
py::arg("num_nodes"),
121+
py::arg("targets"),
122+
R"pbdoc(
121123
Construct a word graph from a number of nodes and an list of targets.
122124
123125
This function constructs a word graph from its arguments whose

0 commit comments

Comments
 (0)