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.

NativeEGIDMultiMapper.cs 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #if UNITY_NATIVE
  2. using System;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS.Native
  5. {
  6. /// <summary>
  7. /// Note: this class should really be ref struct by design. It holds the reference of a dictionary that can become
  8. /// invalid. Unfortunately it can be a ref struct, because Jobs needs to hold if by paramater. So the deal is
  9. /// that a job can use it as long as nothing else is modifying the entities database and the NativeEGIDMultiMapper
  10. /// is disposed right after the use.
  11. /// </summary>
  12. public struct NativeEGIDMultiMapper<T> : IDisposable where T : unmanaged, IEntityComponent
  13. {
  14. public NativeEGIDMultiMapper
  15. (SveltoDictionary<ExclusiveGroupStruct,
  16. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  17. NativeStrategy<int>>, NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  18. NativeStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  19. NativeStrategy<int>>>, NativeStrategy<int>> dictionary)
  20. {
  21. _dic = dictionary;
  22. }
  23. public int count => (int) _dic.count;
  24. public void Dispose()
  25. {
  26. _dic.Dispose();
  27. }
  28. public ref T Entity(EGID entity)
  29. {
  30. #if DEBUG && !PROFILE_SVELTO
  31. if (Exists(entity) == false)
  32. throw new Exception("NativeEGIDMultiMapper: Entity not found");
  33. #endif
  34. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  35. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  36. }
  37. public bool Exists(EGID entity)
  38. {
  39. return _dic.TryFindIndex(entity.groupID, out var index)
  40. && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  41. }
  42. public bool TryGetEntity(EGID entity, out T component)
  43. {
  44. component = default;
  45. return _dic.TryFindIndex(entity.groupID, out var index)
  46. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  47. }
  48. SveltoDictionary<ExclusiveGroupStruct,
  49. SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  50. NativeStrategy<int>>, NativeStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
  51. NativeStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
  52. NativeStrategy<int>>>, NativeStrategy<int>> _dic;
  53. }
  54. }
  55. #endif