2023-11-20 23:11:17 +00:00
|
|
|
using System;
|
2023-11-03 19:40:26 +00:00
|
|
|
using MoonTools.ECS.Collections;
|
|
|
|
|
|
|
|
namespace MoonTools.ECS;
|
|
|
|
|
2023-11-20 23:11:17 +00:00
|
|
|
internal class IdAssigner : IDisposable
|
2023-11-03 19:40:26 +00:00
|
|
|
{
|
|
|
|
uint Next;
|
|
|
|
NativeArray<uint> AvailableIds = new NativeArray<uint>();
|
|
|
|
|
2023-11-20 23:11:17 +00:00
|
|
|
private bool IsDisposed;
|
|
|
|
|
2023-11-16 18:36:11 +00:00
|
|
|
public uint Assign()
|
2023-11-03 19:40:26 +00:00
|
|
|
{
|
2023-11-16 18:36:11 +00:00
|
|
|
if (AvailableIds.TryPop(out var id))
|
2023-11-03 19:40:26 +00:00
|
|
|
{
|
2023-11-08 01:46:44 +00:00
|
|
|
return id;
|
2023-11-03 19:40:26 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 01:46:44 +00:00
|
|
|
id = Next;
|
|
|
|
Next += 1;
|
2023-11-03 19:40:26 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unassign(uint id)
|
|
|
|
{
|
|
|
|
AvailableIds.Append(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(IdAssigner other)
|
|
|
|
{
|
|
|
|
AvailableIds.CopyTo(other.AvailableIds);
|
|
|
|
other.Next = Next;
|
|
|
|
}
|
2023-11-20 23:11:17 +00:00
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!IsDisposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
AvailableIds.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
IsDisposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
|
|
// ~IdAssigner()
|
|
|
|
// {
|
|
|
|
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
// Dispose(disposing: false);
|
|
|
|
// }
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
Dispose(disposing: true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
2023-11-03 19:40:26 +00:00
|
|
|
}
|