Add image processing and memory management features
Added new namespace `Misaki.HighPerformance.Image` for image processing, including classes for animated GIF handling and memory management. Added `AnimatedFrameResult` class for individual frames in animated images. Added `AnimatedGifEnumerator` class for enumerating frames in animated GIFs. Added `ColorComponents` enum for different color formats. Added `ImageInfo` struct for image dimensions and color components. Added `CRuntime` class for low-level memory management functions. Added `MemoryStats` class to track memory allocation statistics. Added utility functions for creating multi-dimensional arrays. Added new structures for fixed-size UTF-8 encoded strings. Added benchmarking classes to test new memory management features. Changed `StbImage.cs` to include new namespaces and functionality for image data manipulation. Changed project files to target .NET 9.0 and enable new features. Changed `Arena.cs` and `DynamicArena.cs` to use `nuint` for size parameters. Changed `BitSet.cs` to enhance bit manipulation methods. Changed `Program.cs` to run `FunctionPtrBenchmark` for performance testing. Removed memory tracking code from `AllocationManager.cs`, including the `_allocated` dictionary and related logic. Removed `Free` method from `IAllocator.cs` interface. Removed `UNSAFE_COLLECTION_CHECK` preprocessor directive from the codebase. Refactored various files to improve organization, moving from `Unsafe` to `LowLevel` namespace. Refactored `MemoryUtilities` class to include new memory operation methods. Refactored `UnsafeUtilities.cs` to support new collection structures.
This commit is contained in:
129
Misaki.HighPerformance.LowLevel/Collections/UnsafeHashSet.cs
Normal file
129
Misaki.HighPerformance.LowLevel/Collections/UnsafeHashSet.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
|
||||
using Misaki.HighPerformance.LowLevel.Contracts;
|
||||
using Misaki.HighPerformance.LowLevel.Helpers;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.LowLevel.Collections;
|
||||
|
||||
/// <summary>
|
||||
/// A collection that provides fast, unsafe operations for managing a set of unmanaged types. It supports adding,
|
||||
/// removing, and checking for values.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Represents an unmanaged type that can be compared for equality.</typeparam>
|
||||
public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
|
||||
where T : unmanaged, IEquatable<T>
|
||||
{
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
{
|
||||
internal HashMapHelper<T>.Enumerator _enumerator;
|
||||
|
||||
public Enumerator(HashMapHelper<T>* hashMap)
|
||||
{
|
||||
_enumerator = new HashMapHelper<T>.Enumerator(hashMap);
|
||||
}
|
||||
|
||||
public T Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _enumerator.buffer->_keys[_enumerator.index];
|
||||
}
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() => _enumerator.MoveNext();
|
||||
|
||||
public void Reset() => _enumerator.Reset();
|
||||
|
||||
public readonly void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private HashMapHelper<T> _hashMap;
|
||||
|
||||
public readonly int Count => _hashMap.Count;
|
||||
public readonly int Capacity => _hashMap.Capacity;
|
||||
public readonly bool IsCreated => _hashMap.IsCreated;
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new Enumerator((HashMapHelper<T>*)UnsafeUtilities.AddressOf(ref _hashMap));
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public UnsafeHashSet(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
|
||||
{
|
||||
_hashMap = new HashMapHelper<T>(capacity, 0, HashMapHelper<T>.MINIMAL_CAPACITY, ref handle, allocationOption);
|
||||
}
|
||||
|
||||
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
|
||||
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new value (unless it is already present).
|
||||
/// </summary>
|
||||
/// <param name="item">The value to add.</param>
|
||||
/// <returns>True if the value was not already present.</returns>
|
||||
public bool Add(T item)
|
||||
{
|
||||
return -1 != _hashMap.TryAdd(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a particular value.
|
||||
/// </summary>
|
||||
/// <param name="item">The value to remove.</param>
|
||||
/// <returns>True if the value was present.</returns>
|
||||
public bool Remove(T item)
|
||||
{
|
||||
return -1 != _hashMap.TryRemove(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if a particular value is present.
|
||||
/// </summary>
|
||||
/// <param name="item">The value to check for.</param>
|
||||
/// <returns>True if the value was present.</returns>
|
||||
public bool Contains(T item)
|
||||
{
|
||||
return -1 != _hashMap.Find(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the capacity to match what it would be if it had been originally initialized with all its entries.
|
||||
/// </summary>
|
||||
public void TrimExcess() => _hashMap.TrimExcess();
|
||||
|
||||
/// <summary>
|
||||
/// Returns an array with a copy of this set's values (in no particular order).
|
||||
/// </summary>
|
||||
/// <param name="allocator">The allocator to use.</param>
|
||||
/// <returns>An array with a copy of the set's values.</returns>
|
||||
public UnsafeArray<T> ToNativeArray(Allocator allocator)
|
||||
{
|
||||
return _hashMap.GetKeyArray(allocator);
|
||||
}
|
||||
|
||||
public void Resize(int newSize)
|
||||
{
|
||||
_hashMap.Resize(newSize);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_hashMap.Clear();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void* GetUnsafePtr()
|
||||
{
|
||||
return _hashMap.Buffer;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_hashMap.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user