2023-11-21 19:50:17 +00:00
|
|
|
using System;
|
|
|
|
using MoonTools.ECS.Collections;
|
|
|
|
|
|
|
|
namespace MoonTools.ECS;
|
|
|
|
|
|
|
|
internal class IdAssigner : IDisposable
|
|
|
|
{
|
2023-12-19 01:58:08 +00:00
|
|
|
int Next = 0;
|
|
|
|
NativeArray<int> AvailableIds = new NativeArray<int>();
|
2023-11-21 19:50:17 +00:00
|
|
|
|
|
|
|
private bool IsDisposed;
|
|
|
|
|
2023-12-19 01:58:08 +00:00
|
|
|
public int Assign()
|
2023-11-21 19:50:17 +00:00
|
|
|
{
|
|
|
|
if (AvailableIds.TryPop(out var id))
|
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = Next;
|
|
|
|
Next += 1;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2023-12-19 01:58:08 +00:00
|
|
|
public void Unassign(int id)
|
2023-11-21 19:50:17 +00:00
|
|
|
{
|
|
|
|
AvailableIds.Append(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(IdAssigner other)
|
|
|
|
{
|
|
|
|
AvailableIds.CopyTo(other.AvailableIds);
|
|
|
|
other.Next = Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!IsDisposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
AvailableIds.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
Dispose(disposing: true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
}
|