mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-19 10:34:37 +08:00
45 lines
1.0 KiB
C#
45 lines
1.0 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
|
|
namespace DCFApixels.DragonECS
|
|
{
|
|
internal sealed class IntDispenser
|
|
{
|
|
private readonly ConcurrentStack<int> _freeInts;
|
|
private int _increment;
|
|
|
|
#region Properties
|
|
public int LastInt => _increment;
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public IntDispenser()
|
|
{
|
|
_freeInts = new ConcurrentStack<int>();
|
|
_increment = 0;
|
|
}
|
|
public IntDispenser(int startIncrement)
|
|
{
|
|
_freeInts = new ConcurrentStack<int>();
|
|
_increment = startIncrement;
|
|
}
|
|
#endregion
|
|
|
|
#region GetFree/Release
|
|
public int GetFree()
|
|
{
|
|
if (!_freeInts.TryPop(out int result))
|
|
{
|
|
result = Interlocked.Increment(ref _increment);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void Release(int released)
|
|
{
|
|
_freeInts.Push(released);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|