Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions src/forest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,7 @@ Constructs a forest with *n* nodes, that is initialised so that the
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);
return make<Forest>(to_ints(parents), to_ints(labels));
}),
py::arg("parents"),
py::arg("labels"),
Expand Down
28 changes: 28 additions & 0 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
#ifndef SRC_MAIN_HPP_
#define SRC_MAIN_HPP_

#include <variant>

#include <pybind11/pybind11.h>

#include <libsemigroups/constants.hpp>

namespace libsemigroups {

namespace py = pybind11;
Expand Down Expand Up @@ -70,6 +74,30 @@ namespace libsemigroups {
void init_word_graph(py::module&);
void init_words(py::module&);

template <typename Int>
std::vector<Int>
to_ints(std::vector<std::variant<Int, Undefined>> const& vec) {
std::vector<Int> vec_as_ints;
for (auto const& val : vec) {
if (std::holds_alternative<Int>(val)) {
vec_as_ints.push_back(std::get<0>(val));
} else {
vec_as_ints.push_back(static_cast<Int>(std::get<1>(val)));
}
}
return vec_as_ints;
}

template <typename Int>
std::vector<std::vector<Int>>
to_ints(std::vector<std::vector<std::variant<Int, Undefined>>> const& vec) {
std::vector<std::vector<Int>> vec_as_ints;
for (auto const& val : vec) {
vec_as_ints.push_back(to_ints(val));
}
return vec_as_ints;
}

} // namespace libsemigroups

#endif // SRC_MAIN_HPP_
11 changes: 1 addition & 10 deletions src/transf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,7 @@ among the points where :math:`f` is defined).
thing.def(
py::init(
[](std::vector<std::variant<Scalar, Undefined>> const& imgs) {
std::vector<Scalar> imgs_as_ints;
for (auto const& val : imgs) {
if (std::holds_alternative<Scalar>(val)) {
imgs_as_ints.push_back(std::get<0>(val));
} else {
imgs_as_ints.push_back(
static_cast<Scalar>(std::get<1>(val)));
}
}
return make<PPerm_>(std::move(imgs_as_ints));
return make<PPerm_>(to_ints(imgs));
}),
py::arg("imgs"),
R"pbdoc(
Expand Down
16 changes: 9 additions & 7 deletions src/word-graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ out-degree of any node is *n*. There are no edges in the defined word graph.
:math:`O(mn)` where *m* is the number of nodes, and *n* is the
out-degree of the word graph.)pbdoc");

thing.def(py::init([](size_t num_nodes,
std::vector<std::vector<node_type>> const& targets) {
return make<WordGraph_>(num_nodes, targets);
}),
py::arg("num_nodes"),
py::arg("targets"),
R"pbdoc(
thing.def(
py::init([](size_t num_nodes,
std::vector<std::vector<
std::variant<node_type, Undefined>>> const& targets) {
return make<WordGraph_>(num_nodes, to_ints(targets));
}),
py::arg("num_nodes"),
py::arg("targets"),
R"pbdoc(
Construct a word graph from a number of nodes and an list of targets.

This function constructs a word graph from its arguments whose
Expand Down
Loading