Mirror of Svelto.ECS because we're a fan of it
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

301 řádky
13KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Threading;
  4. using Svelto.Common;
  5. using Svelto.DataStructures;
  6. namespace Svelto.ECS.Internal
  7. {
  8. sealed class ManagedTypeSafeDictionary<TValue> : ITypeSafeDictionary<TValue>
  9. where TValue : struct, _IInternalEntityComponent
  10. {
  11. //todo: would this be better to not be static to avoid overhead?
  12. static readonly ThreadLocal<IEntityIDs> cachedEntityIDM =
  13. new ThreadLocal<IEntityIDs>(() => new ManagedEntityIDs());
  14. public ManagedTypeSafeDictionary(uint size)
  15. {
  16. implMgd = new SveltoDictionary<uint, TValue, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<TValue>,
  17. ManagedStrategy<int>>(size, Allocator.Managed);
  18. }
  19. public IEntityIDs entityIDs
  20. {
  21. get
  22. {
  23. ref var unboxed = ref Unsafe.Unbox<ManagedEntityIDs>(cachedEntityIDM.Value);
  24. unboxed.Update(implMgd.unsafeKeys.ToRealBuffer());
  25. return cachedEntityIDM.Value;
  26. }
  27. }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public bool ContainsKey(uint egidEntityId)
  30. {
  31. return implMgd.ContainsKey(egidEntityId);
  32. }
  33. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  34. public uint GetIndex(uint valueEntityId)
  35. {
  36. return implMgd.GetIndex(valueEntityId);
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public ref TValue GetOrAdd(uint idEntityId)
  40. {
  41. return ref implMgd.GetOrAdd(idEntityId);
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public IBuffer<TValue> GetValues(out uint count)
  45. {
  46. return implMgd.UnsafeGetValues(out count);
  47. }
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public ref TValue GetDirectValueByRef(uint key)
  50. {
  51. return ref implMgd.GetDirectValueByRef(key);
  52. }
  53. public ref TValue GetValueByRef(uint key)
  54. {
  55. return ref implMgd.GetValueByRef(key);
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public bool Has(uint key)
  59. {
  60. return implMgd.ContainsKey(key);
  61. }
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public bool TryFindIndex(uint entityId, out uint index)
  64. {
  65. return implMgd.TryFindIndex(entityId, out index);
  66. }
  67. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  68. public bool TryGetValue(uint entityId, out TValue item)
  69. {
  70. return implMgd.TryGetValue(entityId, out item);
  71. }
  72. public int count
  73. {
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. get => implMgd.count;
  76. }
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. public ITypeSafeDictionary Create()
  79. {
  80. return new ManagedTypeSafeDictionary<TValue>(1);
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public void Clear()
  84. {
  85. implMgd.Clear();
  86. }
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public void EnsureCapacity(uint size)
  89. {
  90. implMgd.EnsureCapacity(size);
  91. }
  92. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  93. public void IncreaseCapacityBy(uint size)
  94. {
  95. implMgd.IncreaseCapacityBy(size);
  96. }
  97. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  98. public void Trim()
  99. {
  100. implMgd.Trim();
  101. }
  102. public void KeysEvaluator(Action<uint> action)
  103. {
  104. foreach (var key in implMgd.keys)
  105. action(key);
  106. }
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. public void Add(uint egidEntityId, in TValue entityComponent)
  109. {
  110. implMgd.Add(egidEntityId, entityComponent);
  111. }
  112. public void Dispose()
  113. {
  114. implMgd.Dispose();
  115. GC.SuppressFinalize(this);
  116. }
  117. /// *********************************
  118. /// the following methods are executed during the submission of entities
  119. /// *********************************
  120. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  121. public void AddEntitiesToDictionary
  122. (ITypeSafeDictionary toDictionary, ExclusiveGroupStruct groupId
  123. #if SLOW_SVELTO_SUBMISSION
  124. , in EnginesRoot.EntityReferenceMap entityLocator
  125. #endif
  126. )
  127. {
  128. TypeSafeDictionaryMethods.AddEntitiesToDictionary(implMgd, toDictionary as ITypeSafeDictionary<TValue>
  129. #if SLOW_SVELTO_SUBMISSION
  130. , entityLocator
  131. #endif
  132. , groupId);
  133. }
  134. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  135. public void RemoveEntitiesFromDictionary
  136. (FasterList<(uint, string)> infosToProcess, FasterList<uint> entityIDsAffectedByRemoval)
  137. {
  138. TypeSafeDictionaryMethods.RemoveEntitiesFromDictionary(infosToProcess, ref implMgd, entityIDsAffectedByRemoval);
  139. }
  140. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  141. public void SwapEntitiesBetweenDictionaries
  142. (FasterList<(uint, uint, string)> infosToProcess, ExclusiveGroupStruct fromGroup
  143. , ExclusiveGroupStruct toGroup, ITypeSafeDictionary toComponentsDictionary
  144. , FasterList<uint> entityIDsAffectedByRemoval)
  145. {
  146. TypeSafeDictionaryMethods.SwapEntitiesBetweenDictionaries(infosToProcess, ref implMgd
  147. , toComponentsDictionary as
  148. ITypeSafeDictionary<TValue>, fromGroup
  149. , toGroup, entityIDsAffectedByRemoval);
  150. }
  151. /// <summary>
  152. /// Execute all the engine IReactOnAdd callbacks linked to components added this submit
  153. /// </summary>
  154. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  155. public void ExecuteEnginesAddCallbacks
  156. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnAdd>>> entityComponentEnginesDB
  157. , ITypeSafeDictionary toDic, ExclusiveGroupStruct toGroup, in PlatformProfiler profiler)
  158. {
  159. TypeSafeDictionaryMethods.ExecuteEnginesAddCallbacks(ref implMgd, (ITypeSafeDictionary<TValue>)toDic
  160. , toGroup, entityComponentEnginesDB, in profiler);
  161. }
  162. /// <summary>
  163. /// Execute all the engine IReactOnSwap callbacks linked to components swapped this submit
  164. /// </summary>
  165. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  166. public void ExecuteEnginesSwapCallbacks
  167. (FasterList<(uint, uint, string)> infosToProcess
  168. , FasterList<ReactEngineContainer<IReactOnSwap>> reactiveEnginesSwap, ExclusiveGroupStruct fromGroup
  169. , ExclusiveGroupStruct toGroup, in PlatformProfiler profiler)
  170. {
  171. TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacks(infosToProcess, ref implMgd, reactiveEnginesSwap
  172. , toGroup, fromGroup, in profiler);
  173. }
  174. /// <summary>
  175. /// Execute all the engine IReactOnREmove callbacks linked to components removed this submit
  176. /// </summary>
  177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  178. public void ExecuteEnginesRemoveCallbacks
  179. (FasterList<(uint, string)> infosToProcess
  180. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemove>>> reactiveEnginesRemove
  181. , ExclusiveGroupStruct fromGroup, in PlatformProfiler sampler)
  182. {
  183. TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacks(infosToProcess, ref implMgd, reactiveEnginesRemove
  184. , fromGroup, in sampler);
  185. }
  186. /// <summary>
  187. /// Execute all the engine IReactOnAddEx callbacks linked to components added this submit
  188. /// </summary>
  189. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  190. public void ExecuteEnginesAddEntityCallbacksFast
  191. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnAddEx>>> reactiveEnginesAdd
  192. , ExclusiveGroupStruct groupID, (uint, uint) rangeOfSubmittedEntitiesIndicies, in PlatformProfiler profiler)
  193. {
  194. TypeSafeDictionaryMethods.ExecuteEnginesAddEntityCallbacksFast(
  195. reactiveEnginesAdd, groupID, rangeOfSubmittedEntitiesIndicies, entityIDs, this, profiler);
  196. }
  197. /// <summary>
  198. /// Execute all the engine IReactOnSwapEx callbacks linked to components swapped this submit
  199. /// </summary>
  200. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  201. public void ExecuteEnginesSwapCallbacksFast
  202. (FasterList<ReactEngineContainer<IReactOnSwapEx>> reactiveEnginesSwap, ExclusiveGroupStruct fromGroup
  203. , ExclusiveGroupStruct toGroup, (uint, uint) rangeOfSubmittedEntitiesIndicies, in PlatformProfiler sampler)
  204. {
  205. TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacksFast(reactiveEnginesSwap, fromGroup, toGroup, entityIDs
  206. , this, rangeOfSubmittedEntitiesIndicies, sampler);
  207. }
  208. /// <summary>
  209. /// Execute all the engine IReactOnRemoveEx callbacks linked to components removed this submit
  210. /// </summary>
  211. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  212. public void ExecuteEnginesRemoveCallbacksFast
  213. (FasterList<ReactEngineContainer<IReactOnRemoveEx>> reactiveEnginesRemoveEx, ExclusiveGroupStruct fromGroup
  214. , (uint, uint) rangeOfSubmittedEntitiesIndicies, in PlatformProfiler sampler)
  215. {
  216. TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacksFast(reactiveEnginesRemoveEx, fromGroup
  217. , rangeOfSubmittedEntitiesIndicies, entityIDs
  218. , this, sampler);
  219. }
  220. /// <summary>
  221. /// Execute all the engine IReactOnSwap and IReactOnSwapEx callbacks linked to components swapped between
  222. /// whole groups swapped during this submit
  223. /// </summary>
  224. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  225. public void ExecuteEnginesSwapCallbacks_Group
  226. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnSwap>>> reactiveEnginesSwap
  227. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnSwapEx>>> reactiveEnginesSwapEx
  228. , ITypeSafeDictionary toDictionary, ExclusiveGroupStruct fromGroup, ExclusiveGroupStruct toGroup
  229. , in PlatformProfiler profiler)
  230. {
  231. TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacks_Group(
  232. ref implMgd, (ITypeSafeDictionary<TValue>)toDictionary, toGroup, fromGroup, reactiveEnginesSwap
  233. , reactiveEnginesSwapEx, entityIDs, in profiler);
  234. }
  235. /// <summary>
  236. /// Execute all the engine IReactOnRemove and IReactOnRemoveEx callbacks linked to components remove from
  237. /// whole groups removed during this submit
  238. /// </summary>
  239. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  240. public void ExecuteEnginesRemoveCallbacks_Group
  241. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemove>>> reactiveEnginesRemove
  242. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemoveEx>>> reactiveEnginesRemoveEx
  243. , ExclusiveGroupStruct group, in PlatformProfiler profiler)
  244. {
  245. TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacks_Group(
  246. ref implMgd, reactiveEnginesRemove, reactiveEnginesRemoveEx, entityIDs, group
  247. , in profiler);
  248. }
  249. /// <summary>
  250. /// Execute all the engine IReactOnDispose for each component registered in the DB when it's disposed of
  251. /// </summary>
  252. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  253. public void ExecuteEnginesDisposeCallbacks_Group
  254. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnDispose>>> reactiveEnginesDispose,
  255. FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnDisposeEx>>> reactiveEnginesDisposeEx,
  256. ExclusiveGroupStruct group, in PlatformProfiler profiler)
  257. {
  258. TypeSafeDictionaryMethods.ExecuteEnginesDisposeCallbacks_Group(
  259. ref implMgd, reactiveEnginesDispose, reactiveEnginesDisposeEx, entityIDs, group, in profiler);
  260. }
  261. SveltoDictionary<uint, TValue, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<TValue>,
  262. ManagedStrategy<int>> implMgd;
  263. }
  264. }