浏览代码

Rewrite `DepthFirstCollectNodesFromNode` to be non-recursive

/main
Peter Bay Bastian 7 年前
当前提交
b1ca732a
共有 6 个文件被更改,包括 97 次插入25 次删除
  1. 58
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/NodeUtils.cs
  2. 8
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/ObjectPool.cs
  3. 25
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/QueuePool.cs
  4. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/QueuePool.cs.meta
  5. 25
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/StackPool.cs
  6. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/StackPool.cs.meta

58
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/NodeUtils.cs


if (node == null)
return;
// already added this node
if (nodeList.Contains(node))
return;
var remapper = node as INodeGroupRemapper;
if (remapper != null)
using (var stackDisposable = StackPool<INode>.GetDisposable())
using (var queueDisposable = QueuePool<INode>.GetDisposable())
remapper.DepthFirstCollectNodesFromNodeSlotList(nodeList, includeSelf);
return;
}
var stack = stackDisposable.value;
var queue = queueDisposable.value;
queue.Enqueue(node);
var ids = node.GetInputSlots<ISlot>().Select(x => x.id);
if (slotIds != null)
ids = node.GetInputSlots<ISlot>().Where(x => slotIds.Contains(x.id)).Select(x => x.id);
foreach (var slot in ids)
{
foreach (var edge in node.owner.GetEdges(node.GetSlotReference(slot)))
while (queue.Any())
var outputNode = node.owner.GetNodeFromGuid(edge.outputSlot.nodeGuid);
DepthFirstCollectNodesFromNode(nodeList, outputNode);
var fromNode = queue.Dequeue();
// already added this node
if (nodeList.Contains(fromNode))
continue;
var remapper = fromNode as INodeGroupRemapper;
if (remapper != null)
{
remapper.DepthFirstCollectNodesFromNodeSlotList(nodeList, includeSelf);
continue;
}
foreach (var slot in fromNode.GetInputSlots<ISlot>())
{
if (slotIds != null && !slotIds.Contains(slot.id))
continue;
foreach (var edge in fromNode.owner.GetEdges(fromNode.GetSlotReference(slot.id)))
{
var outputNode = fromNode.owner.GetNodeFromGuid(edge.outputSlot.nodeGuid);
queue.Enqueue(outputNode);
stack.Push(outputNode);
}
}
}
if (includeSelf == IncludeSelf.Include)
nodeList.Add(node);
while (stack.Any())
nodeList.Add(stack.Pop());
if (includeSelf == IncludeSelf.Include)
nodeList.Add(node);
}
}
public static void CollectNodesNodeFeedsInto(List<INode> nodeList, INode node, IncludeSelf includeSelf = IncludeSelf.Include)

8
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/ObjectPool.cs


namespace UnityEngine.Graphing
{
internal class ObjectPool<T> where T : new()
class ObjectPool<T> where T : new()
private readonly Stack<T> m_Stack = new Stack<T>();
private readonly UnityAction<T> m_ActionOnGet;
private readonly UnityAction<T> m_ActionOnRelease;
readonly Stack<T> m_Stack = new Stack<T>();
readonly UnityAction<T> m_ActionOnGet;
readonly UnityAction<T> m_ActionOnRelease;
public int countAll { get; private set; }
public int countActive { get { return countAll - countInactive; } }

25
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/QueuePool.cs


using System.Collections.Generic;
namespace UnityEngine.Graphing
{
public static class QueuePool<T>
{
// Object pool to avoid allocations.
static readonly ObjectPool<Queue<T>> k_QueuePool = new ObjectPool<Queue<T>>(null, l => l.Clear());
public static Queue<T> Get()
{
return k_QueuePool.Get();
}
public static PooledObject<Queue<T>> GetDisposable()
{
return k_QueuePool.GetDisposable();
}
public static void Release(Queue<T> toRelease)
{
k_QueuePool.Release(toRelease);
}
}
}

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/QueuePool.cs.meta


fileFormatVersion: 2
guid: 4e895afeaf2a438195fbc4de17a4bedc
timeCreated: 1505278549

25
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/StackPool.cs


using System.Collections.Generic;
namespace UnityEngine.Graphing
{
public static class StackPool<T>
{
// Object pool to avoid allocations.
static readonly ObjectPool<Stack<T>> k_StackPool = new ObjectPool<Stack<T>>(null, l => l.Clear());
public static Stack<T> Get()
{
return k_StackPool.Get();
}
public static PooledObject<Stack<T>> GetDisposable()
{
return k_StackPool.GetDisposable();
}
public static void Release(Stack<T> toRelease)
{
k_StackPool.Release(toRelease);
}
}
}

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/StackPool.cs.meta


fileFormatVersion: 2
guid: 93c41a4331c94d09a7561ea6b4983439
timeCreated: 1505275069
正在加载...
取消
保存