using System.Collections.Generic;
namespace IFramework
{
///
/// ArrayPoolArg
///
public struct ArrayPoolArg : IEventArgs
{
///
/// 长度
///
public int length;
///
///
///
///
public ArrayPoolArg(int length)
{
this.length = length;
}
}
///
/// 数组
///
///
public class ArrayPool : ObjectPool
{
private Queue _lengthqueue = new Queue();
///
/// 创建
///
///
///
protected override T[] CreateNew(IEventArgs arg)
{
ArrayPoolArg len = (ArrayPoolArg)arg;
return new T[len.length];
}
Queue queue = new Queue();
///
/// 获取
///
///
///
public override T[] Get(IEventArgs arg = null)
{
ArrayPoolArg len = (ArrayPoolArg)arg;
int length = len.length;
lock (para)
{
T[] t;
if (pool.Count > 0 && _lengthqueue.Contains(length))
{
while (_lengthqueue.Peek() != length)
{
_lengthqueue.Dequeue();
queue.Enqueue(pool.Dequeue());
}
t = pool.Dequeue();
while (pool.Count != 0) queue.Enqueue(pool.Dequeue());
int _count = queue.Count;
for (int i = 0; i < _count; i++)
{
var tmp = queue.Dequeue();
int _len = tmp.Length;
_lengthqueue.Enqueue(_len);
pool.Enqueue(tmp);
}
}
else
{
t = CreateNew(arg);
OnCreate(t, arg);
}
OnGet(t, arg);
return t;
}
}
///
/// 回收
///
///
///
///
public override bool Set(T[] t, IEventArgs arg = null)
{
lock (para)
{
if (!pool.Contains(t))
{
if (OnSet(t, arg))
{
int _len = t.Length;
_lengthqueue.Enqueue(_len);
pool.Enqueue(t);
}
return true;
}
else
{
Log.E("Set Err: Exist " + type);
return false;
}
}
}
///
/// 释放
///
protected override void OnDispose()
{
base.OnDispose();
if (_lengthqueue != null)
{
_lengthqueue.Clear();
}
}
}
}