Skip to content

Commit 3b080f6

Browse files
committed
Changed hash calculation.
1 parent 40fb501 commit 3b080f6

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Nodes/NodeUuid.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static bool IsReserved(NodeUuid uuid)
2525
}
2626

2727
private byte[] uuidBytes;
28+
private readonly int uuidHash = 0;
2829

2930
/// <summary>Get the 16 UUID bytes.</summary>
3031
public byte[] UuidBytes => uuidBytes;
@@ -44,17 +45,21 @@ public NodeUuid(bool createNew)
4445
{
4546
SetZero();
4647
}
48+
49+
uuidHash = CalculateHash();
4750
}
4851

4952
/// <summary>Construct a new UUID object.</summary>
5053
/// <param name="valueBytes">Initial value of the <see cref="NodeUuid"/> object.</param>
51-
public NodeUuid(byte[] valueBytes)
54+
private NodeUuid(byte[] valueBytes)
5255
{
5356
Contract.Requires(valueBytes != null);
5457
Contract.Requires(valueBytes.Length == UuidSize);
5558
Contract.Ensures(uuidBytes != null);
5659

5760
SetValue(valueBytes);
61+
62+
uuidHash = CalculateHash();
5863
}
5964

6065
public static NodeUuid FromBase64String(string base64, bool createNew)
@@ -146,23 +151,24 @@ public bool Equals(NodeUuid other)
146151
return true;
147152
}
148153

149-
private int hash = 0;
150-
public override int GetHashCode()
154+
private int CalculateHash()
151155
{
152-
if (hash == 0)
156+
var hash = 17;
157+
unchecked
153158
{
154-
unchecked
159+
foreach (var b in uuidBytes)
155160
{
156-
hash = 17;
157-
foreach (var b in uuidBytes)
158-
{
159-
hash = hash * 31 + b.GetHashCode();
160-
}
161+
hash = hash * 31 + b.GetHashCode();
161162
}
162163
}
163164
return hash;
164165
}
165166

167+
public override int GetHashCode()
168+
{
169+
return uuidHash;
170+
}
171+
166172
public int CompareTo(NodeUuid other)
167173
{
168174
for (var i = 0; i < UuidSize; ++i)

0 commit comments

Comments
 (0)