Skip to content

Commit

Permalink
added WangHash for DtNodePool
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Feb 4, 2024
1 parent 94ed3d6 commit 2d3ca5b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion src/DotRecast.Core/RcHashCodes.cs
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;
}
}
}
8 changes: 5 additions & 3 deletions src/DotRecast.Detour/DtNodePool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ namespace DotRecast.Detour
{
public class DtNodePool
{
private readonly Dictionary<long, List<DtNode>> m_map = new Dictionary<long, List<DtNode>>();
private readonly List<DtNode> m_nodes = new List<DtNode>();
private readonly Dictionary<long, List<DtNode>> m_map;
private readonly List<DtNode> m_nodes;

public DtNodePool()
{
m_map = new Dictionary<long, List<DtNode>>();
m_nodes = new List<DtNode>();
}

public void Clear()
Expand Down Expand Up @@ -112,4 +114,4 @@ public Dictionary<long, List<DtNode>> GetNodeMap()
return m_map;
}
}
}
}
40 changes: 40 additions & 0 deletions test/DotRecast.Core.Test/RcHashCodesTest.cs
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace DotRecast.Core.Test;

public class Vector3Tests
public class Vector3Test
{
[Test]
[Repeat(100000)]
Expand Down

0 comments on commit 2d3ca5b

Please sign in to comment.