@@ -8,7 +8,7 @@ namespace BitFaster.Caching
88 /// A reference counting class suitable for use with compare and swap algorithms.
99 /// </summary>
1010 /// <typeparam name="TValue">The value type.</typeparam>
11- public class ReferenceCount < TValue >
11+ public class ReferenceCount < TValue > : IEquatable < ReferenceCount < TValue > >
1212 {
1313 private readonly TValue value ;
1414 private readonly int count ;
@@ -41,17 +41,6 @@ public int Count
4141 }
4242 }
4343
44- public override int GetHashCode ( )
45- {
46- return this . value . GetHashCode ( ) ^ this . count ;
47- }
48-
49- public override bool Equals ( object obj )
50- {
51- ReferenceCount < TValue > refCount = obj as ReferenceCount < TValue > ;
52- return refCount != null && refCount . Value != null && refCount . Value . Equals ( this . value ) && refCount . count == this . count ;
53- }
54-
5544 public ReferenceCount < TValue > IncrementCopy ( )
5645 {
5746 if ( this . count <= 0 && this . value is IDisposable )
@@ -66,5 +55,35 @@ public ReferenceCount<TValue> DecrementCopy()
6655 {
6756 return new ReferenceCount < TValue > ( this . value , this . count - 1 ) ;
6857 }
58+
59+ public override bool Equals ( object obj )
60+ {
61+ return Equals ( obj as ReferenceCount < TValue > ) ;
62+ }
63+
64+ public bool Equals ( ReferenceCount < TValue > other )
65+ {
66+ return other != null &&
67+ EqualityComparer < TValue > . Default . Equals ( value , other . value ) &&
68+ count == other . count ;
69+ }
70+
71+ public override int GetHashCode ( )
72+ {
73+ var hashCode = - 1491496004 ;
74+ hashCode = hashCode * - 1521134295 + EqualityComparer < TValue > . Default . GetHashCode ( value ) ;
75+ hashCode = hashCode * - 1521134295 + count . GetHashCode ( ) ;
76+ return hashCode ;
77+ }
78+
79+ public static bool operator == ( ReferenceCount < TValue > left , ReferenceCount < TValue > right )
80+ {
81+ return EqualityComparer < ReferenceCount < TValue > > . Default . Equals ( left , right ) ;
82+ }
83+
84+ public static bool operator != ( ReferenceCount < TValue > left , ReferenceCount < TValue > right )
85+ {
86+ return ! ( left == right ) ;
87+ }
6988 }
7089}
0 commit comments