refactor project structure and add documents.

Rename ReadOnlyUnsafeCollection<T> to ReadOnlyView<T>

Renamed ReadOnlyUnsafeCollection<T> to ReadOnlyView<T> across the codebase for clarity. Updated all references, constructors, methods, and XML docs accordingly. Changed Reinterpret<U>() and implicit conversions to use ReadOnlyView<T>. Improved UnsafeArray<T>.AsSpan methods to return Span<T>.Empty if not created. Bumped assembly version to 1.7.2.
This commit is contained in:
2026-05-15 17:22:35 +09:00
parent 99bfad88fc
commit e1dd7f95e2
4 changed files with 27 additions and 19 deletions

View File

@@ -14,18 +14,18 @@ namespace Misaki.HighPerformance.LowLevel.Collections;
/// The goal of this struc is similar to <see cref="ReadOnlySpan{T}"/>, but it can be used in contexts where spans are not allowed, such as fields in structs and shared across threads. /// The goal of this struc is similar to <see cref="ReadOnlySpan{T}"/>, but it can be used in contexts where spans are not allowed, such as fields in structs and shared across threads.
/// </remarks> /// </remarks>
/// <typeparam name="T">The type of elements in the collection. Must be an unmanaged type.</typeparam> /// <typeparam name="T">The type of elements in the collection. Must be an unmanaged type.</typeparam>
public readonly unsafe struct ReadOnlyUnsafeCollection<T> : IEnumerable<T> public readonly unsafe struct ReadOnlyView<T> : IEnumerable<T>
where T : unmanaged where T : unmanaged
{ {
public struct Enumerator : IEnumerator<T> public struct Enumerator : IEnumerator<T>
{ {
private readonly ReadOnlyUnsafeCollection<T> _collection; private readonly ReadOnlyView<T> _collection;
private int _index; private int _index;
public readonly T Current => _collection[_index]; public readonly T Current => _collection[_index];
readonly object IEnumerator.Current => Current; readonly object IEnumerator.Current => Current;
public Enumerator(ref readonly ReadOnlyUnsafeCollection<T> array) public Enumerator(ref readonly ReadOnlyView<T> array)
{ {
_collection = array; _collection = array;
_index = -1; _index = -1;
@@ -89,7 +89,7 @@ public readonly unsafe struct ReadOnlyUnsafeCollection<T> : IEnumerable<T>
return GetEnumerator(); return GetEnumerator();
} }
public ReadOnlyUnsafeCollection(T* buffer, int count) public ReadOnlyView(T* buffer, int count)
{ {
_buffer = buffer; _buffer = buffer;
_count = count; _count = count;
@@ -119,9 +119,9 @@ public readonly unsafe struct ReadOnlyUnsafeCollection<T> : IEnumerable<T>
/// Reinterprets the underlying collection as a read-only collection of a different unmanaged type without copying the data. /// Reinterprets the underlying collection as a read-only collection of a different unmanaged type without copying the data.
/// </summary> /// </summary>
/// <typeparam name="U">The unmanaged type to reinterpret the collection elements as.</typeparam> /// <typeparam name="U">The unmanaged type to reinterpret the collection elements as.</typeparam>
/// <returns>A new <see cref="ReadOnlyUnsafeCollection{U}"/> that provides a read-only view of the same memory, interpreted as elements of type U.</returns> /// <returns>A new <see cref="ReadOnlyView{U}"/> that provides a read-only view of the same memory, interpreted as elements of type U.</returns>
/// <exception cref="InvalidOperationException">Thrown if the total size of the underlying collection is not a multiple of the size of type U, making the reinterpretation invalid.</exception> /// <exception cref="InvalidOperationException">Thrown if the total size of the underlying collection is not a multiple of the size of type U, making the reinterpretation invalid.</exception>
public ReadOnlyUnsafeCollection<U> Reinterpret<U>() public ReadOnlyView<U> Reinterpret<U>()
where U : unmanaged where U : unmanaged
{ {
var totalSize = Count * sizeof(T); var totalSize = Count * sizeof(T);
@@ -131,7 +131,7 @@ public readonly unsafe struct ReadOnlyUnsafeCollection<T> : IEnumerable<T>
} }
var newCount = totalSize / sizeof(U); var newCount = totalSize / sizeof(U);
return new ReadOnlyUnsafeCollection<U>((U*)_buffer, newCount); return new ReadOnlyView<U>((U*)_buffer, newCount);
} }
/// <summary> /// <summary>
@@ -144,7 +144,7 @@ public readonly unsafe struct ReadOnlyUnsafeCollection<T> : IEnumerable<T>
return _buffer; return _buffer;
} }
public static implicit operator ReadOnlySpan<T>(ReadOnlyUnsafeCollection<T> collection) public static implicit operator ReadOnlySpan<T>(ReadOnlyView<T> collection)
{ {
return collection.AsSpan(); return collection.AsSpan();
} }

View File

@@ -188,11 +188,11 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
/// <summary> /// <summary>
/// Returns a read-only view of the current collection. /// Returns a read-only view of the current collection.
/// </summary> /// </summary>
/// <returns>A <see cref="ReadOnlyUnsafeCollection{T}"/> that provides a read-only view of the elements in the current collection.</returns> /// <returns>A <see cref="ReadOnlyView{T}"/> that provides a read-only view of the elements in the current collection.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlyUnsafeCollection<T> AsReadOnly() public readonly ReadOnlyView<T> AsReadOnly()
{ {
return new ReadOnlyUnsafeCollection<T>(_buffer, _count); return new ReadOnlyView<T>(_buffer, _count);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -246,14 +246,22 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<T> AsSpan(int start) public readonly Span<T> AsSpan(int start)
{ {
ThrowIfNotCreated(); if (!IsCreated)
{
return Span<T>.Empty;
}
return new Span<T>(_buffer + start, _count - start); return new Span<T>(_buffer + start, _count - start);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<T> AsSpan(int start, int length) public readonly Span<T> AsSpan(int start, int length)
{ {
ThrowIfNotCreated(); if (!IsCreated)
{
return Span<T>.Empty;
}
return new Span<T>(_buffer + start, length); return new Span<T>(_buffer + start, length);
} }
@@ -386,7 +394,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
_count = 0; _count = 0;
} }
public static implicit operator ReadOnlyUnsafeCollection<T>(UnsafeArray<T> array) public static implicit operator ReadOnlyView<T>(UnsafeArray<T> array)
{ {
return array.AsReadOnly(); return array.AsReadOnly();
} }

View File

@@ -282,10 +282,10 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// <summary> /// <summary>
/// Converts the current list to a read-only collection that provides unsafe access to its elements. /// Converts the current list to a read-only collection that provides unsafe access to its elements.
/// </summary> /// </summary>
/// <returns>A new <see cref="ReadOnlyUnsafeCollection{T}"/> instance that allows for read-only access to the list's elements without copying.</returns> /// <returns>A new <see cref="ReadOnlyView{T}"/> instance that allows for read-only access to the list's elements without copying.</returns>
public readonly ReadOnlyUnsafeCollection<T> AsReadOnly() public readonly ReadOnlyView<T> AsReadOnly()
{ {
return new ReadOnlyUnsafeCollection<T>((T*)_array.GetUnsafePtr(), _count); return new ReadOnlyView<T>((T*)_array.GetUnsafePtr(), _count);
} }
/// <summary> /// <summary>
@@ -592,7 +592,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
_count = 0; _count = 0;
} }
public static implicit operator ReadOnlyUnsafeCollection<T>(UnsafeList<T> list) public static implicit operator ReadOnlyView<T>(UnsafeList<T> list)
{ {
return list.AsReadOnly(); return list.AsReadOnly();
} }

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.7.1</AssemblyVersion> <AssemblyVersion>1.7.2</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>