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:
2026-03-25 17:37:21 +09:00
parent 69b054e81d
commit a32b0668de
6 changed files with 57 additions and 5 deletions

View File

@@ -5,7 +5,7 @@
<IncludeBuildOutput>False</IncludeBuildOutput> <IncludeBuildOutput>False</IncludeBuildOutput>
<SuppressDependenciesWhenPacking>True</SuppressDependenciesWhenPacking> <SuppressDependenciesWhenPacking>True</SuppressDependenciesWhenPacking>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>1.0.0</AssemblyVersion> <AssemblyVersion>1.1.0</AssemblyVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>

View File

@@ -5,6 +5,7 @@ using System.Collections;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Misaki.HighPerformance.LowLevel.Collections; namespace Misaki.HighPerformance.LowLevel.Collections;
@@ -319,4 +320,14 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
_buffer = null; _buffer = null;
_count = 0; _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();
}
} }

View File

@@ -346,7 +346,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// <summary> /// <summary>
/// Adds a range of elements to the collection. /// Adds a range of elements to the collection.
/// </summary> /// </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) public void AddRange(Span<T> values)
{ {
var newSize = _count + values.Length; var newSize = _count + values.Length;
@@ -363,6 +363,32 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
_count += values.Length; _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> /// <summary>
/// Adds the elements of the specified collection to the current list without resizing the underlying storage. /// Adds the elements of the specified collection to the current list without resizing the underlying storage.
/// </summary> /// </summary>
@@ -494,4 +520,19 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
_array.Dispose(); _array.Dispose();
_count = 0; _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();
}
} }

View File

@@ -48,7 +48,7 @@ public class MemoryLeakException : Exception
var frame = stackTrace.GetFrame(i); var frame = stackTrace.GetFrame(i);
var fileName = frame?.GetFileName(); var fileName = frame?.GetFileName();
if (frame != null && fileName != null) if (frame != null)
{ {
stringBuilder.AppendLine($"File: {fileName}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}"); stringBuilder.AppendLine($"File: {fileName}, Method: {DiagnosticMethodInfo.Create(frame)?.Name}, Line: {frame.GetFileLineNumber()}");
} }

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.5.2</AssemblyVersion> <AssemblyVersion>1.5.3</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl> <RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.0.4</AssemblyVersion> <AssemblyVersion>1.0.5</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl> <RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>