using System; using System.Collections.Generic; using Svelto.DataStructures; using Svelto.ECS.Internal; namespace Svelto.ECS { public sealed class StructNodes where T:struct, IStructNodeWithID { public T[] GetList(out int numberOfItems) { numberOfItems = _internalList.Count; return _internalList.ToArrayFast(); } public StructNodes(SharedStructNodeLists container) { _internalList = SharedStructNodeLists.NoVirt.GetList(container); } public void Add(T node) { T convert = (T)node; _internalList.Add(convert); } readonly FasterList _internalList; } public struct StructGroupNodes where T : struct, IStructNodeWithID { public StructGroupNodes(SharedGroupedStructNodesLists container) { _container = container; indices = new Dictionary(); } public void Add(int groupID, T node) { T convert = (T)node; var fasterList = (SharedGroupedStructNodesLists.NoVirt.GetList(_container, groupID) as FasterList); indices[node.ID] = fasterList.Count; fasterList.Add(convert); } public void Remove(int groupID, T node) { var fasterList = (SharedGroupedStructNodesLists.NoVirt.GetList(_container, groupID) as FasterList); var index = indices[node.ID]; indices.Remove(node.ID); if (fasterList.UnorderedRemoveAt(index)) indices[fasterList[index].ID] = index; } public T[] GetList(int groupID, out int numberOfItems) { var fasterList = (SharedGroupedStructNodesLists.NoVirt.GetList(_container, groupID) as FasterList); numberOfItems = FasterList.NoVirt.Count(fasterList); return FasterList.NoVirt.ToArrayFast(fasterList); } readonly SharedGroupedStructNodesLists _container; readonly Dictionary indices; } public class SharedStructNodeLists { internal SharedStructNodeLists() { _collection = new Dictionary(); } internal static class NoVirt { internal static FasterList GetList(SharedStructNodeLists obj) where T : struct { ITypeSafeList list; if (obj._collection.TryGetValue(typeof(T), out list)) { return list as FasterList; } list = new TypeSafeFasterList(); obj._collection.Add(typeof(T), list); return (FasterList)list; } } readonly Dictionary _collection; } public class SharedGroupedStructNodesLists { internal SharedGroupedStructNodesLists() { _collection = new Dictionary>(); } internal static class NoVirt { internal static ITypeSafeList GetList(SharedGroupedStructNodesLists list, int groupID) where T : struct { Dictionary dic = GetGroup(list); ITypeSafeList localList; if (dic.TryGetValue(groupID, out localList)) return localList; localList = new TypeSafeFasterList(); dic.Add(groupID, localList); return localList; } internal static Dictionary GetGroup(SharedGroupedStructNodesLists list) where T : struct { Dictionary dic; if (list._collection.TryGetValue(typeof(T), out dic)) { return dic; } dic = new Dictionary(); list._collection.Add(typeof(T), dic); return dic; } } readonly Dictionary> _collection; } }