@@ -71,7 +71,55 @@ namespace Svelto.DataStructures | |||
T[] _buffer; | |||
int _counter; | |||
int _size; | |||
T _current; | |||
T _current; | |||
} | |||
public struct FasterListEnumeratorCast<T, U> : IEnumerator<T> where T:U | |||
{ | |||
public T Current | |||
{ | |||
get { return (T)_buffer.Current; } | |||
} | |||
object IEnumerator.Current | |||
{ | |||
get { return (T)_buffer.Current; } | |||
} | |||
T IEnumerator<T>.Current | |||
{ | |||
get { return (T)_buffer.Current; } | |||
} | |||
public FasterListEnumeratorCast(FasterListEnumerator<U> 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<U> _buffer; | |||
} | |||
public struct FasterReadOnlyList<T> : IList<T> | |||
@@ -144,6 +192,77 @@ namespace Svelto.DataStructures | |||
readonly FasterList<T> _list; | |||
} | |||
public struct FasterReadOnlyListCast<T, U> : IList<U> where U:T | |||
{ | |||
public FasterReadOnlyListCast(FasterList<T> list) | |||
{ | |||
_list = list; | |||
} | |||
IEnumerator<U> IEnumerable<U>.GetEnumerator() | |||
{ | |||
return GetEnumerator(); | |||
} | |||
IEnumerator IEnumerable.GetEnumerator() | |||
{ | |||
return GetEnumerator(); | |||
} | |||
public FasterListEnumeratorCast<U, T> GetEnumerator() | |||
{ | |||
return new FasterListEnumeratorCast<U, T>(_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<T> _list; | |||
static public FasterList<T> DefaultList = new FasterList<T>(); | |||
} | |||
public class FasterList<T> : IList<T> | |||
{ | |||
public int Count | |||
@@ -12,14 +12,14 @@ namespace Svelto.ES | |||
this._nodesDBdic = nodesDBdic; | |||
} | |||
public FasterReadOnlyList<INode> QueryNodes<T>() where T:INode | |||
public FasterReadOnlyListCast<INode, T> QueryNodes<T>() where T:INode | |||
{ | |||
var type = typeof(T); | |||
if (_nodesDB.ContainsKey(type) == false) | |||
return _defaultEmptyNodeList; | |||
return new FasterReadOnlyListCast<INode, T>(FasterReadOnlyListCast<INode, T>.DefaultList); | |||
return new FasterReadOnlyList<INode>(_nodesDB[type]); | |||
return new FasterReadOnlyListCast<INode, T>(_nodesDB[type]); | |||
} | |||
public ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T:INode | |||
@@ -50,10 +50,21 @@ namespace Svelto.ES | |||
return false; | |||
} | |||
public T QueryNode<T>(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<Type, FasterList<INode>> _nodesDB; | |||
Dictionary<Type, Dictionary<int, INode>> _nodesDBdic; | |||
Dictionary<Type, Dictionary<int, INode>> _nodesDBdic; | |||
FasterReadOnlyList<INode> _defaultEmptyNodeList = new FasterReadOnlyList<INode>(new FasterList<INode>()); | |||
ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>()); | |||
ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>()); | |||
} | |||
} |
@@ -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<INode>, baseType.GetGenericArguments(), _nodeEngines); | |||
@@ -19,7 +19,7 @@ namespace Svelto.ES | |||
IEngineNodeDB nodesDB { set; } | |||
} | |||
public abstract class SingleManagedNodeEngine<TNodeType> : INodeEngine<INode> where TNodeType:class, INode | |||
public abstract class SingleNodeEngine<TNodeType> : INodeEngine<INode> where TNodeType:class, INode | |||
{ | |||
void INodeEngine<INode>.Add(INode obj) | |||
{ | |||
@@ -4,8 +4,9 @@ namespace Svelto.ES | |||
{ | |||
public interface IEngineNodeDB | |||
{ | |||
ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T : INode; | |||
ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T:INode; | |||
bool QueryNode<T>(int ID, out T node) where T:INode; | |||
FasterReadOnlyList<INode> QueryNodes<T>() where T : INode; | |||
T QueryNode<T>(int ID) where T:INode; | |||
FasterReadOnlyListCast<INode, T> QueryNodes<T>() where T:INode; | |||
} | |||
} |