feat(collections): add implicit conversions and AddRange overloads
Added implicit conversion operators to UnsafeArray<T> and UnsafeList<T> for easier conversion to ReadOnlyUnsafeCollection<T> and Span<T>. Introduced new AddRange overloads in UnsafeList<T> for ReadOnlyUnsafeCollection<T> and pointer-based sources. Updated AssemblyVersion in all projects. Improved MemoryLeakException stack trace output and fixed a documentation comment.
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Collections;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace Misaki.HighPerformance.LowLevel.Collections;
|
||||
|
||||
@@ -319,4 +320,14 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
|
||||
_buffer = null;
|
||||
_count = 0;
|
||||
}
|
||||
|
||||
public static implicit operator ReadOnlyUnsafeCollection<T>(UnsafeArray<T> array)
|
||||
{
|
||||
return array.AsReadOnly();
|
||||
}
|
||||
|
||||
public static implicit operator Span<T>(UnsafeArray<T> array)
|
||||
{
|
||||
return array.AsSpan();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,7 +346,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
||||
/// <summary>
|
||||
/// Adds a range of elements to the collection.
|
||||
/// </summary>
|
||||
/// <param name="values">A span containing the elements to add. The span must not exceed the specified <paramref name="count"/>.</param>
|
||||
/// <param name="values">A span containing the elements to add.</param>
|
||||
public void AddRange(Span<T> values)
|
||||
{
|
||||
var newSize = _count + values.Length;
|
||||
@@ -363,6 +363,32 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
||||
_count += values.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a range of elements to the collection.
|
||||
/// </summary>
|
||||
/// <param name="values">A collection containing the elements to add.</param>
|
||||
public void AddRange(ReadOnlyUnsafeCollection<T> collection)
|
||||
{
|
||||
AddRange((T*)collection.GetUnsafePtr(), collection.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a range of elements from a pointer to the collection.
|
||||
/// </summary>
|
||||
/// <param name="ptr">Points to the source data to be copied into the collection.</param>
|
||||
/// <param name="count">Indicates the number of elements to be added from the source data.</param>
|
||||
public void AddRange(T* ptr, int count)
|
||||
{
|
||||
var newSize = _count + count;
|
||||
if (newSize > Capacity)
|
||||
{
|
||||
Resize(Capacity + count);
|
||||
}
|
||||
|
||||
MemCpy(UnsafeUtility.ReadArrayElementUnsafe<T>(_array.GetUnsafePtr(), _count), ptr, (uint)(count * sizeof(T)));
|
||||
_count += count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the elements of the specified collection to the current list without resizing the underlying storage.
|
||||
/// </summary>
|
||||
@@ -494,4 +520,19 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
|
||||
_array.Dispose();
|
||||
_count = 0;
|
||||
}
|
||||
|
||||
public static implicit operator UnsafeArray<T>(UnsafeList<T> list)
|
||||
{
|
||||
return list.AsUnsafeArray();
|
||||
}
|
||||
|
||||
public static implicit operator ReadOnlyUnsafeCollection<T>(UnsafeList<T> list)
|
||||
{
|
||||
return list.AsReadOnly();
|
||||
}
|
||||
|
||||
public static implicit operator Span<T>(UnsafeList<T> list)
|
||||
{
|
||||
return list.AsSpan();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user