From 2d3ca5b3211cfb31e59101ca33344e145218ceb0 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 4 Feb 2024 13:24:09 +0900 Subject: [PATCH] added WangHash for DtNodePool --- CHANGELOG.md | 1 + src/DotRecast.Core/RcHashCodes.cs | 18 ++++++++- src/DotRecast.Detour/DtNodePool.cs | 8 ++-- test/DotRecast.Core.Test/RcHashCodesTest.cs | 40 +++++++++++++++++++ .../{Vector3Tests.cs => Vector3Test.cs} | 2 +- 5 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 test/DotRecast.Core.Test/RcHashCodesTest.cs rename test/DotRecast.Core.Test/{Vector3Tests.cs => Vector3Test.cs} (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec241a70..ae32d800 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Added DtNodePool tests +- Added WangHash() for DtNodePool - Added avg, min, max, sampling updated times in CrowdAgentProfilingTool ### Fixed diff --git a/src/DotRecast.Core/RcHashCodes.cs b/src/DotRecast.Core/RcHashCodes.cs index d94f6cf4..3202c9aa 100644 --- a/src/DotRecast.Core/RcHashCodes.cs +++ b/src/DotRecast.Core/RcHashCodes.cs @@ -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; + } } } \ No newline at end of file diff --git a/src/DotRecast.Detour/DtNodePool.cs b/src/DotRecast.Detour/DtNodePool.cs index 2b840e8d..473e9609 100644 --- a/src/DotRecast.Detour/DtNodePool.cs +++ b/src/DotRecast.Detour/DtNodePool.cs @@ -24,11 +24,13 @@ namespace DotRecast.Detour { public class DtNodePool { - private readonly Dictionary> m_map = new Dictionary>(); - private readonly List m_nodes = new List(); + private readonly Dictionary> m_map; + private readonly List m_nodes; public DtNodePool() { + m_map = new Dictionary>(); + m_nodes = new List(); } public void Clear() @@ -112,4 +114,4 @@ public Dictionary> GetNodeMap() return m_map; } } -} +} \ No newline at end of file diff --git a/test/DotRecast.Core.Test/RcHashCodesTest.cs b/test/DotRecast.Core.Test/RcHashCodesTest.cs new file mode 100644 index 00000000..0d500c81 --- /dev/null +++ b/test/DotRecast.Core.Test/RcHashCodesTest.cs @@ -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)); + } +} \ No newline at end of file diff --git a/test/DotRecast.Core.Test/Vector3Tests.cs b/test/DotRecast.Core.Test/Vector3Test.cs similarity index 99% rename from test/DotRecast.Core.Test/Vector3Tests.cs rename to test/DotRecast.Core.Test/Vector3Test.cs index 975a733e..f845040e 100644 --- a/test/DotRecast.Core.Test/Vector3Tests.cs +++ b/test/DotRecast.Core.Test/Vector3Test.cs @@ -5,7 +5,7 @@ namespace DotRecast.Core.Test; -public class Vector3Tests +public class Vector3Test { [Test] [Repeat(100000)]