From 07b50d93916c2436274203001c420e3ab0295695 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 21 Jan 2024 09:33:55 +0900 Subject: [PATCH] refactor: added RcStackArray4, RcStackArray8 - https://github.com/ikpil/DotRecast/issues/41 --- .../Collections/RcStackArray4.cs | 59 +++++++++++++++ .../Collections/RcStackArray8.cs | 71 +++++++++++++++++++ src/DotRecast.Core/RcRand.cs | 5 ++ test/DotRecast.Core.Test/RcStackArrayTest.cs | 53 ++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 src/DotRecast.Core/Collections/RcStackArray4.cs create mode 100644 src/DotRecast.Core/Collections/RcStackArray8.cs create mode 100644 test/DotRecast.Core.Test/RcStackArrayTest.cs diff --git a/src/DotRecast.Core/Collections/RcStackArray4.cs b/src/DotRecast.Core/Collections/RcStackArray4.cs new file mode 100644 index 0000000..165cc0a --- /dev/null +++ b/src/DotRecast.Core/Collections/RcStackArray4.cs @@ -0,0 +1,59 @@ +using System; +using System.Runtime.CompilerServices; + +namespace DotRecast.Core.Collections +{ + public struct RcStackArray4 where T : struct + { + public static readonly RcStackArray4 Empty = new RcStackArray4(); + + private const int Size = 4; + public int Length => Size; + + public T V0; + public T V1; + public T V2; + public T V3; + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ThrowExceptionIfIndexOutOfRange(int index) + { + if (0 > index || index >= Size) + { + throw new IndexOutOfRangeException($"{index}"); + } + } + + public T this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ThrowExceptionIfIndexOutOfRange(index); + + return index switch + { + 0 => V0, + 1 => V1, + 2 => V2, + 3 => V3, + _ => throw new IndexOutOfRangeException($"{index}") + }; + } + + set + { + ThrowExceptionIfIndexOutOfRange(index); + + switch (index) + { + case 0: V0 = value; break; + case 1: V1 = value; break; + case 2: V2 = value; break; + case 3: V3 = value; break; + } + } + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Core/Collections/RcStackArray8.cs b/src/DotRecast.Core/Collections/RcStackArray8.cs new file mode 100644 index 0000000..39ebb8d --- /dev/null +++ b/src/DotRecast.Core/Collections/RcStackArray8.cs @@ -0,0 +1,71 @@ +using System; +using System.Runtime.CompilerServices; + +namespace DotRecast.Core.Collections +{ + public struct RcStackArray8 + { + public static readonly RcStackArray8 Empty = new RcStackArray8(); + + private const int Size = 8; + public int Length => Size; + + public T V0; + public T V1; + public T V2; + public T V3; + public T V4; + public T V5; + public T V6; + public T V7; + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ThrowExceptionIfIndexOutOfRange(int index) + { + if (0 > index || index >= Size) + { + throw new IndexOutOfRangeException($"{index}"); + } + } + + public T this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ThrowExceptionIfIndexOutOfRange(index); + + return index switch + { + 0 => V0, + 1 => V1, + 2 => V2, + 3 => V3, + 4 => V4, + 5 => V5, + 6 => V6, + 7 => V7, + _ => throw new IndexOutOfRangeException($"{index}") + }; + } + + set + { + ThrowExceptionIfIndexOutOfRange(index); + + switch (index) + { + case 0: V0 = value; break; + case 1: V1 = value; break; + case 2: V2 = value; break; + case 3: V3 = value; break; + case 4: V4 = value; break; + case 5: V5 = value; break; + case 6: V6 = value; break; + case 7: V7 = value; break; + } + } + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Core/RcRand.cs b/src/DotRecast.Core/RcRand.cs index c1fc36b..279590a 100644 --- a/src/DotRecast.Core/RcRand.cs +++ b/src/DotRecast.Core/RcRand.cs @@ -20,5 +20,10 @@ namespace DotRecast.Core { return (float)_r.NextDouble(); } + + public int NextInt32() + { + return _r.Next(); + } } } \ No newline at end of file diff --git a/test/DotRecast.Core.Test/RcStackArrayTest.cs b/test/DotRecast.Core.Test/RcStackArrayTest.cs new file mode 100644 index 0000000..37bde51 --- /dev/null +++ b/test/DotRecast.Core.Test/RcStackArrayTest.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using DotRecast.Core.Collections; +using NUnit.Framework; + +namespace DotRecast.Core.Test; + +[Parallelizable] +public class RcStackArrayTest +{ + [Test] + public void TestRcStackArray() + { + var rand = new RcRand(); + + // excepted values + var list = new List(); + for (int i = 0; i < 1024; ++i) + { + list.Add(rand.NextInt32()); + } + + { + RcStackArray4 array = RcStackArray4.Empty; + for (int i = 0; i < array.Length; ++i) + { + array[i] = list[i]; + } + + for (int i = 0; i < array.Length; ++i) + { + Assert.That(array[i], Is.EqualTo(list[i])); + } + } + + { + RcStackArray8 array = RcStackArray8.Empty; + for (int i = 0; i < array.Length; ++i) + { + array[i] = list[i]; + } + + for (int i = 0; i < array.Length; ++i) + { + Assert.That(array[i], Is.EqualTo(list[i])); + } + } + } + + public void Test(T a) + { + T s = a; + } +} \ No newline at end of file