Skip to content

Commit

Permalink
Require IEquatable<T> on Span methods to avoid boxing; use SequenceEq…
Browse files Browse the repository at this point in the history
…ual instead of == equality. #35
  • Loading branch information
paulirwin committed Aug 31, 2023
1 parent abd0383 commit 8a49b7f
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/F23.StringSimilarity/Damerau.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public double Distance(string s1, string s2)
=> Distance(s1.AsSpan(), s2.AsSpan());

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand All @@ -68,7 +69,7 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
throw new ArgumentNullException(nameof(s2));
}

if (s1 == s2)
if (s1.SequenceEqual(s2))
{
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/F23.StringSimilarity/Interfaces/IMetricSpanDistance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IMetricSpanDistance : ISpanDistance
/// <param name="b1">The first span.</param>
/// <param name="b2">The second span.</param>
/// <returns>The metric distance.</returns>
new double Distance<T>(ReadOnlySpan<T> b1, ReadOnlySpan<T> b2);
new double Distance<T>(ReadOnlySpan<T> b1, ReadOnlySpan<T> b2)
where T : IEquatable<T>;
}
}
3 changes: 2 additions & 1 deletion src/F23.StringSimilarity/Interfaces/ISpanDistance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface ISpanDistance
/// <param name="b1">The first span.</param>
/// <param name="b2">The second span.</param>
/// <returns>The measure of distance between the spans.</returns>
double Distance<T>(ReadOnlySpan<T> b1, ReadOnlySpan<T> b2);
double Distance<T>(ReadOnlySpan<T> b1, ReadOnlySpan<T> b2)
where T : IEquatable<T>;
}
}
3 changes: 2 additions & 1 deletion src/F23.StringSimilarity/Interfaces/ISpanSimilarity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface ISpanSimilarity
/// <param name="s1">The first span</param>
/// <param name="s2">The second span</param>
/// <returns>Similarity (0 means both spans are completely different)</returns>
double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2);
double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>;
}
}
5 changes: 4 additions & 1 deletion src/F23.StringSimilarity/JaroWinkler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public double Similarity(string s1, string s2)
=> Similarity(s1.AsSpan(), s2.AsSpan());

public double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand All @@ -90,7 +91,7 @@ public double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
throw new ArgumentNullException(nameof(s2));
}

if (s1 == s2)
if (s1.SequenceEqual(s2))
{
return 1f;
}
Expand Down Expand Up @@ -123,9 +124,11 @@ public double Distance(string s1, string s2)
=> 1.0 - Similarity(s1, s2);

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
=> 1.0 - Similarity(s1, s2);

private static int[] Matches<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
ReadOnlySpan<T> max, min;
if (s1.Length > s2.Length)
Expand Down
4 changes: 3 additions & 1 deletion src/F23.StringSimilarity/Levenshtein.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ public double Distance(string s1, string s2, int limit)
=> Distance(s1.AsSpan(), s2.AsSpan(), limit);

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
=> Distance(s1, s2, int.MaxValue);

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2, int limit)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand All @@ -89,7 +91,7 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2, int limit)
throw new ArgumentNullException(nameof(s2));
}

if (s1 == s2)
if (s1.SequenceEqual(s2))
{
return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion src/F23.StringSimilarity/LongestCommonSubsequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public double Distance(string s1, string s2)
=> Distance(s1.AsSpan(), s2.AsSpan());

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand All @@ -72,7 +73,7 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
throw new ArgumentNullException(nameof(s2));
}

if (s1 == s2)
if (s1.SequenceEqual(s2))
{
return 0;
}
Expand All @@ -92,6 +93,7 @@ public int Length(string s1, string s2)
=> Length(s1.AsSpan(), s2.AsSpan());

internal static int Length<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/F23.StringSimilarity/MetricLCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public double Distance(string s1, string s2)
=> Distance(s1.AsSpan(), s2.AsSpan());

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand All @@ -56,7 +57,7 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
throw new ArgumentNullException(nameof(s2));
}

if (s1 == s2)
if (s1.SequenceEqual(s2))
{
return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion src/F23.StringSimilarity/NormalizedLevenshtein.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public double Distance(string s1, string s2)
=> Distance(s1.AsSpan(), s2.AsSpan());

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand All @@ -57,7 +58,7 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
throw new ArgumentNullException(nameof(s2));
}

if (s1 == s2)
if (s1.SequenceEqual(s2))
{
return 0.0;
}
Expand All @@ -83,6 +84,7 @@ public double Similarity(string s1, string s2)
=> 1.0 - Distance(s1, s2);

public double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
=> 1.0 - Distance(s1, s2);
}
}
3 changes: 2 additions & 1 deletion src/F23.StringSimilarity/OptimalStringAlignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public double Distance(string s1, string s2)
=> Distance(s1.AsSpan(), s2.AsSpan());

public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
Expand All @@ -56,7 +57,7 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
throw new ArgumentNullException(nameof(s2));
}

if (s1 == s2)
if (s1.SequenceEqual(s2))
{
return 0;
}
Expand Down

0 comments on commit 8a49b7f

Please sign in to comment.