From 12678816a750fc0002391c90b39b7af1a47c2b93 Mon Sep 17 00:00:00 2001 From: sebas77 Date: Fri, 20 May 2016 23:10:46 +0100 Subject: [PATCH] improve code --- DataStructures/FasterList.cs | 121 ++++++++++++++++++++++++++++++++++- ECS/EngineNodeDB.cs | 23 +++++-- ECS/EnginesRoot.cs | 3 +- ECS/IEngine.cs | 2 +- ECS/IEngineNodeDB.cs | 5 +- 5 files changed, 143 insertions(+), 11 deletions(-) diff --git a/DataStructures/FasterList.cs b/DataStructures/FasterList.cs index da36e94..15dc5b1 100644 --- a/DataStructures/FasterList.cs +++ b/DataStructures/FasterList.cs @@ -71,7 +71,55 @@ namespace Svelto.DataStructures T[] _buffer; int _counter; int _size; - T _current; + T _current; + } + + public struct FasterListEnumeratorCast : IEnumerator where T:U + { + public T Current + { + get { return (T)_buffer.Current; } + } + + object IEnumerator.Current + { + get { return (T)_buffer.Current; } + } + + T IEnumerator.Current + { + get { return (T)_buffer.Current; } + } + + public FasterListEnumeratorCast(FasterListEnumerator buffer) + { + _buffer = buffer; + } + + public void Dispose() + {} + + bool IEnumerator.MoveNext() + { + return MoveNext(); + } + + void IEnumerator.Reset() + { + Reset(); + } + + public bool MoveNext() + { + return _buffer.MoveNext(); + } + + public void Reset() + { + _buffer.Reset(); + } + + FasterListEnumerator _buffer; } public struct FasterReadOnlyList : IList @@ -144,6 +192,77 @@ namespace Svelto.DataStructures readonly FasterList _list; } + public struct FasterReadOnlyListCast : IList where U:T + { + public FasterReadOnlyListCast(FasterList list) + { + _list = list; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public FasterListEnumeratorCast GetEnumerator() + { + return new FasterListEnumeratorCast(_list.GetEnumerator()); + } + + public void Add(U item) + { + throw new NotImplementedException(); + } + + public void Clear() + { + throw new NotImplementedException(); + } + + public bool Contains(U item) + { + return _list.Contains(item); + } + + public void CopyTo(U[] array, int arrayIndex) + { + throw new NotImplementedException(); + } + + public bool Remove(U item) + { + throw new NotImplementedException(); + } + + public int Count { get { return _list.Count; } } + public bool IsReadOnly { get { return true; } } + + public int IndexOf(U item) + { + return _list.IndexOf(item); + } + + public void Insert(int index, U item) + { + throw new NotImplementedException(); + } + + public void RemoveAt(int index) + { + throw new NotImplementedException(); + } + + public U this[int index] { get { return (U)_list[index]; } set { throw new NotImplementedException(); } } + + readonly FasterList _list; + static public FasterList DefaultList = new FasterList(); + } + public class FasterList : IList { public int Count diff --git a/ECS/EngineNodeDB.cs b/ECS/EngineNodeDB.cs index 33842c4..3eb6f61 100644 --- a/ECS/EngineNodeDB.cs +++ b/ECS/EngineNodeDB.cs @@ -12,14 +12,14 @@ namespace Svelto.ES this._nodesDBdic = nodesDBdic; } - public FasterReadOnlyList QueryNodes() where T:INode + public FasterReadOnlyListCast QueryNodes() where T:INode { var type = typeof(T); if (_nodesDB.ContainsKey(type) == false) - return _defaultEmptyNodeList; + return new FasterReadOnlyListCast(FasterReadOnlyListCast.DefaultList); - return new FasterReadOnlyList(_nodesDB[type]); + return new FasterReadOnlyListCast(_nodesDB[type]); } public ReadOnlyDictionary QueryIndexableNodes() where T:INode @@ -50,10 +50,21 @@ namespace Svelto.ES return false; } + public T QueryNode(int ID) where T:INode + { + var type = typeof(T); + + INode internalNode; + + if (_nodesDBdic.ContainsKey(type) && _nodesDBdic[type].TryGetValue(ID, out internalNode)) + return (T)internalNode; + + throw new Exception("Node Not Found"); + } + Dictionary> _nodesDB; - Dictionary> _nodesDBdic; + Dictionary> _nodesDBdic; - FasterReadOnlyList _defaultEmptyNodeList = new FasterReadOnlyList(new FasterList()); - ReadOnlyDictionary _defaultEmptyNodeDict = new ReadOnlyDictionary(new Dictionary()); + ReadOnlyDictionary _defaultEmptyNodeDict = new ReadOnlyDictionary(new Dictionary()); } } diff --git a/ECS/EnginesRoot.cs b/ECS/EnginesRoot.cs index 9836516..4a71518 100644 --- a/ECS/EnginesRoot.cs +++ b/ECS/EnginesRoot.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using Svelto.DataStructures; using Svelto.Ticker; +using Nodes.Player; namespace Svelto.ES { @@ -48,7 +49,7 @@ namespace Svelto.ES { var genericType = baseType.GetGenericTypeDefinition(); - if (genericType == typeof(SingleManagedNodeEngine<>)) + if (genericType == typeof(SingleNodeEngine<>)) { AddEngine(engine as INodeEngine, baseType.GetGenericArguments(), _nodeEngines); diff --git a/ECS/IEngine.cs b/ECS/IEngine.cs index fa6b1aa..0d295ef 100644 --- a/ECS/IEngine.cs +++ b/ECS/IEngine.cs @@ -19,7 +19,7 @@ namespace Svelto.ES IEngineNodeDB nodesDB { set; } } - public abstract class SingleManagedNodeEngine : INodeEngine where TNodeType:class, INode + public abstract class SingleNodeEngine : INodeEngine where TNodeType:class, INode { void INodeEngine.Add(INode obj) { diff --git a/ECS/IEngineNodeDB.cs b/ECS/IEngineNodeDB.cs index e6868a9..5e7b46f 100644 --- a/ECS/IEngineNodeDB.cs +++ b/ECS/IEngineNodeDB.cs @@ -4,8 +4,9 @@ namespace Svelto.ES { public interface IEngineNodeDB { - ReadOnlyDictionary QueryIndexableNodes() where T : INode; + ReadOnlyDictionary QueryIndexableNodes() where T:INode; bool QueryNode(int ID, out T node) where T:INode; - FasterReadOnlyList QueryNodes() where T : INode; + T QueryNode(int ID) where T:INode; + FasterReadOnlyListCast QueryNodes() where T:INode; } }