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.

104 lines
3.2KB

  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 = nodesDB;
  13. _nodesDBdic = nodesDBdic;
  14. _nodesDBgroups = nodesDBgroups;
  15. }
  16. public FasterReadOnlyListCast<INode, T> QueryNodes<T>() where T:INode
  17. {
  18. var type = typeof(T);
  19. if (_nodesDB.ContainsKey(type) == false)
  20. return RetrieveEmptyNodeList<T>();
  21. return new FasterReadOnlyListCast<INode, T>(_nodesDB[type]);
  22. }
  23. public ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T:INode
  24. {
  25. var type = typeof(T);
  26. if (_nodesDBdic.ContainsKey(type) == false)
  27. return _defaultEmptyNodeDict;
  28. return new ReadOnlyDictionary<int, INode>(_nodesDBdic[type]);
  29. }
  30. public T QueryNodeFromGroup<T>(int groupID) where T : INode
  31. {
  32. return QueryNode<T>(groupID);
  33. }
  34. public bool QueryNodeFromGroup<T>(int groupID, out T node) where T : INode
  35. {
  36. return QueryNode<T>(groupID, out node);
  37. }
  38. public FasterReadOnlyListCast<INode, T> QueryNodesFromGroups<T>() where T : INode
  39. {
  40. var type = typeof(T);
  41. if (_nodesDBgroups.ContainsKey(type) == false)
  42. return RetrieveEmptyNodeList<T>();
  43. return new FasterReadOnlyListCast<INode, T>(_nodesDBgroups[type]);
  44. }
  45. public bool QueryNode<T>(int ID, out T node) where T:INode
  46. {
  47. var type = typeof(T);
  48. INode internalNode;
  49. if (_nodesDBdic.ContainsKey(type) && _nodesDBdic[type].TryGetValue(ID, out internalNode))
  50. {
  51. node = (T)internalNode;
  52. return true;
  53. }
  54. node = default(T);
  55. return false;
  56. }
  57. public T QueryNode<T>(int ID) where T:INode
  58. {
  59. var type = typeof(T);
  60. INode internalNode;
  61. if (_nodesDBdic.ContainsKey(type) && _nodesDBdic[type].TryGetValue(ID, out internalNode))
  62. return (T)internalNode;
  63. throw new Exception("Node Not Found");
  64. }
  65. static FasterReadOnlyListCast<INode, T> RetrieveEmptyNodeList<T>() where T : INode
  66. {
  67. return new FasterReadOnlyListCast<INode, T>(FasterReadOnlyListCast<INode, T>.DefaultList);
  68. }
  69. Dictionary<Type, FasterList<INode>> _nodesDB;
  70. Dictionary<Type, Dictionary<int, INode>> _nodesDBdic;
  71. Dictionary<Type, FasterList<INode>> _nodesDBgroups;
  72. //Dictionary<Type, ThreadSafeFasterList<INode>> _nodesDB;
  73. //Dictionary<Type, ThreadsSafeDictionary<int, INode>> _nodesDBdic;
  74. // Dictionary<Type, ThreadSafeFasterList<INode>> _nodesDBgroups;
  75. ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>());
  76. }
  77. }