DAG를 빌드하고 트래버스하려고합니다. 가능한 두 가지 방법이있을 것 같습니다 : 가장자리에 Rc<RefCell<Node>>
을 사용하거나 아레나 할당 자 및 일부 unsafe
코드를 사용합니다. (See details here.)방향 지정 비순환 그래프를 안전하게 트래버스
내가 이전 말인가하지만,에 의존하는 자식 노드의 대출로, 가장자리에 그래프를 통과하는 어려움에 봉착는 부모에게 빌려 :
이use std::cell::RefCell;
use std::rc::Rc;
// See: https://aminb.gitbooks.io/rust-for-c/content/graphs/index.html,
// https://github.com/nrc/r4cppp/blob/master/graphs/src/ref_graph.rs
pub type Link<T> = Rc<RefCell<T>>;
pub struct DagNode {
/// Each node can have several edges flowing *into* it, i.e. several owners,
/// hence the use of Rc. RefCell is used so we can have mutability
/// while building the graph.
pub edge: Option<Link<DagNode>>,
// Other data here
}
// Attempt to walk down the DAG until we reach a leaf.
fn walk_to_end(node: &Link<DagNode>) -> &Link<DagNode> {
let nb = node.borrow();
match nb.edge {
Some(ref prev) => walk_to_end(prev),
// Here be dragons: the borrow relies on all previous borrows,
// so this fails to compile.
None => node
}
}
나는를 수정할 수 있습니다 참조 카운트, 즉
fn walk_to_end(node: Link<HistoryNode>) -> Link<HistoryNode> {
let nb = node.borrow();
match nb.previous {
Some(ref prev) => walk_to_end(prev.clone()),
None => node.clone()
}
}
하지만 노드를 통과 할 때마다 레퍼런스 카운트가 부딪히는 것은 상당히 해킹 된 것처럼 보입니다. 여기서 관용적 접근은 무엇입니까?
http://cglab.ca/~abeinges/blah/too-many-lists/book/fourth-iteration.html#iter? 그것은'Rc' 대기열에 관한 것이지만 실제로 도움이 될 수 있습니다. – aSpex
... 섹션을 올바르게 읽는다면 작성자는 내가 겪고있는 것과 동일한 문제에 먼저 얼굴을 마주 보았습니다. 재치있게 말하면, "Rc 정말 정말 우리를 실망 시켰습니다." –
@MattKline : 그건 사실 "어쨌든, 그건 Iter와 IterMut에서 포기하고 우리는 할 수 있지만 우우."그래서 Gankro의 마음은 가능하지만 어쩌면 우아하지 않을 수 있습니다. –