-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
197 additions
and
14 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
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
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,25 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Unity.Collections.LowLevel.Unsafe; | ||
|
||
namespace andywiecko.BurstCollections | ||
{ | ||
internal static class InternalUtility | ||
{ | ||
public static Id AsId<Id>(int value) | ||
where Id : unmanaged, IIndexer | ||
{ | ||
CheckIdSize<Id>(); | ||
return UnsafeUtility.As<int, Id>(ref value); | ||
} | ||
|
||
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] | ||
unsafe private static void CheckIdSize<Id>() where Id : unmanaged, IIndexer | ||
{ | ||
if (sizeof(Id) != sizeof(int)) | ||
{ | ||
throw new ArgumentException($"{typeof(Id).Name} does not match `int` size."); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,40 @@ | ||
using System; | ||
|
||
namespace andywiecko.BurstCollections | ||
{ | ||
public struct IdEnumerator<Id> where Id : unmanaged, IIndexer | ||
{ | ||
private int current; | ||
private readonly int start; | ||
private readonly int length; | ||
|
||
public IdEnumerator(int start, int length) | ||
{ | ||
this.start = start; | ||
this.current = -1; | ||
this.length = length; | ||
} | ||
|
||
public Id Current => InternalUtility.AsId<Id>(current + start); | ||
public bool MoveNext() => ++current < length; | ||
public IdEnumerator<Id> GetEnumerator() => this; | ||
} | ||
|
||
public ref struct IdValueEnumerator<Id, T> | ||
where Id : unmanaged, IIndexer | ||
where T : unmanaged | ||
{ | ||
private ReadOnlySpan<T> data; | ||
private int current; | ||
|
||
public IdValueEnumerator(ReadOnlySpan<T> data) | ||
{ | ||
this.data = data; | ||
current = -1; | ||
} | ||
|
||
public (Id, T) Current => (InternalUtility.AsId<Id>(current), data[current]); | ||
public bool MoveNext() => ++current < data.Length; | ||
public IdValueEnumerator<Id, T> GetEnumerator() => this; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,45 @@ | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
|
||
namespace andywiecko.BurstCollections.Editor.Tests | ||
{ | ||
public class IdEnumeratorsEditorTests | ||
{ | ||
[Test] | ||
public void IdEnumeratorTest() | ||
{ | ||
var iter = new IdEnumerator<Id<int>>(start: 2, length: 4); | ||
|
||
var result = new List<Id<int>>(); | ||
foreach (var i in iter) | ||
{ | ||
result.Add(i); | ||
} | ||
|
||
var expectedResult = new[] | ||
{ | ||
(Id<int>)2, (Id<int>)3, (Id<int>)4, (Id<int>)5 | ||
}; | ||
Assert.That(result, Is.EqualTo(expectedResult)); | ||
} | ||
|
||
[Test] | ||
public void IdValueEnumeratorTest() | ||
{ | ||
var a = new[] { 1, 24, 3 }; | ||
var iter = new IdValueEnumerator<Id<int>, int>(a); | ||
|
||
var result = new List<(Id<int>, int)>(); | ||
foreach (var tuple in new IdValueEnumerator<Id<int>, int>(a)) | ||
{ | ||
result.Add(tuple); | ||
} | ||
|
||
var expectedResult = new[] | ||
{ | ||
((Id<int>)0, 1), ((Id<int>)1, 24),((Id<int>)2, 3), | ||
}; | ||
Assert.That(result, Is.EqualTo(expectedResult)); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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