Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Difference.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5
7{
11 public static class Difference
12 {
27 public static EditScript<T> Analyze<T>(T[] S1, T[] S2)
28 {
29 int c1 = S1.Length;
30 int c2 = S2.Length;
31 int c1p = c1 + 1;
32 int c2p = c2 + 1;
33 BitArray Processed = new BitArray(c1p * c2p);
34 LinkedList<State<T>> Current = new LinkedList<State<T>>();
35 LinkedList<State<T>> Next = new LinkedList<State<T>>();
36 LinkedList<State<T>> NextNext = new LinkedList<State<T>>();
37 LinkedList<State<T>> Temp;
38 State<T> P, Q;
39 bool b1, b2;
40 int i;
41
42 P = new State<T>()
43 {
44 Op = EditOperation.Keep,
45 i1 = 0,
46 i2 = 0,
47 Prev = null
48 };
49
50 while (true)
51 {
52 if (P.i1 == c1 && P.i2 == c2)
53 break;
54
55 i = P.i1 + P.i2 * c1p;
56 if (!Processed[i])
57 {
58 Processed[i] = true;
59
60 if (b2 = (P.i2 < c2))
61 {
62 if (!Processed[i + c1p])
63 {
64 Q = new State<T>()
65 {
66 Op = EditOperation.Insert,
67 i1 = P.i1,
68 i2 = P.i2 + 1,
69 Prev = P
70 };
71
72 if (P.Op == EditOperation.Insert)
73 Next.AddLast(Q);
74 else
75 NextNext.AddLast(Q);
76 }
77 }
78
79 if (b1 = (P.i1 < c1))
80 {
81 if (!Processed[i + 1])
82 {
83 Q = new State<T>()
84 {
85 Op = EditOperation.Delete,
86 i1 = P.i1 + 1,
87 i2 = P.i2,
88 Prev = P
89 };
90
91 if (P.Op == EditOperation.Delete)
92 Next.AddLast(Q);
93 else
94 NextNext.AddLast(Q);
95 }
96 }
97
98 if (b1 && b2 && S1[P.i1].Equals(S2[P.i2]))
99 {
100 if (!Processed[i + 1 + c1p])
101 {
102 Q = new State<T>()
103 {
104 Op = EditOperation.Keep,
105 i1 = P.i1 + 1,
106 i2 = P.i2 + 1,
107 Prev = P
108 };
109
110 Current.AddLast(Q);
111
112 if (Q.i1 == c1 && Q.i2 == c2)
113 {
114 P = Q;
115 break;
116 }
117 }
118 }
119 }
120
121 if (Current.First is null)
122 {
123 Temp = Current;
124
125 if (Next.First is null)
126 Current = NextNext;
127 else
128 {
129 Current = Next;
130 Next = NextNext;
131 }
132
133 NextNext = Temp;
134 }
135
136 P = Current.Last.Value;
137 Current.RemoveLast();
138 }
139
140 LinkedList<T> SameOp = new LinkedList<T>();
141 LinkedList<Step<T>> Steps = new LinkedList<Step<T>>();
142 EditOperation Op = P.Op;
143 State<T> First = P;
144 T[] Symbols;
145
146 c1 = 0;
147 c2 = 0;
148
149 while (!((Q = P.Prev) is null))
150 {
151 if (P.Op != Op)
152 {
153 Symbols = new T[c1];
154 SameOp.CopyTo(Symbols, 0);
155 Steps.AddFirst(new Step<T>(Symbols, First.i1, First.i2, Op));
156
157 SameOp.Clear();
158 c1 = 0;
159 c2++;
160 Op = P.Op;
161 }
162
163 switch (Op)
164 {
165 case EditOperation.Keep:
166 case EditOperation.Delete:
167 SameOp.AddFirst(S1[P.i1 - 1]);
168 break;
169
170 case EditOperation.Insert:
171 SameOp.AddFirst(S2[P.i2 - 1]);
172 break;
173 }
174
175 c1++;
176 First = P;
177 P = Q;
178 }
179
180 Symbols = new T[c1];
181 SameOp.CopyTo(Symbols, 0);
182 Steps.AddFirst(new Step<T>(Symbols, First.i1, First.i2, Op));
183 c2++;
184
185 Step<T>[] Result = new Step<T>[c2];
186 Steps.CopyTo(Result, 0);
187
188 return new EditScript<T>(S1, S2, Result);
189 }
190
191 private class State<T>
192 {
193 public EditOperation Op;
194 public int i1;
195 public int i2;
196 public State<T> Prev;
197 }
198
205 public static EditScript<char> AnalyzeStrings(string s1, string s2)
206 {
207 return Analyze(s1.ToCharArray(), s2.ToCharArray());
208 }
209
216 public static EditScript<string> AnalyzeRows(string Text1, string Text2)
217 {
218 return Analyze(ExtractRows(Text1), ExtractRows(Text2));
219 }
220
226 public static string[] ExtractRows(string Text)
227 {
228 return Text.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
229 }
230
231 }
232}
Computes the difference between two sequences of symbols.
Definition: Difference.cs:12
static EditScript< string > AnalyzeRows(string Text1, string Text2)
Analyzes two texts, estimating the difference between them, as a sequence of rows.
Definition: Difference.cs:216
static string[] ExtractRows(string Text)
Extracts the rows from a text.
Definition: Difference.cs:226
static EditScript< char > AnalyzeStrings(string s1, string s2)
Analyzes two text strings, estimating the difference between them.
Definition: Difference.cs:205
static EditScript< T > Analyze< T >(T[] S1, T[] S2)
Analyzes two sequences of symbols to estimate the difference between them.
Definition: Difference.cs:27
Represents an Edit-script, converting one sequence of symbols to another.
Definition: EditScript.cs:11
Represents a sub-sequence of symbols.
Definition: Step.cs:12
EditOperation
Type of edit-operation