You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
namespace Mapbox.Unity.MeshGeneration.Data
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class ObjectPool<T>
|
|
|
|
|
{
|
|
|
|
|
private Queue<T> _objects;
|
|
|
|
|
private Func<T> _objectGenerator;
|
|
|
|
|
|
|
|
|
|
public ObjectPool(Func<T> objectGenerator)
|
|
|
|
|
{
|
|
|
|
|
if (objectGenerator == null) throw new ArgumentNullException("objectGenerator");
|
|
|
|
|
_objects = new Queue<T>();
|
|
|
|
|
_objectGenerator = objectGenerator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetObject()
|
|
|
|
|
{
|
|
|
|
|
if (_objects.Count > 0)
|
|
|
|
|
return _objects.Dequeue();
|
|
|
|
|
return _objectGenerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Put(T item)
|
|
|
|
|
{
|
|
|
|
|
_objects.Enqueue(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_objects.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T> GetQueue()
|
|
|
|
|
{
|
|
|
|
|
return _objects;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|