-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: added RcStackArray4, RcStackArray8
- #41
- Loading branch information
Showing
4 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,10 @@ public float Next() | |
{ | ||
return (float)_r.NextDouble(); | ||
} | ||
|
||
public int NextInt32() | ||
{ | ||
return _r.Next(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |