mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
graph: extract function that reverses graph edges
ReverseGraphIterator will be inlined. It doesn't make sense the iterator yields Item = Result<..> whereas all possible errors are confined by constructor.
This commit is contained in:
parent
7bf31c1557
commit
d6b84da382
1 changed files with 26 additions and 18 deletions
|
@ -85,24 +85,7 @@ where
|
|||
pub fn new(
|
||||
input: impl Iterator<Item = Result<GraphNode<N>, RevsetEvaluationError>>,
|
||||
) -> Result<Self, RevsetEvaluationError> {
|
||||
let mut entries = vec![];
|
||||
let mut reverse_edges: HashMap<N, Vec<GraphEdge<N>>> = HashMap::new();
|
||||
for item in input {
|
||||
let (node, edges) = item?;
|
||||
for GraphEdge { target, edge_type } in edges {
|
||||
reverse_edges.entry(target).or_default().push(GraphEdge {
|
||||
target: node.clone(),
|
||||
edge_type,
|
||||
});
|
||||
}
|
||||
entries.push(node);
|
||||
}
|
||||
|
||||
let mut items = vec![];
|
||||
for node in entries.into_iter() {
|
||||
let edges = reverse_edges.get(&node).cloned().unwrap_or_default();
|
||||
items.push((node, edges));
|
||||
}
|
||||
let items = reverse_graph(input)?;
|
||||
Ok(Self { items })
|
||||
}
|
||||
}
|
||||
|
@ -115,6 +98,31 @@ impl<N> Iterator for ReverseGraphIterator<N> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates new graph in which edges are reversed.
|
||||
fn reverse_graph<N: Clone + Eq + Hash, E>(
|
||||
input: impl Iterator<Item = Result<GraphNode<N>, E>>,
|
||||
) -> Result<Vec<GraphNode<N>>, E> {
|
||||
let mut entries = vec![];
|
||||
let mut reverse_edges: HashMap<N, Vec<GraphEdge<N>>> = HashMap::new();
|
||||
for item in input {
|
||||
let (node, edges) = item?;
|
||||
for GraphEdge { target, edge_type } in edges {
|
||||
reverse_edges.entry(target).or_default().push(GraphEdge {
|
||||
target: node.clone(),
|
||||
edge_type,
|
||||
});
|
||||
}
|
||||
entries.push(node);
|
||||
}
|
||||
|
||||
let mut items = vec![];
|
||||
for node in entries.into_iter() {
|
||||
let edges = reverse_edges.get(&node).cloned().unwrap_or_default();
|
||||
items.push((node, edges));
|
||||
}
|
||||
Ok(items)
|
||||
}
|
||||
|
||||
/// Graph iterator adapter to group topological branches.
|
||||
///
|
||||
/// Basic idea is DFS from the heads. At fork point, the other descendant
|
||||
|
|
Loading…
Reference in a new issue