Refactor core APIs, fix bugs, and improve safety
- Make image result/info structs readonly; improve error handling and memory safety in image library - Introduce IJobScheduler interface; move job scheduling docs to interface - Remove "index 0 invalid" convention from slot/sparse maps; fix Count logic - Add Owner<T> for disposable value types in low-level utilities - Improve ObjectPool<T> thread safety and logic - Change List<T>.RemoveAndSwapBack to return bool - Remove unsafe methods from generated math types; add debug range checks - Update benchmarks and enable collection checks in tests - Improve documentation, comments, and error messages - Bump assembly versions across all projects
This commit is contained in:
@@ -81,7 +81,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
private int _count;
|
||||
private int _capacity;
|
||||
|
||||
public readonly int Count => _count - 1;
|
||||
public readonly int Count => _count;
|
||||
public readonly int Capacity => _capacity;
|
||||
|
||||
public readonly bool IsCreated => _data.IsCreated && _generations.IsCreated && _freeSlots.IsCreated && _validBits.IsCreated;
|
||||
@@ -126,9 +126,6 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
|
||||
_count = 0;
|
||||
_capacity = capacity;
|
||||
|
||||
// Add a default item for invalid slot
|
||||
Add(default, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -212,8 +209,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
|
||||
/// <returns>true if the slot at the specified index is valid and its generation matches the specified value; otherwise, false.</returns>
|
||||
public readonly bool Contains(int slotIndex, int generation)
|
||||
{
|
||||
// 0 is reserved for invalid slot
|
||||
if (slotIndex <= 0 || slotIndex >= _capacity)
|
||||
if (slotIndex < 0 || slotIndex >= _capacity)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
|
||||
private int _nextId; // Next available sparse index
|
||||
private int _capacity;
|
||||
|
||||
public readonly int Count => _count - 1;
|
||||
public readonly int Count => _count;
|
||||
public readonly int Capacity => _capacity;
|
||||
public readonly bool IsCreated => _dense.IsCreated && _sparse.IsCreated && _reverse.IsCreated;
|
||||
|
||||
@@ -133,8 +133,6 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
|
||||
|
||||
_sparse.AsSpan().Fill(-1);
|
||||
_generations.Clear();
|
||||
|
||||
Add(default, out _); // Make index 0 invalid
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -240,8 +238,7 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool Contains(int sparseIndex, int generation)
|
||||
{
|
||||
// 0 is reserved as invalid index
|
||||
if (sparseIndex <= 0 || sparseIndex >= _sparse.Count)
|
||||
if (sparseIndex < 0 || sparseIndex >= _sparse.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
<Authors>Misaki</Authors>
|
||||
<AssemblyVersion>1.3.2</AssemblyVersion>
|
||||
<AssemblyVersion>1.3.3</AssemblyVersion>
|
||||
<Version>$(AssemblyVersion)</Version>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
|
||||
|
||||
@@ -67,7 +67,7 @@ public readonly unsafe struct SharedPtr<T> : IEquatable<SharedPtr<T>>
|
||||
/// Ensures that the pointer is not shared and can be safely transferred or detached.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// UniquePtr<T> is designed to encapsulate a raw pointer, enforcing unique ownership semantics similar to C++'s std::unique_ptr.
|
||||
/// <see cref="UniquePtr{T}"/> is designed to encapsulate a raw pointer, enforcing unique ownership semantics similar to C++'s std::unique_ptr.
|
||||
/// </remarks>
|
||||
/// <typeparam name="T">The unmanaged type of the value to which the pointer refers.</typeparam>
|
||||
[NonCopyable]
|
||||
@@ -174,3 +174,47 @@ public ref struct Ref<T>
|
||||
return !(left == right);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides a wrapper for a value type that implements <see cref="IDisposable"/>, ensuring proper disposal of the contained value.
|
||||
/// </summary>
|
||||
/// <remarks>The <see cref="Owner{T}"/> class manages the lifetime of the contained value by calling its
|
||||
/// <see cref="IDisposable.Dispose"/> method when the wrapper is disposed or finalized. After disposal, accessing the value
|
||||
/// will throw an <see cref="ObjectDisposedException"/>.</remarks>
|
||||
/// <typeparam name="T">The value type to wrap. Must be a struct that implements <see cref="IDisposable"/>.</typeparam>
|
||||
public class Owner<T> : IDisposable
|
||||
where T : struct, IDisposable
|
||||
{
|
||||
private T _value;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
public Owner(T value)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
~Owner()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public ref T Get()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
return ref _value;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_value.Dispose();
|
||||
|
||||
_disposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user