-
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.
- Loading branch information
1 parent
0a923ce
commit cc980aa
Showing
6 changed files
with
252 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Unity.Collections; | ||
using Unity.Jobs; | ||
|
||
namespace andywiecko.BurstCollections | ||
{ | ||
public struct NativeStackedLists<T> : | ||
INativeDisposable, | ||
IEnumerable<IEnumerable<T>> | ||
where T : unmanaged | ||
{ | ||
private struct List | ||
{ | ||
public int Start { get; set; } | ||
public int Length { get; set; } | ||
public List(int start, int length) => (Start, Length) = (start, length); | ||
public static List operator ++(List @this) => new(@this.Start, @this.Length + 1); | ||
} | ||
|
||
public struct Enumerator | ||
{ | ||
private NativeStackedLists<T> owner; | ||
private int i; | ||
public Enumerator(NativeStackedLists<T> owner) => (this.owner, i) = (owner, -1); | ||
public NativeArray<T> Current => owner[i]; | ||
public bool MoveNext() => ++i < owner.stack.Length; | ||
public Enumerator GetEnumerator() => this; | ||
} | ||
|
||
public int Length => stack.Length; | ||
public NativeArray<T> this[int i] => elements.AsArray().GetSubArray(stack[i].Start, stack[i].Length); | ||
|
||
private NativeList<List> stack; | ||
private NativeList<T> elements; | ||
|
||
public NativeStackedLists(int elementsInitialCapacity, int stackInitialCapacity, Allocator allocator) | ||
{ | ||
elements = new(elementsInitialCapacity, allocator); | ||
stack = new(stackInitialCapacity, allocator); | ||
} | ||
public NativeStackedLists(int elementsInitialCapacity, Allocator allocator) : this(elementsInitialCapacity, stackInitialCapacity: 1, allocator) { } | ||
public NativeStackedLists(Allocator allocator) : this(elementsInitialCapacity: 1, stackInitialCapacity: 1, allocator) { } | ||
|
||
public void Push() => stack.Add(new() { Start = elements.Length }); | ||
|
||
public void Add(T element) | ||
{ | ||
elements.Add(element); | ||
++stack[^1]; | ||
} | ||
|
||
public void Clear() | ||
{ | ||
stack.Clear(); | ||
elements.Clear(); | ||
} | ||
|
||
public JobHandle Dispose(JobHandle dependencies) | ||
{ | ||
dependencies = stack.Dispose(dependencies); | ||
dependencies = elements.Dispose(dependencies); | ||
return dependencies; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
stack.Dispose(); | ||
elements.Dispose(); | ||
} | ||
|
||
public Enumerator GetEnumerator() => new(this); | ||
|
||
IEnumerator<IEnumerable<T>> IEnumerable<IEnumerable<T>>.GetEnumerator() | ||
{ | ||
foreach (var list in this) | ||
{ | ||
yield return list; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => (this as IEnumerable<IEnumerable<T>>).GetEnumerator(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Unity.Collections; | ||
using UnityEngine; | ||
|
||
namespace andywiecko.BurstCollections.Editor.Tests | ||
{ | ||
public class NativeStackedListsEditorTests | ||
{ | ||
[Test] | ||
public void InitTest() | ||
{ | ||
using var stackedLists = new NativeStackedLists<int>(64, 4, Allocator.Persistent); | ||
Assert.That(stackedLists.Length, Is.Zero); | ||
} | ||
|
||
[Test] | ||
public void ThrowsOnAddWhenLengthZero() | ||
{ | ||
using var stackedLists = new NativeStackedLists<int>(64, 4, Allocator.Persistent); | ||
Assert.Throws<IndexOutOfRangeException>(() => stackedLists.Add(default)); | ||
} | ||
|
||
[Test] | ||
public void PushTest([Values(1, 3, 5)] int count) | ||
{ | ||
using var stackedLists = new NativeStackedLists<int>(64, 4, Allocator.Persistent); | ||
|
||
for (int i = 0; i < count; i++) | ||
{ | ||
stackedLists.Push(); | ||
} | ||
|
||
Assert.That(stackedLists.Length, Is.EqualTo(count)); | ||
} | ||
|
||
[Test] | ||
public void AddTest() | ||
{ | ||
using var stackedLists = new NativeStackedLists<int>(64, 4, Allocator.Persistent); | ||
|
||
stackedLists.Push(); | ||
stackedLists.Add(1); | ||
stackedLists.Add(6); | ||
stackedLists.Add(8); | ||
|
||
Assert.That(stackedLists[0][0], Is.EqualTo(1)); | ||
Assert.That(stackedLists[0][1], Is.EqualTo(6)); | ||
Assert.That(stackedLists[0][2], Is.EqualTo(8)); | ||
} | ||
|
||
[Test] | ||
public void ClearTest() | ||
{ | ||
using var stackedLists = new NativeStackedLists<int>(64, 4, Allocator.Persistent); | ||
|
||
stackedLists.Push(); | ||
stackedLists.Add(1); | ||
stackedLists.Add(6); | ||
stackedLists.Add(8); | ||
|
||
var wasNotEmpty = stackedLists.Length != 0; | ||
stackedLists.Clear(); | ||
|
||
Assert.That(wasNotEmpty, Is.True); | ||
Assert.That(stackedLists, Has.Length.Zero); | ||
} | ||
|
||
[Test] | ||
public void EnumerableTest() | ||
{ | ||
using var stackedLists = new NativeStackedLists<int>(64, 4, Allocator.Persistent); | ||
|
||
stackedLists.Push(); | ||
stackedLists.Add(1); | ||
stackedLists.Add(2); | ||
stackedLists.Add(4); | ||
|
||
stackedLists.Push(); | ||
stackedLists.Add(1); | ||
stackedLists.Add(3); | ||
stackedLists.Add(9); | ||
stackedLists.Add(27); | ||
|
||
int[][] expected = | ||
{ | ||
new int[] { 1, 2, 4 }, | ||
new int[] { 1, 3, 9, 27 } | ||
}; | ||
Assert.That(stackedLists, Is.EqualTo(expected)); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.