diff --git a/BitFaster.Caching/ReferenceCount.cs b/BitFaster.Caching/ReferenceCount.cs index fbb55e14..316e692f 100644 --- a/BitFaster.Caching/ReferenceCount.cs +++ b/BitFaster.Caching/ReferenceCount.cs @@ -8,7 +8,7 @@ namespace BitFaster.Caching /// A reference counting class suitable for use with compare and swap algorithms. /// /// The value type. - public class ReferenceCount + public class ReferenceCount : IEquatable> { private readonly TValue value; private readonly int count; @@ -41,17 +41,6 @@ public int Count } } - public override int GetHashCode() - { - return this.value.GetHashCode() ^ this.count; - } - - public override bool Equals(object obj) - { - ReferenceCount refCount = obj as ReferenceCount; - return refCount != null && refCount.Value != null && refCount.Value.Equals(this.value) && refCount.count == this.count; - } - public ReferenceCount IncrementCopy() { if (this.count <= 0 && this.value is IDisposable) @@ -66,5 +55,35 @@ public ReferenceCount DecrementCopy() { return new ReferenceCount(this.value, this.count - 1); } + + public override bool Equals(object obj) + { + return Equals(obj as ReferenceCount); + } + + public bool Equals(ReferenceCount other) + { + return other != null && + EqualityComparer.Default.Equals(value, other.value) && + count == other.count; + } + + public override int GetHashCode() + { + var hashCode = -1491496004; + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(value); + hashCode = hashCode * -1521134295 + count.GetHashCode(); + return hashCode; + } + + public static bool operator ==(ReferenceCount left, ReferenceCount right) + { + return EqualityComparer>.Default.Equals(left, right); + } + + public static bool operator !=(ReferenceCount left, ReferenceCount right) + { + return !(left == right); + } } }