encompass-cs/encompass-cs/IDManager.cs

29 lines
527 B
C#
Raw Normal View History

2019-12-20 20:52:38 +00:00
using System.Collections.Generic;
namespace Encompass
{
internal class IDManager
{
2020-03-20 07:09:57 +00:00
int _nextID = 0;
2019-12-20 20:52:38 +00:00
2020-03-20 07:09:57 +00:00
private readonly Stack<int> _availableIDs = new Stack<int>();
2019-12-20 20:52:38 +00:00
public int NextID()
{
2020-03-20 07:09:57 +00:00
if (_availableIDs.Count > 0)
2019-12-20 20:52:38 +00:00
{
2020-03-20 07:09:57 +00:00
return _availableIDs.Pop();
2019-12-20 20:52:38 +00:00
}
else
{
2020-03-20 07:09:57 +00:00
return _nextID++;
2019-12-20 20:52:38 +00:00
}
}
2020-03-20 07:09:57 +00:00
public void Free(int id)
2019-12-20 20:52:38 +00:00
{
2020-03-20 07:09:57 +00:00
_availableIDs.Push(id);
2019-12-20 20:52:38 +00:00
}
}
}