mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-19 02:24:37 +08:00
update DependencyGraph
This commit is contained in:
parent
3ed500ed6b
commit
c787efe29f
@ -63,7 +63,7 @@ namespace DCFApixels.DragonECS
|
|||||||
Injector.AddNode<EcsAspect>();
|
Injector.AddNode<EcsAspect>();
|
||||||
Injector.AddNode<EcsPipeline>();
|
Injector.AddNode<EcsPipeline>();
|
||||||
|
|
||||||
var graph = new DependencyGraph(BASIC_LAYER);
|
var graph = new DependencyGraph<string>(BASIC_LAYER);
|
||||||
Layers = new LayersMap(graph, this, PRE_BEGIN_LAYER, BEGIN_LAYER, BASIC_LAYER, END_LAYER, POST_END_LAYER);
|
Layers = new LayersMap(graph, this, PRE_BEGIN_LAYER, BEGIN_LAYER, BASIC_LAYER, END_LAYER, POST_END_LAYER);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -22,6 +22,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
|
|
||||||
private readonly IDependencyGraph<string> _graph;
|
private readonly IDependencyGraph<string> _graph;
|
||||||
private readonly EcsPipeline.Builder _pipelineBuilder;
|
private readonly EcsPipeline.Builder _pipelineBuilder;
|
||||||
|
private readonly string _preBeginLayer;
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
public EcsPipeline.Builder Back
|
public EcsPipeline.Builder Back
|
||||||
@ -206,6 +207,37 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
{
|
{
|
||||||
_graph.MergeWith(other);
|
_graph.MergeWith(other);
|
||||||
}
|
}
|
||||||
|
//[Obsolete("Use MergeWith(LayersMap)")]
|
||||||
|
public void MergeWith(IReadOnlyList<string> other)
|
||||||
|
{
|
||||||
|
var enumerator = other.GetEnumerator();
|
||||||
|
string prev = null;
|
||||||
|
if (_preBeginLayer != null)
|
||||||
|
{
|
||||||
|
while (enumerator.MoveNext())
|
||||||
|
{
|
||||||
|
var layer = enumerator.Current;
|
||||||
|
if (layer == _preBeginLayer) { break; }
|
||||||
|
|
||||||
|
Add(layer);
|
||||||
|
if (prev != null)
|
||||||
|
{
|
||||||
|
Move(prev).Before(layer);
|
||||||
|
}
|
||||||
|
prev = layer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (enumerator.MoveNext())
|
||||||
|
{
|
||||||
|
var layer = enumerator.Current;
|
||||||
|
Add(layer);
|
||||||
|
if (prev != null)
|
||||||
|
{
|
||||||
|
Move(layer).After(prev);
|
||||||
|
}
|
||||||
|
prev = layer;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion;
|
#endregion;
|
||||||
|
|
||||||
#region Other
|
#region Other
|
||||||
@ -279,31 +311,23 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
//int i = 0;
|
int i = 0;
|
||||||
//foreach (var item in this)
|
foreach (var item in this)
|
||||||
//{
|
{
|
||||||
// if (i == index)
|
if (i == index)
|
||||||
// {
|
{
|
||||||
// return item;
|
return item;
|
||||||
// }
|
}
|
||||||
// i++;
|
i++;
|
||||||
//}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[Obsolete("Use MergeWith(LayersMap)")]
|
|
||||||
public void MergeWith(IReadOnlyList<string> other)
|
|
||||||
{
|
|
||||||
foreach (var layer in other)
|
|
||||||
{
|
|
||||||
Add(layer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum DependencyGraphVertextID : short { NULL = 0 }
|
public enum DependencyGraphVertextID : short { NULL = 0 }
|
||||||
public interface IDependencyGraph<T> : IReadOnlyCollection<string>
|
public interface IDependencyGraph<T> : IReadOnlyCollection<T>
|
||||||
{
|
{
|
||||||
ReadonlyDependenciesCollection<T> Dependencies { get; }
|
ReadonlyDependenciesCollection<T> Dependencies { get; }
|
||||||
VertexID AddVertex(T vertex, bool isLocked);
|
VertexID AddVertex(T vertex, bool isLocked);
|
||||||
@ -361,14 +385,14 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
public void Dispose() { }
|
public void Dispose() { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public unsafe partial class DependencyGraph : IDependencyGraph<string>
|
public unsafe partial class DependencyGraph<T> : IDependencyGraph<T>
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, VertexID> _vertexIDs = new Dictionary<string, VertexID>(32);
|
private readonly Dictionary<T, VertexID> _vertexIDs = new Dictionary<T, VertexID>(32);
|
||||||
private StructList<VertexInfo> _vertexInfos = new StructList<VertexInfo>(32);
|
private StructList<VertexInfo> _vertexInfos = new StructList<VertexInfo>(32);
|
||||||
|
|
||||||
private List<(VertexID from, VertexID to)> _dependencies = new List<(VertexID, VertexID)>(16);
|
private List<(VertexID from, VertexID to)> _dependencies = new List<(VertexID, VertexID)>(16);
|
||||||
private readonly VertexID _basicVertexID;
|
private readonly VertexID _basicVertexID;
|
||||||
private int _increment = 0;
|
private int _increment = 1;
|
||||||
private int _count;
|
private int _count;
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
@ -376,28 +400,30 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
{
|
{
|
||||||
get { return _count; }
|
get { return _count; }
|
||||||
}
|
}
|
||||||
public ReadonlyDependenciesCollection<string> Dependencies
|
public ReadonlyDependenciesCollection<T> Dependencies
|
||||||
{
|
{
|
||||||
get { return new ReadonlyDependenciesCollection<string>(this, _dependencies); }
|
get { return new ReadonlyDependenciesCollection<T>(this, _dependencies); }
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public DependencyGraph()
|
public DependencyGraph()
|
||||||
{
|
{
|
||||||
GetVertexID("");
|
//GetVertexID("");
|
||||||
|
_vertexInfos.Add(default);
|
||||||
_basicVertexID = VertexID.NULL;
|
_basicVertexID = VertexID.NULL;
|
||||||
}
|
}
|
||||||
public DependencyGraph(string basicVertexName)
|
public DependencyGraph(T basicVertexName)
|
||||||
{
|
{
|
||||||
GetVertexID("");
|
//GetVertexID("");
|
||||||
|
_vertexInfos.Add(default);
|
||||||
_basicVertexID = GetVertexID(basicVertexName);
|
_basicVertexID = GetVertexID(basicVertexName);
|
||||||
LockVertex(basicVertexName);
|
LockVertex(basicVertexName);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
public VertexID GetVertexID(string vertext)
|
public VertexID GetVertexID(T vertext)
|
||||||
{
|
{
|
||||||
if (_vertexIDs.TryGetValue(vertext, out VertexID layerID) == false)
|
if (_vertexIDs.TryGetValue(vertext, out VertexID layerID) == false)
|
||||||
{
|
{
|
||||||
@ -410,7 +436,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
}
|
}
|
||||||
return layerID;
|
return layerID;
|
||||||
}
|
}
|
||||||
public string GetVertexFromID(VertexID vertexID)
|
public T GetVertexFromID(VertexID vertexID)
|
||||||
{
|
{
|
||||||
return GetVertexInfo(vertexID).value;
|
return GetVertexInfo(vertexID).value;
|
||||||
}
|
}
|
||||||
@ -426,7 +452,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
{
|
{
|
||||||
return _vertexInfos.Count;
|
return _vertexInfos.Count;
|
||||||
}
|
}
|
||||||
public VertexID AddVertex(string vertex, bool isLocked)
|
public VertexID AddVertex(T vertex, bool isLocked)
|
||||||
{
|
{
|
||||||
var result = GetVertexID(vertex);
|
var result = GetVertexID(vertex);
|
||||||
AddVertexByID(result);
|
AddVertexByID(result);
|
||||||
@ -436,7 +462,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
private void LockVertex(string vertex)
|
private void LockVertex(T vertex)
|
||||||
{
|
{
|
||||||
LockVertex(GetVertexID(vertex));
|
LockVertex(GetVertexID(vertex));
|
||||||
}
|
}
|
||||||
@ -454,7 +480,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
}
|
}
|
||||||
info.insertionIndex = _increment++;
|
info.insertionIndex = _increment++;
|
||||||
}
|
}
|
||||||
public bool RemoveVertex(string vertex)
|
public bool RemoveVertex(T vertex)
|
||||||
{
|
{
|
||||||
var result = GetVertexID(vertex);
|
var result = GetVertexID(vertex);
|
||||||
return RemoveVertexByID(result);
|
return RemoveVertexByID(result);
|
||||||
@ -485,9 +511,9 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region MergeWith
|
#region MergeWith
|
||||||
public void MergeWith(IDependencyGraph<string> other)
|
public void MergeWith(IDependencyGraph<T> other)
|
||||||
{
|
{
|
||||||
if(other is DependencyGraph graph)
|
if(other is DependencyGraph<T> graph)
|
||||||
{
|
{
|
||||||
foreach (var otherDependency in graph._dependencies)
|
foreach (var otherDependency in graph._dependencies)
|
||||||
{
|
{
|
||||||
@ -511,7 +537,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Sort
|
#region Sort
|
||||||
public string[] Sort()
|
public T[] Sort()
|
||||||
{
|
{
|
||||||
const int BUFFER_THRESHOLD = 256;
|
const int BUFFER_THRESHOLD = 256;
|
||||||
if (_count <= BUFFER_THRESHOLD)
|
if (_count <= BUFFER_THRESHOLD)
|
||||||
@ -521,7 +547,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
TopoSorting(buffer);
|
TopoSorting(buffer);
|
||||||
ReoderInsertionIndexes(buffer);
|
ReoderInsertionIndexes(buffer);
|
||||||
TopoSorting(buffer);
|
TopoSorting(buffer);
|
||||||
return ConvertIdsToStringsArray(buffer);
|
return ConvertIdsToTsArray(buffer);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -530,7 +556,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
TopoSorting(buffer);
|
TopoSorting(buffer);
|
||||||
ReoderInsertionIndexes(buffer);
|
ReoderInsertionIndexes(buffer);
|
||||||
TopoSorting(buffer);
|
TopoSorting(buffer);
|
||||||
return ConvertIdsToStringsArray(buffer);
|
return ConvertIdsToTsArray(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void TopoSorting(UnsafeArray<VertexID> sortingBuffer)
|
private void TopoSorting(UnsafeArray<VertexID> sortingBuffer)
|
||||||
@ -660,14 +686,14 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private static void MoveElement<T>(ref UnsafeArray<T> array, int oldIndex, int newIndex) where T : unmanaged
|
private static void MoveElement<TValue>(ref UnsafeArray<TValue> array, int oldIndex, int newIndex) where TValue : unmanaged
|
||||||
{
|
{
|
||||||
if (oldIndex == newIndex) return;
|
if (oldIndex == newIndex) return;
|
||||||
|
|
||||||
var ptr = array.ptr;
|
var ptr = array.ptr;
|
||||||
T item = ptr[oldIndex];
|
TValue item = ptr[oldIndex];
|
||||||
|
|
||||||
int elementSize = sizeof(T);
|
int elementSize = sizeof(TValue);
|
||||||
int copyLength = Math.Abs(newIndex - oldIndex);
|
int copyLength = Math.Abs(newIndex - oldIndex);
|
||||||
|
|
||||||
byte* source;
|
byte* source;
|
||||||
@ -690,9 +716,9 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
|
|
||||||
ptr[newIndex] = item;
|
ptr[newIndex] = item;
|
||||||
}
|
}
|
||||||
private string[] ConvertIdsToStringsArray(UnsafeArray<VertexID> buffer)
|
private T[] ConvertIdsToTsArray(UnsafeArray<VertexID> buffer)
|
||||||
{
|
{
|
||||||
string[] result = new string[buffer.Length];
|
T[] result = new T[buffer.Length];
|
||||||
for (int i = 0; i < result.Length; i++)
|
for (int i = 0; i < result.Length; i++)
|
||||||
{
|
{
|
||||||
result[i] = GetVertexInfo(buffer.ptr[i]).value;
|
result[i] = GetVertexInfo(buffer.ptr[i]).value;
|
||||||
@ -775,31 +801,31 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Other
|
#region Other
|
||||||
public bool ContainsVertex(string vertex)
|
public bool ContainsVertex(T vertex)
|
||||||
{
|
{
|
||||||
return GetVertexInfo(GetVertexID(vertex)).isContained;
|
return GetVertexInfo(GetVertexID(vertex)).isContained;
|
||||||
}
|
}
|
||||||
public Enumerator GetEnumerator() { return new Enumerator(this); }
|
public Enumerator GetEnumerator() { return new Enumerator(this); }
|
||||||
IEnumerator<string> IEnumerable<string>.GetEnumerator() { return GetEnumerator(); }
|
IEnumerator<T> IEnumerable<T>.GetEnumerator() { return GetEnumerator(); }
|
||||||
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
|
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
|
||||||
public struct Enumerator : IEnumerator<string>
|
public struct Enumerator : IEnumerator<T>
|
||||||
{
|
{
|
||||||
private DependencyGraph _map;
|
private DependencyGraph<T> _graph;
|
||||||
private int _index;
|
private int _index;
|
||||||
public Enumerator(DependencyGraph map)
|
public Enumerator(DependencyGraph<T> graph)
|
||||||
{
|
{
|
||||||
_map = map;
|
_graph = graph;
|
||||||
_index = -1;
|
_index = -1;
|
||||||
}
|
}
|
||||||
public string Current
|
public T Current
|
||||||
{
|
{
|
||||||
get { return _map.GetVertexInfo(_index).value; }
|
get { return _graph.GetVertexInfo(_index).value; }
|
||||||
}
|
}
|
||||||
object IEnumerator.Current { get { return Current; } }
|
object IEnumerator.Current { get { return Current; } }
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
if (_index++ >= _map.GetVertexInfosCount()) { return false; }
|
if (_index++ >= _graph.GetVertexInfosCount()) { return false; }
|
||||||
ref var info = ref _map.GetVertexInfo(_index);
|
ref var info = ref _graph.GetVertexInfo(_index);
|
||||||
if(info.isContained == false)
|
if(info.isContained == false)
|
||||||
{
|
{
|
||||||
return MoveNext();
|
return MoveNext();
|
||||||
@ -818,7 +844,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
[DebuggerDisplay("{value}")]
|
[DebuggerDisplay("{value}")]
|
||||||
private struct VertexInfo
|
private struct VertexInfo
|
||||||
{
|
{
|
||||||
public string value;
|
public T value;
|
||||||
public int insertionIndex;
|
public int insertionIndex;
|
||||||
public bool isLocked;
|
public bool isLocked;
|
||||||
public bool isContained;
|
public bool isContained;
|
||||||
@ -828,7 +854,7 @@ namespace DCFApixels.DragonECS.Core
|
|||||||
public int inDegree;
|
public int inDegree;
|
||||||
public int sortingIndex;
|
public int sortingIndex;
|
||||||
public int leftBeforeIndex;
|
public int leftBeforeIndex;
|
||||||
public VertexInfo(string name) : this()
|
public VertexInfo(T name) : this()
|
||||||
{
|
{
|
||||||
this.value = name;
|
this.value = name;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user