Skip to content

Commit

Permalink
refactor: added RcStackArray4, RcStackArray8
Browse files Browse the repository at this point in the history
- #41
  • Loading branch information
ikpil committed Jan 21, 2024
1 parent f2923a9 commit 021b765
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/DotRecast.Core/Collections/RcStackArray4.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
}
71 changes: 71 additions & 0 deletions src/DotRecast.Core/Collections/RcStackArray8.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
}
5 changes: 5 additions & 0 deletions src/DotRecast.Core/RcRand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ public float Next()
{
return (float)_r.NextDouble();
}

public int NextInt32()
{
return _r.Next();
}
}
}
53 changes: 53 additions & 0 deletions test/DotRecast.Core.Test/RcStackArrayTest.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 021b765

Please sign in to comment.