-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
namespace DotRecast.Core | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace DotRecast.Core | ||
{ | ||
public static class RcHashCodes | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int CombineHashCodes(int h1, int h2) | ||
{ | ||
return (((h1 << 5) + h1) ^ h2); | ||
} | ||
|
||
// From Thomas Wang, https://gist.github.com/badboy/6267743 | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static uint WangHash(uint a) | ||
{ | ||
a = (~a) + (a << 18); // a = (a << 18) - a - 1; | ||
a = a ^ (a >> 31); | ||
a = a * 21; // a = (a + (a << 2)) + (a << 4); | ||
a = a ^ (a >> 11); | ||
a = a + (a << 6); | ||
a = a ^ (a >> 22); | ||
return (uint)a; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using NUnit.Framework; | ||
|
||
namespace DotRecast.Core.Test; | ||
|
||
public class RcHashCodesTest | ||
{ | ||
[Test] | ||
public void TestCombineHashCodes() | ||
{ | ||
Assert.That(RcHashCodes.CombineHashCodes(0, 0), Is.EqualTo(0)); | ||
Assert.That(RcHashCodes.CombineHashCodes(int.MaxValue, int.MaxValue), Is.EqualTo(32)); | ||
Assert.That(RcHashCodes.CombineHashCodes(int.MaxValue, int.MinValue), Is.EqualTo(-33)); | ||
Assert.That(RcHashCodes.CombineHashCodes(int.MinValue, int.MinValue), Is.EqualTo(0)); | ||
Assert.That(RcHashCodes.CombineHashCodes(int.MinValue, int.MaxValue), Is.EqualTo(-1)); | ||
Assert.That(RcHashCodes.CombineHashCodes(int.MaxValue / 2, int.MaxValue / 2), Is.EqualTo(32)); | ||
} | ||
|
||
[Test] | ||
public void TestIntHash() | ||
{ | ||
Assert.That(RcHashCodes.WangHash(0), Is.EqualTo(4158654902)); | ||
Assert.That(RcHashCodes.WangHash(1), Is.EqualTo(357654460)); | ||
Assert.That(RcHashCodes.WangHash(2), Is.EqualTo(715307540)); | ||
Assert.That(RcHashCodes.WangHash(3), Is.EqualTo(1072960876)); | ||
|
||
Assert.That(RcHashCodes.WangHash(4), Is.EqualTo(1430614333)); | ||
Assert.That(RcHashCodes.WangHash(5), Is.EqualTo(1788267159)); | ||
Assert.That(RcHashCodes.WangHash(6), Is.EqualTo(2145921005)); | ||
Assert.That(RcHashCodes.WangHash(7), Is.EqualTo(2503556531)); | ||
|
||
Assert.That(RcHashCodes.WangHash(8), Is.EqualTo(2861226262)); | ||
Assert.That(RcHashCodes.WangHash(9), Is.EqualTo(3218863982)); | ||
Assert.That(RcHashCodes.WangHash(10), Is.EqualTo(3576533554)); | ||
Assert.That(RcHashCodes.WangHash(11), Is.EqualTo(3934169234)); | ||
|
||
// | ||
Assert.That(RcHashCodes.WangHash(int.MaxValue), Is.EqualTo(1755403298)); | ||
Assert.That(RcHashCodes.WangHash(uint.MaxValue), Is.EqualTo(3971045735)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters