diff --git a/Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs b/Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs index c803cac..494a4d8 100644 --- a/Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs +++ b/Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs @@ -36,6 +36,58 @@ public void Add([Values(256,512,1024)] int capacity) That(jaggedArray.Capacity, Is.GreaterThan(capacity)); } + [Test] + public void TryGetValue([Values(256,512,1024)] int capacity) + { + // Initialize the JaggedArray + var jaggedArray = new JaggedArray(16000/Unsafe.SizeOf(), -1, capacity); + + // Add elements to the array + for (var index = 0; index < jaggedArray.Capacity; index++) + { + jaggedArray.Add(index, index); + } + + // Check values using TryGetValue + for (var index = 0; index < jaggedArray.Capacity; index++) + { + var found = jaggedArray.TryGetValue(index, out int value); + That(found, Is.True); + That(value, Is.EqualTo(index)); + } + + // Check for values out of bounds + var outOfBoundsFound = jaggedArray.TryGetValue(jaggedArray.Capacity, out int _); + That(outOfBoundsFound, Is.False); + } + + [Test] + public void TryGetValueRef([Values(256,512,1024)] int capacity) + { + // Initialize the JaggedArray + var jaggedArray = new JaggedArray(16000/Unsafe.SizeOf(), -1, capacity); + + // Add elements to the array + for (var index = 0; index < jaggedArray.Capacity; index++) + { + jaggedArray.Add(index, index); + } + + // Check values using TryGetValue + for (var index = 0; index < jaggedArray.Capacity; index++) + { + bool found; + ref var value = ref jaggedArray.TryGetValue(index, out found); + That(found, Is.True); + That(value, Is.EqualTo(index)); + } + + // Check for values out of bounds + ref var outOfBoundsValue = ref jaggedArray.TryGetValue(jaggedArray.Capacity, out bool outOfBoundsFound); + That(outOfBoundsFound, Is.False); + } + + /// /// Checks if is capable of adding items correctly. /// diff --git a/Arch.LowLevel/Arch.LowLevel.csproj b/Arch.LowLevel/Arch.LowLevel.csproj index a298e76..81637bc 100644 --- a/Arch.LowLevel/Arch.LowLevel.csproj +++ b/Arch.LowLevel/Arch.LowLevel.csproj @@ -13,11 +13,11 @@ Arch.LowLevel Arch.LowLevel - 1.1.0 + 1.1.1 genaray Apache-2.0 LowLevel tools for arch. - Additional fixes. + Fixed several JaggedArray related bugs. c#;.net;.net6;.net7;ecs;game;entity;gamedev; game-development; game-engine; entity-component-system; arch; https://github.com/genaray/Arch.Extended diff --git a/Arch.LowLevel/Jagged/JaggedArray.cs b/Arch.LowLevel/Jagged/JaggedArray.cs index 982d401..5013c6a 100644 --- a/Arch.LowLevel/Jagged/JaggedArray.cs +++ b/Arch.LowLevel/Jagged/JaggedArray.cs @@ -221,13 +221,6 @@ public bool TryGetValue(int index, out T value) IndexToSlot(index, out var bucketIndex, out var itemIndex); - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) - { - value = _filler; - return false; - } - ref var item = ref _bucketArray[bucketIndex][itemIndex]; // If the item is the default then the nobody set its value. @@ -257,14 +250,13 @@ public ref T TryGetValue(int index, out bool @bool) return ref Unsafe.NullRef(); } - IndexToSlot(index, out var bucketIndex, out var itemIndex); - - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) + if (index >= Capacity) { @bool = false; return ref Unsafe.NullRef(); } + + IndexToSlot(index, out var bucketIndex, out var itemIndex); ref var item = ref _bucketArray[bucketIndex][itemIndex]; @@ -276,7 +268,7 @@ public ref T TryGetValue(int index, out bool @bool) } @bool = true; - return ref Unsafe.NullRef(); + return ref item; } /// diff --git a/Arch.LowLevel/Jagged/SparseJaggedArray.cs b/Arch.LowLevel/Jagged/SparseJaggedArray.cs index 7cd6236..26eda11 100644 --- a/Arch.LowLevel/Jagged/SparseJaggedArray.cs +++ b/Arch.LowLevel/Jagged/SparseJaggedArray.cs @@ -202,11 +202,6 @@ public void Add(int index, in T item) { IndexToSlot(index, out var bucketIndex, out var itemIndex); - if (bucketIndex >= _bucketArray.Length) - { - EnsureCapacity(index); - } - ref var bucket = ref _bucketArray[bucketIndex]; bucket.EnsureCapacity(); bucket[itemIndex] = item; @@ -244,16 +239,16 @@ public bool TryGetValue(int index, out T value) value = _filler; return false; } - - IndexToSlot(index, out var bucketIndex, out var itemIndex); - - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) + + // Index greater than capacity? + if (index >= Capacity) { value = _filler; return false; } + IndexToSlot(index, out var bucketIndex, out var itemIndex); + ref var item = ref _bucketArray[bucketIndex][itemIndex]; // If the item is the default then the nobody set its value. @@ -282,16 +277,15 @@ public ref T TryGetValue(int index, out bool @bool) @bool = false; return ref Unsafe.NullRef(); } - - IndexToSlot(index, out var bucketIndex, out var itemIndex); - - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) + + if (index >= Capacity) { @bool = false; return ref Unsafe.NullRef(); } + IndexToSlot(index, out var bucketIndex, out var itemIndex); + ref var item = ref _bucketArray[bucketIndex][itemIndex]; // If the item is the default then the nobody set its value. @@ -302,7 +296,7 @@ public ref T TryGetValue(int index, out bool @bool) } @bool = true; - return ref Unsafe.NullRef(); + return ref item; } /// diff --git a/Arch.LowLevel/Jagged/UnsafeJaggedArray.cs b/Arch.LowLevel/Jagged/UnsafeJaggedArray.cs index 945763e..c1dfb70 100644 --- a/Arch.LowLevel/Jagged/UnsafeJaggedArray.cs +++ b/Arch.LowLevel/Jagged/UnsafeJaggedArray.cs @@ -192,14 +192,13 @@ public bool TryGetValue(int index, out T value) return false; } - IndexToSlot(index, out var bucketIndex, out var itemIndex); - - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) + if (index >= Capacity) { value = _filler; return false; } + + IndexToSlot(index, out var bucketIndex, out var itemIndex); ref var item = ref _bucketArray[bucketIndex][itemIndex]; @@ -229,16 +228,14 @@ public ref T TryGetValue(int index, out bool @bool) @bool = false; return ref Unsafe.NullRef(); } - - IndexToSlot(index, out var bucketIndex, out var itemIndex); - - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) + + if (index >= Capacity) { @bool = false; return ref Unsafe.NullRef(); } + IndexToSlot(index, out var bucketIndex, out var itemIndex); ref var item = ref _bucketArray[bucketIndex][itemIndex]; // If the item is the default then the nobody set its value. @@ -249,7 +246,7 @@ public ref T TryGetValue(int index, out bool @bool) } @bool = true; - return ref Unsafe.NullRef(); + return ref item; } /// diff --git a/Arch.LowLevel/Jagged/UnsafeSparseJaggedArray.cs b/Arch.LowLevel/Jagged/UnsafeSparseJaggedArray.cs index 32f3831..aa19483 100644 --- a/Arch.LowLevel/Jagged/UnsafeSparseJaggedArray.cs +++ b/Arch.LowLevel/Jagged/UnsafeSparseJaggedArray.cs @@ -250,15 +250,14 @@ public bool TryGetValue(int index, out T value) return false; } - IndexToSlot(index, out var bucketIndex, out var itemIndex); - - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) + if (index >= Capacity) { value = _filler; return false; } - + + IndexToSlot(index, out var bucketIndex, out var itemIndex); + ref var item = ref _bucketArray[bucketIndex][itemIndex]; // If the item is the default then the nobody set its value. @@ -288,14 +287,13 @@ public ref T TryGetValue(int index, out bool @bool) return ref Unsafe.NullRef(); } - IndexToSlot(index, out var bucketIndex, out var itemIndex); - - // If the item is outside the array. Then it definetly doesn't exist - if (bucketIndex > _bucketArray.Length) + if (index >= Capacity) { @bool = false; return ref Unsafe.NullRef(); } + + IndexToSlot(index, out var bucketIndex, out var itemIndex); ref var item = ref _bucketArray[bucketIndex][itemIndex]; @@ -307,7 +305,7 @@ public ref T TryGetValue(int index, out bool @bool) } @bool = true; - return ref Unsafe.NullRef(); + return ref item; } /// diff --git a/README.md b/README.md index a545ac3..071f73d 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Download the packages and get started today! dotnet add package Arch.System --version 1.0.5 dotnet add package Arch.System.SourceGenerator --version 1.2.1 dotnet add package Arch.EventBus --version 1.0.2 -dotnet add package Arch.LowLevel --version 1.0.9 +dotnet add package Arch.LowLevel --version 1.1.1 dotnet add package Arch.Relationships --version 1.0.0 dotnet add package Arch.Persistence --version 1.0.4 dotnet add package Arch.AOT.SourceGenerator --version 1.0.1