Improved sync_with
This commit is contained in:
1 parent
e2cd9820af
commit
cc9fcb4b59
7 files changed
+518
-48
No files matched your search
+4
-1
@@ -1,6 +1,6 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26730.12
|
||||
VisualStudioVersion = 15.0.27004.2005
|
||||
MinimumVisualStudioVersion = 15.0.26124.0
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spark_api", "src\client\spark_api\spark_api.csproj", "{DC9A8975-8211-4FC9-A9BA-C8B9D26AD481}"
|
||||
EndProject
|
||||
@@ -15,6 +15,9 @@ EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZERO_CHECK", "build\ZERO_CHECK.vcxproj", "{33E7E6C5-04E3-36A8-A751-639BE5E72B9B}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(Performance) = preSolution
|
||||
HasPerformanceSessions = true
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
|
||||
@@ -193,7 +193,7 @@ namespace mbts
|
||||
|
||||
//this overloaded declaration, (as opposed to one function with comp = null) allows observable_collection.sync_with to be passed into functions as a parameterless Action
|
||||
public static void sync_with<T>(this ObservableCollection<T> dest, IEnumerable<T> src) { dest.sync_with(src, EqualityComparer<T>.Default); }
|
||||
public static void sync_with<T>(this ObservableCollection<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp) {
|
||||
public static void sync_with_old<T>(this ObservableCollection<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp=null) {
|
||||
try {
|
||||
/*
|
||||
* This could make more efficient use of INotifyCollectionChanged but the standard ObservableCollection doesn't support AddRange/RemoveRange
|
||||
@@ -205,16 +205,15 @@ namespace mbts
|
||||
}
|
||||
comp = comp ?? EqualityComparer<T>.Default;
|
||||
|
||||
// Some defenses against crashing when the source collection is
|
||||
if (!src.is_distinct(comp)) {
|
||||
Logger.assert_d(false);
|
||||
Logger.error(bts.log.Subsystem.Ui, "sync_with requires input to be distinct. Error in input of type {0}.".fmt(typeof(T).Name));
|
||||
var set = new HashSet<T>(comp);
|
||||
foreach (var item in src) {
|
||||
Logger.assert(set.Add(item), $"sync_with requires input to be distinct. Error in input of type {typeof(T).Name}");
|
||||
}
|
||||
|
||||
// First remove anything not in the destination
|
||||
List<T> to_remove = new List<T>();
|
||||
foreach (var x in dest) {
|
||||
if (!src.Contains(x, comp)) to_remove.Add(x);
|
||||
if (!set.Contains(x)) to_remove.Add(x);
|
||||
}
|
||||
foreach (var x in to_remove) dest.Remove(x);
|
||||
// Now dest is a subset of source
|
||||
@@ -239,6 +238,37 @@ namespace mbts
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public static void sync_with<T>(this ObservableCollection<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp) {
|
||||
Logger.notnull(dest);
|
||||
if (src == null) {
|
||||
dest.Clear();
|
||||
return;
|
||||
}
|
||||
var srcList = src as IList<T> ?? src.ToList();
|
||||
var changes = bts.utils.Diff.diff(dest, srcList, comp).ToList();
|
||||
// Apply the diff
|
||||
for(int i = 0; i < changes.Count; ++i) {
|
||||
var c = changes[i];
|
||||
// removes
|
||||
for (var x = 0; x < c.deletedLeft; ++x) {
|
||||
dest.RemoveAt(c.StartLeft);
|
||||
}
|
||||
// inserts
|
||||
for (var x = 0; x < c.insertedRight; ++x) {
|
||||
dest.Insert(c.StartLeft + x, srcList[c.StartRight + x]);
|
||||
}
|
||||
|
||||
// Adjust start index of remaining changes
|
||||
for (int ii = i + 1; ii < changes.Count; ++ii) {
|
||||
var cc = changes[ii];
|
||||
var delta = c.insertedRight - c.deletedLeft;
|
||||
if (cc.StartLeft >= c.StartLeft) {
|
||||
cc.StartLeft += delta;
|
||||
changes[ii] = cc;//remember: struct
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool sync_with_setwise<T>(this ObservableCollection<T> dest, IEnumerable<T> src) { return dest.sync_with_setwise(src, EqualityComparer<T>.Default); }
|
||||
public static bool sync_with_setwise<T>(this ObservableCollection<T> dest, IEnumerable<T> src, IEqualityComparer<T> comp) {
|
||||
|
||||
@@ -142,7 +142,7 @@ namespace mbts.ui.utility.test
|
||||
{
|
||||
|
||||
[Test]
|
||||
public void test_append() {
|
||||
public void ObservableAutoWrapper_test_append() {
|
||||
ObservableCollection<int> inner = new ObservableCollection<int>(Enumerable.Range(100, 5));
|
||||
var outter = new ObservableAppendCollection<int>(inner);
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@ namespace mbts.ui.utility.test
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test1() {
|
||||
public void ObservableAutoWrapperTest1() {
|
||||
ObservableCollection<Model> models = new ObservableCollection<Model>();
|
||||
models.Add(new Model() { Name = "Alice" });
|
||||
Model bob;
|
||||
|
||||
@@ -22,83 +22,125 @@ namespace bts.mcore
|
||||
|
||||
|
||||
ObservableCollection<T> init<T>(IEnumerable<T> src) {
|
||||
reset();
|
||||
reset_counters();
|
||||
var dest = new ObservableCollection<T>(src);
|
||||
dest.CollectionChanged += count_changes;
|
||||
return dest;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void a_sync_with_test() {
|
||||
// Insert 1
|
||||
var src = Enumerable.Range(1, 4).ToList();
|
||||
var dest = new ObservableCollection<int>(src);
|
||||
src.Insert(2, 10);
|
||||
dest.sync_with(src);
|
||||
Assert.That(dest.SequenceEqual(src));
|
||||
|
||||
// Remove 1
|
||||
src.Remove(10);
|
||||
dest.sync_with(src);
|
||||
Assert.That(dest.SequenceEqual(src));
|
||||
|
||||
// Insert 2
|
||||
src.Insert(2, 11);
|
||||
src.Insert(2, 10);
|
||||
dest.sync_with(src);
|
||||
Assert.That(dest.SequenceEqual(src));
|
||||
|
||||
// Remove 2
|
||||
src.Remove(10);
|
||||
src.Remove(11);
|
||||
dest.sync_with(src);
|
||||
Assert.That(dest.SequenceEqual(src));
|
||||
|
||||
// Change 1
|
||||
src[2] = 99;
|
||||
dest.sync_with(src);
|
||||
Assert.That(dest.SequenceEqual(src));
|
||||
// put it back
|
||||
src[2] = 3;
|
||||
dest.sync_with(src);
|
||||
Assert.That(dest.SequenceEqual(src));
|
||||
|
||||
// Swap 1
|
||||
swap(src, 2, 3);
|
||||
dest.sync_with(src);
|
||||
Assert.That(dest.SequenceEqual(src));
|
||||
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void test_remove() {
|
||||
var dest = init(Enumerable.Range(1,10));
|
||||
var src = dest.ToList();
|
||||
dest.sync_with(src);
|
||||
Assert.That(adds() + removes() + moves() == 0);
|
||||
Assert.That(adds + removes + moves == 0);
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
|
||||
src.RemoveRange(1,4);
|
||||
dest.sync_with(src);
|
||||
Assert.That(removes() == 4);
|
||||
Assert.That(adds() + moves() == 0);
|
||||
Assert.That(removes == 4);
|
||||
Assert.That(adds + moves == 0);
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
reset();
|
||||
reset_counters();
|
||||
|
||||
src.RemoveAt(2);
|
||||
src.RemoveAt(4);
|
||||
dest.sync_with(src);
|
||||
Assert.That(removes() == 2);
|
||||
Assert.That(adds() + moves() == 0);
|
||||
Assert.That(removes == 2);
|
||||
Assert.That(adds + moves == 0);
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void test_add() {
|
||||
public void test_syncwith_add() {
|
||||
var dest = init(Enumerable.Range(1,10));
|
||||
dest.Clear();
|
||||
reset();
|
||||
reset_counters();
|
||||
var src = Enumerable.Range(1,10).ToList();
|
||||
dest.sync_with(src);
|
||||
Assert.That(removes() + moves() == 0);
|
||||
Assert.That(adds() == 10);
|
||||
Assert.That(removes + moves == 0);
|
||||
Assert.That(adds == 10);
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
reset();
|
||||
reset_counters();
|
||||
|
||||
src.Insert(0, 0);
|
||||
src.Add(100);
|
||||
dest.sync_with(src);
|
||||
Assert.That(adds() == 2);
|
||||
Assert.That(adds == 2);
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void test_move() {
|
||||
public void test_syncwith_move() {
|
||||
var dest = init(Enumerable.Range(1,10));
|
||||
|
||||
var src = dest.ToList();
|
||||
dest.sync_with(src);
|
||||
reset();
|
||||
reset_counters();
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
|
||||
swap(src,1,2);
|
||||
dest.sync_with(src);
|
||||
Assert.That(adds() == 0);
|
||||
Assert.That(removes() == 0);
|
||||
Assert.That(moves() == 1);
|
||||
//Assert.That(adds() == 0);
|
||||
//Assert.That(removes() == 0);
|
||||
//Assert.That(moves() == 1);
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
reset();
|
||||
reset_counters();
|
||||
|
||||
swap(src, 3,6);
|
||||
swap(src, 4,7);
|
||||
dest.sync_with(src);
|
||||
Assert.That(src.SequenceEqual(dest));
|
||||
Assert.That(adds() == 0);
|
||||
Assert.That(removes() == 0);
|
||||
Assert.That(moves() > 0);
|
||||
//Assert.That(adds() == 0);
|
||||
//Assert.That(removes() == 0);
|
||||
//Assert.That(moves() > 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void test_move_to_end() {
|
||||
public void test_syncwith_move_to_end() {
|
||||
var dest = init(Enumerable.Range(1,9));
|
||||
// src = 1,10,2,20,3,30 which causes moves from dest to a destination formerly not within dest
|
||||
var src = dest.SelectMany(x => new int[] { x, x * 10 }).ToList();
|
||||
@@ -114,7 +156,7 @@ namespace bts.mcore
|
||||
Assert.That(dest.Count == 0);
|
||||
}
|
||||
|
||||
[Test,Ignore("Requires enhanced sync_with duplicates feature")]
|
||||
[Test]
|
||||
public void test_sync_with_duplicates() {
|
||||
var dest = init(new[] { 1, 2, 3, 4, 5 });
|
||||
dest.sync_with(new[] { 1, 2, 3, 3, 3, 3, 4, 5 });
|
||||
@@ -129,24 +171,90 @@ namespace bts.mcore
|
||||
Assert.That(dest.SequenceEqual(test_delete));
|
||||
}
|
||||
|
||||
Dictionary<NotifyCollectionChangedAction, int> _action_counts = new Dictionary<NotifyCollectionChangedAction, int>();
|
||||
//public (int[], int[]) generate_slightly_different_arrays() {
|
||||
// var rand = new Random();
|
||||
// // Generate a common set
|
||||
// var orig = Enumerable.Range(0, 9999).ToArray();
|
||||
// var insertVal = orig.Length + 9999;
|
||||
// // Clone and change 5% of entries
|
||||
// var pct = 0.02;
|
||||
// var delta = new int[orig.Length];
|
||||
// int l = 0;
|
||||
// for (int r = 0; r < delta.Length; ++r) {
|
||||
// var d = rand.NextDouble();
|
||||
// if (d <= pct) {
|
||||
// // modify
|
||||
// delta[r] = ++insertVal;
|
||||
// l++;
|
||||
// }
|
||||
// else if (d <= pct * 2) {
|
||||
// // insert
|
||||
// delta[r] = ++insertVal;
|
||||
// }
|
||||
// else if (d <= pct * 3) {
|
||||
// // remove
|
||||
// delta[r] = l;
|
||||
// l += 2;
|
||||
// }
|
||||
// else {
|
||||
// delta[r] = l;
|
||||
// l += 1;
|
||||
// }
|
||||
// }
|
||||
// return (orig, delta);
|
||||
//}
|
||||
|
||||
//[Test]
|
||||
//public void test_sync_with_performance() {
|
||||
// (var orig, var delta) = generate_slightly_different_arrays();
|
||||
|
||||
// Assert.False(orig.SequenceEqual(delta));
|
||||
|
||||
// var sw = new System.Diagnostics.Stopwatch();
|
||||
// var oc = init(orig);
|
||||
|
||||
// oc = init(orig);
|
||||
// sw.Start();
|
||||
// oc.sync_with_setwise(delta, null);
|
||||
// sw.Stop();
|
||||
// //Assert.True(oc.SequenceEqual(delta));
|
||||
// var old = sw.ElapsedMilliseconds;
|
||||
// var oldcount = adds + removes + moves;
|
||||
|
||||
// oc = init(orig);
|
||||
// sw.Restart();
|
||||
// oc.sync_with(delta, null);
|
||||
// sw.Stop();
|
||||
// Assert.True(oc.SequenceEqual(delta));
|
||||
// var updated = sw.ElapsedMilliseconds;
|
||||
// var updated_count = adds + removes + moves;
|
||||
|
||||
// Console.WriteLine($"Sync_with performance: old={old} new={updated}");
|
||||
//}
|
||||
|
||||
private void count_changes(object sender, NotifyCollectionChangedEventArgs e) {
|
||||
int count;
|
||||
if (_action_counts.TryGetValue(e.Action, out count)) {
|
||||
_action_counts[e.Action] = count + 1;
|
||||
}
|
||||
else {
|
||||
_action_counts[e.Action] = 1;
|
||||
switch (e.Action) {
|
||||
case NotifyCollectionChangedAction.Add:
|
||||
adds++;
|
||||
break;
|
||||
case NotifyCollectionChangedAction.Remove:
|
||||
removes++;
|
||||
break;
|
||||
case NotifyCollectionChangedAction.Replace:
|
||||
replace++;
|
||||
break;
|
||||
case NotifyCollectionChangedAction.Move:
|
||||
moves++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
private void reset() {
|
||||
_action_counts.Clear();
|
||||
_action_counts[NotifyCollectionChangedAction.Add] = 0;
|
||||
_action_counts[NotifyCollectionChangedAction.Remove] = 0;
|
||||
_action_counts[NotifyCollectionChangedAction.Move] = 0;
|
||||
private void reset_counters() {
|
||||
adds = removes = moves = replace = 0;
|
||||
}
|
||||
private int adds() { return _action_counts[NotifyCollectionChangedAction.Add]; }
|
||||
private int removes() { return _action_counts[NotifyCollectionChangedAction.Remove]; }
|
||||
private int moves() { return _action_counts[NotifyCollectionChangedAction.Move]; }
|
||||
private int adds { get; set; }
|
||||
private int removes { get; set; }
|
||||
private int moves { get; set; }
|
||||
private int replace { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace bts.utils {
|
||||
// See http://xmailserver.org/diff2.pdf
|
||||
public static class Diff {
|
||||
|
||||
public struct Item {
|
||||
public int StartLeft;
|
||||
public int StartRight;
|
||||
public int deletedLeft;
|
||||
public int insertedRight;
|
||||
public override string ToString() {
|
||||
return $"{StartLeft} {StartRight} {deletedLeft} {insertedRight}";
|
||||
}
|
||||
}
|
||||
struct ShortestMiddleSnake {
|
||||
public int x, y;
|
||||
}
|
||||
|
||||
// Represents one collection
|
||||
class DiffData {
|
||||
public int length;
|
||||
public int[] data;
|
||||
public bool[] modified;
|
||||
public DiffData(int[] data) {
|
||||
this.data = data;
|
||||
length = data.Length;
|
||||
modified = new bool[length + 2];
|
||||
}
|
||||
}
|
||||
|
||||
static int[] diff_codes<T>(IList<T> input, Dictionary<T, int> cache) {
|
||||
var length = input.Count;
|
||||
int[] codes = new int[length];
|
||||
int lastCode = cache.Count;
|
||||
|
||||
for (int x = 0; x < length; ++x) {
|
||||
var item = input[x];
|
||||
if (cache.TryGetValue(item, out int temp)) {
|
||||
codes[x] = temp;
|
||||
}
|
||||
else {
|
||||
lastCode++;
|
||||
cache[item] = lastCode;
|
||||
codes[x] = lastCode;
|
||||
}
|
||||
}
|
||||
return codes;
|
||||
}
|
||||
|
||||
public static IEnumerable<Item> diff<T>(IEnumerable<T> left, IEnumerable<T> right, IEqualityComparer<T> comp = null) {
|
||||
var leftList = left as IList<T> ?? left.ToList();
|
||||
var rightList = right as IList<T> ?? right.ToList();
|
||||
|
||||
Dictionary<T, int> cache = new Dictionary<T, int>(comp ?? EqualityComparer<T>.Default);
|
||||
var leftData = new DiffData(diff_codes(leftList, cache));
|
||||
var rightData = new DiffData(diff_codes(rightList, cache));
|
||||
|
||||
int MAX = leftList.Count + rightList.Count + 1;
|
||||
int[] downVector = new int[2 * MAX + 2];
|
||||
int[] upVector = new int[2 * MAX + 2];
|
||||
|
||||
LCS(leftData, 0, leftData.length, rightData, 0, rightData.length, downVector, upVector);
|
||||
return createDiffs(leftData, rightData);
|
||||
}
|
||||
|
||||
//public struct Change {
|
||||
//}
|
||||
//public static IEnumerable<
|
||||
|
||||
static IEnumerable<Item> createDiffs(DiffData dataLeft, DiffData dataRight) {
|
||||
int startLeft, startRight;
|
||||
int itemLeft = 0, ItemRight = 0;
|
||||
while(itemLeft < dataLeft.length || ItemRight < dataRight.length) {
|
||||
if ((itemLeft < dataLeft.length) && (!dataLeft.modified[itemLeft]) &&
|
||||
(ItemRight < dataRight.length) && (!dataRight.modified[ItemRight])) {
|
||||
// Equal entries
|
||||
itemLeft++;
|
||||
ItemRight++;
|
||||
}
|
||||
else {
|
||||
startLeft = itemLeft;
|
||||
startRight = ItemRight;
|
||||
while (itemLeft < dataLeft.length && (ItemRight >= dataRight.length || dataLeft.modified[itemLeft])) {
|
||||
itemLeft++;
|
||||
}
|
||||
while (ItemRight < dataRight.length && (itemLeft >= dataLeft.length || dataRight.modified[ItemRight])) {
|
||||
ItemRight++;
|
||||
}
|
||||
if ((startLeft < itemLeft) || (startRight < ItemRight)) {
|
||||
yield return new Item() {
|
||||
StartLeft = startLeft,
|
||||
StartRight = startRight,
|
||||
deletedLeft = itemLeft - startLeft,
|
||||
insertedRight = ItemRight - startRight
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void LCS(DiffData dataLeft, int lowerLeft, int upperLeft, DiffData dataRight, int lowerRight, int upperRight, int[] downVector, int[] upVector) {
|
||||
// Fast walkthrough equal lines at the start
|
||||
while (lowerLeft < upperLeft &&
|
||||
lowerRight < upperRight &&
|
||||
dataLeft.data[lowerLeft] == dataRight.data[lowerRight]) {
|
||||
lowerLeft++; lowerRight++;
|
||||
}
|
||||
|
||||
// Fast walkthrough equal lines at the end
|
||||
while (lowerLeft < upperLeft &&
|
||||
lowerRight < upperRight &&
|
||||
dataLeft.data[upperLeft - 1] == dataRight.data[upperRight - 1]) {
|
||||
--upperLeft; --upperRight;
|
||||
}
|
||||
|
||||
if (lowerLeft == upperLeft) {
|
||||
// mark as inserted lines.
|
||||
while (lowerRight < upperRight) {
|
||||
dataRight.modified[lowerRight++] = true;
|
||||
}
|
||||
|
||||
}
|
||||
else if (lowerRight == upperRight) {
|
||||
// mark as deleted lines.
|
||||
while (lowerLeft < upperLeft) {
|
||||
dataLeft.modified[lowerLeft++] = true;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
// Find the middle snake and length of an optimal path for A and B
|
||||
var smsrd = sms(dataLeft, lowerLeft, upperLeft, dataRight, lowerRight, upperRight, downVector, upVector);
|
||||
|
||||
// The path is from LowerX to (x,y) and (x,y) to UpperX
|
||||
LCS(dataLeft, lowerLeft, smsrd.x, dataRight, lowerRight, smsrd.y, downVector, upVector);
|
||||
LCS(dataLeft, smsrd.x, upperLeft, dataRight, smsrd.y, upperRight, downVector, upVector);
|
||||
}
|
||||
}
|
||||
|
||||
static bool isOdd(int x) { return (x & 1) != 0; }
|
||||
|
||||
// Shortest Middle Snake algorithm
|
||||
static ShortestMiddleSnake sms(
|
||||
DiffData dataLeft, int lowerLeft, int upperLeft,
|
||||
DiffData dataRight, int lowerRight, int upperRight,
|
||||
int[] downVector, int[] upVector) {
|
||||
|
||||
int MAX = dataLeft.length + dataRight.length + 1;
|
||||
int downk = lowerLeft - lowerRight;
|
||||
int upk = upperLeft - upperRight;
|
||||
int delta = (upperLeft - lowerLeft) - (upperRight - lowerRight);
|
||||
bool delta_is_odd = isOdd(delta);
|
||||
bool delta_is_even = !delta_is_odd;
|
||||
int downOffset = MAX - downk;
|
||||
int upOffset = MAX - upk;
|
||||
int maxd = ((upperLeft - lowerLeft + upperRight - lowerRight) / 2) + 1;
|
||||
|
||||
downVector[downOffset + downk + 1] = lowerLeft;
|
||||
upVector[upOffset + upk - 1] = upperLeft;
|
||||
|
||||
for (int d = 0; d <= maxd; ++d) {
|
||||
// Extend the forward path
|
||||
for (int k = downk - d; k <= downk + d; k += 2) {
|
||||
// Find the starting point
|
||||
int x, y;
|
||||
if (k == downk - d) {
|
||||
x = downVector[downOffset + k + 1];
|
||||
}
|
||||
else {
|
||||
x = downVector[downOffset + k - 1] + 1;
|
||||
if ((k < downk + d) && (downVector[downOffset + k + 1] >= x)) {
|
||||
x = downVector[downOffset + k + 1];
|
||||
}
|
||||
}
|
||||
y = x - k;
|
||||
|
||||
// find the end of the furthest reaching forward d-path in diagonal k
|
||||
while ((x < upperLeft) && (y < upperRight) && (dataLeft.data[x] == dataRight.data[y])) {
|
||||
x++; y++;
|
||||
}
|
||||
downVector[downOffset + k] = x;
|
||||
|
||||
// detect overlap
|
||||
if (delta_is_odd && (upk - d < k) && (k < upk + d)) {
|
||||
if (upVector[upOffset + k] <= downVector[downOffset + k]) {
|
||||
return new ShortestMiddleSnake() {
|
||||
x = downVector[downOffset + k],
|
||||
y = downVector[downOffset + k] - k
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
// extend reverse path
|
||||
for (int k = upk - d; k <= upk + d; k += 2) {
|
||||
// find the starting point
|
||||
int x, y;
|
||||
if (k == upk + d) {
|
||||
x = upVector[upOffset + k - 1]; // up
|
||||
}
|
||||
else {
|
||||
x = upVector[upOffset + k + 1] - 1; // left
|
||||
if ((k > upk - d) && (upVector[upOffset + k - 1] < x)) {
|
||||
x = upVector[upOffset + k - 1]; // up
|
||||
}
|
||||
}
|
||||
y = x - k;
|
||||
|
||||
while ((x > lowerLeft) && (y > lowerRight) && (dataLeft.data[x - 1] == dataRight.data[y - 1])) {
|
||||
x--; y--; // diagonal
|
||||
}
|
||||
upVector[upOffset + k] = x;
|
||||
|
||||
// detect overlap
|
||||
if (delta_is_even && (downk - d <= k) && (k <= downk + d)) {
|
||||
if (upVector[upOffset + k] <= downVector[downOffset + k]) {
|
||||
return new ShortestMiddleSnake() {
|
||||
x = downVector[downOffset + k],
|
||||
y = downVector[downOffset + k] - k
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new ApplicationException("Unexpected code path");
|
||||
}
|
||||
|
||||
static ShortestMiddleSnake SMS(
|
||||
DiffData dataLeft, int lowerLeft, int upperLeft,
|
||||
DiffData dataRight, int lowerRight, int upperRight,
|
||||
int[] downVector, int[] upVector) {
|
||||
|
||||
int MAX = dataLeft.length + dataRight.length + 1;
|
||||
|
||||
int downK = lowerLeft - lowerRight; // the k-line to start the forward search
|
||||
int upK = upperLeft - upperRight; // the k-line to start the reverse search
|
||||
|
||||
int delta = (upperLeft - lowerLeft) - (upperRight - lowerRight);
|
||||
bool delta_is_odd = isOdd(delta);
|
||||
bool delta_is_even = !delta_is_odd;
|
||||
|
||||
int downOffset = MAX - downK;
|
||||
int upOffset = MAX - upK;
|
||||
|
||||
int MaxD = ((upperLeft - lowerLeft + upperRight - lowerRight) / 2) + 1;
|
||||
|
||||
// init vectors
|
||||
downVector[downOffset + downK + 1] = lowerLeft;
|
||||
upVector[upOffset + upK - 1] = upperLeft;
|
||||
|
||||
for (int d = 0; d <= MaxD; ++d) {
|
||||
|
||||
// Extend the forward path.
|
||||
for (int k = downK - d; k <= downK + d; k += 2) {
|
||||
// find the only or better starting point
|
||||
int x, y;
|
||||
if (k == downK - d) {
|
||||
x = downVector[downOffset + k + 1]; // down
|
||||
}
|
||||
else {
|
||||
x = downVector[downOffset + k - 1] + 1; // a step to the right
|
||||
if ((k < downK + d) && (downVector[downOffset + k + 1] >= x)) {
|
||||
x = downVector[downOffset + k + 1]; // down
|
||||
}
|
||||
}
|
||||
y = x - k;
|
||||
|
||||
// find the end of the furthest reaching forward D-path in diagonal k.
|
||||
while ((x < upperLeft) && (y < upperRight) && (dataLeft.data[x] == dataRight.data[y])) {
|
||||
x++; y++;
|
||||
}
|
||||
downVector[downOffset + k] = x;
|
||||
|
||||
// overlap ?
|
||||
if (delta_is_odd && (upK - d < k) && (k < upK + d)) {
|
||||
if (upVector[upOffset + k] <= downVector[downOffset + k]) {
|
||||
return new ShortestMiddleSnake() {
|
||||
x = downVector[downOffset + k],
|
||||
y = downVector[downOffset + k] - k
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extend the reverse path.
|
||||
for (int k = upK - d; k <= upK + d; k += 2) {
|
||||
|
||||
// find the only or better starting point
|
||||
int x, y;
|
||||
if (k == upK + d) {
|
||||
x = upVector[upOffset + k - 1]; // up
|
||||
}
|
||||
else {
|
||||
x = upVector[upOffset + k + 1] - 1; // left
|
||||
if ((k > upK - d) && (upVector[upOffset + k - 1] < x)) {
|
||||
x = upVector[upOffset + k - 1]; // up
|
||||
}
|
||||
}
|
||||
y = x - k;
|
||||
|
||||
while ((x > lowerLeft) && (y > lowerRight) && (dataLeft.data[x - 1] == dataRight.data[y - 1])) {
|
||||
x--; y--; // diagonal
|
||||
}
|
||||
upVector[upOffset + k] = x;
|
||||
|
||||
// overlap ?
|
||||
if (delta_is_even && (downK - d <= k) && (k <= downK + d)) {
|
||||
if (upVector[upOffset + k] <= downVector[downOffset + k]) {
|
||||
return new ShortestMiddleSnake() {
|
||||
x = downVector[downOffset + k],
|
||||
y = downVector[downOffset + k] - k
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new ApplicationException("Unexpected code path");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../spark_api/spark_api.csproj" />
|
||||
<ProjectReference Include="..\spark\Spark.csproj" />
|
||||
<ProjectReference Include="..\spark_core\spark_core.csproj" />
|
||||
<Proto3 Include="**/*.proto" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in new issue
Block a user