chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:36:30 +08:00
commit 55ab4e4a73
473 changed files with 72932 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
#include "common.h"
graph_edge::graph_edge(node_t u_, node_t v_, edge_attr_dict_factory attr_) : u(u_), v(v_), attr(attr_) {}
+37
View File
@@ -0,0 +1,37 @@
#pragma once
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <map>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <exception>
#include <set>
#include <cmath>
#include <random>
#include <algorithm>
#include <queue>
#include <vector>
#include <thread>
#include <inttypes.h>
#include <limits>
namespace py = pybind11;
typedef int node_t;
typedef float weight_t;
typedef std::map<std::string, weight_t> node_attr_dict_factory; //(weight_key, value)
typedef std::map<std::string, weight_t> edge_attr_dict_factory; //(weight_key, value)
typedef std::unordered_map<node_t, node_attr_dict_factory> node_dict_factory; //(node, node_attr)
typedef std::unordered_map<node_t, edge_attr_dict_factory> adj_attr_dict_factory; //(out_node, (weight_key, value))
typedef std::unordered_map<node_t, adj_attr_dict_factory> adj_dict_factory; //(node, edge_attr)
struct graph_edge {
node_t u, v;
edge_attr_dict_factory attr;
graph_edge(node_t, node_t, edge_attr_dict_factory);
};
+48
View File
@@ -0,0 +1,48 @@
#include "utils.h"
#include "../classes/graph.h"
#include "../classes/linkgraph.h"
py::object attr_to_dict(const node_attr_dict_factory& attr) {
py::dict attr_dict = py::dict();
for (const auto& kv : attr) {
attr_dict[py::cast(kv.first)] = kv.second;
}
return attr_dict;
}
std::string weight_to_string(py::object weight) {
py::object warn = py::module_::import("warnings").attr("warn");
if (!py::isinstance<py::str>(weight)) {
if (!weight.is_none()) {
warn(py::str(weight) + py::str(" would be transformed into an instance of str."));
}
weight = py::str(weight);
}
std::string weight_key = weight.cast<std::string>();
return weight_key;
}
py::object py_sum(py::object o) {
py::object sum = py::module_::import("builtins").attr("sum");
return sum(o);
}
Graph_L graph_to_linkgraph(Graph &G, bool if_directed, std::string weight_key, bool is_deg, bool is_reverse){
int node_num = G.node.size();
const std::vector<graph_edge>& edges = G._get_edges(if_directed);
int edges_num = edges.size();
Graph_L G_l(node_num, if_directed, is_deg);
for(int i = 0; i < edges_num; i++){
graph_edge e = edges[i];
edge_attr_dict_factory& edge_attr = e.attr;
weight_t edge_weight = edge_attr.find(weight_key) != edge_attr.end() ? edge_attr[weight_key] : 1;
if(is_reverse){
std::swap(e.u, e.v);
}
G_l.add_weighted_edge(e.u, e.v, edge_weight);
if (!if_directed){
G_l.add_weighted_edge(e.v, e.u, edge_weight);
}
}
return G_l;
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "common.h"
#include "../classes/linkgraph.h"
#include "../classes/graph.h"
py::object attr_to_dict(const node_attr_dict_factory& attr);
std::string weight_to_string(py::object weight);
py::object py_sum(py::object o);
Graph_L graph_to_linkgraph(Graph &G, bool if_directed=false, std::string weight_key = "weight", bool is_deg = false, bool is_reverse = false);