MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / lcs_length

Function lcs_length

crates/opencode-session/src/revert.rs:330–349  ·  view source on GitHub ↗

Compute the length of the longest common subsequence of two line slices. Uses the classic DP approach with O(min(m,n)) space.

(a: &[&str], b: &[&str])

Source from the content-addressed store, hash-verified

328/// Compute the length of the longest common subsequence of two line slices.
329/// Uses the classic DP approach with O(min(m,n)) space.
330fn lcs_length(a: &[&str], b: &[&str]) -> u64 {
331 let (short, long) = if a.len() <= b.len() { (a, b) } else { (b, a) };
332 let n = short.len();
333 let mut prev = vec![0u64; n + 1];
334 let mut curr = vec![0u64; n + 1];
335
336 for long_line in long {
337 for (j, short_line) in short.iter().enumerate() {
338 if long_line == short_line {
339 curr[j + 1] = prev[j] + 1;
340 } else {
341 curr[j + 1] = curr[j].max(prev[j + 1]);
342 }
343 }
344 std::mem::swap(&mut prev, &mut curr);
345 curr.iter_mut().for_each(|v| *v = 0);
346 }
347
348 *prev.last().unwrap_or(&0)
349}
350
351impl Default for RevertManager {
352 fn default() -> Self {

Callers 1

count_line_changesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected