Add SparseSet Test

This commit is contained in:
2025-12-13 18:26:59 +09:00
parent c882c75760
commit 3016696d76
8 changed files with 85 additions and 16 deletions

View File

@@ -80,6 +80,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
private AllocationHandle _allocationHandle; private AllocationHandle _allocationHandle;
public readonly int Count => _count; public readonly int Count => _count;
public readonly int Length => _count;
public readonly ref T this[int index] public readonly ref T this[int index]
{ {

View File

@@ -54,7 +54,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
public Enumerator(UnsafeSlotMap<T>* collection) public Enumerator(UnsafeSlotMap<T>* collection)
{ {
_collection = collection; _collection = collection;
_currentIndex = -1; _currentIndex = 0;
} }
public bool MoveNext() public bool MoveNext()
@@ -65,7 +65,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
public void Reset() public void Reset()
{ {
_currentIndex = -1; _currentIndex = 0;
} }
public void Dispose() public void Dispose()
@@ -302,6 +302,8 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
_validBits.ClearAll(); _validBits.ClearAll();
_count = 0; _count = 0;
Add(default, out _);
} }
public readonly void* GetUnsafePtr() public readonly void* GetUnsafePtr()

View File

@@ -56,7 +56,7 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
public Enumerator(UnsafeSparseSet<T>* collection) public Enumerator(UnsafeSparseSet<T>* collection)
{ {
_collection = collection; _collection = collection;
_currentIndex = -1; _currentIndex = 0;
} }
public bool MoveNext() public bool MoveNext()
@@ -67,10 +67,10 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
public void Reset() public void Reset()
{ {
_currentIndex = -1; _currentIndex = 0;
} }
public readonly unsafe void Dispose() public readonly void Dispose()
{ {
} }
} }
@@ -345,6 +345,8 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
_count = 0; _count = 0;
_nextId = 0; _nextId = 0;
Add(default, out _);
} }
/// <inheritdoc/> /// <inheritdoc/>

View File

@@ -0,0 +1,52 @@
using Misaki.HighPerformance.Collections;
namespace Misaki.HighPerformance.Test.UnitTest.Collections;
[TestClass]
public class TestSparseSet
{
private SparseSet<int> _sparseSet = null!;
[TestInitialize]
public void Initialize()
{
_sparseSet = new SparseSet<int>(16);
}
[TestMethod]
public void InvalidID()
{
Assert.IsFalse(_sparseSet.Contains(0, 0));
}
[TestMethod]
public void Add()
{
var id = _sparseSet.Add(10, out var gen);
Assert.IsTrue(_sparseSet.Contains(id, gen));
}
[TestMethod]
public void Remove()
{
var id = _sparseSet.Add(20, out var gen);
Assert.IsTrue(_sparseSet.Contains(id, gen));
_sparseSet.Remove(id, gen);
Assert.IsFalse(_sparseSet.Contains(id, gen));
}
[TestMethod]
public void IndexReuse()
{
var id = _sparseSet.Add(20, out var gen);
Assert.IsTrue(_sparseSet.Contains(id, gen));
_sparseSet.Remove(id, gen);
Assert.IsFalse(_sparseSet.Contains(id, gen));
var newId = _sparseSet.Add(30, out var newGen);
Assert.AreEqual(id, newId);
Assert.AreNotEqual(gen, newGen);
}
}

View File

@@ -20,6 +20,12 @@ public class TestUnsafeSparseSet
_sparseSet.Dispose(); _sparseSet.Dispose();
} }
[TestMethod]
public void InvalidID()
{
Assert.IsFalse(_sparseSet.Contains(0, 0));
}
[TestMethod] [TestMethod]
public void Add() public void Add()
{ {

View File

@@ -22,7 +22,7 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
public Enumerator(ConcurrentSlotMap<T> slotMap) public Enumerator(ConcurrentSlotMap<T> slotMap)
{ {
_slotMap = slotMap; _slotMap = slotMap;
_currentIndex = -1; _currentIndex = 0;
} }
public readonly T Current => _slotMap._data[_currentIndex].value!; public readonly T Current => _slotMap._data[_currentIndex].value!;
@@ -42,7 +42,7 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
return false; return false;
} }
public void Reset() => _currentIndex = -1; public void Reset() => _currentIndex = 0;
public void Dispose() public void Dispose()
{ {
@@ -294,5 +294,7 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
} }
_freeSlots.Clear(); _freeSlots.Clear();
Add(default!, out _);
} }
} }

View File

@@ -14,7 +14,7 @@ public class SlotMap<T> : IEnumerable<T>
public Enumerator(SlotMap<T> slotMap) public Enumerator(SlotMap<T> slotMap)
{ {
_slotMap = slotMap; _slotMap = slotMap;
_currentIndex = -1; _currentIndex = 0;
} }
public readonly T Current => _slotMap._data[_currentIndex]; public readonly T Current => _slotMap._data[_currentIndex];
@@ -33,7 +33,7 @@ public class SlotMap<T> : IEnumerable<T>
return false; return false;
} }
public void Reset() => _currentIndex = -1; public void Reset() => _currentIndex = 0;
public void Dispose() public void Dispose()
{ {
@@ -188,5 +188,7 @@ public class SlotMap<T> : IEnumerable<T>
_data.AsSpan().Clear(); _data.AsSpan().Clear();
_freeSlots.Clear(); _freeSlots.Clear();
Add(default!, out _);
} }
} }

View File

@@ -26,7 +26,7 @@ public class SparseSet<T> : IEnumerable<T>
public Enumerator(SparseSet<T> collection) public Enumerator(SparseSet<T> collection)
{ {
_collection = collection; _collection = collection;
_currentIndex = -1; _currentIndex = 0;
} }
public bool MoveNext() public bool MoveNext()
@@ -37,7 +37,7 @@ public class SparseSet<T> : IEnumerable<T>
public void Reset() public void Reset()
{ {
_currentIndex = -1; _currentIndex = 0;
} }
public readonly void Dispose() public readonly void Dispose()
@@ -55,7 +55,7 @@ public class SparseSet<T> : IEnumerable<T>
private int _nextId; // Next available sparse index private int _nextId; // Next available sparse index
private int _capacity; private int _capacity;
public int Count => _count; public int Count => _count - 1;
public int Capacity => _capacity; public int Capacity => _capacity;
public Enumerator GetEnumerator() => new(this); public Enumerator GetEnumerator() => new(this);
@@ -277,6 +277,8 @@ public class SparseSet<T> : IEnumerable<T>
_count = 0; _count = 0;
_nextId = 0; _nextId = 0;
Add(default!, out _);
} }
/// <inheritdoc/> /// <inheritdoc/>