Skip to content

Commit 8fdb519

Browse files
Use UNDEFINED and not 2**32-1
1 parent b33985e commit 8fdb519

6 files changed

Lines changed: 218 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
#ifndef SRC_MAIN_HPP_
2020
#define SRC_MAIN_HPP_
2121

22+
#include <variant>
23+
2224
#include <pybind11/pybind11.h>
2325

26+
#include <libsemigroups/constants.hpp>
27+
2428
namespace libsemigroups {
2529

2630
namespace py = pybind11;
@@ -70,6 +74,30 @@ namespace libsemigroups {
7074
void init_word_graph(py::module&);
7175
void init_words(py::module&);
7276

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

75103
#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)