|
| 1 | +#include <Python.h> |
| 2 | +#include <unordered_map> |
| 3 | +#include <queue> |
| 4 | +#include <string> |
| 5 | +#include "AdjacencyList.hpp" |
| 6 | + |
| 7 | +static inline AdjacencyListGraphNode* get_node(AdjacencyListGraph* graph, const std::string& name) { |
| 8 | + auto it = graph->node_map.find(name); |
| 9 | + return (it != graph->node_map.end()) ? it->second : nullptr; |
| 10 | +} |
| 11 | + |
| 12 | +static PyObject* bfs_adjacency_list(PyObject* self, PyObject* args, PyObject* kwds) { |
| 13 | + static char* kwlist[] = {(char*)"graph", (char*)"source_node", (char*)"operation", (char*)"extra_arg", NULL}; |
| 14 | + |
| 15 | + PyObject* py_graph = nullptr; |
| 16 | + const char* source_node = nullptr; |
| 17 | + PyObject* operation = nullptr; |
| 18 | + PyObject* extra_arg = nullptr; |
| 19 | + |
| 20 | + fprintf(stderr, "[bfs] Parsing arguments...\n"); |
| 21 | + |
| 22 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OsO|O", kwlist, |
| 23 | + &py_graph, &source_node, &operation, &extra_arg)) { |
| 24 | + fprintf(stderr, "[bfs] Failed to parse arguments\n"); |
| 25 | + return NULL; |
| 26 | + } |
| 27 | + |
| 28 | + fprintf(stderr, "[bfs] Arguments parsed:\n"); |
| 29 | + fprintf(stderr, " - source_node: %s\n", source_node); |
| 30 | + fprintf(stderr, " - extra_arg: %s\n", (extra_arg ? Py_TYPE(extra_arg)->tp_name : "NULL")); |
| 31 | + fprintf(stderr, "[bfs] Checking type of py_graph...\n"); |
| 32 | + fprintf(stderr, " - Expected: %s\n", AdjacencyListGraphType.tp_name); |
| 33 | + fprintf(stderr, " - Actual: %s\n", Py_TYPE(py_graph)->tp_name); |
| 34 | + fprintf(stderr, " - Expected address: %p\n", &AdjacencyListGraphType); |
| 35 | + fprintf(stderr, " - Actual type addr: %p\n", (void*)Py_TYPE(py_graph)); |
| 36 | + |
| 37 | + fprintf(stderr, "[bfs] Attempting to import _graph...\n"); |
| 38 | + PyObject* graph_module = PyImport_ImportModule("_graph"); |
| 39 | + if (!graph_module) { |
| 40 | + PyErr_Print(); |
| 41 | + PyErr_SetString(PyExc_ImportError, "Could not import _graph module"); |
| 42 | + return NULL; |
| 43 | + } |
| 44 | + |
| 45 | + PyObject* expected_type = PyObject_GetAttrString(graph_module, "AdjacencyListGraph"); |
| 46 | + Py_DECREF(graph_module); |
| 47 | + |
| 48 | + if (!expected_type || !PyType_Check(expected_type)) { |
| 49 | + Py_XDECREF(expected_type); |
| 50 | + PyErr_SetString(PyExc_TypeError, "Could not retrieve AdjacencyListGraph type"); |
| 51 | + return NULL; |
| 52 | + } |
| 53 | + |
| 54 | + if (!PyObject_IsInstance(py_graph, expected_type)) { |
| 55 | + Py_DECREF(expected_type); |
| 56 | + PyErr_SetString(PyExc_TypeError, "Expected an AdjacencyListGraph instance"); |
| 57 | + return NULL; |
| 58 | + } |
| 59 | + |
| 60 | + if (!PyCallable_Check(operation)) { |
| 61 | + PyErr_SetString(PyExc_TypeError, "Expected a callable for operation"); |
| 62 | + fprintf(stderr, "[bfs] operation is not callable\n"); |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + |
| 66 | + AdjacencyListGraph* graph = (AdjacencyListGraph*)py_graph; |
| 67 | + |
| 68 | + if (!get_node(graph, source_node)) { |
| 69 | + PyErr_SetString(PyExc_ValueError, "Source node does not exist in the graph"); |
| 70 | + fprintf(stderr, "[bfs] source_node not found in graph\n"); |
| 71 | + return NULL; |
| 72 | + } |
| 73 | + |
| 74 | + fprintf(stderr, "[bfs] Starting BFS from node: %s\n", source_node); |
| 75 | + |
| 76 | + std::unordered_map<std::string, bool> visited; |
| 77 | + std::queue<std::string> q; |
| 78 | + |
| 79 | + q.push(source_node); |
| 80 | + visited[source_node] = true; |
| 81 | + |
| 82 | + while (!q.empty()) { |
| 83 | + std::string curr = q.front(); |
| 84 | + q.pop(); |
| 85 | + |
| 86 | + fprintf(stderr, "[bfs] Visiting node: %s\n", curr.c_str()); |
| 87 | + |
| 88 | + auto* curr_node = get_node(graph, curr); |
| 89 | + if (!curr_node) { |
| 90 | + fprintf(stderr, "[bfs] Warning: node %s not found in node_map\n", curr.c_str()); |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + const auto& neighbors = curr_node->adjacent; |
| 95 | + |
| 96 | + if (!neighbors.empty()) { |
| 97 | + for (const auto& [next_name, _] : neighbors) { |
| 98 | + if (!visited[next_name]) { |
| 99 | + fprintf(stderr, "[bfs] Considering neighbor: %s\n", next_name.c_str()); |
| 100 | + |
| 101 | + PyObject* result = nullptr; |
| 102 | + |
| 103 | + if (extra_arg) |
| 104 | + result = PyObject_CallFunction(operation, "ssO", curr.c_str(), next_name.c_str(), extra_arg); |
| 105 | + else |
| 106 | + result = PyObject_CallFunction(operation, "ss", curr.c_str(), next_name.c_str()); |
| 107 | + |
| 108 | + if (!result) { |
| 109 | + fprintf(stderr, "[bfs] PyObject_CallFunction failed on (%s, %s)\n", curr.c_str(), next_name.c_str()); |
| 110 | + PyErr_Print(); |
| 111 | + return NULL; |
| 112 | + } |
| 113 | + |
| 114 | + int keep_going = PyObject_IsTrue(result); |
| 115 | + Py_DECREF(result); |
| 116 | + |
| 117 | + if (!keep_going) { |
| 118 | + fprintf(stderr, "[bfs] Operation requested to stop traversal at edge (%s -> %s)\n", curr.c_str(), next_name.c_str()); |
| 119 | + Py_RETURN_NONE; |
| 120 | + } |
| 121 | + |
| 122 | + visited[next_name] = true; |
| 123 | + q.push(next_name); |
| 124 | + } |
| 125 | + } |
| 126 | + } else { |
| 127 | + fprintf(stderr, "[bfs] Leaf node reached: %s\n", curr.c_str()); |
| 128 | + |
| 129 | + PyObject* result = nullptr; |
| 130 | + |
| 131 | + if (extra_arg) |
| 132 | + result = PyObject_CallFunction(operation, "sO", curr.c_str(), extra_arg); |
| 133 | + else |
| 134 | + result = PyObject_CallFunction(operation, "s", curr.c_str()); |
| 135 | + |
| 136 | + if (!result) { |
| 137 | + fprintf(stderr, "[bfs] PyObject_CallFunction failed at leaf node (%s)\n", curr.c_str()); |
| 138 | + PyErr_Print(); |
| 139 | + return NULL; |
| 140 | + } |
| 141 | + |
| 142 | + int keep_going = PyObject_IsTrue(result); |
| 143 | + Py_DECREF(result); |
| 144 | + |
| 145 | + if (!keep_going) { |
| 146 | + fprintf(stderr, "[bfs] Operation requested to stop traversal at leaf node %s\n", curr.c_str()); |
| 147 | + Py_RETURN_NONE; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + fprintf(stderr, "[bfs] BFS traversal complete\n"); |
| 153 | + Py_RETURN_NONE; |
| 154 | +} |
0 commit comments