2023-02-05 19:59:45 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
|
|
|
public interface IEcsFieldPool
|
|
|
|
{
|
|
|
|
public bool Has(int index);
|
|
|
|
public void Add(int index);
|
|
|
|
}
|
|
|
|
public class EcsFieldPool<T> : IEcsFieldPool
|
|
|
|
{
|
2023-02-06 01:27:32 +08:00
|
|
|
private int _id;
|
2023-02-05 19:59:45 +08:00
|
|
|
private SparseSet _sparseSet;
|
|
|
|
private T[] _denseItems;
|
|
|
|
|
2023-02-06 01:27:32 +08:00
|
|
|
public int ID => _id;
|
2023-02-05 19:59:45 +08:00
|
|
|
|
|
|
|
public ref T this[int index]
|
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
get => ref _denseItems[_sparseSet[index]];
|
|
|
|
}
|
|
|
|
|
2023-02-06 01:27:32 +08:00
|
|
|
public EcsFieldPool(int capacity)
|
|
|
|
{
|
|
|
|
_denseItems = new T[capacity];
|
|
|
|
_sparseSet = new SparseSet(capacity);
|
|
|
|
}
|
|
|
|
|
2023-02-05 19:59:45 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public ref T Add(int index)
|
|
|
|
{
|
|
|
|
_sparseSet.Add(index);
|
|
|
|
_sparseSet.Normalize(ref _denseItems);
|
|
|
|
return ref _denseItems[_sparseSet.IndexOf(index)];
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public bool Has(int index)
|
|
|
|
{
|
|
|
|
return _sparseSet.Contains(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IEcsFieldPool
|
|
|
|
void IEcsFieldPool.Add(int index)
|
|
|
|
{
|
|
|
|
Add(index);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|