Skip to content

Commit

Permalink
Lucene.Net.Util.Automaton.State: Removed IEquatable<T> and other equa…
Browse files Browse the repository at this point in the history
…lity checking, as implementing Equals() to compare other than reference equality causes IndexOutOfRangeException to randomly occur when using FuzzyTermsEnum. Fixes #296.
  • Loading branch information
NightOwl888 committed Nov 5, 2020
1 parent 3b13b9e commit 6ea55d1
Showing 1 changed file with 7 additions and 51 deletions.
58 changes: 7 additions & 51 deletions src/Lucene.Net/Util/Automaton/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Lucene.Net.Util.Automaton
/// <para/>
/// @lucene.experimental
/// </summary>
public class State : IComparable<State>, IEquatable<State> // LUCENENET specific: Implemented IEquatable, since this class is used in hashtables
public class State : IComparable<State>
{
internal bool accept;
[WritableArray]
Expand Down Expand Up @@ -355,60 +355,16 @@ public virtual int CompareTo(State s)
return s.id - id;
}

#region Equality
// LUCENENET specific - implemented IEquatable.
public bool Equals(State other)
{
if (other == null)
return false;
return id.Equals(other.id);
}
// LUCENENET NOTE: DO NOT IMPLEMENT Equals()!!!
// Although it doesn't match GetHashCode(), checking for
// reference equality is by design.
// Implementing Equals() causes difficult to diagnose
// IndexOutOfRangeExceptions when using FuzzyTermsEnum.
// See GH-296.

public override int GetHashCode()
{
return id;
}

public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is State other && Equals(other);
}

public static bool operator ==(State left, State right)
{
if (left is null)
{
return right is null;
}

return left.Equals(right);
}

public static bool operator !=(State left, State right)
{
return !(left == right);
}

public static bool operator <(State left, State right)
{
return left is null ? !(right is null) : left.CompareTo(right) < 0;
}

public static bool operator <=(State left, State right)
{
return left is null || left.CompareTo(right) <= 0;
}

public static bool operator >(State left, State right)
{
return !(left is null) && left.CompareTo(right) > 0;
}

public static bool operator >=(State left, State right)
{
return left is null ? right is null : left.CompareTo(right) >= 0;
}

#endregion
}
}

0 comments on commit 6ea55d1

Please sign in to comment.