Unreal Engine implements a priority queue (priority_queue
brief description
Priority queue is implemented using heap sort. The access method depends on the heapsort method.
There is a heap sorting method in TArray, which can easily realize the heap sorting of TArray type.
Heapify: initialize the heap
HeapPush: push elements to the heap
HeapPop: pop the top element of the heap
TArray<int32> openSet;
for (int32 i=0;i<10;i++)
{
openSet.Emplace(UKismetMathLibrary::RandomInteger(100));
}
FString logText = "";
for (int32 i=0;i<openSet.Num();i++)
{
logText.Append(FString::FromInt(openSet[i]));
logText.Append(" ");
}
UE_LOG(LogTemp, Warning, TEXT("===logText %s"),*logText);
openSet.Heapify();
logText = "";
for (int32 i = 0; i < openSet.Num(); i++)
{
logText.Append(FString::FromInt(openSet[i]));
logText.Append(" ");
}
UE_LOG(LogTemp, Warning, TEXT("===logText Heapify %s"), *logText);
openSet.HeapPush(50);
openSet.HeapPush(50);
openSet.HeapPush(50);
openSet.HeapPush(60);
logText = "";
for (int32 i = 0; i < openSet.Num(); i++)
{
logText.Append(FString::FromInt(openSet[i]));
logText.Append(" ");
}
UE_LOG(LogTemp, Warning, TEXT("===logText HeapPush %s"), *logText);
logText = "";
while (openSet.Num()>0)
{
int32 _i = 0;
openSet.HeapPop(_i);
logText.Append(FString::FromInt( _i));
logText.Append(" ");
}
UE_LOG(LogTemp, Warning, TEXT("===logText HeapPop %s"), *logText);
In practical applications, it is often not a simple int push sort, there will be more complex requirements, and a more customized heap sort method needs to be implemented to realize the priority queue.
References
UE4's TArray (3)
UE – Priority Queue priority queue
demo
First, you need to write two Structs, one is the element of TArray, and the other is sorted by heap
//TArray的元素
struct TNode
{
TNode() {
};
TNode(FString _key, int32 _prority) :key(_key), prority(_prority) {
};
int32 prority;
FString key;
};
//TArray的堆排序规则
struct TNodeLess
{
FORCEINLINE bool operator()(const TNode& A, const TNode& B) const
{
return A.prority < B.prority;
}
};
test code
//创建一个TNode的数组,并往数组里添加元素
TArray<TNode> openSet;
for (int32 i = 0; i < 10; i++)
{
TNode n(FString::FromInt(i), UKismetMathLibrary::RandomInteger(100));//随机添加优先级
openSet.Emplace(n);
}
//打印原始数组
FString logText = "";
for (int32 i = 0; i < openSet.Num(); i++)
{
logText.Append(openSet[i].key);
logText.Append("-");
logText.Append(FString::FromInt(openSet[i].prority));
logText.Append(" ");
}
UE_LOG(LogTemp, Warning, TEXT("===logText %s"), *logText);
//将数组按照排序规则进行堆排序
openSet.Heapify(TNodeLess());
//打印堆排序之后的元素
logText = "";
for (int32 i = 0; i < openSet.Num(); i++)
{
logText.Append(openSet[i].key);
logText.Append("-");
logText.Append(FString::FromInt(openSet[i].prority));
logText.Append(" ");
}
UE_LOG(LogTemp, Warning, TEXT("===logText Heapify %s"), *logText);
//往数组里堆排序规则添加元素
TNode n1("n1", 50);
TNode n2("n2", 60);
TNode n3("n3", 70);
openSet.HeapPush(n1,TNodeLess());
openSet.HeapPush(n2, TNodeLess());
openSet.HeapPush(n3, TNodeLess());
//打印添加新元素之后的数组
logText = "";
while (openSet.Num() > 0)
{
TNode n;
openSet.HeapPop(n, TNodeLess());//弹出最优先元素
logText.Append(n.key);
logText.Append("-");
logText.Append(FString::FromInt(n.prority));
logText.Append(" ");
}
UE_LOG(LogTemp, Warning, TEXT("===logText HeapPop %s"), *logText);