chore: use file-scoped namespace

This commit is contained in:
NaBian committed 2022-03-13 20:28:58 +08:00
1 parent 99d2fafb72
commit bbf1b7d4c6
748 files changed
+57929 -58678

No files matched your search

@@ -1,9 +1,8 @@
namespace HandyControl.Collections
{
public interface IPool<T>
{
T Acquire();
namespace HandyControl.Collections;
bool Release(T instance);
}
}
public interface IPool<T>
{
T Acquire();
bool Release(T instance);
}
@@ -2,63 +2,62 @@
using System;
namespace HandyControl.Collections
namespace HandyControl.Collections;
public class SimplePool<T> : IPool<T>
{
public class SimplePool<T> : IPool<T>
private readonly object[] _pool;
private int _poolSize;
public SimplePool(int maxPoolSize)
{
private readonly object[] _pool;
private int _poolSize;
public SimplePool(int maxPoolSize)
if (maxPoolSize <= 0)
{
if (maxPoolSize <= 0)
{
throw new ArgumentException("The max pool size must be > 0");
}
_pool = new object[maxPoolSize];
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!");
}
public virtual T Acquire()
if (_poolSize < _pool.Length)
{
if (_poolSize > 0)
{
var lastPooledIndex = _poolSize - 1;
var instance = (T) _pool[lastPooledIndex];
_pool[lastPooledIndex] = null;
_poolSize--;
return instance;
}
return default;
_pool[_poolSize] = instance;
_poolSize++;
return true;
}
public virtual bool Release(T instance)
{
if (IsInPool(instance))
{
throw new Exception("Already in the pool!");
}
return false;
}
if (_poolSize < _pool.Length)
private bool IsInPool(T instance)
{
for (var i = 0; i < _poolSize; i++)
{
if (Equals(_pool[i], instance))
{
_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;
}
return false;
}
}
}
@@ -1,28 +1,27 @@
namespace HandyControl.Collections
namespace HandyControl.Collections;
public class SynchronizedPool<T> : SimplePool<T>
{
public class SynchronizedPool<T> : SimplePool<T>
private readonly object _lockObj = new();
public SynchronizedPool(int maxPoolSize) : base(maxPoolSize)
{
private readonly object _lockObj = new();
public SynchronizedPool(int maxPoolSize) : base(maxPoolSize)
}
public override T Acquire()
{
lock (_lockObj)
{
}
public override T Acquire()
{
lock (_lockObj)
{
return base.Acquire();
}
}
public override bool Release(T element)
{
lock (_lockObj)
{
return base.Release(element);
}
return base.Acquire();
}
}
}
public override bool Release(T element)
{
lock (_lockObj)
{
return base.Release(element);
}
}
}