diff --git a/VContainer/Assets/VContainer/Runtime/Internal/FreeList.cs b/VContainer/Assets/VContainer/Runtime/Internal/FreeList.cs index cc07ab9b..2ef8a296 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/FreeList.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/FreeList.cs @@ -1,28 +1,18 @@ -#nullable enable using System; using System.Collections.Generic; -using System.Runtime.InteropServices; - -namespace VContainer.Internal -{ - static class UnsafeHelper - { - public static ref TTo As(ref TFrom from) - { #if UNITY_2021_3_OR_NEWER - return ref global::Unity.Collections.LowLevel.Unsafe.UnsafeUtility.As(ref from); -#else - return ref System.Runtime.CompilerServices.Unsafe.As(ref from); +using Unity.Collections.LowLevel.Unsafe; #endif - } - } +namespace VContainer.Internal +{ class FreeList where T : class { public bool IsDisposed => lastIndex == -2; + public int Length => lastIndex + 1; readonly object gate = new object(); - T?[] values; + T[] values; int lastIndex = -1; public FreeList(int initialCapacity) @@ -31,17 +21,17 @@ public FreeList(int initialCapacity) } #if NETSTANDARD2_1 - public ReadOnlySpan AsSpan() + public ReadOnlySpan AsSpan() { if (lastIndex < 0) { - return ReadOnlySpan.Empty; + return ReadOnlySpan.Empty; } return values.AsSpan(0, lastIndex + 1); } #endif - public T? this[int index] => values[index]; + public T this[int index] => values[index]; public void Add(T item) { @@ -94,10 +84,9 @@ public bool Remove(T value) if (lastIndex < 0) return false; var index = -1; - var span = values.AsSpan(0, lastIndex + 1); - for (var i = 0; i < span.Length; i++) + for (var i = 0; i < values.Length; i++) { - if (span[i] == value) + if (values[i] == value) { index = i; break; @@ -110,6 +99,7 @@ public bool Remove(T value) return true; } } + return false; } @@ -119,7 +109,7 @@ public void Clear() { if (lastIndex > 0) { - values.AsSpan(0, lastIndex + 1).Clear(); + Array.Clear(values, 0, lastIndex + 1); lastIndex = -1; } } @@ -141,9 +131,10 @@ void CheckDispose() } } - static unsafe int FindNullIndex(T?[] target) +#if UNITY_2021_3_OR_NEWER + static unsafe int FindNullIndex(T[] target) { - ref var head = ref UnsafeHelper.As(ref MemoryMarshal.GetReference(target.AsSpan())); + ref var head = ref UnsafeUtility.As(ref MemoryMarshal.GetReference(target.AsSpan())); fixed (void* p = &head) { var span = new ReadOnlySpan(p, target.Length); @@ -160,9 +151,9 @@ static unsafe int FindNullIndex(T?[] target) } } - static unsafe int FindLastNonNullIndex(T?[] target, int lastIndex) + static unsafe int FindLastNonNullIndex(T[] target, int lastIndex) { - ref var head = ref UnsafeHelper.As(ref MemoryMarshal.GetReference(target.AsSpan())); + ref var head = ref UnsafeUtility.As(ref MemoryMarshal.GetReference(target.AsSpan())); fixed (void* p = &head) { var span = new ReadOnlySpan(p, lastIndex); // without lastIndexed value. @@ -175,5 +166,24 @@ static unsafe int FindLastNonNullIndex(T?[] target, int lastIndex) return -1; } } +#else + static int FindNullIndex(T[] target) + { + for (var i = 0; i < target.Length; i++) + { + if (target[i] == null) return i; + } + return -1; + } + + static int FindLastNonNullIndex(T[] target, int lastIndex) + { + for (var i = lastIndex; i >= 0; i--) + { + if (target[i] != null) return i; + } + return -1; + } +#endif } -} \ No newline at end of file +}