Skip to content

Commit 33f0663

Browse files
action-digraph: add action_digraph_helper::make
1 parent 85212e6 commit 33f0663

File tree

2 files changed

+78
-38
lines changed

2 files changed

+78
-38
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright (c) 2022, J. D. Mitchell
4+
#
5+
# Distributed under the terms of the GPL license version 3.
6+
#
7+
# The full license is in the file LICENSE, distributed with this software.
8+
9+
# pylint: disable=no-name-in-module, invalid-name, unused-import
10+
11+
"""
12+
This package provides the user-facing python part of libsemigroups_pybind11 for
13+
the action_digraph_helper namespace from libsemigroups.
14+
"""
15+
16+
from _libsemigroups_pybind11 import action_digraph_helper_make as make

src/action-digraph.cpp

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,38 @@
3434
#include <libsemigroups/int-range.hpp> // for IntegralRange<>::value_type
3535

3636
// pybind11....
37-
#include <pybind11/pybind11.h> // for class_, make_iterator, init, enum_
38-
#include <pybind11/stl.h> // for conversion of C++ to py types
37+
#include <pybind11/operators.h> // for self, self_t, operator!=, operator*
38+
#include <pybind11/pybind11.h> // for class_, make_iterator, init, enum_
39+
#include <pybind11/stl.h> // for conversion of C++ to py types
3940

4041
// libsemigroups_pybind11....
4142
#include "main.hpp" // for init_action_digraph
4243

4344
namespace py = pybind11;
4445

4546
namespace libsemigroups {
47+
namespace libsemigroups_pybind11 {
48+
// TODO Avoid duplicate code the following is a copy of
49+
// libsemigroups::action_digraph_helper::make, which can't be used because
50+
// it has initializer_list's as 2nd arg.
51+
ActionDigraph<size_t> make(size_t num_nodes,
52+
std::vector<std::vector<size_t>> il) {
53+
ActionDigraph<size_t> result(num_nodes, il.begin()->size());
54+
for (size_t i = 0; i < il.size(); ++i) {
55+
for (size_t j = 0; j < (il.begin() + i)->size(); ++j) {
56+
auto val = *((il.begin() + i)->begin() + j);
57+
if (val != UNDEFINED) {
58+
result.add_edge(i, *((il.begin() + i)->begin() + j), j);
59+
}
60+
}
61+
}
62+
return result;
63+
}
64+
} // namespace libsemigroups_pybind11
65+
//
4666
using node_type = ActionDigraph<size_t>::node_type;
4767
using algorithm = ActionDigraph<size_t>::algorithm;
48-
void init_action_digraph(py::module &m) {
68+
void init_action_digraph(py::module& m) {
4969
////////////////////////////////////////////////////////////////////////
5070
// ActionDigraph
5171
////////////////////////////////////////////////////////////////////////
@@ -83,12 +103,12 @@ namespace libsemigroups {
83103
- **n** (int) the out-degree of every node (default:
84104
``0``).
85105
)pbdoc")
86-
.def(py::init<ActionDigraph<size_t> const &>(),
106+
.def(py::init<ActionDigraph<size_t> const&>(),
87107
R"pbdoc(
88108
Construct a copy.
89109
)pbdoc")
90110
.def("__repr__",
91-
[](ActionDigraph<size_t> const &d) {
111+
[](ActionDigraph<size_t> const& d) {
92112
std::string result = "<action digraph with ";
93113
result += std::to_string(d.number_of_nodes());
94114
result += " nodes, ";
@@ -98,6 +118,7 @@ namespace libsemigroups {
98118
result += " out-degree>";
99119
return result;
100120
})
121+
.def(pybind11::self == pybind11::self)
101122
.def("number_of_nodes",
102123
&ActionDigraph<size_t>::number_of_nodes,
103124
R"pbdoc(
@@ -445,7 +466,7 @@ namespace libsemigroups {
445466
)pbdoc")
446467
.def(
447468
"number_of_paths",
448-
[](ActionDigraph<size_t> const &ad,
469+
[](ActionDigraph<size_t> const& ad,
449470
node_type source,
450471
node_type target,
451472
size_t min,
@@ -469,7 +490,7 @@ namespace libsemigroups {
469490
)pbdoc")
470491
.def(
471492
"number_of_paths",
472-
[](ActionDigraph<size_t> const &ad,
493+
[](ActionDigraph<size_t> const& ad,
473494
node_type source,
474495
size_t min,
475496
size_t max) { return ad.number_of_paths(source, min, max); },
@@ -488,15 +509,15 @@ namespace libsemigroups {
488509
)pbdoc")
489510
.def(
490511
"nodes_iterator",
491-
[](ActionDigraph<size_t> const &ad) {
512+
[](ActionDigraph<size_t> const& ad) {
492513
return py::make_iterator(ad.cbegin_nodes(), ad.cend_nodes());
493514
},
494515
R"pbdoc(
495516
Returns an iterator to the nodes of the digraph.
496517
)pbdoc")
497518
.def(
498519
"reverse_nodes_iterator",
499-
[](ActionDigraph<size_t> const &ad) {
520+
[](ActionDigraph<size_t> const& ad) {
500521
return py::make_iterator(ad.crbegin_nodes(), ad.crend_nodes());
501522
},
502523
R"pbdoc(
@@ -505,7 +526,7 @@ namespace libsemigroups {
505526

506527
.def(
507528
"edges_iterator",
508-
[](ActionDigraph<size_t> const &ad, size_t const i) {
529+
[](ActionDigraph<size_t> const& ad, size_t const i) {
509530
return py::make_iterator(ad.cbegin_edges(i), ad.cend_edges(i));
510531
},
511532
py::arg("i"),
@@ -519,15 +540,15 @@ namespace libsemigroups {
519540
)pbdoc")
520541
.def(
521542
"sccs_iterator",
522-
[](ActionDigraph<size_t> const &ad) {
543+
[](ActionDigraph<size_t> const& ad) {
523544
return py::make_iterator(ad.cbegin_sccs(), ad.cend_sccs());
524545
},
525546
R"pbdoc(
526547
Returns an iterator for the nodes in the scc.
527548
)pbdoc")
528549
.def(
529550
"scc_iterator",
530-
[](ActionDigraph<size_t> const &ad, size_t const i) {
551+
[](ActionDigraph<size_t> const& ad, size_t const i) {
531552
return py::make_iterator(ad.cbegin_scc(i), ad.cend_scc(i));
532553
},
533554
R"pbdoc(
@@ -536,7 +557,7 @@ namespace libsemigroups {
536557

537558
.def(
538559
"scc_roots_iterator",
539-
[](ActionDigraph<size_t> const &ad) {
560+
[](ActionDigraph<size_t> const& ad) {
540561
return py::make_iterator(ad.cbegin_scc_roots(),
541562
ad.cend_scc_roots());
542563
},
@@ -545,10 +566,10 @@ namespace libsemigroups {
545566
)pbdoc")
546567
.def(
547568
"panilo_iterator",
548-
[](ActionDigraph<size_t> const &ad,
549-
node_type const & source,
550-
size_t const & mn,
551-
size_t const & mx) {
569+
[](ActionDigraph<size_t> const& ad,
570+
node_type const& source,
571+
size_t const& mn,
572+
size_t const& mx) {
552573
return py::make_iterator(ad.cbegin_panilo(source, mn, mx),
553574
ad.cend_panilo());
554575
},
@@ -574,10 +595,10 @@ namespace libsemigroups {
574595
)pbdoc")
575596
.def(
576597
"panislo_iterator",
577-
[](ActionDigraph<size_t> const &ad,
578-
node_type const & source,
579-
size_t const & mn,
580-
size_t const & mx) {
598+
[](ActionDigraph<size_t> const& ad,
599+
node_type const& source,
600+
size_t const& mn,
601+
size_t const& mx) {
581602
return py::make_iterator(ad.cbegin_panislo(source, mn, mx),
582603
ad.cend_panislo());
583604
},
@@ -603,10 +624,10 @@ namespace libsemigroups {
603624
)pbdoc")
604625
.def(
605626
"pilo_iterator",
606-
[](ActionDigraph<size_t> const &ad,
607-
node_type const & source,
608-
size_t const & mn,
609-
size_t const & mx) {
627+
[](ActionDigraph<size_t> const& ad,
628+
node_type const& source,
629+
size_t const& mn,
630+
size_t const& mx) {
610631
return py::make_iterator(ad.cbegin_pilo(source, mn, mx),
611632
ad.cend_pilo());
612633
},
@@ -631,10 +652,10 @@ namespace libsemigroups {
631652
)pbdoc")
632653
.def(
633654
"pislo_iterator",
634-
[](ActionDigraph<size_t> const &ad,
635-
node_type const & source,
636-
size_t const & mn,
637-
size_t const & mx) {
655+
[](ActionDigraph<size_t> const& ad,
656+
node_type const& source,
657+
size_t const& mn,
658+
size_t const& mx) {
638659
return py::make_iterator(ad.cbegin_pislo(source, mn, mx),
639660
ad.cend_pislo());
640661
},
@@ -661,11 +682,11 @@ namespace libsemigroups {
661682
)pbdoc")
662683
.def(
663684
"pstislo_iterator",
664-
[](ActionDigraph<size_t> const &ad,
665-
node_type const & source,
666-
node_type const & target,
667-
size_t const & mn,
668-
size_t const & mx) {
685+
[](ActionDigraph<size_t> const& ad,
686+
node_type const& source,
687+
node_type const& target,
688+
size_t const& mn,
689+
size_t const& mx) {
669690
return py::make_iterator(
670691
ad.cbegin_pstislo(source, target, mn, mx), ad.cend_pstislo());
671692
},
@@ -739,7 +760,7 @@ namespace libsemigroups {
739760
// can be included here!
740761

741762
m.def("add_cycle",
742-
py::overload_cast<ActionDigraph<size_t> &, size_t>(
763+
py::overload_cast<ActionDigraph<size_t>&, size_t>(
743764
&action_digraph_helper::add_cycle<size_t>),
744765
py::arg("ad"),
745766
py::arg("N"),
@@ -754,7 +775,7 @@ namespace libsemigroups {
754775
:return: None.
755776
)pbdoc");
756777
m.def("is_acyclic",
757-
py::overload_cast<ActionDigraph<size_t> const &>(
778+
py::overload_cast<ActionDigraph<size_t> const&>(
758779
&action_digraph_helper::is_acyclic<size_t>),
759780
py::arg("ad"),
760781
R"pbdoc(
@@ -768,7 +789,7 @@ namespace libsemigroups {
768789
:return: A ``bool``.
769790
)pbdoc");
770791
m.def("topological_sort",
771-
py::overload_cast<ActionDigraph<size_t> const &>(
792+
py::overload_cast<ActionDigraph<size_t> const&>(
772793
&action_digraph_helper::topological_sort<size_t>),
773794
py::arg("ad"),
774795
R"pbdoc(
@@ -783,7 +804,7 @@ namespace libsemigroups {
783804
:Returns: A list of ``int``.
784805
)pbdoc");
785806
m.def("topological_sort",
786-
py::overload_cast<ActionDigraph<size_t> const &, size_t>(
807+
py::overload_cast<ActionDigraph<size_t> const&, size_t>(
787808
&action_digraph_helper::topological_sort<size_t>),
788809
py::arg("ad"),
789810
py::arg("source"),
@@ -822,5 +843,8 @@ namespace libsemigroups {
822843
if ``source`` is not a node in the digraph or ``path`` contains a
823844
value that is not an edge-label.
824845
)pbdoc");
846+
m.def("action_digraph_helper_make",
847+
&libsemigroups_pybind11::make,
848+
R"pbdoc( TOOD )pbdoc");
825849
}
826850
} // namespace libsemigroups

0 commit comments

Comments
 (0)