Skip to content

Commit

Permalink
Fixed several JaggedArray related bugs and released new LowLevels-Ver…
Browse files Browse the repository at this point in the history
…sion.
  • Loading branch information
genaray committed Jun 12, 2024
1 parent cb44ec9 commit 1fa6450
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 51 deletions.
52 changes: 52 additions & 0 deletions Arch.LowLevel.Tests/Jagged/JaggedArrayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(16000/Unsafe.SizeOf<int>(), -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<int>(16000/Unsafe.SizeOf<int>(), -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);
}


/// <summary>
/// Checks if <see cref="JaggedArray{T}"/> is capable of adding items correctly.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Arch.LowLevel/Arch.LowLevel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

<PackageId>Arch.LowLevel</PackageId>
<Title>Arch.LowLevel</Title>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<Authors>genaray</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Description>LowLevel tools for arch.</Description>
<PackageReleaseNotes>Additional fixes. </PackageReleaseNotes>
<PackageReleaseNotes>Fixed several JaggedArray related bugs. </PackageReleaseNotes>
<PackageTags>c#;.net;.net6;.net7;ecs;game;entity;gamedev; game-development; game-engine; entity-component-system; arch;</PackageTags>

<PackageProjectUrl>https://github.com/genaray/Arch.Extended</PackageProjectUrl>
Expand Down
16 changes: 4 additions & 12 deletions Arch.LowLevel/Jagged/JaggedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -257,14 +250,13 @@ public ref T TryGetValue(int index, out bool @bool)
return ref Unsafe.NullRef<T>();
}

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<T>();
}

IndexToSlot(index, out var bucketIndex, out var itemIndex);

ref var item = ref _bucketArray[bucketIndex][itemIndex];

Expand All @@ -276,7 +268,7 @@ public ref T TryGetValue(int index, out bool @bool)
}

@bool = true;
return ref Unsafe.NullRef<T>();
return ref item;
}

/// <summary>
Expand Down
26 changes: 10 additions & 16 deletions Arch.LowLevel/Jagged/SparseJaggedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -282,16 +277,15 @@ public ref T TryGetValue(int index, out bool @bool)
@bool = false;
return ref Unsafe.NullRef<T>();
}

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<T>();
}

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.
Expand All @@ -302,7 +296,7 @@ public ref T TryGetValue(int index, out bool @bool)
}

@bool = true;
return ref Unsafe.NullRef<T>();
return ref item;
}

/// <summary>
Expand Down
17 changes: 7 additions & 10 deletions Arch.LowLevel/Jagged/UnsafeJaggedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -229,16 +228,14 @@ public ref T TryGetValue(int index, out bool @bool)
@bool = false;
return ref Unsafe.NullRef<T>();
}

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<T>();
}

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.
Expand All @@ -249,7 +246,7 @@ public ref T TryGetValue(int index, out bool @bool)
}

@bool = true;
return ref Unsafe.NullRef<T>();
return ref item;
}

/// <summary>
Expand Down
18 changes: 8 additions & 10 deletions Arch.LowLevel/Jagged/UnsafeSparseJaggedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -288,14 +287,13 @@ public ref T TryGetValue(int index, out bool @bool)
return ref Unsafe.NullRef<T>();
}

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<T>();
}

IndexToSlot(index, out var bucketIndex, out var itemIndex);

ref var item = ref _bucketArray[bucketIndex][itemIndex];

Expand All @@ -307,7 +305,7 @@ public ref T TryGetValue(int index, out bool @bool)
}

@bool = true;
return ref Unsafe.NullRef<T>();
return ref item;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1fa6450

Please sign in to comment.