当前位置:网站首页>Breadth first search for node editor runtime traversal
Breadth first search for node editor runtime traversal
2022-06-13 00:50:00 【Small fish game development】
Algorithm principle see other articles
Use UIElements Authored node editor :https://gitee.com/weifen/node-editor
Basic algorithm :
public static class BFSHelper
{
public static T[] BFSForEach<T>(this List<T> array, T start, Func<T, T, bool> filter)
{
return BFSForEach(array.ToArray(), start, filter);
}
public static T[] BFSForEach<T>(this T[] array, T start, Func<T, T, bool> filter)
{
var count = array.Length;
List<T> results = new List<T>();
Queue<T> queue = new Queue<T>();
HashSet<T> visited = new HashSet<T>();
queue.Enqueue(start);
visited.Add(start);
while (queue.Count != 0)
{
var _ = queue.Dequeue();
results.Add(_);
for (var i = 0; i < count; i++)
{
var __ = array[i];
if (!visited.Contains(__) && filter(_, __))
{
queue.Enqueue(__);
visited.Add(__);
}
}
}
return results.ToArray();
}
}
The algorithm is extended to the tree . Traverse the node tree
When entering a node , The result of the previous node accessed must be the calculated result
So you need to execute the node from the left , The next node can obtain the operation results of all previous nodes 
public interface INode
{
INode[] GetLefts();
INode[] GetRights();
}
public static class TreeHelper
{
public static T[] ForEach<T>(this T start)where T :INode
{
Dictionary<T, bool> visited = new Dictionary<T, bool>();
Queue<T> queue = new Queue<T>();
List<T> nodes = new List<T>();
queue.Enqueue(start);
visited.Add(start, false);
while (queue.Count > 0)
{
var _ = queue.Dequeue();
int count = 0;
foreach (T __ in _.GetLefts())
{
if (!visited.ContainsKey(__))
{
count++;
queue.Enqueue(__);
visited.Add(__, false);
}
}
if (visited.ContainsKey(_) && count == 0)
{
if(!visited[_])
{
visited[_] = true;
nodes.Add(_);
}
foreach (T __ in _.GetRights())
{
queue.Enqueue(__);
if (!visited.ContainsKey(__))
{
visited.Add(__, false);
}
}
}
}
return nodes.ToArray();
}
}
Traverse demo:
Disconnect the root node first , get N A tree that needs to be traversed
var rootNode = runtimeDict[tree.root.guid];
List<RuntimeNode> list = new List<RuntimeNode>();
foreach (var port in rootNode.outputNodes)
{
list.Add(port.input);
}
rootNode.Clear();
foreach (var item in list)
{
runtimeNodes.Add(item.ForEach());
}
Simple traversal
public void ForEach(Func<RuntimeNode,bool> func)
{
foreach (var nodes in runtimeNodes)
{
Debug.LogError(">>>>>");
foreach (var node in nodes)
{
if (func?.Invoke(node) ?? false)
break;
}
}
}
Condition node traversal test

边栏推荐
猜你喜欢

Arduino control tm1637 common positive four digit nixie tube

pytorch和tensorflow有什么区别?

How many steps are appropriate for each cycle of deep learning?
![[buglist] serial port programming does not read data](/img/bf/8e63f679bf139fbbf222878792ae21.jpg)
[buglist] serial port programming does not read data

Cve-2021-24078 vulnerability analysis

Today's sleep quality record 74 points

Another year 1024, happy programmer's Day!

Expression tree - medium order printout

Arduino control soil moisture sensor

Build your own PE manually from winpe of ADK
随机推荐
Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
How to solve the duplication problem when MySQL inserts data in batches?
ImportError: cannot import name &#039;get_ora_doc&#039; from partially initialized module
antdPro - ProTable 实现两个选择框联动效果
[sca-cnn interpretation] spatial and channel wise attention
What is pytorch? Explain the basic concepts of pytorch
[JS component] calendar
Cve-2021-24078 vulnerability analysis
Map from getting started to performance optimization
[virtual machine] notes on virtual machine environment problems
1. Google grpc framework source code analysis Hello World
Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
kotlin 协程withContext切换线程
Dynamic planning - good article link
今日在家休息
【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
What is the difference between pytorch and tensorflow?
Biological unlocking - Fingerprint entry process
Binary tree -- using hierarchical sequence and middle sequence to determine a tree
Aof persistence