Skip to content

Commit

Permalink
[Release] v. 1.2.0
Browse files Browse the repository at this point in the history
* Id Enumerators
  • Loading branch information
andywiecko committed Dec 9, 2021
1 parent f965d25 commit 931b12c
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change log

## [1.2.0] - 2021-12-09

### Features

- `IdEnumerator<Id>` and `IdValueEnumerator<Id, T>` added.
- Unity version update: 2021.2.5f1.

## [1.1.0] - 2021-12-04

### Features
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ var area = areas[triangleId];
var neighborCount = neighborsCount[triangleId];
```

`NativeIndexedArray<Id, T>` can be enumarated by using values, ids or id–value tuples:

```csharp
using var data = new NativeIndexedArray<Id<int>, int>(new[]{1, 42, 6}, Allocator.Persistent);

foreach(var value in data)
{
UnityEngine.Debug.Log(value);
} // Expected: 1, 42, 6.
foreach(var id in data.Ids)
{
UnityEngine.Debug.Log(id);
} // Expected: (Id<int>)0, (Id<int>)1, (Id<int>)2.
foreach(var (id, value) in data.IdsValues)
{
UnityEngine.Debug.Log(id);
} // Expected: ((Id<int>)0, 1), ((Id<int>)1, 42), ((Id<int>)2, 6).
```

## NativeIndexedList{Id, T}

Wrapper for `NativeList<T>` which supports indexing via `Id<T>` instead of `int`, where `T` is a non-constraint generic parameter.
Expand Down
25 changes: 25 additions & 0 deletions Runtime/InternalUtility.cs
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.");
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/InternalUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Runtime/NativeIndexedCollections/Id.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace andywiecko.BurstCollections
{
public static readonly Id<T> Invalid = new Id<T>(-1);
public static readonly Id<T> Zero = new Id<T>(0);

public readonly int Value { get; }

public bool IsValid => Value != -1;
Expand Down Expand Up @@ -37,7 +37,7 @@ namespace andywiecko.BurstCollections
public static Id<T> operator -(Id<T> left, int right) => new Id<T>(left.Value - right);
public static Id<T> operator +(int left, Id<T> right) => new Id<T>(left + right.Value);
public static Id<T> operator -(int left, Id<T> right) => new Id<T>(left - right.Value);
public static Id<T> operator++(Id<T> id)=> new Id<T>(id.Value + 1);
public static Id<T> operator--(Id<T> id)=> new Id<T>(id.Value - 1);
public static Id<T> operator ++(Id<T> id) => new Id<T>(id.Value + 1);
public static Id<T> operator --(Id<T> id) => new Id<T>(id.Value - 1);
}
}
40 changes: 40 additions & 0 deletions Runtime/NativeIndexedCollections/IdEnumerators.cs
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;
}
}
11 changes: 11 additions & 0 deletions Runtime/NativeIndexedCollections/IdEnumerators.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions Runtime/NativeIndexedCollections/NativeIndexedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;

namespace andywiecko.BurstCollections
Expand Down Expand Up @@ -39,7 +40,10 @@ public NativeIndexedArray(int length, Allocator allocator, NativeArrayOptions op
public bool Equals(NativeIndexedArray<Id, T> other) => array.Equals(other.GetInnerArray());

public NativeArray<T> GetInnerArray() => array;
public ReadOnly AsReadOnly() => new ReadOnly(this);
public ReadOnly AsReadOnly() => new(this);
public ReadOnlySpan<T> AsReadOnlySpan() => AsReadOnly().AsReadOnlySpan();
public IdEnumerator<Id> Ids => AsReadOnly().Ids;
public IdValueEnumerator<Id, T> IdsValues => AsReadOnly().IdsValues;

#region ReadOnly
public struct ReadOnly
Expand All @@ -52,8 +56,12 @@ public struct ReadOnly

public void CopyTo(T[] array) => this.array.CopyTo(array);
public void CopyTo(NativeArray<T> array) => this.array.CopyTo(array);
public NativeIndexedArray<Id, U>.ReadOnly Reinterpret<U>() where U : unmanaged => new NativeIndexedArray<Id, U>.ReadOnly() { array = array.Reinterpret<U>() };
public NativeIndexedArray<Id, U>.ReadOnly Reinterpret<U>() where U : unmanaged => new() { array = array.Reinterpret<U>() };
public T[] ToArray() => array.ToArray();
public NativeArray<T>.ReadOnly.Enumerator GetEnumerator() => array.GetEnumerator();
unsafe public ReadOnlySpan<T> AsReadOnlySpan() => new(array.GetUnsafeReadOnlyPtr(), Length);
public IdEnumerator<Id> Ids => new(start: 0, Length);
public IdValueEnumerator<Id, T> IdsValues => new(AsReadOnlySpan());
}
#endregion
}
Expand Down
14 changes: 9 additions & 5 deletions Runtime/NativeIndexedCollections/NativeIndexedList.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
Expand Down Expand Up @@ -39,19 +40,22 @@ public NativeIndexedList(int initialCapacity, AllocatorManager.AllocatorHandle a
public void RemoveRangeSwapBack(int index, int count) => list.RemoveRangeSwapBack(index, count);
public void RemoveAt(int index) => list.RemoveAt(index);
public void RemoveRange(int index, int count) => list.RemoveRange(index, count);
public NativeIndexedArray<Id, T> AsArray() => new NativeIndexedArray<Id, T>() { array = list.AsArray() };
public NativeIndexedArray<Id, T> AsDeferredJobArray() => new NativeIndexedArray<Id, T>() { array = list.AsDeferredJobArray() };
public NativeIndexedArray<Id, T> AsArray() => new() { array = list.AsArray() };
public NativeIndexedArray<Id, T> AsDeferredJobArray() => new() { array = list.AsDeferredJobArray() };
public T[] ToArray() => list.ToArray();
public NativeIndexedArray<Id, T> ToArray(AllocatorManager.AllocatorHandle allocator) => new NativeIndexedArray<Id, T>() { array = list.ToArray(allocator) };
public NativeIndexedArray<Id, T> ToArray(AllocatorManager.AllocatorHandle allocator) => new() { array = list.ToArray(allocator) };
public NativeArray<T>.Enumerator GetEnumerator() => list.GetEnumerator();
IEnumerator<T> IEnumerable<T>.GetEnumerator() => (list as IEnumerable<T>).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => (list as IEnumerable).GetEnumerator();

public void CopyFrom(NativeArray<T> array) => list.CopyFrom(array);
public void Resize(int length, NativeArrayOptions options) => list.Resize(length, options);
public void ResizeUninitialized(int length) => list.ResizeUninitialized(length);
public NativeIndexedArray<Id, T>.ReadOnly AsParallelReader() => new NativeIndexedArray<Id, T>.ReadOnly() { array = list.AsParallelReader() };
public ParallelWriter AsParallelWriter() => new ParallelWriter(this);
public NativeIndexedArray<Id, T>.ReadOnly AsParallelReader() => new() { array = list.AsParallelReader() };
public ParallelWriter AsParallelWriter() => new(this);
unsafe public ReadOnlySpan<T> AsReadOnlySpan() => new(list.GetUnsafeReadOnlyPtr(), Length);
public IdEnumerator<Id> Ids => new(start: 0, Length);
public IdValueEnumerator<Id, T> IdsValues => new(AsReadOnlySpan());

public struct ParallelWriter
{
Expand Down
45 changes: 45 additions & 0 deletions Tests/IdEnumeratorsEditorTests.cs
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));
}
}
}
11 changes: 11 additions & 0 deletions Tests/IdEnumeratorsEditorTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "com.andywiecko.burst.collections",
"version": "1.1.0",
"version": "1.2.0",
"displayName": "Burst Collections",
"description": "Burst friendly (special) native collections for Unity.",
"unity": "2021.1",
"unityRelease": "16f1",
"unity": "2021.2",
"unityRelease": "5f1",
"keywords": [
"Burst",
"Collections"
Expand All @@ -19,7 +19,7 @@
"url": "git+https://github.com/andywiecko/BurstCollections"
},
"dependencies": {
"com.unity.burst": "1.5.5",
"com.unity.burst": "1.6.3",
"com.unity.jobs": "0.11.0-preview.6"
}
}

0 comments on commit 931b12c

Please sign in to comment.