This commit is contained in:
Mikhail 2024-02-03 22:45:03 +08:00
parent 5d4573f431
commit 2473c5dc78
3 changed files with 88 additions and 2 deletions

View File

@ -165,9 +165,28 @@ namespace DCFApixels.DragonECS.Relations.Internal
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T* Resize<T>(void* oldPointer, int newCount) where T : unmanaged
{
return (T*)(Marshal.ReAllocHGlobal(
return (T*)Marshal.ReAllocHGlobal(
new IntPtr(oldPointer),
new IntPtr(Marshal.SizeOf<T>(default) * newCount))).ToPointer();
new IntPtr(Marshal.SizeOf<T>(default) * newCount)).ToPointer();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T* ResizeAndInit<T>(void* oldPointer, int oldSize, int newSize) where T : unmanaged
{
int sizeT = Marshal.SizeOf<T>(default);
T* result = (T*)Marshal.ReAllocHGlobal(
new IntPtr(oldPointer),
new IntPtr(sizeT * newSize)).ToPointer();
Init((byte*)result, sizeT * oldSize, sizeT * newSize);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Init(byte* pointer, int startByteIndex, int endByteIndex)
{
for (int i = startByteIndex; i < endByteIndex; i++)
{
*(pointer + i) = 0;
}
}
}

View File

@ -82,6 +82,11 @@ namespace DCFApixels.DragonECS
return _nodes[nodeIndex].value;
}
private Node GetNode(int nodeIndex)
{
return _nodes[nodeIndex];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveFromBasket(int basketIndex, int nodeIndex)
@ -350,10 +355,57 @@ namespace DCFApixels.DragonECS
return result;
}
}
public IEnumerable<Node> Recycled
{
get
{
List<Node> result = new List<Node>();
Node curNode = new Node();
curNode.index = _basketList._recycledListLast;
while (curNode.index != -1)
{
BasketList.Node x = _basketList.GetNode(curNode.index);
curNode.prev = x.prev;
curNode.next = x.next;
result.Add(curNode);
curNode = new Node();
curNode.index = curNode.prev;
}
return result;
}
}
public IEnumerable<Node> AllNodes
{
get
{
List<Node> result = new List<Node>();
for (int i = 0; i < _basketList._nodes.Length; i++)
{
result.Add(new Node(_basketList._nodes[i].prev, i, _basketList._nodes[i].next));
}
return result;
}
}
public DebuggerProxy(BasketList basketList)
{
_basketList = basketList;
}
public struct Node
{
public int prev;
public int index;
public int next;
public Node(int prev, int index, int next)
{
this.prev = prev;
this.index = index;
this.next = next;
}
public override string ToString() => $"node({prev}< {index} >{next})";
}
public struct BasketIteratorDebbugerProxy
{
private BasketIterator _iterrator;

View File

@ -8,6 +8,21 @@ using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS.Relations.Internal
{
internal unsafe static class UnsafeArray
{
public static void Resize<T>(ref UnsafeArray<T> array, int newSize)
where T : unmanaged
{
array.ptr = UnmanagedArrayUtility.Resize<T>(array.ptr, newSize);
array.Length = newSize;
}
public static void ResizeAndInit<T>(ref UnsafeArray<T> array, int newSize)
where T : unmanaged
{
array.ptr = UnmanagedArrayUtility.ResizeAndInit<T>(array.ptr, array.Length, newSize);
array.Length = newSize;
}
}
[DebuggerTypeProxy(typeof(UnsafeArray<>.DebuggerProxy))]
internal unsafe struct UnsafeArray<T> : IDisposable, IEnumerable<T>
where T : unmanaged