UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StableSort.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Algo/BinarySearch.h"
6#include "Algo/Rotate.h"
7#include "Templates/IdentityFunctor.h"
8#include "Templates/Invoke.h"
9#include "Templates/Less.h"
10#include "Templates/UnrealTemplate.h" // For GetData, GetNum, Swap
11
12
13namespace AlgoImpl
14{
15 template <typename T, typename ProjectionType, typename PredicateType>
37
38 inline constexpr int32 MinMergeSubgroupSize = 2;
39
49 template <typename T, typename ProjectionType, typename PredicateType>
51 {
53
54 if constexpr (MinMergeSubgroupSize > 1)
55 {
56 if constexpr (MinMergeSubgroupSize > 2)
57 {
58 // First pass with simple bubble-sort.
59 do
60 {
62 if (Num < GroupEnd)
63 {
64 GroupEnd = Num;
65 }
66 do
67 {
68 for (int32 It = SubgroupStart; It < GroupEnd - 1; ++It)
69 {
70 if (Invoke(Predicate, Invoke(Projection, First[It + 1]), Invoke(Projection, First[It])))
71 {
72 Swap(First[It], First[It + 1]);
73 }
74 }
75 GroupEnd--;
76 }
77 while (GroupEnd - SubgroupStart > 1);
78
80 }
81 while (SubgroupStart < Num);
82 }
83 else
84 {
85 for (int32 Subgroup = 0; Subgroup < Num; Subgroup += 2)
86 {
87 if (Subgroup + 1 < Num && Invoke(Predicate, Invoke(Projection, First[Subgroup + 1]), Invoke(Projection, First[Subgroup])))
88 {
90 }
91 }
92 }
93 }
94
96 while (SubgroupSize < Num)
97 {
98 SubgroupStart = 0;
99 do
100 {
102 if (Num - SubgroupStart < MergeNum)
103 {
105 }
106
109 }
110 while (SubgroupStart < Num);
111
112 SubgroupSize <<= 1;
113 }
114 }
115}
116
117namespace Algo
118{
124 template <typename RangeType>
125 UE_REWRITE void StableSort(RangeType&& Range)
126 {
128 }
129
136 template <typename RangeType, typename PredicateType>
137 UE_REWRITE void StableSort(RangeType&& Range, PredicateType Pred)
138 {
140 }
141
148 template <typename RangeType, typename ProjectionType>
150 {
152 }
153
161 template <typename RangeType, typename ProjectionType, typename PredicateType>
166}
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_REWRITE
Definition Platform.h:747
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
auto GetNum(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Length())
Definition StringConv.h:808
auto GetData(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Get())
Definition StringConv.h:802
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition BinarySearch.h:10
int32 RotateInternal(T *First, int32 Num, int32 Count)
Definition Rotate.h:11
constexpr int32 MinMergeSubgroupSize
Definition StableSort.h:38
SizeType UpperBoundInternal(RangeValueType *First, const SizeType Num, const PredicateValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate)
Definition BinarySearch.h:56
SizeType LowerBoundInternal(RangeValueType *First, const SizeType Num, const PredicateValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate)
Definition BinarySearch.h:23
void StableSortInternal(T *First, int32 Num, ProjectionType Projection, PredicateType Predicate)
Definition StableSort.h:50
Definition ParallelSort.h:13
UE_REWRITE void StableSortBy(RangeType &&Range, ProjectionType Proj)
Definition StableSort.h:149
UE_REWRITE void StableSort(RangeType &&Range)
Definition StableSort.h:125
Definition IdentityFunctor.h:11
Definition Less.h:19