From d5d1dbcd3e895793e2258542f6aeddd9761faa76 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 1 Jun 2023 14:02:50 +0900 Subject: [PATCH] dag_walk: reorder and adjust signature of neighbors_fn for consistency --- lib/src/dag_walk.rs | 12 ++++++------ lib/src/op_heads_store.rs | 7 +++++-- lib/src/transaction.rs | 2 +- src/cli_util.rs | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/src/dag_walk.rs b/lib/src/dag_walk.rs index 782c04030..1c2022eae 100644 --- a/lib/src/dag_walk.rs +++ b/lib/src/dag_walk.rs @@ -88,8 +88,8 @@ where pub fn leaves( start: II, - mut neighbors_fn: impl FnMut(&T) -> NI, id_fn: impl Fn(&T) -> ID, + mut neighbors_fn: impl FnMut(&T) -> NI, ) -> HashSet where T: Hash + Eq + Clone, @@ -125,8 +125,8 @@ where /// start set. pub fn heads( start: II, - neighbors_fn: impl Fn(&T) -> NI, id_fn: impl Fn(&T) -> ID, + mut neighbors_fn: impl FnMut(&T) -> NI, ) -> HashSet where T: Hash + Eq + Clone, @@ -149,8 +149,8 @@ where pub fn closest_common_node( set1: II1, set2: II2, - neighbors_fn: impl Fn(&T) -> NI, id_fn: impl Fn(&T) -> ID, + mut neighbors_fn: impl FnMut(&T) -> NI, ) -> Option 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']); } diff --git a/lib/src/op_heads_store.rs b/lib/src/op_heads_store.rs index 9a5f486bd..88f5d966e 100644 --- a/lib/src/op_heads_store.rs +++ b/lib/src/op_heads_store.rs @@ -52,10 +52,13 @@ pub trait OpHeadsStore: Send + Sync + Debug { /// storage. fn handle_ancestor_ops(&self, op_heads: Vec) -> Vec { 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); diff --git a/lib/src/transaction.rs b/lib/src/transaction.rs index 685362a99..42953ab51 100644 --- a/lib/src/transaction.rs +++ b/lib/src/transaction.rs @@ -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(); diff --git a/src/cli_util.rs b/src/cli_util.rs index 68f0c6532..caf24faf3 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -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() {