Fixed bug in ImageResultFloat

Added scoped to in TKey key
This commit is contained in:
2026-03-15 17:22:22 +09:00
parent 2e08a8ad95
commit 8edb04263f
10 changed files with 171 additions and 56 deletions

View File

@@ -200,7 +200,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetBucket(in TKey key)
private readonly int GetBucket(scoped in TKey key)
{
var h = key.GetHashCode();
return GetBucket(h);
@@ -216,7 +216,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int AllocateEntry(in TKey key)
private int AllocateEntry(scoped in TKey key)
{
int idx;
@@ -323,7 +323,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
ResizeExact(capacity, capacity * 2);
}
public int Find(in TKey key)
public int Find(scoped in TKey key)
{
ThrowIfNotCreated();
@@ -354,7 +354,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return -1;
}
public int TryAdd(in TKey key)
public int TryAdd(scoped in TKey key)
{
ThrowIfNotCreated();
@@ -366,14 +366,14 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return AllocateEntry(key);
}
public int Add(in TKey key)
public int Add(scoped in TKey key)
{
ThrowIfNotCreated();
return AllocateEntry(key);
}
public int TryRemove(in TKey key)
public int TryRemove(scoped in TKey key)
{
ThrowIfNotCreated();
@@ -425,7 +425,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return 0 != removed ? removed : -1;
}
public int RemoveAll(in TKey key)
public int RemoveAll(scoped in TKey key)
{
ThrowIfNotCreated();
@@ -469,7 +469,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return removed;
}
public bool TryGetValue<TValue>(in TKey key, out TValue item)
public bool TryGetValue<TValue>(scoped in TKey key, out TValue item)
where TValue : unmanaged
{
ThrowIfNotCreated();
@@ -486,7 +486,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return false;
}
public int FindNext(int entryIdx, in TKey key)
public int FindNext(int entryIdx, scoped in TKey key)
{
ThrowIfNotCreated();
@@ -509,7 +509,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return -1;
}
public int CountValuesForKey(in TKey key)
public int CountValuesForKey(scoped in TKey key)
{
ThrowIfNotCreated();
@@ -522,8 +522,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return count;
}
[UnscopedRef]
public ref TValue GetValueRef<TValue>(in TKey key, out bool exists)
public ref TValue GetValueRef<TValue>(scoped in TKey key, out bool exists)
where TValue : unmanaged
{
ThrowIfNotCreated();
@@ -539,8 +538,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return ref Unsafe.NullRef<TValue>();
}
[UnscopedRef]
public ref TValue GetValueRefOrAddDefault<TValue>(in TKey key, out bool exists)
public ref TValue GetValueRefOrAddDefault<TValue>(scoped in TKey key, out bool exists)
where TValue : unmanaged
{
ThrowIfNotCreated();

View File

@@ -116,7 +116,8 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// <param name="key">The key to add.</param>
/// <param name="item">The value to add.</param>
/// <returns>True if the key-value pair was added.</returns>
public bool TryAdd(in TKey key, TValue item)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAdd(scoped in TKey key, TValue item)
{
var idx = _helper.TryAdd(key);
if (idx != -1)
@@ -135,7 +136,8 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// <param name="key">The key to add.</param>
/// <param name="item">The value to add.</param>
/// <exception cref="ArgumentException">Thrown if the key was already present.</exception>
public void Add(in TKey key, TValue item)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(scoped in TKey key, TValue item)
{
var result = TryAdd(key, item);
if (!result)
@@ -149,7 +151,8 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// </summary>
/// <param name="item">The value to remove.</param>
/// <returns>True if the value was present.</returns>
public bool Remove(in TKey key)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(scoped in TKey key)
{
return -1 != _helper.TryRemove(key);
}
@@ -160,7 +163,8 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// <param name="key">The key to look up.</param>
/// <param name="item">Outputs the value associated with the key. Outputs default if the key was not present.</param>
/// <returns>True if the key was present.</returns>
public bool TryGetValue(in TKey key, out TValue item)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetValue(scoped in TKey key, out TValue item)
{
return _helper.TryGetValue(key, out item);
}
@@ -171,7 +175,8 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// <param name="key">The key whose value to retrieve.</param>
/// <param name="defaultValue">The value to return if the specified key does not exist. If not specified, the default value for the type is used.</param>
/// <returns>The value associated with the specified key if the key is found; otherwise, the specified default value.</returns>
public TValue GetValueOrDefault(in TKey key, TValue defaultValue = default)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TValue GetValueOrDefault(scoped in TKey key, TValue defaultValue = default)
{
if (_helper.TryGetValue<TValue>(key, out var value))
{
@@ -181,14 +186,26 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
return defaultValue;
}
[UnscopedRef]
public ref TValue GetValueRef(in TKey key, out bool exists)
/// <summary>
/// Returns a reference to the value associated with the specified key, and a boolean indicating whether the key exists in the hash map.
/// </summary>
/// <param name="key">The key whose value to retrieve.</param>
/// <param name="exists">Outputs true if the key exists in the hash map; otherwise, false.</param>
/// <returns>A reference to the value associated with the specified key.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref TValue GetValueRef(scoped in TKey key, out bool exists)
{
return ref _helper.GetValueRef<TValue>(key, out exists);
}
[UnscopedRef]
public ref TValue GetValueRefOrAddDefault(in TKey key, out bool exists)
/// <summary>
/// Returns a reference to the value associated with the specified key if it exists; otherwise, adds a new key with a default value and returns a reference to that value. The method also outputs a boolean indicating whether the key already existed in the hash map.
/// </summary>
/// <param name="key">The key whose value to retrieve or add.</param>
/// <param name="exists">Outputs true if the key already existed in the hash map; otherwise, false.</param>
/// <returns>A reference to the value associated with the specified key.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref TValue GetValueRefOrAddDefault(scoped in TKey key, out bool exists)
{
return ref _helper.GetValueRefOrAddDefault<TValue>(key, out exists);
}
@@ -198,7 +215,8 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// </summary>
/// <param name="key">The key to look up.</param>
/// <returns>True if the key was present.</returns>
public bool ContainsKey(in TKey key)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ContainsKey(scoped in TKey key)
{
return -1 != _helper.Find(key);
}
@@ -206,16 +224,19 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// <summary>
/// Sets the capacity to match what it would be if it had been originally initialized with all its entries.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimExcess()
{
_helper.TrimExcess();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Resize(int newSize, AllocationOption option = AllocationOption.None)
{
_helper.Resize(newSize);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
_helper.Clear();
@@ -225,6 +246,7 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// Retrieves an array of keys from the hash map.
/// </summary>
/// <returns>An array containing the keys stored in the hash map.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UnsafeArray<TKey> GetKeyArray(Allocator allocator)
{
return _helper.GetKeyArray(allocator);
@@ -234,6 +256,7 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// Retrieves an array of values from the underlying hash map.
/// </summary>
/// <returns>An UnsafeArray containing the values stored in the hash map.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UnsafeArray<TValue> GetValueArray(Allocator allocator)
{
return _helper.GetValueArray<TValue>(allocator);
@@ -244,6 +267,7 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
/// TValue.
/// </summary>
/// <returns>Returns an UnsafeArray containing KeyValuePair objects.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UnsafeArray<KeyValuePair<TKey, TValue>> GetKeyValueArrays(Allocator allocator)
{
return _helper.GetKeyValueArrays<TValue>(allocator);

View File

@@ -86,6 +86,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
/// </summary>
/// <param name="item">The value to add.</param>
/// <returns>True if the value was not already present.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Add(T item)
{
return -1 != _helper.TryAdd(item);
@@ -96,6 +97,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
/// </summary>
/// <param name="item">The value to remove.</param>
/// <returns>True if the value was present.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(T item)
{
return -1 != _helper.TryRemove(item);
@@ -106,6 +108,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
/// </summary>
/// <param name="item">The value to check for.</param>
/// <returns>True if the value was present.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(T item)
{
return -1 != _helper.Find(item);
@@ -114,6 +117,7 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
/// <summary>
/// Sets the capacity to match what it would be if it had been originally initialized with all its entries.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimExcess()
{
_helper.TrimExcess();
@@ -124,16 +128,19 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
/// </summary>
/// <param name="allocator">The allocator to use.</param>
/// <returns>An array with a copy of the set's values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UnsafeArray<T> ToNativeArray(Allocator allocator)
{
return _helper.GetKeyArray(allocator);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Resize(int newSize, AllocationOption option = AllocationOption.None)
{
_helper.Resize(newSize);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
_helper.Clear();

View File

@@ -43,7 +43,7 @@ public unsafe struct UnsafeMultiHashMap<TKey, TValue> : IUnsafeHashCollection<Ke
internal TKey _key;
internal int _entryIndex;
internal Iterator(in TKey key, int entryIndex)
internal Iterator(scoped in TKey key, int entryIndex)
{
_key = key;
_entryIndex = entryIndex;
@@ -55,7 +55,7 @@ public unsafe struct UnsafeMultiHashMap<TKey, TValue> : IUnsafeHashCollection<Ke
private readonly HashMapHelper<TKey>* _data;
private readonly TKey _key;
internal ValueEnumerable(HashMapHelper<TKey>* data, in TKey key)
internal ValueEnumerable(HashMapHelper<TKey>* data, scoped in TKey key)
{
_data = data;
_key = key;
@@ -77,7 +77,7 @@ public unsafe struct UnsafeMultiHashMap<TKey, TValue> : IUnsafeHashCollection<Ke
public readonly TValue Current => UnsafeUtility.ReadArrayElement<TValue>(_data->Buffer, _entryIndex);
readonly object IEnumerator.Current => Current;
internal ValueEnumerator(HashMapHelper<TKey>* data, in TKey key)
internal ValueEnumerator(HashMapHelper<TKey>* data, scoped in TKey key)
{
_data = data;
_key = key;
@@ -151,18 +151,18 @@ public unsafe struct UnsafeMultiHashMap<TKey, TValue> : IUnsafeHashCollection<Ke
{
}
public void Add(in TKey key, TValue item)
public void Add(scoped in TKey key, TValue item)
{
var idx = _helper.Add(key);
UnsafeUtility.WriteArrayElement(_helper.Buffer, idx, item);
}
public bool Remove(in TKey key)
public bool Remove(scoped in TKey key)
{
return _helper.RemoveAll(key) != 0;
}
public bool TryGetFirstValue(in TKey key, out TValue item, out Iterator iterator)
public bool TryGetFirstValue(scoped in TKey key, out TValue item, out Iterator iterator)
{
var entryIndex = _helper.Find(key);
if (entryIndex == -1)
@@ -198,12 +198,12 @@ public unsafe struct UnsafeMultiHashMap<TKey, TValue> : IUnsafeHashCollection<Ke
return true;
}
public bool TryGetValue(in TKey key, out TValue item)
public bool TryGetValue(scoped in TKey key, out TValue item)
{
return _helper.TryGetValue(key, out item);
}
public TValue GetValueOrDefault(in TKey key, TValue defaultValue = default)
public TValue GetValueOrDefault(scoped in TKey key, TValue defaultValue = default)
{
if (_helper.TryGetValue<TValue>(key, out var value))
{
@@ -213,17 +213,17 @@ public unsafe struct UnsafeMultiHashMap<TKey, TValue> : IUnsafeHashCollection<Ke
return defaultValue;
}
public ValueEnumerable GetValuesForKey(in TKey key)
public ValueEnumerable GetValuesForKey(scoped in TKey key)
{
return new((HashMapHelper<TKey>*)UnsafeUtility.AddressOf(ref this), key);
}
public int CountValuesForKey(in TKey key)
public int CountValuesForKey(scoped in TKey key)
{
return _helper.CountValuesForKey(key);
}
public bool ContainsKey(in TKey key)
public bool ContainsKey(scoped in TKey key)
{
return _helper.Find(key) != -1;
}