### Summary Container enumeration is in dire need of optimization. Thanks to @stefanomerotta for initiating this work with PR #1443. This PR handles a small part of what Stefan started. Also included are some bug fixes. ### Method Signatures ```cs // Use with foreach without moving/deleting items FindItemsByTypeEnumerator<T> FindItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) // Use with foreach when moving/deleting items QueuedItemsEnumerator<T> EnumerateItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) // Use when iterating multiple times or queuing PooledRefQueue<T> QueueItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) // Use when iterating multiples times or manipulating elements without traversing PooledRefList<T> ListItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) ``` * `FindItemsByType<T>` has changed from returning `List<T>` to `FindItemsByTypeEnumerator<T>` - This method is not safe to use in situations where an item may get consumed, deleted, or moved. * `EnumerateItemsByType<T>` was added as a safe way to iterate and manipulate items. * **Note**: EnumerateItemsByType will _completely traverse the container_ before iteration starts because it uses `QueueItemsByType` under the hood. * `QueueItemsByType<T>` and `ListItemsByType<T>` was added to return a queue or list of items to iterate multiple times and manipulate the items. This isn't the most efficient since it uses a predicate and can result in 2 or 3 total iterations unnecessarily. ### Bug Fixes - [X] Fishing had an error in the random check that may have caused slight bias.
73 lines
1.9 KiB
C#
73 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using Server.Items;
|
|
using Xunit;
|
|
|
|
namespace Server.Tests;
|
|
|
|
public class ContainerTests : IClassFixture<ServerFixture>
|
|
{
|
|
[Fact]
|
|
public void TestFindItemsByType()
|
|
{
|
|
var staticSerial = (Serial)0x3;
|
|
|
|
var container = new Container((Serial)0x1);
|
|
container.AddItem(new Item((Serial)0x2));
|
|
container.AddItem(new Static(staticSerial));
|
|
|
|
Static staticItem = null;
|
|
foreach (var item in container.FindItemsByType<Static>())
|
|
{
|
|
staticItem = item;
|
|
}
|
|
|
|
Assert.NotNull(staticItem);
|
|
Assert.Equal(staticSerial, staticItem.Serial);
|
|
}
|
|
|
|
[Fact]
|
|
public void TestFindItemsByTypeNested()
|
|
{
|
|
var static1 = new Static((Serial)0x3);
|
|
var static2 = new Static((Serial)0x6);
|
|
|
|
var container = new Container((Serial)0x1);
|
|
container.AddItem(new Item((Serial)0x2));
|
|
var container2 = new Container((Serial)0x4);
|
|
container.AddItem(container2);
|
|
|
|
var container3 = new Container((Serial)0x5);
|
|
container2.AddItem(container3);
|
|
container3.AddItem(static2);
|
|
|
|
container2.AddItem(static1);
|
|
|
|
List<Static> statics = new List<Static>();
|
|
foreach (var item in container.FindItemsByType<Static>())
|
|
{
|
|
statics.Add(item);
|
|
}
|
|
|
|
Assert.Equal(2, statics.Count);
|
|
Assert.Equal(static1, statics[0]);
|
|
Assert.Equal(static2, statics[1]);
|
|
}
|
|
|
|
[Fact]
|
|
public void TestFindItemsByTypeNotMatching()
|
|
{
|
|
var container = new Container((Serial)0x1);
|
|
container.AddItem(new Item((Serial)0x2));
|
|
var container2 = new Container((Serial)0x4);
|
|
container.AddItem(container2);
|
|
container2.AddItem(new Item((Serial)0x5));
|
|
|
|
Static staticItem = null;
|
|
foreach (var item in container.FindItemsByType<Static>())
|
|
{
|
|
staticItem = item;
|
|
}
|
|
|
|
Assert.Null(staticItem);
|
|
}
|
|
}
|