@@ -8,7 +8,7 @@ namespace BitFaster.Caching
8
8
/// A reference counting class suitable for use with compare and swap algorithms.
9
9
/// </summary>
10
10
/// <typeparam name="TValue">The value type.</typeparam>
11
- public class ReferenceCount < TValue >
11
+ public class ReferenceCount < TValue > : IEquatable < ReferenceCount < TValue > >
12
12
{
13
13
private readonly TValue value ;
14
14
private readonly int count ;
@@ -41,17 +41,6 @@ public int Count
41
41
}
42
42
}
43
43
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
-
55
44
public ReferenceCount < TValue > IncrementCopy ( )
56
45
{
57
46
if ( this . count <= 0 && this . value is IDisposable )
@@ -66,5 +55,35 @@ public ReferenceCount<TValue> DecrementCopy()
66
55
{
67
56
return new ReferenceCount < TValue > ( this . value , this . count - 1 ) ;
68
57
}
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
+ }
69
88
}
70
89
}
0 commit comments