dag_walk: unbox topo_order_reverse() callback

This commit is contained in:
Yuya Nishihara 2023-05-20 10:13:13 +09:00
parent 3ba544414c
commit a28e672633
5 changed files with 19 additions and 25 deletions

View file

@ -43,10 +43,10 @@ where
}
/// Returns neighbors before the node itself.
pub fn topo_order_reverse<'a, T, ID, II, NI>(
pub fn topo_order_reverse<T, ID, II, NI>(
start: II,
id_fn: Box<dyn Fn(&T) -> ID + 'a>,
mut neighbors_fn: Box<dyn FnMut(&T) -> NI + 'a>,
id_fn: impl Fn(&T) -> ID,
mut neighbors_fn: impl FnMut(&T) -> NI,
) -> Vec<T>
where
T: Hash + Eq + Clone,
@ -215,11 +215,8 @@ mod tests {
'C' => vec!['B'],
};
let common = topo_order_reverse(
vec!['C'],
Box::new(|node| *node),
Box::new(move |node| neighbors[node].clone()),
);
let common =
topo_order_reverse(vec!['C'], |node| *node, move |node| neighbors[node].clone());
assert_eq!(common, vec!['C', 'B', 'A']);
}
@ -245,11 +242,8 @@ mod tests {
'F' => vec!['E', 'D'],
};
let common = topo_order_reverse(
vec!['F'],
Box::new(|node| *node),
Box::new(move |node| neighbors[node].clone()),
);
let common =
topo_order_reverse(vec!['F'], |node| *node, move |node| neighbors[node].clone());
assert_eq!(common, vec!['F', 'E', 'D', 'C', 'B', 'A']);
}
@ -279,8 +273,8 @@ mod tests {
let common = topo_order_reverse(
vec!['F', 'C'],
Box::new(|node| *node),
Box::new(move |node| neighbors[node].clone()),
|node| *node,
move |node| neighbors[node].clone(),
);
assert_eq!(common, vec!['F', 'E', 'D', 'C', 'B', 'A']);

View file

@ -851,14 +851,14 @@ impl MutableRepo {
} else {
let missing_commits = topo_order_reverse(
vec![head.clone()],
Box::new(|commit: &Commit| commit.id().clone()),
Box::new(|commit: &Commit| -> Vec<Commit> {
|commit: &Commit| commit.id().clone(),
|commit: &Commit| -> Vec<Commit> {
commit
.parents()
.into_iter()
.filter(|parent| !self.index().has_id(parent.id()))
.collect()
}),
},
);
for missing_commit in missing_commits.iter().rev() {
self.index.add_commit(missing_commit);

View file

@ -189,8 +189,8 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
// rewritten, make sure we rebase the rewritten parent first.
let to_visit = dag_walk::topo_order_reverse(
to_visit,
Box::new(|commit| commit.id().clone()),
Box::new(|commit| {
|commit| commit.id().clone(),
|commit| {
visited.insert(commit.id().clone());
let mut dependents = vec![];
for parent in commit.parents() {
@ -206,7 +206,7 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
}
}
dependents
}),
},
);
let new_commits = rewritten.values().flatten().cloned().collect();

View file

@ -1676,8 +1676,8 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result
let commits = topo_order_reverse(
vec![start_commit],
Box::new(|commit: &Commit| commit.id().clone()),
Box::new(|commit: &Commit| commit.predecessors()),
|commit: &Commit| commit.id().clone(),
|commit: &Commit| commit.predecessors(),
);
if !args.no_graph {
let mut graph = get_graphlog(command.settings(), formatter.raw());

View file

@ -84,8 +84,8 @@ fn cmd_op_log(
let default_node_symbol = graph.default_node_symbol().to_owned();
for op in topo_order_reverse(
vec![head_op],
Box::new(|op: &Operation| op.id().clone()),
Box::new(|op: &Operation| op.parents()),
|op: &Operation| op.id().clone(),
|op: &Operation| op.parents(),
) {
let mut edges = vec![];
for parent in op.parents() {