using System; using System.Collections.Generic; using Svelto.DataStructures; using Svelto.ECS.Internal; namespace Svelto.ECS { public class StructNodes where T:struct, IStructNodeWithID { public T[] GetList(out int numberOfItems) { numberOfItems = _internalList.Count; return _internalList.ToArrayFast(); } public StructNodes(SharedStructNodeLists container) { _internalList = container.GetList(); } public void Add(T node) { T convert = (T)node; _internalList.Add(convert); } readonly FasterList _internalList; } public class StructGroupNodes where T : struct, IGroupedStructNodeWithID { public StructGroupNodes(SharedGroupedStructNodesLists container) { _container = container; } public void Add(int groupID, T node) { T convert = (T)node; var fasterList = (_container.GetList(groupID) as FasterList); indices[node.ID] = fasterList.Count; fasterList.Add(convert); } public void Remove(int groupID, T node) { var fasterList = (_container.GetList(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 = (_container.GetList(groupID) as FasterList); numberOfItems = fasterList.Count; return fasterList.ToArrayFast(); } readonly SharedGroupedStructNodesLists _container; readonly Dictionary indices = new Dictionary(); } public class SharedStructNodeLists { readonly Dictionary _collection; internal SharedStructNodeLists() { _collection = new Dictionary(); } internal FasterList GetList() where T:struct { IFasterList list; if (_collection.TryGetValue(typeof (T), out list)) { return list as FasterList; } list = new FasterList(); _collection.Add(typeof (T), list); return (FasterList) list; } } public class SharedGroupedStructNodesLists { internal SharedGroupedStructNodesLists() { _collection = new Dictionary>(); } internal IFasterList GetList(int groupID) where T : struct { Dictionary dic = GetGroup(); IFasterList localList; if (dic.TryGetValue(groupID, out localList)) return localList; localList = new FasterList(); dic.Add(groupID, localList); return localList; } internal Dictionary GetGroup() where T : struct { Dictionary dic; if (_collection.TryGetValue(typeof(T), out dic)) { return dic; } dic = new Dictionary(); _collection.Add(typeof(T), dic); return dic; } readonly Dictionary> _collection; } }