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.

StructNodes.cs 4.0KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. public sealed class StructNodes<T> where T:struct, IStructNodeWithID
  8. {
  9. public T[] GetList(out int numberOfItems)
  10. {
  11. numberOfItems = _internalList.Count;
  12. return _internalList.ToArrayFast();
  13. }
  14. public StructNodes(SharedStructNodeLists container)
  15. {
  16. _internalList = SharedStructNodeLists.NoVirt.GetList<T>(container);
  17. }
  18. public void Add(T node)
  19. {
  20. T convert = (T)node;
  21. _internalList.Add(convert);
  22. }
  23. readonly FasterList<T> _internalList;
  24. }
  25. public struct StructGroupNodes<T>
  26. where T : struct, IStructNodeWithID
  27. {
  28. public StructGroupNodes(SharedGroupedStructNodesLists container)
  29. {
  30. _container = container;
  31. indices = new Dictionary<int, int>();
  32. }
  33. public void Add(int groupID, T node)
  34. {
  35. T convert = (T)node;
  36. var fasterList = (SharedGroupedStructNodesLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>);
  37. indices[node.ID] = fasterList.Count;
  38. fasterList.Add(convert);
  39. }
  40. public void Remove(int groupID, T node)
  41. {
  42. var fasterList = (SharedGroupedStructNodesLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>);
  43. var index = indices[node.ID];
  44. indices.Remove(node.ID);
  45. if (fasterList.UnorderedRemoveAt(index))
  46. indices[fasterList[index].ID] = index;
  47. }
  48. public T[] GetList(int groupID, out int numberOfItems)
  49. {
  50. var fasterList = (SharedGroupedStructNodesLists.NoVirt.GetList<T>(_container, groupID) as FasterList<T>);
  51. return FasterList<T>.NoVirt.ToArrayFast(fasterList, out numberOfItems);
  52. }
  53. readonly SharedGroupedStructNodesLists _container;
  54. readonly Dictionary<int, int> indices;
  55. }
  56. public class SharedStructNodeLists
  57. {
  58. internal SharedStructNodeLists()
  59. {
  60. _collection = new Dictionary<Type, IFasterList>();
  61. }
  62. internal static class NoVirt
  63. {
  64. internal static FasterList<T> GetList<T>(SharedStructNodeLists obj) where T : struct
  65. {
  66. IFasterList list;
  67. if (obj._collection.TryGetValue(typeof(T), out list))
  68. {
  69. return list as FasterList<T>;
  70. }
  71. list = new FasterList<T>();
  72. obj._collection.Add(typeof(T), list);
  73. return (FasterList<T>)list;
  74. }
  75. }
  76. readonly Dictionary<Type, IFasterList> _collection;
  77. }
  78. public class SharedGroupedStructNodesLists
  79. {
  80. internal SharedGroupedStructNodesLists()
  81. {
  82. _collection = new Dictionary<Type, Dictionary<int, IFasterList>>();
  83. }
  84. internal static class NoVirt
  85. {
  86. internal static IFasterList GetList<T>(SharedGroupedStructNodesLists list, int groupID) where T : struct
  87. {
  88. Dictionary<int, IFasterList> dic = GetGroup<T>(list);
  89. IFasterList localList;
  90. if (dic.TryGetValue(groupID, out localList))
  91. return localList;
  92. localList = new FasterList<T>();
  93. dic.Add(groupID, localList);
  94. return localList;
  95. }
  96. internal static Dictionary<int, IFasterList> GetGroup<T>(SharedGroupedStructNodesLists list) where T : struct
  97. {
  98. Dictionary<int, IFasterList> dic;
  99. if (list._collection.TryGetValue(typeof(T), out dic))
  100. {
  101. return dic;
  102. }
  103. dic = new Dictionary<int, IFasterList>();
  104. list._collection.Add(typeof(T), dic);
  105. return dic;
  106. }
  107. }
  108. readonly Dictionary<Type, Dictionary<int, IFasterList>> _collection;
  109. }
  110. }