|
| 1 | +#include <pybind11/functional.h> // for std::function support |
| 2 | +#include <pybind11/operators.h> |
| 3 | +#include <pybind11/pybind11.h> |
| 4 | +#include <pybind11/stl.h> |
| 5 | + |
| 6 | +#include "CXXGraph/CXXGraph.hpp" |
| 7 | + |
| 8 | +namespace py = pybind11; |
| 9 | + |
| 10 | +PYBIND11_MODULE(cxxgraph, m) { |
| 11 | + py::class_<CXXGraph::Node<int>>(m, "Node") |
| 12 | + .def(py::init<const std::string &, const int &>(), py::arg("user_id"), |
| 13 | + py::arg("data")) |
| 14 | + .def(py::init<const std::string &, int &&>(), py::arg("user_id"), |
| 15 | + py::arg("data")) |
| 16 | + |
| 17 | + // Read-only property accessors |
| 18 | + .def("get_id", &CXXGraph::Node<int>::getId) |
| 19 | + .def("get_user_id", &CXXGraph::Node<int>::getUserId) |
| 20 | + .def("get_data", static_cast<const int &(CXXGraph::Node<int>::*)() const>( |
| 21 | + &CXXGraph::Node<int>::getData)) |
| 22 | + .def("get_data_mut", static_cast<int &(CXXGraph::Node<int>::*)()>( |
| 23 | + &CXXGraph::Node<int>::getData)) |
| 24 | + |
| 25 | + // Mutator |
| 26 | + .def("set_data", &CXXGraph::Node<int>::setData) |
| 27 | + |
| 28 | + // Operators |
| 29 | + .def(py::self == py::self) |
| 30 | + .def(py::self < py::self) |
| 31 | + |
| 32 | + // String representation |
| 33 | + .def("__repr__", [](const CXXGraph::Node<int> &n) { |
| 34 | + std::ostringstream oss; |
| 35 | + oss << n; |
| 36 | + return oss.str(); |
| 37 | + }); |
| 38 | + |
| 39 | + py::class_<CXXGraph::Edge<int>>(m, "Edge").def( |
| 40 | + py::init<const CXXGraph::id_t, const CXXGraph::Node<int> &, |
| 41 | + const CXXGraph::Node<int> &>()); // default ctor, |
| 42 | + |
| 43 | + py::class_<CXXGraph::Graph<int>>(m, "Graph") |
| 44 | + .def(py::init<>()) // default ctor, no bool argument |
| 45 | + .def("add_node", |
| 46 | + static_cast<void (CXXGraph::Graph<int>::*)( |
| 47 | + const CXXGraph::Node<int> *)>(&CXXGraph::Graph<int>::addNode)) |
| 48 | + .def("add_edge", |
| 49 | + static_cast<void (CXXGraph::Graph<int>::*)( |
| 50 | + const CXXGraph::Edge<int> *)>(&CXXGraph::Graph<int>::addEdge)) |
| 51 | + |
| 52 | + .def("dfs", &CXXGraph::Graph<int>::depth_first_search); |
| 53 | +} |
0 commit comments