using Svelto.Common;
using Svelto.DataStructures;
using Svelto.ECS.Internal;
namespace Svelto.ECS
{
///
/// ToDo it would be interesting to have a version of this dedicated to unmanaged, IEntityComponent
/// that can be burstifiable
///
///
public readonly struct AllGroupsEnumerable where T1 : struct, IEntityComponent
{
public ref struct GroupCollection
{
internal EntityCollection collection;
internal ExclusiveGroupStruct group;
public void Deconstruct(out EntityCollection collection, out ExclusiveGroupStruct group)
{
collection = this.collection;
group = this.@group;
}
}
public AllGroupsEnumerable(EntitiesDB db)
{
_db = db;
}
public ref struct GroupsIterator
{
public GroupsIterator(EntitiesDB db) : this()
{
_db = db.FindGroups_INTERNAL(TypeCache.type).GetEnumerator();
}
public bool MoveNext()
{
//attention, the while is necessary to skip empty groups
while (_db.MoveNext() == true)
{
FasterDictionary.KeyValuePairFast group = _db.Current;
ITypeSafeDictionary typeSafeDictionary = @group.Value as ITypeSafeDictionary;
if (typeSafeDictionary.count == 0) continue;
_array.collection = new EntityCollection(typeSafeDictionary.GetValues(out var count), count);
_array.@group = new ExclusiveGroupStruct(group.Key);
return true;
}
return false;
}
public GroupCollection Current => _array;
FasterDictionary.FasterDictionaryKeyValueEnumerator _db;
GroupCollection _array;
}
public GroupsIterator GetEnumerator()
{
return new GroupsIterator(_db);
}
readonly EntitiesDB _db;
}
}