Mirror of Svelto.ECS because we're a fan of it
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

301 lignes
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 uint Add(uint egidEntityId, in TValue entityComponent)
  109. {
  110. if (implMgd.TryAdd(egidEntityId, entityComponent, out uint index) == false)
  111. throw new TypeSafeDictionaryException("Key already present");
  112. return index;
  113. }
  114. public void Dispose()
  115. {
  116. implMgd.Dispose();
  117. GC.SuppressFinalize(this);
  118. }
  119. /// *********************************
  120. /// the following methods are executed during the submission of entities
  121. /// *********************************
  122. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  123. public void AddEntitiesToDictionary
  124. (ITypeSafeDictionary toDictionary, ExclusiveGroupStruct groupId
  125. #if SLOW_SVELTO_SUBMISSION
  126. , in EnginesRoot.EntityReferenceMap entityLocator
  127. #endif
  128. )
  129. {
  130. TypeSafeDictionaryMethods.AddEntitiesToDictionary(implMgd, toDictionary as ITypeSafeDictionary<TValue>
  131. #if SLOW_SVELTO_SUBMISSION
  132. , entityLocator
  133. #endif
  134. , groupId);
  135. }
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. public void RemoveEntitiesFromDictionary(FasterList<(uint, string)> infosToProcess, FasterDictionary<uint, uint> entityIDsAffectedByRemoveAtSwapBack)
  138. {
  139. TypeSafeDictionaryMethods.RemoveEntitiesFromDictionary(infosToProcess, ref implMgd, entityIDsAffectedByRemoveAtSwapBack);
  140. }
  141. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  142. public void SwapEntitiesBetweenDictionaries(in FasterDictionary<uint, SwapInfo> infosToProcess,
  143. ExclusiveGroupStruct fromGroup
  144. , ExclusiveGroupStruct toGroup, ITypeSafeDictionary toComponentsDictionary
  145. , FasterDictionary<uint, uint> entityIDsAffectedByRemoveAtSwapBack)
  146. {
  147. TypeSafeDictionaryMethods.SwapEntitiesBetweenDictionaries(infosToProcess, ref implMgd
  148. , toComponentsDictionary as ITypeSafeDictionary<TValue>, fromGroup
  149. , toGroup, entityIDsAffectedByRemoveAtSwapBack);
  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(FasterDictionary<uint, SwapInfo> infosToProcess
  167. , FasterList<ReactEngineContainer<IReactOnSwap>> reactiveEnginesSwap, ExclusiveGroupStruct fromGroup
  168. , ExclusiveGroupStruct toGroup, in PlatformProfiler profiler)
  169. {
  170. TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacks(infosToProcess, ref implMgd, reactiveEnginesSwap
  171. , toGroup, fromGroup, in profiler);
  172. }
  173. /// <summary>
  174. /// Execute all the engine IReactOnREmove callbacks linked to components removed this submit
  175. /// </summary>
  176. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  177. public void ExecuteEnginesRemoveCallbacks
  178. (FasterList<(uint, string)> infosToProcess
  179. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemove>>> reactiveEnginesRemove
  180. , ExclusiveGroupStruct fromGroup, in PlatformProfiler sampler)
  181. {
  182. TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacks(infosToProcess, ref implMgd, reactiveEnginesRemove
  183. , fromGroup, in sampler);
  184. }
  185. /// <summary>
  186. /// Execute all the engine IReactOnAddEx callbacks linked to components added this submit
  187. /// </summary>
  188. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  189. public void ExecuteEnginesAddEntityCallbacksFast
  190. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnAddEx>>> reactiveEnginesAdd
  191. , ExclusiveGroupStruct groupID, (uint, uint) rangeOfSubmittedEntitiesIndicies, in PlatformProfiler profiler)
  192. {
  193. TypeSafeDictionaryMethods.ExecuteEnginesAddEntityCallbacksFast(
  194. reactiveEnginesAdd, groupID, rangeOfSubmittedEntitiesIndicies, entityIDs, this, profiler);
  195. }
  196. /// <summary>
  197. /// Execute all the engine IReactOnSwapEx callbacks linked to components swapped this submit
  198. /// </summary>
  199. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  200. public void ExecuteEnginesSwapCallbacksFast
  201. (FasterList<ReactEngineContainer<IReactOnSwapEx>> reactiveEnginesSwap, ExclusiveGroupStruct fromGroup
  202. , ExclusiveGroupStruct toGroup, (uint, uint) rangeOfSubmittedEntitiesIndicies, in PlatformProfiler sampler)
  203. {
  204. TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacksFast(reactiveEnginesSwap, fromGroup, toGroup, entityIDs
  205. , this, rangeOfSubmittedEntitiesIndicies, sampler);
  206. }
  207. /// <summary>
  208. /// Execute all the engine IReactOnRemoveEx callbacks linked to components removed this submit
  209. /// </summary>
  210. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  211. public void ExecuteEnginesRemoveCallbacksFast
  212. (FasterList<ReactEngineContainer<IReactOnRemoveEx>> reactiveEnginesRemoveEx, ExclusiveGroupStruct fromGroup
  213. , (uint, uint) rangeOfSubmittedEntitiesIndicies, in PlatformProfiler sampler)
  214. {
  215. TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacksFast(reactiveEnginesRemoveEx, fromGroup
  216. , rangeOfSubmittedEntitiesIndicies, entityIDs
  217. , this, sampler);
  218. }
  219. /// <summary>
  220. /// Execute all the engine IReactOnSwap and IReactOnSwapEx callbacks linked to components swapped between
  221. /// whole groups swapped during this submit
  222. /// </summary>
  223. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  224. public void ExecuteEnginesSwapCallbacks_Group
  225. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnSwap>>> reactiveEnginesSwap
  226. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnSwapEx>>> reactiveEnginesSwapEx
  227. , ITypeSafeDictionary toDictionary, ExclusiveGroupStruct fromGroup, ExclusiveGroupStruct toGroup
  228. , in PlatformProfiler profiler)
  229. {
  230. TypeSafeDictionaryMethods.ExecuteEnginesSwapCallbacks_Group(
  231. ref implMgd, (ITypeSafeDictionary<TValue>)toDictionary, toGroup, fromGroup, reactiveEnginesSwap
  232. , reactiveEnginesSwapEx, entityIDs, in profiler);
  233. }
  234. /// <summary>
  235. /// Execute all the engine IReactOnRemove and IReactOnRemoveEx callbacks linked to components remove from
  236. /// whole groups removed during this submit
  237. /// </summary>
  238. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  239. public void ExecuteEnginesRemoveCallbacks_Group
  240. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemove>>> reactiveEnginesRemove
  241. , FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnRemoveEx>>> reactiveEnginesRemoveEx
  242. , ExclusiveGroupStruct group, in PlatformProfiler profiler)
  243. {
  244. TypeSafeDictionaryMethods.ExecuteEnginesRemoveCallbacks_Group(
  245. ref implMgd, reactiveEnginesRemove, reactiveEnginesRemoveEx, entityIDs, group
  246. , in profiler);
  247. }
  248. /// <summary>
  249. /// Execute all the engine IReactOnDispose for each component registered in the DB when it's disposed of
  250. /// </summary>
  251. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  252. public void ExecuteEnginesDisposeCallbacks_Group
  253. (FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnDispose>>> reactiveEnginesDispose,
  254. FasterDictionary<ComponentID, FasterList<ReactEngineContainer<IReactOnDisposeEx>>> reactiveEnginesDisposeEx,
  255. ExclusiveGroupStruct group, in PlatformProfiler profiler)
  256. {
  257. TypeSafeDictionaryMethods.ExecuteEnginesDisposeCallbacks_Group(
  258. ref implMgd, reactiveEnginesDispose, reactiveEnginesDisposeEx, entityIDs, group, in profiler);
  259. }
  260. internal SveltoDictionary<uint, TValue, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<TValue>,
  261. ManagedStrategy<int>> implMgd;
  262. }
  263. }