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.

EngineNodeDB.cs 4.5KB

8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS
  5. {
  6. class EngineNodeDB : IEngineNodeDB
  7. {
  8. internal EngineNodeDB( Dictionary<Type, FasterList<INode>> nodesDB,
  9. Dictionary<Type, Dictionary<int, INode>> nodesDBdic,
  10. Dictionary<Type, FasterList<INode>> nodesDBgroups)
  11. {
  12. _nodesDB = new DataStructures.WeakReference<Dictionary<Type, FasterList<INode>>>(nodesDB);
  13. _nodesDBdic = new DataStructures.WeakReference<Dictionary<Type, Dictionary<int, INode>>>(nodesDBdic);
  14. _nodesDBgroups = new DataStructures.WeakReference<Dictionary<Type, FasterList<INode>>>(nodesDBgroups);
  15. }
  16. public FasterReadOnlyListCast<INode, T> QueryNodes<T>() where T:INode
  17. {
  18. var type = typeof(T);
  19. if (_nodesDB.IsValid == false || _nodesDB.Target.ContainsKey(type) == false)
  20. return RetrieveEmptyNodeList<T>();
  21. return new FasterReadOnlyListCast<INode, T>(_nodesDB.Target[type]);
  22. }
  23. /* public FasterReadOnlyList<T> QueryStructNodes<T>() where T:struct
  24. {
  25. var type = typeof(T);
  26. if (_nodesDBStructs.ContainsKey(type) == false)
  27. return RetrieveEmptyStructNodeList<T>();
  28. return new FasterReadOnlyList<T>(((StructNodeList<T>)(_nodesDBStructs[type])).list);
  29. }*/
  30. public ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T:INode
  31. {
  32. var type = typeof(T);
  33. if (_nodesDB.IsValid == false || _nodesDBdic.Target.ContainsKey(type) == false)
  34. return _defaultEmptyNodeDict;
  35. return new ReadOnlyDictionary<int, INode>(_nodesDBdic.Target[type]);
  36. }
  37. public T QueryNodeFromGroup<T>(int groupID) where T : INode
  38. {
  39. return QueryNode<T>(groupID);
  40. }
  41. public bool QueryNodeFromGroup<T>(int groupID, out T node) where T : INode
  42. {
  43. return QueryNode<T>(groupID, out node);
  44. }
  45. public FasterReadOnlyListCast<INode, T> QueryNodesFromGroups<T>() where T : INode
  46. {
  47. var type = typeof(T);
  48. if (_nodesDBgroups.IsValid == false || _nodesDBgroups.Target.ContainsKey(type) == false)
  49. return RetrieveEmptyNodeList<T>();
  50. return new FasterReadOnlyListCast<INode, T>(_nodesDBgroups.Target[type]);
  51. }
  52. public bool QueryNode<T>(int ID, out T node) where T:INode
  53. {
  54. var type = typeof(T);
  55. INode internalNode;
  56. if (_nodesDBdic.IsValid && _nodesDBdic.Target.ContainsKey(type) && _nodesDBdic.Target[type].TryGetValue(ID, out internalNode))
  57. {
  58. node = (T)internalNode;
  59. return true;
  60. }
  61. node = default(T);
  62. return false;
  63. }
  64. public T QueryNode<T>(int ID) where T:INode
  65. {
  66. var type = typeof(T);
  67. INode internalNode;
  68. if (_nodesDBdic.IsValid && _nodesDBdic.Target.ContainsKey(type) && _nodesDBdic.Target[type].TryGetValue(ID, out internalNode))
  69. return (T)internalNode;
  70. throw new Exception("Node Not Found");
  71. }
  72. static FasterReadOnlyListCast<INode, T> RetrieveEmptyNodeList<T>() where T : INode
  73. {
  74. return new FasterReadOnlyListCast<INode, T>(FasterList<INode>.DefaultList);
  75. }
  76. static FasterReadOnlyList<T> RetrieveEmptyStructNodeList<T>() where T : struct
  77. {
  78. return new FasterReadOnlyList<T>(FasterList<T>.DefaultList);
  79. }
  80. Svelto.DataStructures.WeakReference<Dictionary<Type, FasterList<INode>>> _nodesDB;
  81. Svelto.DataStructures.WeakReference<Dictionary<Type, Dictionary<int, INode>>> _nodesDBdic;
  82. Svelto.DataStructures.WeakReference<Dictionary<Type, FasterList<INode>>> _nodesDBgroups;
  83. // Dictionary<Type, StructNodeList> _nodesDBStructs;
  84. //Dictionary<Type, ThreadSafeFasterList<INode>> _nodesDB;
  85. //Dictionary<Type, ThreadsSafeDictionary<int, INode>> _nodesDBdic;
  86. // Dictionary<Type, ThreadSafeFasterList<INode>> _nodesDBgroups;
  87. ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>());
  88. class StructNodeList
  89. { }
  90. class StructNodeList<T> : StructNodeList where T : struct
  91. {
  92. public FasterList<T> list = new FasterList<T>();
  93. }
  94. }
  95. }