Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AllGroupsEnumerable.cs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Svelto.Common;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. /// <summary>
  7. /// ToDo it would be interesting to have a version of this dedicated to unmanaged, IEntityComponent
  8. /// that can be burstifiable
  9. /// </summary>
  10. /// <typeparam name="T1"></typeparam>
  11. public readonly struct AllGroupsEnumerable<T1> where T1 : struct, IEntityComponent
  12. {
  13. public ref struct GroupCollection
  14. {
  15. internal EntityCollection<T1> collection;
  16. internal ExclusiveGroupStruct group;
  17. public void Deconstruct(out EntityCollection<T1> collection, out ExclusiveGroupStruct group)
  18. {
  19. collection = this.collection;
  20. group = this.@group;
  21. }
  22. }
  23. public AllGroupsEnumerable(EntitiesDB db)
  24. {
  25. _db = db;
  26. }
  27. public ref struct GroupsIterator
  28. {
  29. public GroupsIterator(EntitiesDB db) : this()
  30. {
  31. _db = db.FindGroups_INTERNAL(TypeCache<T1>.type).GetEnumerator();
  32. }
  33. public bool MoveNext()
  34. {
  35. //attention, the while is necessary to skip empty groups
  36. while (_db.MoveNext() == true)
  37. {
  38. FasterDictionary<ExclusiveGroupStruct, ITypeSafeDictionary>.KeyValuePairFast group = _db.Current;
  39. ITypeSafeDictionary<T1> typeSafeDictionary = @group.Value as ITypeSafeDictionary<T1>;
  40. if (typeSafeDictionary.count == 0) continue;
  41. _array.collection = new EntityCollection<T1>(typeSafeDictionary.GetValues(out var count), count);
  42. _array.@group = new ExclusiveGroupStruct(group.Key);
  43. return true;
  44. }
  45. return false;
  46. }
  47. public GroupCollection Current => _array;
  48. FasterDictionary<ExclusiveGroupStruct, ITypeSafeDictionary>.FasterDictionaryKeyValueEnumerator _db;
  49. GroupCollection _array;
  50. }
  51. public GroupsIterator GetEnumerator()
  52. {
  53. return new GroupsIterator(_db);
  54. }
  55. readonly EntitiesDB _db;
  56. }
  57. }