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.

103 lines
3.3KB

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