Mirror of Svelto.ECS because we're a fan of it
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

237 Zeilen
6.5KB

  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using System.Threading;
  4. using Svelto.DataStructures;
  5. using Svelto.ECS.Internal;
  6. namespace Svelto.ECS.Experimental
  7. {
  8. struct GroupsList
  9. {
  10. public static GroupsList Init()
  11. {
  12. var group = new GroupsList();
  13. group._groups = new FasterList<ExclusiveGroupStruct>();
  14. group._sets = new HashSet<ExclusiveGroupStruct>();
  15. return group;
  16. }
  17. public void Reset()
  18. {
  19. _sets.Clear();
  20. }
  21. public void AddRange(ExclusiveGroupStruct[] groupsToAdd, int length)
  22. {
  23. for (var i = 0; i < length; i++) _sets.Add(groupsToAdd[i]);
  24. }
  25. public void Add(ExclusiveGroupStruct group)
  26. {
  27. _sets.Add(group);
  28. }
  29. public void Exclude(ExclusiveGroupStruct[] groupsToIgnore, int length)
  30. {
  31. for (var i = 0; i < length; i++) _sets.Remove(groupsToIgnore[i]);
  32. }
  33. public void Exclude(ExclusiveGroupStruct groupsToIgnore)
  34. {
  35. _sets.Remove(groupsToIgnore);
  36. }
  37. public void Resize(uint preparecount)
  38. {
  39. _groups.Resize(preparecount);
  40. }
  41. public FasterList<ExclusiveGroupStruct> Evaluate()
  42. {
  43. _groups.Clear();
  44. foreach (var item in _sets) _groups.Add(item);
  45. return _groups;
  46. }
  47. FasterList<ExclusiveGroupStruct> _groups;
  48. HashSet<ExclusiveGroupStruct> _sets;
  49. }
  50. public ref struct QueryGroups
  51. {
  52. static readonly ThreadLocal<GroupsList> groups;
  53. static QueryGroups()
  54. {
  55. groups = new ThreadLocal<GroupsList>(GroupsList.Init);
  56. }
  57. public QueryGroups(LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
  58. {
  59. var groupsValue = QueryGroups.groups.Value;
  60. groupsValue.Reset();
  61. groupsValue.AddRange(groups.ToArrayFast(out var count), count);
  62. }
  63. public QueryGroups(ExclusiveGroupStruct group)
  64. {
  65. var groupsValue = groups.Value;
  66. groupsValue.Reset();
  67. groupsValue.Add(group);
  68. }
  69. public QueryGroups(uint preparecount)
  70. {
  71. var groupsValue = groups.Value;
  72. groupsValue.Reset();
  73. groupsValue.Resize(preparecount);
  74. }
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public QueryGroups Union(ExclusiveGroupStruct group)
  77. {
  78. var groupsValue = groups.Value;
  79. groupsValue.Add(group);
  80. return this;
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public QueryGroups Union(LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
  84. {
  85. var groupsValue = QueryGroups.groups.Value;
  86. groupsValue.AddRange(groups.ToArrayFast(out var count), count);
  87. return this;
  88. }
  89. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  90. public QueryGroups Except(ExclusiveGroupStruct group)
  91. {
  92. var groupsValue = groups.Value;
  93. groupsValue.Exclude(group);
  94. return this;
  95. }
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public QueryGroups Except(ExclusiveGroupStruct[] groupsToIgnore)
  98. {
  99. var groupsValue = groups.Value;
  100. groupsValue.Exclude(groupsToIgnore, groupsToIgnore.Length);
  101. return this;
  102. }
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. public QueryGroups Except(LocalFasterReadOnlyList<ExclusiveGroupStruct> groupsToIgnore)
  105. {
  106. var groupsValue = groups.Value;
  107. groupsValue.Exclude(groupsToIgnore.ToArrayFast(out var count), count);
  108. return this;
  109. }
  110. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  111. public QueryGroups Except(FasterList<ExclusiveGroupStruct> groupsToIgnore)
  112. {
  113. var groupsValue = groups.Value;
  114. groupsValue.Exclude(groupsToIgnore.ToArrayFast(out var count), count);
  115. return this;
  116. }
  117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  118. public QueryGroups Except(FasterReadOnlyList<ExclusiveGroupStruct> groupsToIgnore)
  119. {
  120. var groupsValue = groups.Value;
  121. groupsValue.Exclude(groupsToIgnore.ToArrayFast(out var count), count);
  122. return this;
  123. }
  124. // public QueryGroups WithAny<T>(EntitiesDB entitiesDB)
  125. // where T : struct, IEntityComponent
  126. // {
  127. // var group = groups.Value.reference;
  128. // var groupsCount = group.count;
  129. //
  130. // for (uint i = 0; i < groupsCount; i++)
  131. // {
  132. // if (entitiesDB.Count<T>(group[i]) == 0)
  133. // {
  134. // group.UnorderedRemoveAt(i);
  135. // i--;
  136. // groupsCount--;
  137. // }
  138. // }
  139. //
  140. // return this;
  141. // }
  142. public QueryResult Evaluate()
  143. {
  144. var groupsValue = groups.Value;
  145. return new QueryResult(groupsValue.Evaluate());
  146. }
  147. public void Evaluate(FasterList<ExclusiveGroupStruct> group)
  148. {
  149. var groupsValue = groups.Value;
  150. groupsValue.Evaluate().CopyTo(group.ToArrayFast(out var count), count);
  151. }
  152. }
  153. public readonly ref struct QueryResult
  154. {
  155. public QueryResult(FasterList<ExclusiveGroupStruct> group)
  156. {
  157. _group = group;
  158. }
  159. public LocalFasterReadOnlyList<ExclusiveGroupStruct> result => _group;
  160. readonly FasterReadOnlyList<ExclusiveGroupStruct> _group;
  161. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  162. public int Count<T>(EntitiesDB entitiesDB) where T : struct, _IInternalEntityComponent
  163. {
  164. var count = 0;
  165. var groupsCount = result.count;
  166. for (var i = 0; i < groupsCount; ++i) count += entitiesDB.Count<T>(result[i]);
  167. return count;
  168. }
  169. public int Max<T>(EntitiesDB entitiesDB) where T : struct, _IInternalEntityComponent
  170. {
  171. var max = 0;
  172. var groupsCount = result.count;
  173. for (var i = 0; i < groupsCount; ++i)
  174. {
  175. var count = entitiesDB.Count<T>(result[i]);
  176. if (count > max) max = count;
  177. }
  178. return max;
  179. }
  180. }
  181. }