34
34
#include < libsemigroups/int-range.hpp> // for IntegralRange<>::value_type
35
35
36
36
// 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
39
40
40
41
// libsemigroups_pybind11....
41
42
#include " main.hpp" // for init_action_digraph
42
43
43
44
namespace py = pybind11;
44
45
45
46
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
+ //
46
66
using node_type = ActionDigraph<size_t >::node_type;
47
67
using algorithm = ActionDigraph<size_t >::algorithm;
48
- void init_action_digraph (py::module & m) {
68
+ void init_action_digraph (py::module & m) {
49
69
// //////////////////////////////////////////////////////////////////////
50
70
// ActionDigraph
51
71
// //////////////////////////////////////////////////////////////////////
@@ -83,12 +103,12 @@ namespace libsemigroups {
83
103
- **n** (int) the out-degree of every node (default:
84
104
``0``).
85
105
)pbdoc" )
86
- .def (py::init<ActionDigraph<size_t > const &>(),
106
+ .def (py::init<ActionDigraph<size_t > const &>(),
87
107
R"pbdoc(
88
108
Construct a copy.
89
109
)pbdoc" )
90
110
.def (" __repr__" ,
91
- [](ActionDigraph<size_t > const & d) {
111
+ [](ActionDigraph<size_t > const & d) {
92
112
std::string result = " <action digraph with " ;
93
113
result += std::to_string (d.number_of_nodes ());
94
114
result += " nodes, " ;
@@ -98,6 +118,7 @@ namespace libsemigroups {
98
118
result += " out-degree>" ;
99
119
return result;
100
120
})
121
+ .def (pybind11::self == pybind11::self)
101
122
.def (" number_of_nodes" ,
102
123
&ActionDigraph<size_t >::number_of_nodes,
103
124
R"pbdoc(
@@ -445,7 +466,7 @@ namespace libsemigroups {
445
466
)pbdoc" )
446
467
.def (
447
468
" number_of_paths" ,
448
- [](ActionDigraph<size_t > const & ad,
469
+ [](ActionDigraph<size_t > const & ad,
449
470
node_type source,
450
471
node_type target,
451
472
size_t min,
@@ -469,7 +490,7 @@ namespace libsemigroups {
469
490
)pbdoc" )
470
491
.def (
471
492
" number_of_paths" ,
472
- [](ActionDigraph<size_t > const & ad,
493
+ [](ActionDigraph<size_t > const & ad,
473
494
node_type source,
474
495
size_t min,
475
496
size_t max) { return ad.number_of_paths (source, min, max); },
@@ -488,15 +509,15 @@ namespace libsemigroups {
488
509
)pbdoc" )
489
510
.def (
490
511
" nodes_iterator" ,
491
- [](ActionDigraph<size_t > const & ad) {
512
+ [](ActionDigraph<size_t > const & ad) {
492
513
return py::make_iterator (ad.cbegin_nodes (), ad.cend_nodes ());
493
514
},
494
515
R"pbdoc(
495
516
Returns an iterator to the nodes of the digraph.
496
517
)pbdoc" )
497
518
.def (
498
519
" reverse_nodes_iterator" ,
499
- [](ActionDigraph<size_t > const & ad) {
520
+ [](ActionDigraph<size_t > const & ad) {
500
521
return py::make_iterator (ad.crbegin_nodes (), ad.crend_nodes ());
501
522
},
502
523
R"pbdoc(
@@ -505,7 +526,7 @@ namespace libsemigroups {
505
526
506
527
.def (
507
528
" edges_iterator" ,
508
- [](ActionDigraph<size_t > const & ad, size_t const i) {
529
+ [](ActionDigraph<size_t > const & ad, size_t const i) {
509
530
return py::make_iterator (ad.cbegin_edges (i), ad.cend_edges (i));
510
531
},
511
532
py::arg (" i" ),
@@ -519,15 +540,15 @@ namespace libsemigroups {
519
540
)pbdoc" )
520
541
.def (
521
542
" sccs_iterator" ,
522
- [](ActionDigraph<size_t > const & ad) {
543
+ [](ActionDigraph<size_t > const & ad) {
523
544
return py::make_iterator (ad.cbegin_sccs (), ad.cend_sccs ());
524
545
},
525
546
R"pbdoc(
526
547
Returns an iterator for the nodes in the scc.
527
548
)pbdoc" )
528
549
.def (
529
550
" scc_iterator" ,
530
- [](ActionDigraph<size_t > const & ad, size_t const i) {
551
+ [](ActionDigraph<size_t > const & ad, size_t const i) {
531
552
return py::make_iterator (ad.cbegin_scc (i), ad.cend_scc (i));
532
553
},
533
554
R"pbdoc(
@@ -536,7 +557,7 @@ namespace libsemigroups {
536
557
537
558
.def (
538
559
" scc_roots_iterator" ,
539
- [](ActionDigraph<size_t > const & ad) {
560
+ [](ActionDigraph<size_t > const & ad) {
540
561
return py::make_iterator (ad.cbegin_scc_roots (),
541
562
ad.cend_scc_roots ());
542
563
},
@@ -545,10 +566,10 @@ namespace libsemigroups {
545
566
)pbdoc" )
546
567
.def (
547
568
" 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) {
552
573
return py::make_iterator (ad.cbegin_panilo (source, mn, mx),
553
574
ad.cend_panilo ());
554
575
},
@@ -574,10 +595,10 @@ namespace libsemigroups {
574
595
)pbdoc" )
575
596
.def (
576
597
" 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) {
581
602
return py::make_iterator (ad.cbegin_panislo (source, mn, mx),
582
603
ad.cend_panislo ());
583
604
},
@@ -603,10 +624,10 @@ namespace libsemigroups {
603
624
)pbdoc" )
604
625
.def (
605
626
" 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) {
610
631
return py::make_iterator (ad.cbegin_pilo (source, mn, mx),
611
632
ad.cend_pilo ());
612
633
},
@@ -631,10 +652,10 @@ namespace libsemigroups {
631
652
)pbdoc" )
632
653
.def (
633
654
" 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) {
638
659
return py::make_iterator (ad.cbegin_pislo (source, mn, mx),
639
660
ad.cend_pislo ());
640
661
},
@@ -661,11 +682,11 @@ namespace libsemigroups {
661
682
)pbdoc" )
662
683
.def (
663
684
" 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) {
669
690
return py::make_iterator (
670
691
ad.cbegin_pstislo (source, target, mn, mx), ad.cend_pstislo ());
671
692
},
@@ -739,7 +760,7 @@ namespace libsemigroups {
739
760
// can be included here!
740
761
741
762
m.def (" add_cycle" ,
742
- py::overload_cast<ActionDigraph<size_t > &, size_t >(
763
+ py::overload_cast<ActionDigraph<size_t >&, size_t >(
743
764
&action_digraph_helper::add_cycle<size_t >),
744
765
py::arg (" ad" ),
745
766
py::arg (" N" ),
@@ -754,7 +775,7 @@ namespace libsemigroups {
754
775
:return: None.
755
776
)pbdoc" );
756
777
m.def (" is_acyclic" ,
757
- py::overload_cast<ActionDigraph<size_t > const &>(
778
+ py::overload_cast<ActionDigraph<size_t > const &>(
758
779
&action_digraph_helper::is_acyclic<size_t >),
759
780
py::arg (" ad" ),
760
781
R"pbdoc(
@@ -768,7 +789,7 @@ namespace libsemigroups {
768
789
:return: A ``bool``.
769
790
)pbdoc" );
770
791
m.def (" topological_sort" ,
771
- py::overload_cast<ActionDigraph<size_t > const &>(
792
+ py::overload_cast<ActionDigraph<size_t > const &>(
772
793
&action_digraph_helper::topological_sort<size_t >),
773
794
py::arg (" ad" ),
774
795
R"pbdoc(
@@ -783,7 +804,7 @@ namespace libsemigroups {
783
804
:Returns: A list of ``int``.
784
805
)pbdoc" );
785
806
m.def (" topological_sort" ,
786
- py::overload_cast<ActionDigraph<size_t > const &, size_t >(
807
+ py::overload_cast<ActionDigraph<size_t > const &, size_t >(
787
808
&action_digraph_helper::topological_sort<size_t >),
788
809
py::arg (" ad" ),
789
810
py::arg (" source" ),
@@ -822,5 +843,8 @@ namespace libsemigroups {
822
843
if ``source`` is not a node in the digraph or ``path`` contains a
823
844
value that is not an edge-label.
824
845
)pbdoc" );
846
+ m.def (" action_digraph_helper_make" ,
847
+ &libsemigroups_pybind11::make,
848
+ R"pbdoc( TOOD )pbdoc" );
825
849
}
826
850
} // namespace libsemigroups
0 commit comments