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.

153 lines
4.4KB

  1. using System.Runtime.CompilerServices;
  2. using System.Threading;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS.Experimental
  5. {
  6. struct GroupsList
  7. {
  8. static GroupsList()
  9. {
  10. groups = new FasterList<ExclusiveGroupStruct>();
  11. }
  12. static readonly FasterList<ExclusiveGroupStruct> groups;
  13. public FasterList<ExclusiveGroupStruct> reference => groups;
  14. }
  15. public ref struct QueryGroups
  16. {
  17. static readonly ThreadLocal<GroupsList> groups = new ThreadLocal<GroupsList>();
  18. public QueryGroups(LocalFasterReadOnlyList<ExclusiveGroupStruct> findGroups)
  19. {
  20. var groupsValue = groups.Value;
  21. var group = groupsValue.reference;
  22. group.FastClear();
  23. for (int i = 0; i < findGroups.count; i++)
  24. group.Add(findGroups[i]);
  25. }
  26. public QueryGroups(ExclusiveGroupStruct findGroups)
  27. {
  28. var groupsValue = groups.Value;
  29. var group = groupsValue.reference;
  30. group.FastClear();
  31. group.Add(findGroups);
  32. }
  33. public QueryGroups(uint preparecount)
  34. {
  35. var groupsValue = groups.Value;
  36. var group = groupsValue.reference;
  37. group.FastClear();
  38. group.EnsureCapacity(preparecount);
  39. }
  40. public QueryResult Except(ExclusiveGroupStruct[] groupsToIgnore)
  41. {
  42. var group = groups.Value.reference;
  43. var groupsCount = group.count;
  44. for (int i = 0; i < groupsToIgnore.Length; i++)
  45. for (int j = 0; j < groupsCount; j++)
  46. {
  47. if (groupsToIgnore[i] == group[j])
  48. {
  49. group.UnorderedRemoveAt(j);
  50. j--;
  51. groupsCount--;
  52. }
  53. }
  54. return new QueryResult(group);
  55. }
  56. public QueryResult Except(FasterList<ExclusiveGroupStruct> groupsToIgnore)
  57. {
  58. var group = groups.Value.reference;
  59. var groupsCount = group.count;
  60. for (int i = 0; i < groupsToIgnore.count; i++)
  61. for (int j = 0; j < groupsCount; j++)
  62. {
  63. if (groupsToIgnore[i] == group[j])
  64. {
  65. group.UnorderedRemoveAt(j);
  66. j--;
  67. groupsCount--;
  68. }
  69. }
  70. return new QueryResult(group);
  71. }
  72. public QueryResult Except(ExclusiveGroupStruct groupsToIgnore)
  73. {
  74. var group = groups.Value.reference;
  75. var groupsCount = group.count;
  76. for (int j = 0; j < groupsCount; j++)
  77. if (groupsToIgnore == group[j])
  78. {
  79. group.UnorderedRemoveAt(j);
  80. j--;
  81. groupsCount--;
  82. }
  83. return new QueryResult(group);
  84. }
  85. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  86. public int Count<T>
  87. (EntitiesDB entitiesDB, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) where T : struct, IEntityComponent
  88. {
  89. int count = 0;
  90. var groupsCount = groups.count;
  91. for (int i = 0; i < groupsCount; ++i)
  92. {
  93. count += entitiesDB.Count<T>(groups[i]);
  94. }
  95. return count;
  96. }
  97. public QueryResult WithAny<T>(EntitiesDB entitiesDB)
  98. where T : struct, IEntityComponent
  99. {
  100. var group = groups.Value.reference;
  101. var groupsCount = group.count;
  102. for (var i = 0; i < groupsCount; i++)
  103. {
  104. if (entitiesDB.Count<T>(group[i]) == 0)
  105. {
  106. group.UnorderedRemoveAt(i);
  107. i--;
  108. groupsCount--;
  109. }
  110. }
  111. return new QueryResult(group);
  112. }
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. public void Add(ExclusiveGroupStruct group)
  115. {
  116. groups.Value.reference.Add(group);
  117. }
  118. }
  119. public readonly ref struct QueryResult
  120. {
  121. readonly FasterReadOnlyList<ExclusiveGroupStruct> _group;
  122. public QueryResult(FasterList<ExclusiveGroupStruct> @group) { _group = @group; }
  123. public FasterReadOnlyList<ExclusiveGroupStruct> result => _group;
  124. }
  125. }