Container searching fixed and optimized (#88)
This commit is contained in:
parent
005e97eb22
commit
e97d2cb131
2 changed files with 169 additions and 47 deletions
|
|
@ -23,6 +23,8 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using Server.Network;
|
||||
using Server.Utilities;
|
||||
using QueuePool = Server.Utilities.RefPool<Server.Utilities.QueueRef<Server.Items.Container>>;
|
||||
|
||||
namespace Server.Items
|
||||
{
|
||||
|
|
@ -34,6 +36,7 @@ namespace Server.Items
|
|||
|
||||
public class Container : Item
|
||||
{
|
||||
private static QueuePool m_QueuePool = new QueuePool(QueueRef<Container>.Generate, preGenerateCount: 2, maxRefrenceRetention: 5);
|
||||
private static List<Item> m_FindItemsList = new List<Item>();
|
||||
|
||||
private ContainerData m_ContainerData;
|
||||
|
|
@ -1512,63 +1515,68 @@ namespace Server.Items
|
|||
|
||||
public List<T> FindItemsByType<T>(Predicate<T> predicate) where T : Item => FindItemsByType(true, predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Performs a Breadth-First search through all the <see cref="Item"/>s and
|
||||
/// nested <see cref="Container"/>s within this <see cref="Container"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of objects being searched for</typeparam>
|
||||
/// <param name="recurse">Optional: If true, the search will recursively
|
||||
/// check any nested <see cref="Container"/>s; otherwise, nested
|
||||
/// <see cref="Container"/>s will not be searched.</param>
|
||||
/// <param name="predicate">Optional: A predicate to check if the <see cref="Item"/>
|
||||
/// of type <typeparamref name="T"/> is one of the targets of the search.</param>
|
||||
/// <returns>A list of <see cref="Item"/>s of type <typeparamref name="T"/> that matche the optional <paramref name="predicate"/>.</returns>
|
||||
public List<T> FindItemsByType<T>(bool recurse = true, Predicate<T> predicate = null) where T : Item
|
||||
{
|
||||
List<T> list = new List<T>();
|
||||
RecurseFindItemsByType(this, recurse, list, predicate);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void RecurseFindItemsByType<T>(Item current, bool recurse, List<T> list, Predicate<T> predicate)
|
||||
where T : Item
|
||||
{
|
||||
if (current == null || current.Items.Count == 0)
|
||||
return;
|
||||
|
||||
List<Item> items = current.Items;
|
||||
|
||||
for (int i = 0; i < items.Count; ++i)
|
||||
using (var queue = m_QueuePool.Get())
|
||||
{
|
||||
Item item = items[i];
|
||||
|
||||
if (item is T typedItem)
|
||||
if (predicate?.Invoke(typedItem) == true)
|
||||
list.Add(typedItem);
|
||||
|
||||
if (recurse && item is Container)
|
||||
RecurseFindItemsByType(item, true, list, predicate);
|
||||
queue.Enqueue(this);
|
||||
var items = new List<T>();
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var container = queue.Dequeue();
|
||||
foreach (var item in container.Items)
|
||||
{
|
||||
if (item is T typedItem && predicate?.Invoke(typedItem) != false)
|
||||
items.Add(typedItem);
|
||||
else if (recurse && item is Container itemContainer)
|
||||
queue.Enqueue(itemContainer);
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
||||
public T FindItemByType<T>(bool recurse = true) where T : Item => RecurseFindItemByType<T>(this, recurse);
|
||||
|
||||
private static T RecurseFindItemByType<T>(Item current, bool recurse = true, Predicate<T> predicate = null) where T : Item
|
||||
/// <summary>
|
||||
/// Performs a Breadth-First search through all the <see cref="Item"/>s and
|
||||
/// nested <see cref="Container"/>s within this <see cref="Container"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of object being searched for</typeparam>
|
||||
/// <param name="recurse">Optional: If true, the search will recursively
|
||||
/// check any nested <see cref="Container"/>s; otherwise, nested
|
||||
/// <see cref="Container"/>s will not be searched.</param>
|
||||
/// <param name="predicate">Optional: A predicate to check if the <see cref="Item"/>
|
||||
/// of type <typeparamref name="T"/> is the target of the search.</param>
|
||||
/// <returns>The first <see cref="Item"/> of type <typeparamref name="T"/> that matches the optional <paramref name="predicate"/>.</returns>
|
||||
public T FindItemByType<T>(bool recurse = true, Predicate<T> predicate = null) where T : Item
|
||||
{
|
||||
if (current == null || current.Items.Count == 0)
|
||||
using (var queue = m_QueuePool.Get())
|
||||
{
|
||||
queue.Enqueue(this);
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var container = queue.Dequeue();
|
||||
foreach (var item in container.Items)
|
||||
{
|
||||
if (item is T typedItem && predicate?.Invoke(typedItem) != false)
|
||||
return typedItem;
|
||||
if (recurse && item is Container itemContainer)
|
||||
queue.Enqueue(itemContainer);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
List<Item> list = current.Items;
|
||||
|
||||
for (int i = 0; i < list.Count; ++i)
|
||||
{
|
||||
Item item = list[i];
|
||||
|
||||
if (item is T typedItem)
|
||||
{
|
||||
if (predicate?.Invoke(typedItem) == true)
|
||||
return typedItem;
|
||||
}
|
||||
else if (recurse && item is Container)
|
||||
{
|
||||
T check = RecurseFindItemByType(item, true, predicate);
|
||||
|
||||
if (check != null)
|
||||
return check;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
114
Projects/Server/Utilities/RefPool.cs
Normal file
114
Projects/Server/Utilities/RefPool.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Utilities
|
||||
{
|
||||
/// <summary> A resource reference object that can be disposed. </summary>
|
||||
/// <remarks>
|
||||
/// Disposing the reference is expected to return itself back into the
|
||||
/// original pool that created it.
|
||||
/// </remarks>
|
||||
public interface IRef : IDisposable { }
|
||||
|
||||
/// <summary>
|
||||
/// Base implementation of the <see cref="IRef"/> interface.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// New implementations of <see cref="IRef"/> should either derive from, or mirror
|
||||
/// the functionality of this base implementation.
|
||||
/// </remarks>
|
||||
/// <typeparam name="TDerived"></typeparam>
|
||||
public abstract class BaseRef<TDerived> : IRef where TDerived : IRef
|
||||
{
|
||||
private RefPool<TDerived> m_Pool;
|
||||
public BaseRef(RefPool<TDerived> pool)
|
||||
{
|
||||
m_Pool = pool;
|
||||
}
|
||||
protected abstract void OnDispose();
|
||||
public void Dispose()
|
||||
{
|
||||
OnDispose();
|
||||
m_Pool.Return((TDerived)(object)this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A resource reference pool that manages a collection of reusable resources.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRef">The <see cref="IRef"/> resource type the pool will contain.</typeparam>
|
||||
public class RefPool<TRef> where TRef : IRef
|
||||
{
|
||||
public delegate TRef Generator(RefPool<TRef> targetPool);
|
||||
public const int DEFAULT_RESOURCE_RETENTION = 10;
|
||||
|
||||
private Stack<TRef> m_Resources = new Stack<TRef>();
|
||||
private Generator m_Generator;
|
||||
private int m_MaxRefrenceRetention;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum number of unused resources to hold in the pool.
|
||||
/// </summary>
|
||||
public int MaxRefrenceRetention
|
||||
{
|
||||
get => m_MaxRefrenceRetention;
|
||||
set
|
||||
{
|
||||
m_MaxRefrenceRetention = value;
|
||||
while (m_Resources.Count > value) m_Resources.Pop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <param name="generator">The generator function for creating new resources.</param>
|
||||
/// <param name="preGenerateCount">An amount of resources that should be pre-generated during initialization of the resource pool.</param>
|
||||
public RefPool(Generator generator, int preGenerateCount = 0, int maxRefrenceRetention = DEFAULT_RESOURCE_RETENTION)
|
||||
{
|
||||
if (generator == null)
|
||||
throw new ArgumentNullException(nameof(generator));
|
||||
if (preGenerateCount > maxRefrenceRetention)
|
||||
throw new IndexOutOfRangeException($"{nameof(preGenerateCount)} greater than {nameof(maxRefrenceRetention)}");
|
||||
m_Generator = generator;
|
||||
m_MaxRefrenceRetention = maxRefrenceRetention;
|
||||
while (--preGenerateCount >= 0) m_Resources.Push(generator(this));
|
||||
}
|
||||
/// <summary>
|
||||
/// Retrieves a resource reference that is managed by this <see cref="RefPool{TRef}"/>. If the pool is has unused resources,
|
||||
/// it will remove one from the pool and return it; otherwise, a new resource will be generated.
|
||||
/// </summary>
|
||||
/// <returns>Unused resource, or a new resource if no unused resources available.</returns>
|
||||
public TRef Get() => m_Resources.TryPop(out TRef item) ? item : m_Generator(this);
|
||||
/// <summary>
|
||||
/// Returns a resource reference to the pool of unused resources.
|
||||
/// </summary>
|
||||
/// <param name="queueRef">Resource to be returned.</param>
|
||||
public void Return(TRef queueRef)
|
||||
{
|
||||
if (m_Resources.Count < MaxRefrenceRetention)
|
||||
m_Resources.Push(queueRef);
|
||||
}
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public class QueueRef<T> : Queue<T>, IRef
|
||||
{
|
||||
private RefPool<QueueRef<T>> m_Pool;
|
||||
private QueueRef(RefPool<QueueRef<T>> pool) { m_Pool = pool; }
|
||||
/// <summary>Clears the queue and returns this resource to its parent resource pool.</summary>
|
||||
public void Dispose() { Clear(); m_Pool.Return(this); }
|
||||
/// <summary>
|
||||
/// Generator function for creating instances of the <see cref="QueueRef{T}"/> resource.
|
||||
/// </summary>
|
||||
public static RefPool<QueueRef<T>>.Generator Generate = (targetPool) => new QueueRef<T>(targetPool);
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public class StackRef<T> : Stack<T>, IRef
|
||||
{
|
||||
private RefPool<StackRef<T>> m_Pool;
|
||||
private StackRef(RefPool<StackRef<T>> pool) { m_Pool = pool; }
|
||||
/// <summary>Clears the stack and returns this resource to its parent resource pool.</summary>
|
||||
public void Dispose() { Clear(); m_Pool.Return(this); }
|
||||
/// <summary>
|
||||
/// Generator function for creating instances of the <see cref="StackRef{T}"/> resource.
|
||||
/// </summary>
|
||||
public static RefPool<StackRef<T>>.Generator Generate = (targetPool) => new StackRef<T>(targetPool);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue