using System.Collections.Generic;
using UnityEngine;
namespace Unity.XR.CoreUtils
{
///
/// A pool of collection objects for avoiding allocations when new empty collections are needed frequently.
///
/// The desired type of collection.
/// The value type of the ICollection specified in TCollection.
public static class CollectionPool where TCollection : ICollection, new()
{
static readonly Queue k_CollectionQueue = new Queue();
///
/// Gets a collection of the given type from the pool. Creates a new collection object if the pool is empty.
///
/// An empty collection.
public static TCollection GetCollection()
{
return k_CollectionQueue.Count > 0 ? k_CollectionQueue.Dequeue() : new TCollection();
}
///
/// Returns a collection to the pool. The collection is cleared.
///
/// The collection to be added to the pool.
public static void RecycleCollection(TCollection collection)
{
collection.Clear();
k_CollectionQueue.Enqueue(collection);
}
}
}