Skip to content

Commit 4d1daf8

Browse files
committed
Introduced a first pythonBinding
1 parent 1b59995 commit 4d1daf8

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ endif()
5353
add_subdirectory(test)
5454
add_subdirectory(benchmark)
5555
add_subdirectory(examples)
56+
add_subdirectory(python)
5657

5758
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)

python/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
option(PYTHON_BINDINGS "Enable Python Bindings Compilation" OFF)
2+
if(PYTHON_BINDINGS)
3+
4+
add_subdirectory(bindings)
5+
6+
7+
endif(PYTHON_BINDINGS)

python/bindings/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(CXXGraphPythonBindings LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
# Fetch pybind11
8+
include(FetchContent)
9+
FetchContent_Declare(
10+
pybind11
11+
GIT_REPOSITORY https://github.com/pybind/pybind11.git
12+
GIT_TAG v2.12.0
13+
)
14+
FetchContent_MakeAvailable(pybind11)
15+
16+
17+
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES /usr/local/lib ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})
18+
# Include headers
19+
include_directories(${CMAKE_SOURCE_DIR}/include)
20+
21+
# # Add library target (header-only here)
22+
# add_library(CXXGraphLib INTERFACE)
23+
# target_include_directories(CXXGraphLib INTERFACE ${CMAKE_SOURCE_DIR}/include)
24+
25+
# Add pybind11 module
26+
pybind11_add_module(cxxgraph bindings.cpp)

python/bindings/bindings.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

python/tests/simpleTest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
sys.path.append("../../build/python/bindings")
3+
4+
import cxxgraph
5+
6+
g = cxxgraph.Graph()
7+
node1 = cxxgraph.Node("Node_1", 1)
8+
node2 = cxxgraph.Node("Node_2", 2)
9+
g.add_node(node1)
10+
g.add_node(node1)
11+
edge1 = cxxgraph.Edge(1,node1,node2)
12+
g.add_edge(edge1)
13+
14+
15+
result = g.dfs(node1)
16+
17+
for node in result:
18+
print(node.get_id(), node.get_user_id(), node.get_data())
19+

0 commit comments

Comments
 (0)