From 95b34614054c48145c98a686d9053a2723c6c113 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Sat, 6 Jun 2026 04:41:53 +0200 Subject: [PATCH] refactor: centralize graph traversal logic in walk Refactor `leavable` and `reachable` to eliminate duplicated graph traversal logic by mutually delegating via `WalkState`. `leavable` now returns `self.walk(graph).is_some()`, while `reachable` delegates to the inverted `direct` state's `leavable` check. This centralizes kmer extension and visited-state validation in `walk`, simplifying control flow and reducing code duplication. --- src/obidebruinj/src/debruijn.rs | 52 ++------------------------------- 1 file changed, 3 insertions(+), 49 deletions(-) diff --git a/src/obidebruinj/src/debruijn.rs b/src/obidebruinj/src/debruijn.rs index 6c6ec20..6342427 100644 --- a/src/obidebruinj/src/debruijn.rs +++ b/src/obidebruinj/src/debruijn.rs @@ -189,58 +189,12 @@ impl WalkState { } pub fn leavable(&self, graph: &GraphDeBruijn) -> bool { - WalkState { - kmer: self.kmer, - node: self.node, - direct: !self.direct, - } - .reachable(graph) + self.walk(graph).is_some() } pub fn reachable(&self, graph: &GraphDeBruijn) -> bool { - if self.direct { - if self.node.can_extend_left() { - let next = self.kmer.into_kmer().push_left(self.node.left_nuc()); - let cnext = next.canonical(); - let dnext = next.raw() == cnext.raw(); - let next_node = Node( - graph - .nodes - .get(&cnext) - .unwrap() - .load(std::sync::atomic::Ordering::Relaxed), - ); - !next_node.is_visited() - && if dnext { - next_node.can_extend_right() - } else { - next_node.can_extend_left() - } - } else { - false - } - } else { - if self.node.can_extend_right() { - let next = self.kmer.into_kmer().push_right(self.node.right_nuc()); - let cnext = next.canonical(); - let dnext = next.raw() == cnext.raw(); - let next_node = Node( - graph - .nodes - .get(&cnext) - .unwrap() - .load(std::sync::atomic::Ordering::Relaxed), - ); - !next_node.is_visited() - && if dnext { - next_node.can_extend_left() - } else { - next_node.can_extend_right() - } - } else { - false - } - } + WalkState { kmer: self.kmer, node: self.node, direct: !self.direct } + .leavable(graph) } pub fn walk(&self, graph: &GraphDeBruijn) -> Option {