|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.Threading; |
|
|
using System.Threading.Tasks; |
|
|
|
|
|
namespace SlobViewer.Collections.Text |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public class LongestCommonSubstringsOfStringAndStringArray |
|
|
{ |
|
|
private LongestCommonSubstringOfTwoStrings _twoStringsEvaluator = new LongestCommonSubstringOfTwoStrings(); |
|
|
private int[] _counts = new int[0]; |
|
|
private string[] _yTemp = new string[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Evaluate(string X, string[] Y, CancellationToken cancellationToken) |
|
|
{ |
|
|
if (Y.Length != _yTemp.Length) |
|
|
{ |
|
|
_yTemp = new string[Y.Length]; |
|
|
_counts = new int[Y.Length]; |
|
|
} |
|
|
|
|
|
if (cancellationToken.IsCancellationRequested) |
|
|
{ |
|
|
return; |
|
|
} |
|
|
|
|
|
Array.Copy(Y, _yTemp, Y.Length); |
|
|
|
|
|
for (int i = 0; i < Y.Length; ++i) |
|
|
{ |
|
|
_counts[i] = _twoStringsEvaluator.LongestCommonSubstring(X, Y[i]); |
|
|
if (cancellationToken.IsCancellationRequested) |
|
|
{ |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
Array.Sort(_counts, _yTemp); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public LongestCommonSubstringsOfStringAndStringArray Preallocate(int maxLengthOfFirstString, int maxLengthOfArrayStrings) |
|
|
{ |
|
|
_twoStringsEvaluator.Preallocate(maxLengthOfFirstString, maxLengthOfArrayStrings); |
|
|
return this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<(string Phrase, int CommonCharCount)> BestMatches |
|
|
{ |
|
|
get |
|
|
{ |
|
|
for (int i = _yTemp.Length - 1; i >= 0; --i) |
|
|
{ |
|
|
yield return (_yTemp[i], _counts[i]); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|