UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
BinaryHeap.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Templates/Invoke.h" // for older _MSC_FULL_VER only - see usage below
8
9#include <type_traits>
10
11namespace AlgoImpl
12{
19 template <typename IndexType>
21 {
22 return Index * 2 + 1;
23 }
24
31 template <typename IndexType>
32 FORCEINLINE bool HeapIsLeaf(IndexType Index, IndexType Count)
33 {
35 }
36
43 template <typename IndexType>
45 {
46 return (Index - 1) / 2;
47 }
48
58 template <typename RangeValueType, typename IndexType, typename ProjectionType, typename PredicateType>
59 inline void HeapSiftDown(RangeValueType* Heap, IndexType Index, const IndexType Count, const ProjectionType& InProj, const PredicateType& Predicate)
60 {
61// Workaround for a codegen bug that was discovered related to member function pointer projections that are virtual
62#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 194134123
63 const ProjectionType& Proj = InProj;
64#else
65 auto&& Proj = Projection(InProj);
66#endif
67 while (!HeapIsLeaf(Index, Count))
68 {
70 const IndexType RightChildIndex = LeftChildIndex + 1;
71
72 IndexType MinChildIndex = LeftChildIndex;
74 {
75// Workaround for a codegen bug that was discovered related to member function pointer projections that are virtual
76#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 194134123
78#else
80#endif
81 }
82
83#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 194134123
84 if (!Predicate( Invoke(Proj, Heap[MinChildIndex]), Invoke(Proj, Heap[Index]) ))
85#else
86 if (!Predicate( Proj(Heap[MinChildIndex]), Proj(Heap[Index]) ))
87#endif
88 {
89 break;
90 }
91
94 }
95 }
96
108 template <class RangeValueType, typename IndexType, typename ProjectionType, class PredicateType>
109 inline IndexType HeapSiftUp(RangeValueType* Heap, IndexType RootIndex, IndexType NodeIndex, const ProjectionType& InProj, const PredicateType& Predicate)
110 {
111// Workaround for a codegen bug that was discovered related to member function pointer projections that are virtual
112#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 194134123
113 const ProjectionType& Proj = InProj;
114#else
115 auto&& Proj = Projection(InProj);
116#endif
117 while (NodeIndex > RootIndex)
118 {
119 IndexType ParentIndex = HeapGetParentIndex(NodeIndex);
120// Workaround for a codegen bug that was discovered related to member function pointer projections that are virtual
121#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 194134123
122 if (!Predicate( Invoke(Proj, Heap[NodeIndex]), Invoke(Proj, Heap[ParentIndex]) ))
123#else
124 if (!Predicate( Proj(Heap[NodeIndex]), Proj(Heap[ParentIndex]) ))
125#endif
126 {
127 break;
128 }
129
130 Swap(Heap[NodeIndex], Heap[ParentIndex]);
131 NodeIndex = ParentIndex;
132 }
133
134 return NodeIndex;
135 }
136
146 template <typename RangeValueType, typename IndexType, typename ProjectionType, typename PredicateType>
148 {
149 if constexpr (std::is_signed_v<IndexType>)
150 {
151 checkf(Num >= 0, TEXT("Algo::HeapifyInternal called with negative count"));
152 }
153
154 if (Num == 0)
155 {
156 return;
157 }
158
159 IndexType Index = HeapGetParentIndex(Num - 1);
160 for (;;)
161 {
162 HeapSiftDown(First, Index, Num, Proj, Predicate);
163 if (Index == 0)
164 {
165 return;
166 }
167 --Index;
168 }
169 }
170
180 template <typename RangeValueType, typename IndexType, typename ProjectionType, class PredicateType>
182 {
183 if constexpr (std::is_signed_v<IndexType>)
184 {
185 checkf(Num >= 0, TEXT("Algo::HeapSortInternal called with negative count"));
186 }
187
188 if (Num == 0)
189 {
190 return;
191 }
192
193 TReversePredicate< PredicateType > ReversePredicateWrapper(Predicate); // Reverse the predicate to build a max-heap instead of a min-heap
195
196 for (IndexType Index = Num - 1; Index > 0; Index--)
197 {
198 Swap(First[0], First[Index]);
199
201 }
202 }
203}
204
205#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_6
206#include "Templates/Invoke.h"
207#endif
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
AUTORTFM_INFER UE_FORCEINLINE_HINT constexpr auto Invoke(FuncType &&Func, ArgTypes &&... Args) -> decltype(((FuncType &&) Func)((ArgTypes &&) Args...))
Definition Invoke.h:44
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Num
Definition MetalRHIPrivate.h:234
AUTORTFM_INFER constexpr auto Projection(Invocable0Type &&Invocable0, InvocableTypes &&... Invocables)
Definition Projection.h:108
Definition ReversePredicate.h:14
Definition BinarySearch.h:10
void HeapSortInternal(RangeValueType *First, IndexType Num, ProjectionType Proj, PredicateType Predicate)
Definition BinaryHeap.h:181
IndexType HeapSiftUp(RangeValueType *Heap, IndexType RootIndex, IndexType NodeIndex, const ProjectionType &InProj, const PredicateType &Predicate)
Definition BinaryHeap.h:109
void HeapifyInternal(RangeValueType *First, IndexType Num, ProjectionType Proj, PredicateType Predicate)
Definition BinaryHeap.h:147
FORCEINLINE IndexType HeapGetParentIndex(IndexType Index)
Definition BinaryHeap.h:44
void HeapSiftDown(RangeValueType *Heap, IndexType Index, const IndexType Count, const ProjectionType &InProj, const PredicateType &Predicate)
Definition BinaryHeap.h:59
FORCEINLINE bool HeapIsLeaf(IndexType Index, IndexType Count)
Definition BinaryHeap.h:32
FORCEINLINE IndexType HeapGetLeftChildIndex(IndexType Index)
Definition BinaryHeap.h:20
U16 Index
Definition radfft.cpp:71