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.

EGIDMultiMapper.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. using Svelto.DataStructures;
  5. using Svelto.DataStructures.Native;
  6. using Svelto.ECS.Hybrid;
  7. namespace Svelto.ECS
  8. {
  9. namespace Native
  10. {
  11. public struct EGIDMultiMapper<T> where T : unmanaged, IBaseEntityComponent
  12. {
  13. public EGIDMultiMapper
  14. (SveltoDictionary<ExclusiveGroupStruct,
  15. SveltoDictionary<uint, T, NativeStrategy<
  16. SveltoDictionaryNode<uint>>, NativeStrategy<T>, NativeStrategy<int>>,
  17. ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  18. ManagedStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>
  19. , NativeStrategy<int>>>, NativeStrategy<int>> dictionary)
  20. {
  21. _dic = dictionary;
  22. }
  23. public int count
  24. {
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. get => _dic.count;
  27. }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public ref T Entity(EGID entity)
  30. {
  31. #if DEBUG && !PROFILE_SVELTO
  32. if (Exists(entity) == false)
  33. throw new Exception("NativeEGIDMultiMapper: Entity not found");
  34. #endif
  35. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  36. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public bool Exists(EGID entity)
  40. {
  41. return _dic.TryFindIndex(entity.groupID, out var index)
  42. && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public bool TryGetEntity(EGID entity, out T component)
  46. {
  47. component = default;
  48. return _dic.TryFindIndex(entity.groupID, out var index)
  49. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  50. }
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. public bool FindIndex(ExclusiveGroupStruct group, uint entityID, out uint index)
  53. {
  54. index = 0;
  55. return _dic.TryFindIndex(group, out var groupIndex) &&
  56. _dic.GetDirectValueByRef(groupIndex).TryFindIndex(entityID, out index);
  57. }
  58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. public uint GetIndex(ExclusiveGroupStruct group, uint entityID)
  60. {
  61. uint groupIndex = _dic.GetIndex(group);
  62. return _dic.GetDirectValueByRef(groupIndex).GetIndex(entityID);
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public bool Exists(ExclusiveGroupStruct group, uint entityID)
  66. {
  67. return _dic.TryFindIndex(group, out var groupIndex) &&
  68. _dic.GetDirectValueByRef(groupIndex).ContainsKey(entityID);
  69. }
  70. public Type entityType => TypeCache<T>.type;
  71. SveltoDictionary<ExclusiveGroupStruct,
  72. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  73. NativeStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  74. ManagedStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  75. NativeStrategy<int>>>, NativeStrategy<int>> _dic;
  76. }
  77. public interface IEGIDMultiMapper
  78. {
  79. bool FindIndex(ExclusiveGroupStruct group, uint entityID, out uint index);
  80. uint GetIndex(ExclusiveGroupStruct group, uint entityID);
  81. bool Exists(ExclusiveGroupStruct group, uint entityID);
  82. Type entityType { get; }
  83. }
  84. }
  85. public struct EGIDMultiMapper<T> where T : struct, IEntityViewComponent
  86. {
  87. public EGIDMultiMapper
  88. (SveltoDictionary<ExclusiveGroupStruct,
  89. SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  90. ManagedStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  91. ManagedStrategy<SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  92. ManagedStrategy<int>>>, ManagedStrategy<int>> dictionary)
  93. {
  94. _dic = dictionary;
  95. }
  96. public int count
  97. {
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. get => _dic.count;
  100. }
  101. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  102. public ref T Entity(EGID entity)
  103. {
  104. #if DEBUG && !PROFILE_SVELTO
  105. if (Exists(entity) == false)
  106. throw new Exception("NativeEGIDMultiMapper: Entity not found");
  107. #endif
  108. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  109. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  110. }
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. public bool Exists(EGID entity)
  113. {
  114. return _dic.TryFindIndex(entity.groupID, out var index)
  115. && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  116. }
  117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  118. public bool TryGetEntity(EGID entity, out T component)
  119. {
  120. component = default;
  121. return _dic.TryFindIndex(entity.groupID, out var index)
  122. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  123. }
  124. SveltoDictionary<ExclusiveGroupStruct,
  125. SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  126. ManagedStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  127. ManagedStrategy<SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
  128. ManagedStrategy<int>>>, ManagedStrategy<int>> _dic;
  129. }
  130. }