adjusted project structure

This commit is contained in:
NaBian committed 2021-09-05 13:26:48 +08:00
1 parent aa7310d495
commit 904b32f986
18 files changed
+79 -212

No files matched your search

@@ -0,0 +1,9 @@
namespace HandyControl.Collections
{
public interface IPool<T>
{
T Acquire();
bool Release(T instance);
}
}
@@ -0,0 +1,64 @@
//adapted from https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/util/Pools.java
using System;
namespace HandyControl.Collections
{
public class SimplePool<T> : IPool<T>
{
private readonly object[] _pool;
private int _poolSize;
public SimplePool(int maxPoolSize)
{
if (maxPoolSize <= 0)
{
throw new ArgumentException("The max pool size must be > 0");
}
_pool = new object[maxPoolSize];
}
public virtual T Acquire()
{
if (_poolSize > 0)
{
var lastPooledIndex = _poolSize - 1;
var instance = (T) _pool[lastPooledIndex];
_pool[lastPooledIndex] = null;
_poolSize--;
return instance;
}
return default;
}
public virtual bool Release(T instance)
{
if (IsInPool(instance))
{
throw new Exception("Already in the pool!");
}
if (_poolSize < _pool.Length)
{
_pool[_poolSize] = instance;
_poolSize++;
return true;
}
return false;
}
private bool IsInPool(T instance)
{
for (var i = 0; i < _poolSize; i++)
{
if (Equals(_pool[i], instance))
{
return true;
}
}
return false;
}
}
}
@@ -0,0 +1,28 @@
namespace HandyControl.Collections
{
public class SynchronizedPool<T> : SimplePool<T>
{
private readonly object _lockObj = new();
public SynchronizedPool(int maxPoolSize) : base(maxPoolSize)
{
}
public override T Acquire()
{
lock (_lockObj)
{
return base.Acquire();
}
}
public override bool Release(T element)
{
lock (_lockObj)
{
return base.Release(element);
}
}
}
}