-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile2
144 lines (122 loc) · 3.42 KB
/
file2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::fs::File;
use std::io::{prelude::*, BufReader, Error, ErrorKind, Result};
fn get_lines(file1: String, file2: String) -> Result<(Vec<String>, Vec<String>)> {
let file = File::open(file1)?;
let lines = BufReader::new(file).lines();
let mut c1 = Vec::<String>::new();
for line in lines.flatten() {
c1.push(line);
}
let file = File::open(file2)?;
let lines = BufReader::new(file).lines();
let mut c2 = Vec::<String>::new();
for line in lines.flatten() {
c2.push(line);
}
Ok((c1, c2))
}
fn edit_g(s1: Vec<String>, s2: Vec<String>) -> Result<Vec<Vec<i8>>> {
// println!("Preparing edit_graph");
let m = s2.len();
let n = s1.len();
let mut mat = Vec::<Vec<i8>>::new();
let mut row: Vec<i8>;
let mut i = 0;
while i <= m {
let mut j = 0;
row = Vec::<i8>::new();
while j <= n {
if i >= m || j >= n {
row.push(0);
j += 1;
continue;
}
if let Some(t1) = s1.get(j) {
if let Some(t2) = s2.get(i) {
if *t1 == *t2 {
row.push(-1)
} else {
row.push(0)
}
} else {
return Err(Error::new(ErrorKind::Other, "new file index overflow"));
}
} else {
return Err(Error::new(ErrorKind::Other, "old file index overflow"));
}
j += 1
}
mat.push(row);
i += 1;
}
// println!("returning mat: {:?}", mat);
Ok(mat)
}
enum PathOp {
EQ,
DEL,
INS,
}
struct PathElem {
i: u32,
j: u32,
op: PathOp,
}
impl PathElem {
fn new(i: u32, j: u32, op: PathOp) -> Self {
PathElem { i, j, op }
}
}
fn ec_dist(i1: u32, j1: u32, i2: u32, j2: u32) -> u64 {
return (((i2 - i1) * (i2 - i1) + (j2 - j1) * (j2 - j1)) as f64).sqrt() as u64;
}
fn edits(s1: Vec<String>, s2: Vec<String>) -> Result<Vec<PathElem>> {
let eg = edit_g(s1.clone(), s2.clone())?;
let m = s2.len() as u32;
let n = s1.len() as u32;
let mut path = Vec::<PathElem>::new();
let mut q: Vec<(u32, u32)> = vec![(0, 0)];
while !q.is_empty() {
let (mut i, mut j) = q[0];
q.remove(0);
while eg[i as usize][j as usize] == -1 && j < n && i < m {
path.push(PathElem::new(i, j, PathOp::EQ));
i += 1;
j += 1;
}
if j >= n && i >= m {
// No movement possible
break;
}
if ec_dist(i, j + 1, m, n) <= ec_dist(i + 1, j, m, n) {
path.push(PathElem::new(i, j, PathOp::DEL));
j += 1;
} else {
path.push(PathElem::new(i, j, PathOp::INS));
i += 1;
}
q.push((i, j))
}
Ok(path)
}
fn main() -> Result<()> {
// println!("Diffing now...");
let tup = get_lines("./file1".to_string(), "./file2".to_string())?;
let c1 = tup.0;
let c2 = tup.1;
let path = edits(c1.clone(), c2.clone())?;
for pe in path.iter() {
match pe.op {
PathOp::EQ => {
println!(" {}", c1[pe.j as usize]);
}
PathOp::DEL => {
println!("- {}", c1[pe.j as usize]);
}
PathOp::INS => {
println!("+ {}", c2[pe.i as usize]);
}
};
}
Ok(())
}