Rom*_*nL. 1 graph rust connected-components
我正在使用petgraph,我想提取连接组件.
我想要一个HashMap<u32, Vec<&petgraph::graph::NodeIndex>>
带有a u32作为连接组件的标识符,以及一个Vec带有对连接组件中所有节点的引用的容器.
如果这是一个糟糕的设计,请毫不犹豫地指出一个更好的设计; 我是一个Rust初学者.
我试过这样的事情:
extern crate fnv;
extern crate petgraph;
use petgraph::visit::Dfs;
use fnv::FnvHashMap; // a faster hash for small key
use fnv::FnvHashSet;
// structure definition
pub struct NodeAttr {
pub name_real: String,
}
impl Default for NodeAttr {
fn default() -> Self {
NodeAttr {
name_real: "default_name_for_testing".to_string(),
}
}
}
pub struct EdgesAttr {
pub eval: f64,
pub pid: f32,
pub cov: f32, // minimum coverage
}
impl Default for EdgesAttr {
fn default() -> Self {
EdgesAttr {
eval: 0.0,
pid: 100.0,
cov: 100.0,
}
}
}
pub fn cc_dfs<'a>(
myGraph: &petgraph::Graph<NodeAttr, EdgesAttr, petgraph::Undirected>,
) -> FnvHashMap<u32, Vec<&'a petgraph::graph::NodeIndex>> {
let mut already_visited = FnvHashSet::<&petgraph::graph::NodeIndex>::default();
let mut map_real_index: FnvHashMap<u32, Vec<&petgraph::graph::NodeIndex>> =
FnvHashMap::with_capacity_and_hasher(myGraph.node_count(), Default::default());
let mut cpt = 0;
for current_node_indice in myGraph.node_indices() {
let mut current_vec: Vec<&petgraph::graph::NodeIndex> = Vec::new();
if already_visited.contains(¤t_node_indice) {
continue;
}
let mut dfs = Dfs::new(&myGraph, current_node_indice);
while let Some(nx) = dfs.next(&myGraph) {
// the problem is around here
// I believe the just assigned nx live only for the while
//But it should live for the upper for loop. What to do?
current_vec.push(&nx);
already_visited.insert(&nx);
}
map_real_index.insert(cpt, current_vec);
cpt = cpt + 1
}
return map_real_index;
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
Cargo.toml:
enter[dependencies]
fnv="*"
petgraph="*"
Run Code Online (Sandbox Code Playgroud)
编译错误:
error[E0597]: `nx` does not live long enough
--> src/main.rs:59:31
|
59 | current_vec.push(&nx);
| ^^ does not live long enough
60 | already_visited.insert(&nx);
61 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 40:1...
--> src/main.rs:40:1
|
40 | / pub fn cc_dfs<'a>(
41 | | myGraph: &petgraph::Graph<NodeAttr, EdgesAttr, petgraph::Undirected>,
42 | | ) -> FnvHashMap<u32, Vec<&'a petgraph::graph::NodeIndex>> {
43 | | let mut already_visited = FnvHashSet::<&petgraph::graph::NodeIndex>::default();
... |
66 | | return map_real_index;
67 | | }
| |_^
error[E0597]: `nx` does not live long enough
--> src/main.rs:61:9
|
60 | already_visited.insert(&nx);
| -- borrow occurs here
61 | }
| ^ `nx` dropped here while still borrowed
...
67 | }
| - borrowed value needs to live until here
Run Code Online (Sandbox Code Playgroud)
我在我的向量中克隆了节点索引并且有效:
current_vec.push(nx.clone()); // instead of (&nx)
already_visited.insert(nx.clone());`
Run Code Online (Sandbox Code Playgroud)
我相信(可能是错误的)使用引用比复制更有效.
这个小得多的代码表现出同样的问题(游乐场):
let mut v = Vec::new(); // Vec<&'a NodeIndex> ... but what is 'a?
for n in 0..10 {
let nx: NodeIndex = NodeIndex::new(n);
v.push(&nx);
}
Run Code Online (Sandbox Code Playgroud)
也就是说,你NodeIndex在一个循环中创建一个短命的东西,并试图在一个更长寿的存储中存储对它的引用Vec.
在这种情况下,解决方案非常简单:只需移动NodeIndex而不是参考.
v.push(nx)
Run Code Online (Sandbox Code Playgroud)
在您的原始代码中,修复也没有什么不同.
// nit: "indices" is the plural of "index"; there is no singular word "indice"
for current_node_index in myGraph.node_indices() {
// actually you don't need to supply a type here, but if you did...
let mut current_vec: Vec<petgraph::graph::NodeIndex> = Vec::new();
if already_visited.contains(¤t_node_index) {
continue;
}
let mut dfs = Dfs::new(&myGraph, current_node_index);
while let Some(nx) = dfs.next(&myGraph) {
current_vec.push(nx);
// ^-----v- Look Ma, no &s!
already_visited.insert(nx);
}
map_real_index.insert(cpt, current_vec);
cpt = cpt + 1
}
Run Code Online (Sandbox Code Playgroud)
"但是,"你说,"我不想复制整个NodeIndex!我只想指点它!NodeIndex是一个很胖的毛发结构,对吧?"
好吧,如果那个(一个拥有的指针)真的是你需要的,Box那就是你几乎总想要的.但是,如果你想知道这些指数的重量级是多么重要,首先要NodeIndex看看源代码的定义并查看它们:
pub struct NodeIndex<Ix=DefaultIx>(Ix);
Run Code Online (Sandbox Code Playgroud)
A NodeIndex只是一个Ix,(如果你查找DefaultIx)只是一个别名u32.在64位PC上,它实际上比你试图存储的指针小,而在Rust中,你不需要支付任何额外的使用成本 - 在运行时,它实际上只是一个u32.
便利,NodeIndex是Copy(时Ix为Copy),所以你甚至不需要把那多余.clone()的; 你可以做current_vec.push(nx),然后already_visited.insert(nx)就像我上面一样.(但即使你写了.clone(),你也不需要支付运行时费用;这只是不必要的.)
| 归档时间: |
|
| 查看次数: |
225 次 |
| 最近记录: |