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.

GroupsEnumerable.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using DBC.ECS;
  2. using Svelto.DataStructures;
  3. namespace Svelto.ECS
  4. {
  5. /// <summary>
  6. /// NOTE THESE ENUMERABLES EXIST TO AVOID BOILERPLATE CODE AS THEY SKIP 0 SIZED GROUPS
  7. /// However if the normal pattern with the double foreach is used, this is not necessary
  8. /// Note: atm cannot be ref structs because they are returned in a valuetuple
  9. /// </summary>
  10. /// <typeparam name="T1"></typeparam>
  11. /// <typeparam name="T2"></typeparam>
  12. /// <typeparam name="T3"></typeparam>
  13. /// <typeparam name="T4"></typeparam>
  14. public readonly ref struct GroupsEnumerable<T1, T2, T3, T4> where T1 : struct, IEntityComponent
  15. where T2 : struct, IEntityComponent
  16. where T3 : struct, IEntityComponent
  17. where T4 : struct, IEntityComponent
  18. {
  19. readonly EntitiesDB _db;
  20. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  21. public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
  22. {
  23. _db = db;
  24. _groups = groups;
  25. }
  26. public ref struct GroupsIterator
  27. {
  28. public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
  29. {
  30. _groups = groups;
  31. _indexGroup = -1;
  32. _entitiesDB = db;
  33. }
  34. public bool MoveNext()
  35. {
  36. //attention, the while is necessary to skip empty groups
  37. while (++_indexGroup < _groups.count)
  38. {
  39. var entityCollection1 = _entitiesDB.QueryEntities<T1, T2, T3>(_groups[_indexGroup]);
  40. if (entityCollection1.count == 0)
  41. continue;
  42. var entityCollection2 = _entitiesDB.QueryEntities<T4>(_groups[_indexGroup]);
  43. if (entityCollection2.count == 0)
  44. continue;
  45. Check.Assert(entityCollection1.count == entityCollection2.count
  46. , "congratulation, you found a bug in Svelto, please report it");
  47. var array = entityCollection1;
  48. var array2 = entityCollection2;
  49. _buffers = new EntityCollection<T1, T2, T3, T4>(array.buffer1, array.buffer2, array.buffer3, array2);
  50. break;
  51. }
  52. var moveNext = _indexGroup < _groups.count;
  53. if (moveNext == false)
  54. Reset();
  55. return moveNext;
  56. }
  57. public void Reset() { _indexGroup = -1; }
  58. public RefCurrent<T1, T2, T3, T4> Current => new RefCurrent<T1, T2, T3, T4>(_buffers, _groups[_indexGroup]);
  59. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  60. int _indexGroup;
  61. EntityCollection<T1, T2, T3, T4> _buffers;
  62. readonly EntitiesDB _entitiesDB;
  63. }
  64. public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
  65. }
  66. public ref struct RefCurrent<T1, T2, T3, T4> where T1 : struct, IEntityComponent
  67. where T2 : struct, IEntityComponent
  68. where T3 : struct, IEntityComponent
  69. where T4 : struct, IEntityComponent
  70. {
  71. public RefCurrent(in EntityCollection<T1, T2, T3, T4> buffers, ExclusiveGroupStruct group)
  72. {
  73. _buffers = buffers;
  74. _group = group;
  75. }
  76. public void Deconstruct(out EntityCollection<T1, T2, T3, T4> buffers, out ExclusiveGroupStruct group)
  77. {
  78. buffers = _buffers;
  79. group = _group;
  80. }
  81. public readonly EntityCollection<T1, T2, T3, T4> _buffers;
  82. public readonly ExclusiveGroupStruct _group;
  83. }
  84. /// <summary>
  85. /// ToDo source gen could return the implementation of IBuffer directly, but cannot be done manually
  86. /// </summary>
  87. /// <typeparam name="T1"></typeparam>
  88. /// <typeparam name="T2"></typeparam>
  89. /// <typeparam name="T3"></typeparam>
  90. public readonly ref struct GroupsEnumerable<T1, T2, T3> where T1 : struct, IEntityComponent
  91. where T2 : struct, IEntityComponent
  92. where T3 : struct, IEntityComponent
  93. {
  94. readonly EntitiesDB _db;
  95. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  96. public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
  97. {
  98. _db = db;
  99. _groups = groups;
  100. }
  101. public ref struct GroupsIterator
  102. {
  103. public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
  104. {
  105. _groups = groups;
  106. _indexGroup = -1;
  107. _entitiesDB = db;
  108. }
  109. public bool MoveNext()
  110. {
  111. //attention, the while is necessary to skip empty groups
  112. while (++_indexGroup < _groups.count)
  113. {
  114. var entityCollection = _entitiesDB.QueryEntities<T1, T2, T3>(_groups[_indexGroup]);
  115. if (entityCollection.count == 0)
  116. continue;
  117. _buffers = entityCollection;
  118. break;
  119. }
  120. var moveNext = _indexGroup < _groups.count;
  121. if (moveNext == false)
  122. Reset();
  123. return moveNext;
  124. }
  125. public void Reset() { _indexGroup = -1; }
  126. public RefCurrent<T1, T2, T3> Current => new RefCurrent<T1, T2, T3>(_buffers, _groups[_indexGroup]);
  127. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  128. int _indexGroup;
  129. EntityCollection<T1, T2, T3> _buffers;
  130. readonly EntitiesDB _entitiesDB;
  131. }
  132. public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
  133. }
  134. public ref struct RefCurrent<T1, T2, T3> where T1 : struct, IEntityComponent
  135. where T2 : struct, IEntityComponent
  136. where T3 : struct, IEntityComponent
  137. {
  138. public RefCurrent(in EntityCollection<T1, T2, T3> buffers, ExclusiveGroupStruct group)
  139. {
  140. _buffers = buffers;
  141. _group = group;
  142. }
  143. public void Deconstruct(out EntityCollection<T1, T2, T3> buffers, out ExclusiveGroupStruct group)
  144. {
  145. buffers = _buffers;
  146. group = _group;
  147. }
  148. public readonly EntityCollection<T1, T2, T3> _buffers;
  149. public readonly ExclusiveGroupStruct _group;
  150. }
  151. public readonly ref struct GroupsEnumerable<T1, T2>
  152. where T1 : struct, IEntityComponent where T2 : struct, IEntityComponent
  153. {
  154. public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
  155. {
  156. _db = db;
  157. _groups = groups;
  158. }
  159. public ref struct GroupsIterator
  160. {
  161. public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
  162. {
  163. _db = db;
  164. _groups = groups;
  165. _indexGroup = -1;
  166. }
  167. public bool MoveNext()
  168. {
  169. //attention, the while is necessary to skip empty groups
  170. while (++_indexGroup < _groups.count)
  171. {
  172. var entityCollection = _db.QueryEntities<T1, T2>(_groups[_indexGroup]);
  173. if (entityCollection.count == 0)
  174. continue;
  175. _buffers = entityCollection;
  176. break;
  177. }
  178. var moveNext = _indexGroup < _groups.count;
  179. if (moveNext == false)
  180. Reset();
  181. return moveNext;
  182. }
  183. public void Reset() { _indexGroup = -1; }
  184. public RefCurrent<T1, T2> Current => new RefCurrent<T1, T2>(_buffers, _groups[_indexGroup]);
  185. readonly EntitiesDB _db;
  186. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  187. int _indexGroup;
  188. EntityCollection<T1, T2> _buffers;
  189. }
  190. public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
  191. readonly EntitiesDB _db;
  192. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  193. }
  194. public ref struct RefCurrent<T1, T2> where T1 : struct, IEntityComponent where T2 : struct, IEntityComponent
  195. {
  196. public RefCurrent(in EntityCollection<T1, T2> buffers, ExclusiveGroupStruct group)
  197. {
  198. _buffers = buffers;
  199. _group = group;
  200. }
  201. public void Deconstruct(out EntityCollection<T1, T2> buffers, out ExclusiveGroupStruct group)
  202. {
  203. buffers = _buffers;
  204. group = _group;
  205. }
  206. public readonly EntityCollection<T1, T2> _buffers;
  207. public readonly ExclusiveGroupStruct _group;
  208. }
  209. public readonly ref struct GroupsEnumerable<T1> where T1 : struct, IEntityComponent
  210. {
  211. public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
  212. {
  213. _db = db;
  214. _groups = groups;
  215. }
  216. public ref struct GroupsIterator
  217. {
  218. public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
  219. {
  220. _db = db;
  221. _groups = groups;
  222. _indexGroup = -1;
  223. }
  224. public bool MoveNext()
  225. {
  226. //attention, the while is necessary to skip empty groups
  227. while (++_indexGroup < _groups.count)
  228. {
  229. var entityCollection = _db.QueryEntities<T1>(_groups[_indexGroup]);
  230. if (entityCollection.count == 0)
  231. continue;
  232. _buffer = entityCollection;
  233. break;
  234. }
  235. return _indexGroup < _groups.count;
  236. }
  237. public void Reset() { _indexGroup = -1; }
  238. public RefCurrent<T1> Current => new RefCurrent<T1>(_buffer, _groups[_indexGroup]);
  239. readonly EntitiesDB _db;
  240. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  241. int _indexGroup;
  242. EntityCollection<T1> _buffer;
  243. }
  244. public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
  245. readonly EntitiesDB _db;
  246. readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
  247. }
  248. public ref struct RefCurrent<T1> where T1 : struct, IEntityComponent
  249. {
  250. public RefCurrent(in EntityCollection<T1> buffers, ExclusiveGroupStruct group)
  251. {
  252. _buffers = buffers;
  253. _group = group;
  254. }
  255. public void Deconstruct(out EntityCollection<T1> buffers, out ExclusiveGroupStruct group)
  256. {
  257. buffers = _buffers;
  258. group = _group;
  259. }
  260. public readonly EntityCollection<T1> _buffers;
  261. public readonly ExclusiveGroupStruct _group;
  262. }
  263. }