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.1KB

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. numberOfItems = FasterList<T>.NoVirt.Count(fasterList);
  52. return FasterList<T>.NoVirt.ToArrayFast(fasterList);
  53. }
  54. readonly SharedGroupedStructNodesLists _container;
  55. readonly Dictionary<int, int> indices;
  56. }
  57. public class SharedStructNodeLists
  58. {
  59. internal SharedStructNodeLists()
  60. {
  61. _collection = new Dictionary<Type, ITypeSafeList>();
  62. }
  63. internal static class NoVirt
  64. {
  65. internal static FasterList<T> GetList<T>(SharedStructNodeLists obj) where T : struct
  66. {
  67. ITypeSafeList list;
  68. if (obj._collection.TryGetValue(typeof(T), out list))
  69. {
  70. return list as FasterList<T>;
  71. }
  72. list = new TypeSafeFasterList<T>();
  73. obj._collection.Add(typeof(T), list);
  74. return (FasterList<T>)list;
  75. }
  76. }
  77. readonly Dictionary<Type, ITypeSafeList> _collection;
  78. }
  79. public class SharedGroupedStructNodesLists
  80. {
  81. internal SharedGroupedStructNodesLists()
  82. {
  83. _collection = new Dictionary<Type, Dictionary<int, ITypeSafeList>>();
  84. }
  85. internal static class NoVirt
  86. {
  87. internal static ITypeSafeList GetList<T>(SharedGroupedStructNodesLists list, int groupID) where T : struct
  88. {
  89. Dictionary<int, ITypeSafeList> dic = GetGroup<T>(list);
  90. ITypeSafeList localList;
  91. if (dic.TryGetValue(groupID, out localList))
  92. return localList;
  93. localList = new TypeSafeFasterList<T>();
  94. dic.Add(groupID, localList);
  95. return localList;
  96. }
  97. internal static Dictionary<int, ITypeSafeList> GetGroup<T>(SharedGroupedStructNodesLists list) where T : struct
  98. {
  99. Dictionary<int, ITypeSafeList> dic;
  100. if (list._collection.TryGetValue(typeof(T), out dic))
  101. {
  102. return dic;
  103. }
  104. dic = new Dictionary<int, ITypeSafeList>();
  105. list._collection.Add(typeof(T), dic);
  106. return dic;
  107. }
  108. }
  109. readonly Dictionary<Type, Dictionary<int, ITypeSafeList>> _collection;
  110. }
  111. }