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
|
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
using System;
using System.Collections.Generic;
namespace Microsoft.VisualStudio.Text.Differencing
{
#pragma warning disable CA1710 // Identifiers should have correct suffix
/// <summary>
/// Represents a range of matches between two sequences as a pair of spans of equal length.
/// </summary>
/// <remarks>
/// Given two sequences:
/// abCCd (left)
/// abFFd (right)
/// The generated pairs of matches would be:
/// (0, 0), (1, 1), (4, 4)
/// Which would turn into the Matches (left-start, right-start, length):
/// (0, 0, 2) and (4, 4, 1)
///</remarks>
public class Match : IEnumerable<Tuple<int, int>>
#pragma warning restore CA1710 // Identifiers should have correct suffix
{
private Span left;
private Span right;
/// <summary>
/// Creates a match from two spans of equal length.
/// </summary>
/// <param name="left">The span from the left sequence.</param>
/// <param name="right">The span from the right sequence.</param>
/// <exception cref="ArgumentNullException">The left span or right span is null.</exception>
/// <exception cref="ArgumentException">The spans are not of equal length.</exception>
public Match(Span left, Span right)
{
if (left.Length != right.Length)
throw new ArgumentException("Spans must be of equal length");
this.left = left;
this.right = right;
}
/// <summary>
/// Get the left-side range
/// </summary>
public Span Left
{
get { return left; }
}
/// <summary>
/// Gets the right span.
/// </summary>
public Span Right
{
get { return right; }
}
/// <summary>
/// Gets the length of the spans. Both spans have equal lengths.
/// </summary>
public int Length
{
get { return left.Length; }
}
/// <summary>
/// Determines whether two Match objects have the same left and right spans.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
Match other = obj as Match;
if(other != null)
return left.Equals(other.left) && right.Equals(other.right);
return false;
}
/// <summary>
/// Provides a hash function.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return left.GetHashCode() << 16 ^ right.GetHashCode();
}
#region IEnumerable<Tuple<int,int>> Members
/// <summary>
/// Gets an enumerator typed as a <see cref="Tuple"/> of integers.
/// </summary>
/// <returns>The typed enumerator.</returns>
public IEnumerator<Tuple<int, int>> GetEnumerator()
{
for (int i = 0; i < Length; i++)
yield return new Tuple<int, int>(left.Start + i, right.Start + i);
}
#endregion
#region IEnumerable Members
/// <summary>
/// Gets an untyped enumerator.
/// </summary>
/// <returns>The enumerator.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
|