36 lines
682 B
C#
36 lines
682 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Global.Shared.OcTree
|
|
{
|
|
public interface IPoolable
|
|
{
|
|
void Reset();
|
|
}
|
|
|
|
public class Pool<E> where E : IPoolable
|
|
{
|
|
private readonly Func<E> creator;
|
|
private readonly List<E> pool = new List<E>();
|
|
|
|
public Pool(Func<E> creator)
|
|
{
|
|
this.creator = creator;
|
|
}
|
|
|
|
public E Obtain()
|
|
{
|
|
if (pool.Count <= 0) return creator();
|
|
|
|
var e = pool[0];
|
|
pool.RemoveAt(0);
|
|
return e;
|
|
}
|
|
|
|
public void Release(E e)
|
|
{
|
|
e.Reset();
|
|
pool.Add(e);
|
|
}
|
|
}
|
|
} |