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 3.5KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. public 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 = container.GetList<T>();
  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 class StructGroupNodes<T>
  26. where T : struct, IGroupedStructNodeWithID
  27. {
  28. public StructGroupNodes(SharedGroupedStructNodesLists container)
  29. {
  30. _container = container;
  31. }
  32. public void Add(int groupID, T node)
  33. {
  34. T convert = (T)node;
  35. var fasterList = (_container.GetList<T>(groupID) as FasterList<T>);
  36. indices[node.ID] = fasterList.Count;
  37. fasterList.Add(convert);
  38. }
  39. public void Remove(int groupID, T node)
  40. {
  41. var fasterList = (_container.GetList<T>(groupID) as FasterList<T>);
  42. var index = indices[node.ID];
  43. indices.Remove(node.ID);
  44. if (fasterList.UnorderedRemoveAt(index))
  45. indices[fasterList[index].ID] = index;
  46. }
  47. public T[] GetList(int groupID, out int numberOfItems)
  48. {
  49. var fasterList = (_container.GetList<T>(groupID) as FasterList<T>);
  50. numberOfItems = fasterList.Count;
  51. return fasterList.ToArrayFast();
  52. }
  53. readonly SharedGroupedStructNodesLists _container;
  54. readonly Dictionary<int, int> indices = new Dictionary<int, int>();
  55. }
  56. public class SharedStructNodeLists
  57. {
  58. readonly Dictionary<Type, IFasterList> _collection;
  59. internal SharedStructNodeLists()
  60. {
  61. _collection = new Dictionary<Type, IFasterList>();
  62. }
  63. internal FasterList<T> GetList<T>() where T:struct
  64. {
  65. IFasterList list;
  66. if (_collection.TryGetValue(typeof (T), out list))
  67. {
  68. return list as FasterList<T>;
  69. }
  70. list = new FasterList<T>();
  71. _collection.Add(typeof (T), list);
  72. return (FasterList<T>) list;
  73. }
  74. }
  75. public class SharedGroupedStructNodesLists
  76. {
  77. internal SharedGroupedStructNodesLists()
  78. {
  79. _collection = new Dictionary<Type, Dictionary<int, IFasterList>>();
  80. }
  81. internal IFasterList GetList<T>(int groupID) where T : struct
  82. {
  83. Dictionary<int, IFasterList> dic = GetGroup<T>();
  84. IFasterList localList;
  85. if (dic.TryGetValue(groupID, out localList))
  86. return localList;
  87. localList = new FasterList<T>();
  88. dic.Add(groupID, localList);
  89. return localList;
  90. }
  91. internal Dictionary<int, IFasterList> GetGroup<T>() where T : struct
  92. {
  93. Dictionary<int, IFasterList> dic;
  94. if (_collection.TryGetValue(typeof(T), out dic))
  95. {
  96. return dic;
  97. }
  98. dic = new Dictionary<int, IFasterList>();
  99. _collection.Add(typeof(T), dic);
  100. return dic;
  101. }
  102. readonly Dictionary<Type, Dictionary<int, IFasterList>> _collection;
  103. }
  104. }