@@ -25,6 +25,7 @@ public static bool IsReserved(NodeUuid uuid)
25
25
}
26
26
27
27
private byte [ ] uuidBytes ;
28
+ private readonly int uuidHash = 0 ;
28
29
29
30
/// <summary>Get the 16 UUID bytes.</summary>
30
31
public byte [ ] UuidBytes => uuidBytes ;
@@ -44,17 +45,21 @@ public NodeUuid(bool createNew)
44
45
{
45
46
SetZero ( ) ;
46
47
}
48
+
49
+ uuidHash = CalculateHash ( ) ;
47
50
}
48
51
49
52
/// <summary>Construct a new UUID object.</summary>
50
53
/// <param name="valueBytes">Initial value of the <see cref="NodeUuid"/> object.</param>
51
- public NodeUuid ( byte [ ] valueBytes )
54
+ private NodeUuid ( byte [ ] valueBytes )
52
55
{
53
56
Contract . Requires ( valueBytes != null ) ;
54
57
Contract . Requires ( valueBytes . Length == UuidSize ) ;
55
58
Contract . Ensures ( uuidBytes != null ) ;
56
59
57
60
SetValue ( valueBytes ) ;
61
+
62
+ uuidHash = CalculateHash ( ) ;
58
63
}
59
64
60
65
public static NodeUuid FromBase64String ( string base64 , bool createNew )
@@ -146,23 +151,24 @@ public bool Equals(NodeUuid other)
146
151
return true ;
147
152
}
148
153
149
- private int hash = 0 ;
150
- public override int GetHashCode ( )
154
+ private int CalculateHash ( )
151
155
{
152
- if ( hash == 0 )
156
+ var hash = 17 ;
157
+ unchecked
153
158
{
154
- unchecked
159
+ foreach ( var b in uuidBytes )
155
160
{
156
- hash = 17 ;
157
- foreach ( var b in uuidBytes )
158
- {
159
- hash = hash * 31 + b . GetHashCode ( ) ;
160
- }
161
+ hash = hash * 31 + b . GetHashCode ( ) ;
161
162
}
162
163
}
163
164
return hash ;
164
165
}
165
166
167
+ public override int GetHashCode ( )
168
+ {
169
+ return uuidHash ;
170
+ }
171
+
166
172
public int CompareTo ( NodeUuid other )
167
173
{
168
174
for ( var i = 0 ; i < UuidSize ; ++ i )
0 commit comments