refactor: added RcStackArray4, RcStackArray8

- https://github.com/ikpil/DotRecast/issues/41
This commit is contained in:
ikpil 2024-01-21 09:33:55 +09:00 committed by Ikpil
parent f2923a9d84
commit 07b50d9391
4 changed files with 188 additions and 0 deletions

View File

@ -0,0 +1,59 @@
using System;
using System.Runtime.CompilerServices;
namespace DotRecast.Core.Collections
{
public struct RcStackArray4<T> where T : struct
{
public static readonly RcStackArray4<T> Empty = new RcStackArray4<T>();
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;
}
}
}
}
}

View File

@ -0,0 +1,71 @@
using System;
using System.Runtime.CompilerServices;
namespace DotRecast.Core.Collections
{
public struct RcStackArray8<T>
{
public static readonly RcStackArray8<T> Empty = new RcStackArray8<T>();
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;
}
}
}
}
}

View File

@ -20,5 +20,10 @@ namespace DotRecast.Core
{ {
return (float)_r.NextDouble(); return (float)_r.NextDouble();
} }
public int NextInt32()
{
return _r.Next();
}
} }
} }

View File

@ -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<int>();
for (int i = 0; i < 1024; ++i)
{
list.Add(rand.NextInt32());
}
{
RcStackArray4<int> array = RcStackArray4<int>.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<int> array = RcStackArray8<int>.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>(T a)
{
T s = a;
}
}