Skip to content

Commit

Permalink
Support unity 201x
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Jun 21, 2024
1 parent cd38a36 commit 40490db
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions VContainer/Assets/VContainer/Runtime/Internal/FreeList.cs
Original file line number Diff line number Diff line change
@@ -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<TFrom, TTo>(ref TFrom from)
{
#if UNITY_2021_3_OR_NEWER
return ref global::Unity.Collections.LowLevel.Unsafe.UnsafeUtility.As<TFrom, TTo>(ref from);
#else
return ref System.Runtime.CompilerServices.Unsafe.As<TFrom, TTo>(ref from);
using Unity.Collections.LowLevel.Unsafe;
#endif
}
}

namespace VContainer.Internal
{
class FreeList<T> 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)
Expand All @@ -31,17 +21,17 @@ public FreeList(int initialCapacity)
}

#if NETSTANDARD2_1
public ReadOnlySpan<T?> AsSpan()
public ReadOnlySpan<T> AsSpan()
{
if (lastIndex < 0)
{
return ReadOnlySpan<T?>.Empty;
return ReadOnlySpan<T>.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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -110,6 +99,7 @@ public bool Remove(T value)
return true;
}
}

return false;
}

Expand All @@ -119,7 +109,7 @@ public void Clear()
{
if (lastIndex > 0)
{
values.AsSpan(0, lastIndex + 1).Clear();
Array.Clear(values, 0, lastIndex + 1);
lastIndex = -1;
}
}
Expand All @@ -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<T?, IntPtr>(ref MemoryMarshal.GetReference(target.AsSpan()));
ref var head = ref UnsafeUtility.As<T, IntPtr>(ref MemoryMarshal.GetReference(target.AsSpan()));
fixed (void* p = &head)
{
var span = new ReadOnlySpan<IntPtr>(p, target.Length);
Expand All @@ -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<T?, IntPtr>(ref MemoryMarshal.GetReference(target.AsSpan()));
ref var head = ref UnsafeUtility.As<T, IntPtr>(ref MemoryMarshal.GetReference(target.AsSpan()));
fixed (void* p = &head)
{
var span = new ReadOnlySpan<IntPtr>(p, lastIndex); // without lastIndexed value.
Expand All @@ -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
}
}
}

0 comments on commit 40490db

Please sign in to comment.