MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / Graph_add_edges_from

Function Graph_add_edges_from

cpp_easygraph/classes/graph.cpp:288–349  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

286}
287
288py::object Graph_add_edges_from(py::args args, py::kwargs attr) {
289 Graph& self = args[0].cast<Graph&>();
290 self.dirty_nodes = true;
291 self.dirty_adj = true;
292 self.linkgraph_dirty = true;
293 py::list ebunch_to_add = py::list(args[1]);
294 for (int i = 0; i < len(ebunch_to_add); i++) {
295 py::list e = py::list(ebunch_to_add[i]);
296 py::object u, v;
297 py::dict dd;
298 switch (len(e)) {
299 case 2: {
300 u = e[0];
301 v = e[1];
302 break;
303 }
304 case 3: {
305 u = e[0];
306 v = e[1];
307 dd = e[2].cast<py::dict>();
308 break;
309 }
310 default: {
311 PyErr_Format(PyExc_ValueError, "Edge tuple %R must be a 2 - tuple or 3 - tuple.", e.ptr());
312 return py::none();
313 }
314 }
315 node_t u_id, v_id;
316 if (!self.node_to_id.contains(u)) {
317 if (u.is_none()) {
318 PyErr_Format(PyExc_ValueError, "None cannot be a node");
319 return py::none();
320 }
321 u_id = _add_one_node(self, u);
322 } else {
323 u_id = self.node_to_id[u].cast<node_t>();
324 }
325 if (!self.node_to_id.contains(v)) {
326 if (v.is_none()) {
327 PyErr_Format(PyExc_ValueError, "None cannot be a node");
328 return py::none();
329 }
330 v_id = _add_one_node(self, v);
331 } else {
332 v_id = (self.node_to_id[v]).cast<node_t>();
333 }
334 auto datadict = self.adj[u_id].count(v_id) ? self.adj[u_id][v_id] : node_attr_dict_factory();
335 py::list items = py::list(attr.attr("items")());
336 items.attr("extend")(py::list(dd.attr("items")()));
337 for (int i = 0; i < py::len(items); i++) {
338 py::tuple kv = items[i].cast<py::tuple>();
339 py::object pkey = kv[0];
340 std::string weight_key = weight_to_string(pkey);
341 weight_t value = kv[1].cast<weight_t>();
342 datadict.insert(std::make_pair(weight_key, value));
343 }
344 // Warning: in Graph.py the edge attr is directed assigned by the dict extended from the original attr
345 self.adj[u_id][v_id].insert(datadict.begin(), datadict.end());

Callers

nothing calls this directly

Calls 2

_add_one_nodeFunction · 0.85
weight_to_stringFunction · 0.85

Tested by

no test coverage detected