File size: 2,868 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SlobViewer.Collections.Text
{
  /// <summary>
  /// Determination of those strings from a string array, which have the longest common substrings with a provided string.
  /// </summary>
  public class LongestCommonSubstringsOfStringAndStringArray
  {
    private LongestCommonSubstringOfTwoStrings _twoStringsEvaluator = new LongestCommonSubstringOfTwoStrings();
    private int[] _counts = new int[0];
    private string[] _yTemp = new string[0];

    /// <summary>
    /// Evaluates the strings from array <paramref name="Y"/>, which have the longest common
    /// substrings with string <paramref name="X"/>. Those strings can then be found in <see cref="BestMatches"/>.
    /// </summary>
    /// <param name="X">The first string.</param>
    /// <param name="Y">The array of strings.</param>
    /// <param name="cancellationToken">Cancellation token.</param>
    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);
    }

    /// <summary>
    /// Preallocates storage space to help avoid repeated allocations.
    /// </summary>
    /// <param name="maxLengthOfFirstString">The maximum length of the first string.</param>
    /// <param name="maxLengthOfArrayStrings">The maximum length of the strings in the string array.</param>
    /// <returns></returns>
    public LongestCommonSubstringsOfStringAndStringArray Preallocate(int maxLengthOfFirstString, int maxLengthOfArrayStrings)
    {
      _twoStringsEvaluator.Preallocate(maxLengthOfFirstString, maxLengthOfArrayStrings);
      return this;
    }

    /// <summary>
    /// Gets the best matches.
    /// </summary>
    /// <value>
    /// The best matches. Returns tuples, containing the string, and the length of the longest common substring of this match with the search string.
    /// </value>
    public IEnumerable<(string Phrase, int CommonCharCount)> BestMatches
    {
      get
      {
        for (int i = _yTemp.Length - 1; i >= 0; --i)
        {
          yield return (_yTemp[i], _counts[i]);
        }
      }
    }
  }
}