Change api to support archetypes and move stuff to different namespaces
This commit is contained in:
parent
a8ac473666
commit
499cea5b44
10 changed files with 93 additions and 97 deletions
81
src/Internal/Containers/PagedSparseSet.bf
Normal file
81
src/Internal/Containers/PagedSparseSet.bf
Normal file
|
@ -0,0 +1,81 @@
|
|||
namespace Theater_ECS.Internal.Containers;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
class PagedSparseSet
|
||||
{
|
||||
public const uint32 PageSize = 4096;
|
||||
|
||||
private List<List<Entity>> _sparse = new .()~ DeleteContainerAndItems!(_);
|
||||
private List<Entity> _packed = new .() ~ delete _;
|
||||
private UList _packedEntities ~ delete _;
|
||||
|
||||
public this(int_cosize size)
|
||||
{
|
||||
_packedEntities = new .(size);
|
||||
}
|
||||
|
||||
public bool Contains(Entity e)
|
||||
{
|
||||
return _sparse[e.Index/PageSize][e.Index % PageSize] != Entity.Null;
|
||||
}
|
||||
|
||||
public void Add(Entity e, void* toAdd)
|
||||
{
|
||||
EnsureLoadedPage(e.Index/PageSize);
|
||||
_packed.Add(e);
|
||||
_packedEntities.Add(toAdd);
|
||||
_sparse[e.Index/PageSize][e.Index % PageSize] = (.)_packed.Count-1;
|
||||
}
|
||||
|
||||
public void Remove(Entity e)
|
||||
{
|
||||
EnsurePage(e.Index/PageSize);
|
||||
if(_sparse[e.Index/PageSize] == null)
|
||||
return;
|
||||
|
||||
let toRm = _packed[_sparse[e.Index/PageSize][e.Index % PageSize].Index];
|
||||
let b = _packed.Back;
|
||||
_packed[_sparse[e.Index/PageSize][e.Index % PageSize].Index] = b;
|
||||
_packed.Back = toRm;
|
||||
|
||||
_sparse[b.Index/PageSize][b.Index % PageSize] = Entity(e.Index, b.Version);
|
||||
_sparse[e.Index/PageSize][e.Index % PageSize] = Entity.Null;
|
||||
|
||||
_packed.PopBack();
|
||||
}
|
||||
|
||||
[Inline, Unchecked]
|
||||
public void* Get(Entity e)
|
||||
{
|
||||
return _packedEntities[(.)(_sparse[e.Index/PageSize][e.Index % PageSize].Index)];
|
||||
}
|
||||
|
||||
public Span<Entity> GetAll()
|
||||
{
|
||||
return _packed;
|
||||
}
|
||||
|
||||
public int_cosize Count() => (.)_packed.Count;
|
||||
|
||||
///Ensure that a page exist and is loaded
|
||||
public void EnsureLoadedPage(uint32 page)
|
||||
{
|
||||
EnsurePage(page);
|
||||
if(_sparse[page] == null)
|
||||
{
|
||||
_sparse[page] = new List<Entity>(PageSize);
|
||||
_sparse[page].[Friend]Count = 4096;
|
||||
_sparse[page].SetAll(Entity.Null);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
///Ensure that a page exists, it might be null though
|
||||
private void EnsurePage(uint32 page)
|
||||
{
|
||||
while(_sparse.Count < page+1)
|
||||
_sparse.Add(null);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue