您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
70 行
1.5 KiB
70 行
1.5 KiB
using System;
|
|
using Unity.Collections;
|
|
|
|
public unsafe struct UnsafeDFS : IDisposable
|
|
{
|
|
public int position;
|
|
public int depth;
|
|
|
|
private UnsafeArrayULong pending;// pending[i] == (depth << 32) | position
|
|
private int pendingHead;
|
|
|
|
private UnsafeArrayBool visited;
|
|
|
|
public UnsafeDFS(int nodeCount, Allocator allocator = Allocator.Temp)
|
|
{
|
|
this.position = -1;
|
|
this.depth = -1;
|
|
|
|
this.pending = new UnsafeArrayULong(nodeCount, allocator);
|
|
this.pendingHead = 0;
|
|
|
|
this.visited = new UnsafeArrayBool(nodeCount, allocator);
|
|
this.visited.Clear(false);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
pending.Dispose();
|
|
visited.Dispose();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
visited.Clear(false);
|
|
}
|
|
|
|
public void Ignore(int node)
|
|
{
|
|
visited.val[node] = true;
|
|
}
|
|
|
|
public void Insert(int node)
|
|
{
|
|
if (visited.val[node])
|
|
return;
|
|
|
|
ulong pack_position = (ulong)node;
|
|
ulong pack_depth = (ulong)(depth + 1) << 32;
|
|
|
|
pending.val[++pendingHead] = pack_depth | pack_position;
|
|
visited.val[node] = true;
|
|
}
|
|
|
|
public bool MoveNext()
|
|
{
|
|
if (pendingHead > 0)
|
|
{
|
|
ulong packed = pending.val[--pendingHead];
|
|
position = (int)(packed & 0xffffffffuL);
|
|
depth = (int)(packed >> 32);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
position = -1;
|
|
depth = -1;
|
|
return false;
|
|
}
|
|
}
|
|
}
|