Skip to content

Commit 4ff9401

Browse files
lil refactoring
1 parent bcd8bd8 commit 4ff9401

1 file changed

Lines changed: 12 additions & 27 deletions

File tree

src/distance/graph.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ use hashbrown::HashMap;
1212

1313
use super::{EditCosts, TreeNode, tree_distance};
1414

15-
// ============================================================================
16-
// AND-OR Graph Extension (with strict alternation)
17-
// ============================================================================
18-
1915
/// AND node: all children must be included in solution tree.
2016
/// Children must be OR nodes (or leaves).
2117
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -80,7 +76,7 @@ impl<L: Clone + Eq + Hash> AndNode<L> {
8076

8177
/// Find the solution from this AND node without memoization.
8278
fn find_min<C: EditCosts<L>>(&self, target: &TreeNode<L>, costs: &C) -> MinEditResult<L> {
83-
let result_tree = if self.is_leaf() {
79+
let tree = if self.is_leaf() {
8480
TreeNode::new(self.label.clone())
8581
} else {
8682
// For AND nodes, we must include all children.
@@ -94,11 +90,8 @@ impl<L: Clone + Eq + Hash> AndNode<L> {
9490
TreeNode::with_children(self.label.clone(), child_trees)
9591
};
9692

97-
let dist = tree_distance(&result_tree, target, costs);
98-
MinEditResult {
99-
tree: result_tree,
100-
distance: dist,
101-
}
93+
let distance = tree_distance(&tree, target, costs);
94+
MinEditResult { tree, distance }
10295
}
10396

10497
/// Find the solution from this AND node, with memoization.
@@ -113,7 +106,7 @@ impl<L: Clone + Eq + Hash> AndNode<L> {
113106
return cached.clone();
114107
}
115108

116-
let result_tree = if self.is_leaf() {
109+
let tree = if self.is_leaf() {
117110
TreeNode::new(self.label.clone())
118111
} else {
119112
// For AND nodes, we must include all children.
@@ -127,11 +120,8 @@ impl<L: Clone + Eq + Hash> AndNode<L> {
127120
TreeNode::with_children(self.label.clone(), child_trees)
128121
};
129122

130-
let dist = tree_distance(&result_tree, target, costs);
131-
let result = MinEditResult {
132-
tree: result_tree,
133-
distance: dist,
134-
};
123+
let distance = tree_distance(&tree, target, costs);
124+
let result = MinEditResult { tree, distance };
135125
cache.and_cache.insert(self, result.clone());
136126
result
137127
}
@@ -196,19 +186,16 @@ impl<L: Clone + Eq + Hash> OrNode<L> {
196186

197187
// Try each AND child and find the one with minimum edit distance
198188
for and_child in &self.children {
199-
let MinEditResult {
200-
tree: subtree,
201-
distance: _,
202-
} = and_child.find_min(target, costs);
189+
let subtree = and_child.find_min(target, costs).tree;
203190

204191
// OR node label becomes parent of the chosen subtree
205192
let candidate_tree = TreeNode::with_children(self.label.clone(), vec![subtree]);
206193

207194
// Compute actual edit distance for this solution
208-
let dist = tree_distance(&candidate_tree, target, costs);
195+
let distance = tree_distance(&candidate_tree, target, costs);
209196

210-
if dist < best_distance {
211-
best_distance = dist;
197+
if distance < best_distance {
198+
best_distance = distance;
212199
best_tree = Some(candidate_tree);
213200
}
214201
if best_distance == 0 {
@@ -239,10 +226,7 @@ impl<L: Clone + Eq + Hash> OrNode<L> {
239226

240227
// Try each AND child and find the one with minimum edit distance
241228
for and_child in &self.children {
242-
let MinEditResult {
243-
tree: subtree,
244-
distance: _,
245-
} = and_child.find_min_memo(target, costs, cache);
229+
let subtree = and_child.find_min_memo(target, costs, cache).tree;
246230

247231
// OR node label becomes parent of the chosen subtree
248232
let candidate_tree = TreeNode::with_children(self.label.clone(), vec![subtree]);
@@ -274,6 +258,7 @@ impl<L: Clone + Eq + Hash> OrNode<L> {
274258
///
275259
/// Uses pointer addresses as keys - if the same node (by address) appears
276260
/// multiple times in the graph, it will produce the same solution tree.
261+
#[derive(Debug, Clone)]
277262
struct MemoCache<'a, L: Clone + Eq + Hash> {
278263
/// Cache for OR nodes: maps node pointer -> (`best_solution_tree`, `min_distance`)
279264
or_cache: HashMap<&'a OrNode<L>, MinEditResult<L>>,

0 commit comments

Comments
 (0)