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.

104 line
3.4KB

  1. using Svelto.DataStructures;
  2. using Svelto.DataStructures.Native;
  3. namespace Svelto.ECS
  4. {
  5. public struct GroupFilters
  6. {
  7. internal GroupFilters(SharedSveltoDictionaryNative<int, FilterGroup> filters, ExclusiveGroupStruct group)
  8. {
  9. this.filters = filters;
  10. _group = @group;
  11. }
  12. public ref FilterGroup GetFilter(int filterIndex)
  13. {
  14. #if DEBUG && !PROFILE_SVELTO
  15. if (filters.isValid == false)
  16. throw new ECSException($"trying to fetch not existing filters {filterIndex} group {_group.ToName()}");
  17. if (filters.ContainsKey(filterIndex) == false)
  18. throw new ECSException($"trying to fetch not existing filters {filterIndex} group {_group.ToName()}");
  19. #endif
  20. return ref filters.GetValueByRef(filterIndex);
  21. }
  22. public bool HasFilter(int filterIndex) { return filters.ContainsKey(filterIndex); }
  23. public void ClearFilter(int filterIndex)
  24. {
  25. if (filters.TryFindIndex(filterIndex, out var index))
  26. filters.GetValues(out _)[index].Clear();
  27. }
  28. public void ClearFilters()
  29. {
  30. foreach (var filter in filters)
  31. filter.Value.Clear();
  32. }
  33. public bool TryGetFilter(int filterIndex, out FilterGroup filter)
  34. {
  35. return filters.TryGetValue(filterIndex, out filter);
  36. }
  37. public SveltoDictionaryKeyValueEnumerator<int, FilterGroup, NativeStrategy<SveltoDictionaryNode<int>>, NativeStrategy<FilterGroup>
  38. , NativeStrategy<int>> GetEnumerator()
  39. {
  40. return filters.GetEnumerator();
  41. }
  42. //Note the following methods are internal because I was pondering the idea to be able to return
  43. //the list of GroupFilters linked to a specific filter ID. However this would mean to be able to
  44. //maintain a revers map which at this moment seems too much and also would need the following
  45. //method to be for ever internal (at this point in time I am not sure it's a good idea)
  46. internal void DisposeFilter(int filterIndex)
  47. {
  48. if (filters.TryFindIndex(filterIndex, out var index))
  49. {
  50. ref var filterGroup = ref filters.GetValues(out _)[index];
  51. filterGroup.Dispose();
  52. filters.Remove(filterIndex);
  53. }
  54. }
  55. internal void DisposeFilters()
  56. {
  57. //must release the native buffers!
  58. foreach (var filter in filters)
  59. filter.Value.Dispose();
  60. filters.FastClear();
  61. }
  62. internal ref FilterGroup CreateOrGetFilter(int filterID)
  63. {
  64. if (filters.TryFindIndex(filterID, out var index) == false)
  65. {
  66. var orGetFilterForGroup = new FilterGroup(_group, filterID);
  67. filters[filterID] = orGetFilterForGroup;
  68. return ref filters.GetValueByRef(filterID);
  69. }
  70. return ref filters.GetValues(out _)[index];
  71. }
  72. internal void Dispose()
  73. {
  74. foreach (var filter in filters)
  75. {
  76. filter.Value.Dispose();
  77. }
  78. filters.Dispose();
  79. }
  80. readonly ExclusiveGroupStruct _group;
  81. //filterID, filter
  82. SharedSveltoDictionaryNative<int, FilterGroup> filters;
  83. }
  84. }