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

Function Graph_add_edges_from_file

cpp_easygraph/classes/graph.cpp:351–418  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

349}
350
351py::object Graph_add_edges_from_file(Graph& self, py::str file, py::object weighted, py::object is_transform) {
352 self.dirty_nodes = true;
353 self.dirty_adj = true;
354 self.linkgraph_dirty = true;
355 bool _is_transform = is_transform.cast<bool>();
356 struct commactype : std::ctype<char> {
357 commactype() : std::ctype<char>(get_table()) {}
358 std::ctype_base::mask const* get_table() {
359 std::ctype_base::mask* rc = 0;
360 if (rc == 0) {
361 rc = new std::ctype_base::mask[std::ctype<char>::table_size];
362 std::fill_n(rc, std::ctype<char>::table_size, std::ctype_base::mask());
363 rc[','] = std::ctype_base::space;
364 rc[' '] = std::ctype_base::space;
365 rc[' '] = std::ctype_base::space;
366 rc['\t'] = std::ctype_base::space;
367 rc['\n'] = std::ctype_base::space;
368 rc['\r'] = std::ctype_base::space;
369 }
370 return rc;
371 }
372 };
373
374 std::ios::sync_with_stdio(0);
375 std::string file_path = file.cast<std::string>();
376 std::ifstream in;
377 in.open(file_path);
378 if (!in.is_open()) {
379 PyErr_Format(PyExc_FileNotFoundError, "Please check the file and make sure the path only contains English");
380 return py::none();
381 }
382 in.imbue(std::locale(std::locale(), new commactype));
383 std::string data, key("weight");
384 std::string su, sv;
385 weight_t weight;
386 while (in >> su >> sv) {
387 py::str pu(su), pv(sv);
388 node_t u, v;
389 if (!self.node_to_id.contains(pu)) {
390 u = _add_one_node(self, pu);
391 } else {
392 u = self.node_to_id[pu].cast<node_t>();
393 }
394 if (!self.node_to_id.contains(pv)) {
395 v = _add_one_node(self, pv);
396 } else {
397 v = self.node_to_id[pv].cast<node_t>();
398 }
399 if (weighted.cast<bool>()) {
400 in >> weight;
401 self.adj[u][v][key] = self.adj[v][u][key] = weight;
402 } else {
403 if (!self.adj[u].count(v)) {
404 self.adj[u][v] = node_attr_dict_factory();
405 }
406 if (!self.adj[v].count(u)) {
407 self.adj[v][u] = node_attr_dict_factory();
408 }

Callers

nothing calls this directly

Calls 2

_add_one_nodeFunction · 0.85
graph_to_linkgraphFunction · 0.85

Tested by

no test coverage detected