UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LegacySort.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Templates/IdentityFunctor.h"
6#include "Templates/Invoke.h"
7#include "Templates/Less.h"
8#include "Templates/UnrealTemplate.h" // For GetData, GetNum
9
10
11namespace AlgoImpl
12{
22 template <typename T, typename ProjectionType, typename PredicateType>
24 {
25 struct FStack
26 {
27 T* Min;
28 T* Max;
29 };
30
31 if( Num < 2 )
32 {
33 return;
34 }
35 FStack RecursionStack[32]={{First,First+Num-1}}, Current, Inner;
36 for( FStack* StackTop=RecursionStack; StackTop>=RecursionStack; --StackTop ) //-V625
37 {
39 Loop:
40 PTRINT Count = Current.Max - Current.Min + 1;
41 if( Count <= 8 )
42 {
43 // Use simple bubble-sort.
44 while( Current.Max > Current.Min )
45 {
46 T *Max, *Item;
47 for( Max=Current.Min, Item=Current.Min+1; Item<=Current.Max; Item++ )
48 {
49 if( Predicate( Invoke( Projection, *Max ), Invoke( Projection, *Item ) ) )
50 {
51 Max = Item;
52 }
53 }
54 Swap( *Max, *Current.Max-- );
55 }
56 }
57 else
58 {
59 // Grab middle element so sort doesn't exhibit worst-cast behavior with presorted lists.
60 Swap( Current.Min[Count/2], Current.Min[0] );
61
62 // Divide list into two halves, one with items <=Current.Min, the other with items >Current.Max.
63 Inner.Min = Current.Min;
64 Inner.Max = Current.Max+1;
65 for( ; ; )
66 {
67 while( ++Inner.Min<=Current.Max && !Predicate( Invoke( Projection, *Current.Min ), Invoke( Projection, *Inner.Min ) ) );
68 while( --Inner.Max> Current.Min && !Predicate( Invoke( Projection, *Inner.Max ), Invoke( Projection, *Current.Min ) ) );
69 if( Inner.Min>Inner.Max )
70 {
71 break;
72 }
73 Swap( *Inner.Min, *Inner.Max );
74 }
75 Swap( *Current.Min, *Inner.Max );
76
77 // Save big half and recurse with small half.
78 if( Inner.Max-1-Current.Min >= Current.Max-Inner.Min )
79 {
80 if( Current.Min+1 < Inner.Max )
81 {
82 StackTop->Min = Current.Min;
83 StackTop->Max = Inner.Max - 1;
84 StackTop++;
85 }
86 if( Current.Max>Inner.Min )
87 {
88 Current.Min = Inner.Min;
89 goto Loop;
90 }
91 }
92 else
93 {
94 if( Current.Max>Inner.Min )
95 {
96 StackTop->Min = Inner .Min;
97 StackTop->Max = Current.Max;
98 StackTop++;
99 }
100 if( Current.Min+1<Inner.Max )
101 {
102 Current.Max = Inner.Max - 1;
103 goto Loop;
104 }
105 }
106 }
107 }
108 }
109}
110
111namespace Algo
112{
119 template <typename RangeType>
120 UE_REWRITE void LegacySort(RangeType&& Range)
121 {
123 }
124
132 template <typename RangeType, typename PredicateType>
133 UE_REWRITE void LegacySort(RangeType&& Range, PredicateType Pred)
134 {
136 }
137
145 template <typename RangeType, typename ProjectionType>
147 {
149 }
150
159 template <typename RangeType, typename ProjectionType, typename PredicateType>
164}
FPlatformTypes::PTRINT PTRINT
A signed integer the same size as a pointer.
Definition Platform.h:1148
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
void LegacySortInternal(T *First, int32 Num, ProjectionType Projection, PredicateType Predicate)
Definition LegacySort.h:23
Definition ParallelSort.h:13
UE_REWRITE void LegacySortBy(RangeType &&Range, ProjectionType Proj)
Definition LegacySort.h:146
UE_REWRITE void LegacySort(RangeType &&Range)
Definition LegacySort.h:120
Definition IdentityFunctor.h:11
Definition Less.h:19