dag_walk: reorder and adjust signature of neighbors_fn for consistency

This commit is contained in:
Yuya Nishihara 2023-06-01 14:02:50 +09:00
parent a28e672633
commit d5d1dbcd3e
4 changed files with 13 additions and 10 deletions

View file

@ -88,8 +88,8 @@ where
pub fn leaves<T, ID, II, NI>(
start: II,
mut neighbors_fn: impl FnMut(&T) -> NI,
id_fn: impl Fn(&T) -> ID,
mut neighbors_fn: impl FnMut(&T) -> NI,
) -> HashSet<T>
where
T: Hash + Eq + Clone,
@ -125,8 +125,8 @@ where
/// start set.
pub fn heads<T, ID, II, NI>(
start: II,
neighbors_fn: impl Fn(&T) -> NI,
id_fn: impl Fn(&T) -> ID,
mut neighbors_fn: impl FnMut(&T) -> NI,
) -> HashSet<T>
where
T: Hash + Eq + Clone,
@ -149,8 +149,8 @@ where
pub fn closest_common_node<T, ID, II1, II2, NI>(
set1: II1,
set2: II2,
neighbors_fn: impl Fn(&T) -> NI,
id_fn: impl Fn(&T) -> ID,
mut neighbors_fn: impl FnMut(&T) -> NI,
) -> Option<T>
where
T: Hash + Eq + Clone,
@ -309,8 +309,8 @@ mod tests {
let common = closest_common_node(
vec!['E'],
vec!['H'],
|node| neighbors[node].clone(),
|node| *node,
|node| neighbors[node].clone(),
);
// TODO: fix the implementation to return B
@ -340,16 +340,16 @@ mod tests {
let actual = heads(
vec!['A', 'C', 'D', 'F'],
|node| neighbors[node].clone(),
|node| *node,
|node| neighbors[node].clone(),
);
assert_eq!(actual, hashset!['D', 'F']);
// Check with a different order in the start set
let actual = heads(
vec!['F', 'D', 'C', 'A'],
|node| neighbors[node].clone(),
|node| *node,
|node| neighbors[node].clone(),
);
assert_eq!(actual, hashset!['D', 'F']);
}

View file

@ -52,10 +52,13 @@ pub trait OpHeadsStore: Send + Sync + Debug {
/// storage.
fn handle_ancestor_ops(&self, op_heads: Vec<Operation>) -> Vec<Operation> {
let op_head_ids_before: HashSet<_> = op_heads.iter().map(|op| op.id().clone()).collect();
let neighbors_fn = |op: &Operation| op.parents();
// Remove ancestors so we don't create merge operation with an operation and its
// ancestor
let op_heads = dag_walk::heads(op_heads, neighbors_fn, |op: &Operation| op.id().clone());
let op_heads = dag_walk::heads(
op_heads,
|op: &Operation| op.id().clone(),
|op: &Operation| op.parents(),
);
let op_head_ids_after: HashSet<_> = op_heads.iter().map(|op| op.id().clone()).collect();
for removed_op_head in op_head_ids_before.difference(&op_head_ids_after) {
self.remove_op_head(removed_op_head);

View file

@ -68,8 +68,8 @@ impl Transaction {
let ancestor_op = closest_common_node(
self.parent_ops.clone(),
vec![other_op.clone()],
|op: &Operation| op.parents(),
|op: &Operation| op.id().clone(),
|op: &Operation| op.parents(),
)
.unwrap();
let repo_loader = self.base_repo().loader();

View file

@ -1457,8 +1457,8 @@ pub fn check_stale_working_copy(
let maybe_ancestor_op = dag_walk::closest_common_node(
[wc_operation.clone()],
[repo_operation.clone()],
|op: &Operation| op.parents(),
|op: &Operation| op.id().clone(),
|op: &Operation| op.parents(),
);
if let Some(ancestor_op) = maybe_ancestor_op {
if ancestor_op.id() == repo_operation.id() {