Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
# v0.23.0

## What's Added
* `IStackable` interface to represent stackable items with the properties
* `IStackable` - interface to represent stackable items with the properties
* `Amount` - The current amount of the item in the stack.
* `MaxAmount` - The maximum amount of the item that can be in a single stack.
* `AvailableAmount` - The amount of items that can be added to the stack until it reaches its max amount.
* `IContentState` interface to represent the state of the content in a slot with the properties
* `IContentState` - interface to represent the state of the content in a slot with the properties
* `IsEmpty` - Indicates whether the slot is empty (i.e., contains no items).
* `IsFull` - Indicates whether the slot is full (i.e., contains the maximum number of items).
* `IContainer` - Non-generic Interface to represent a container that holds slots with the properties
Expand Down
5 changes: 3 additions & 2 deletions src/Containers/Interfaces/ILazyStackContainer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using TheChest.Core.Abstractions;
using System.Collections.Generic;
using TheChest.Core.Abstractions;

namespace TheChest.Core.Containers.Interfaces
{
/// <summary>
/// Defines a generic stack-like container that supports lazy evaluation and items of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of items that the container can hold.</typeparam>
public interface ILazyStackContainer<in T> : IContainer { }
public interface ILazyStackContainer<out T> : IContainer, IEnumerable<IEnumerable<T>> { }
}
28 changes: 28 additions & 0 deletions src/Containers/LazyStackContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TheChest.Core.Containers.Interfaces;
using TheChest.Core.Extensions;
using TheChest.Core.Slots;
Expand All @@ -13,10 +15,14 @@
/// <typeparam name="T">The type of elements stored in the container.</typeparam>
public class LazyStackContainer<T> : ILazyStackContainer<T>
{
#region Fields
/// <summary>
/// The collection of slots used to store elements of type <typeparamref name="T"/>.
/// </summary>
protected ILazyStackSlot<T>[] slots;

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net7.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net7.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net6.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net10.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net10.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net6.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net8.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net8.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net9.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / Tests (net9.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / SonarCloud coverage (net9.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / SonarCloud coverage (net9.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)

Check warning on line 22 in src/Containers/LazyStackContainer.cs

View workflow job for this annotation

GitHub Actions / SonarCloud coverage (net9.0)

Do not declare visible instance fields (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1051)
#endregion

#region Properties
/// <inheritdoc/>
public virtual int Size => this.slots.Length;
/// <inheritdoc/>
Expand Down Expand Up @@ -47,6 +53,9 @@
return true;
}
}
#endregion

#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="LazyStackContainer{T}"/> class with default size of 20 and max stack size of 1.
/// </summary>
Expand Down Expand Up @@ -103,6 +112,25 @@
{
this.slots = slots ?? throw new ArgumentNullException(nameof(slots));
}
#endregion

#region IEnumerable Implementation
/// <inheritdoc/>
public IEnumerator<IEnumerable<T>> GetEnumerator()
{
foreach (var slot in this.slots)
{
foreach (var items in slot)
{
yield return items;
}
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
8 changes: 3 additions & 5 deletions src/Slots/Interfaces/ILazyStackSlot.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using TheChest.Core.Abstractions;
using System.Collections.Generic;
using TheChest.Core.Abstractions;

namespace TheChest.Core.Slots.Interfaces
{
/// <summary>
/// Represents a slot that can lazily hold a stack of items of a specified type.
/// </summary>
/// <remarks>
/// <see cref="ILazyStackSlot{T}"/> extends <see cref="ISlot{T}"/>
/// </remarks>
/// <typeparam name="T">The type of item the slot can hold</typeparam>
public interface ILazyStackSlot<in T> : IContentState, IStackable { }
public interface ILazyStackSlot<out T> : IContentState, IStackable, IEnumerable<IEnumerable<T>> { }
}
27 changes: 24 additions & 3 deletions src/Slots/LazyStackSlot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using TheChest.Core.Slots.Interfaces;
using TheChest.Core.Validators;

Expand All @@ -14,13 +16,13 @@ namespace TheChest.Core.Slots
/// <typeparam name="T">The type of the content stored in the slot.</typeparam>
public class LazyStackSlot<T> : ILazyStackSlot<T>
{
/// <summary>
/// The content inside the slot
/// </summary>
#region Fields
private object content;
private int amount;
private int maxAmount;
#endregion

#region Properties
/// <summary>
/// The content inside the slot
/// </summary>
Expand Down Expand Up @@ -72,7 +74,9 @@ protected set
public virtual bool IsFull => !this.IsEmpty && this.amount == this.maxAmount;
/// <inheritdoc/>
public virtual bool IsEmpty => this.amount == 0 || this.content is null;
#endregion

#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="LazyStackSlot{T}"/> class with default values.
/// </summary>
Expand Down Expand Up @@ -112,6 +116,23 @@ public LazyStackSlot(T currentItem, int amount, int maxAmount)
this.amount = this.content is null ? 0 : amount;
this.maxAmount = maxAmount;
}
#endregion

#region IEnumerable Implementation

/// <inheritdoc/>
public IEnumerator<IEnumerable<T>> GetEnumerator()
{
for (int i = 0; i < this.amount; i++)
{
yield return new T[1] { this.Content };
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
1 change: 1 addition & 0 deletions src/TheChest.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Containers\Extensions\" />
<Folder Include="Stacks\Abstractions\" />
<Folder Include="Stacks\Containers\" />
<Folder Include="Stacks\Extensions\" />
Expand Down
55 changes: 55 additions & 0 deletions tests/Containers/LazyStackContainer/GetEnumeratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace TheChest.Core.Tests.Containers.LazyStackContainer
{
public class GetEnumeratorTests<T> : LazyStackContainerTests<T>
{
[Test]
[Description("GetEnumerator returns all empty slots for an empty lazy stack container.")]
public void GetEnumerator_EmptyLazyStackContainer_ReturnsEmptySlots()
{
var (size, stackSize) = this.GenerateRandomSizeAndStackSize();
var container = this.containerFactory.Empty(size, stackSize);
var enumeratedItems = container.ToArray();

Assert.That(enumeratedItems, Is.Empty);
}

[Test]
[Description("GetEnumerator returns every slot, including empty slots, with its items.")]
public void GetEnumerator_LazyStackContainerWithItems_ReturnsSlotsAndItems()
{
var (size, stackSize) = this.GenerateRandomSizeAndStackSize();
var item = this.itemFactory.CreateDefault();
var container = this.containerFactory.ShuffledItems(size, stackSize, item);
var enumeratedItems = container.ToArray();

Assert.Multiple(() =>
{
Assert.That(enumeratedItems, Has.Length.EqualTo(stackSize));
Assert.That(enumeratedItems.Select(items => items.Count()), Is.All.EqualTo(1));
});
CollectionAssert.AreEqual(Enumerable.Repeat(item, stackSize), enumeratedItems.SelectMany(items => items));
}

[Test]
[Description("The non-generic GetEnumerator returns each slot and its items.")]
public void GetEnumerator_NonGeneric_ReturnsSlotsAndItems()
{
var (size, stackSize) = this.GenerateRandomSizeAndStackSize();
var item = this.itemFactory.CreateDefault();
IEnumerable container = this.containerFactory.ShuffledItems(size, stackSize, item);
var enumeratedItems = container.Cast<IEnumerable<T>>().ToArray();

Assert.Multiple(() =>
{
Assert.That(enumeratedItems, Has.Length.EqualTo(stackSize));
Assert.That(enumeratedItems.Select(items => items.Count()), Is.All.EqualTo(1));
});
CollectionAssert.AreEqual(Enumerable.Repeat(item, stackSize), enumeratedItems.SelectMany(items => items));
}
}
}
47 changes: 47 additions & 0 deletions tests/Slots/LazyStackSlot/GetEnumeratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace TheChest.Core.Tests.Slots.LazyStackSlot
{
public class GetEnumeratorTests<T> : LazyStackSlotTests<T>
{
[Test]
[Description("GetEnumerator returns no items for an empty lazy stack slot.")]
public void GetEnumerator_EmptySlot_ReturnsNoItems()
{
var maxStackSize = this.GenerateStackSize();
IEnumerable<IEnumerable<T>> slot = this.slotFactory.Empty(0, maxStackSize);

CollectionAssert.IsEmpty(slot);
}

[Test]
[Description("GetEnumerator returns each stored item in its own sequence.")]
public void GetEnumerator_SlotWithItems_ReturnsSingleItemSequences()
{
var maxStackSize = this.GenerateStackSize();
var amount = this.random.Next(1, maxStackSize);
var item = this.itemFactory.CreateDefault();
IEnumerable<IEnumerable<T>> slot = this.slotFactory.WithItem(item, amount, maxStackSize);

CollectionAssert.AreEqual(Enumerable.Repeat(item, amount), slot.Select(items => items.Single()));
}

[Test]
[Description("The non-generic GetEnumerator returns each stored item.")]
public void GetEnumerator_NonGeneric_ReturnsItems()
{
var maxStackSize = this.GenerateStackSize();
var amount = this.random.Next(1, maxStackSize);
var item = this.itemFactory.CreateDefault();
IEnumerable slot = this.slotFactory.WithItem(item, amount, maxStackSize);

CollectionAssert.AreEqual(
Enumerable.Repeat(item, amount),
slot.Cast<IEnumerable<T>>().SelectMany(items => items)
);
}
}
}
Loading