UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IsSorted.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Templates/IdentityFunctor.h"
7#include "Templates/Invoke.h"
8#include "Templates/Less.h"
9#include "Templates/UnrealTemplate.h" // For GetData, GetNum, MoveTemp
10
11
12namespace AlgoImpl
13{
14 template <typename T, typename IndexType, typename ProjectionType, typename PredType>
15 bool IsSortedBy(const T* Range, IndexType RangeSize, ProjectionType Proj, PredType Pred)
16 {
17 if (RangeSize == 0)
18 {
19 return true;
20 }
21
22 // When comparing N elements, we do N-1 comparisons
23 --RangeSize;
24
25 const T* Next = Range + 1;
26 for (;;)
27 {
28 if (RangeSize == 0)
29 {
30 return true;
31 }
32
33 auto&& Ref1 = Invoke(Proj, *Next);
34 auto&& Ref2 = Invoke(Proj, *Range);
35 if (Invoke(Pred, Ref1, Ref2))
36 {
37 return false;
38 }
39
40 ++Range;
41 ++Next;
42 --RangeSize;
43 }
44 }
45
46 template <typename T>
47 struct TLess
48 {
49 FORCEINLINE bool operator()(const T& Lhs, const T& Rhs) const
50 {
51 return Lhs < Rhs;
52 }
53 };
54}
55
56namespace Algo
57{
65 template <typename RangeType>
66 [[nodiscard]] UE_REWRITE bool IsSorted(const RangeType& Range)
67 {
68 return AlgoImpl::IsSortedBy(GetData(Range), GetNum(Range), FIdentityFunctor(), TLess<>());
69 }
70
79 template <typename RangeType, typename PredType>
80 [[nodiscard]] UE_REWRITE bool IsSorted(const RangeType& Range, PredType Pred)
81 {
83 }
84
92 template <typename RangeType, typename ProjectionType>
93 [[nodiscard]] UE_REWRITE bool IsSortedBy(const RangeType& Range, ProjectionType Projection)
94 {
96 }
97
106 template <typename RangeType, typename ProjectionType, typename PredType>
108 {
110 }
111}
#define FORCEINLINE
Definition AndroidPlatform.h:140
#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
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
bool IsSortedBy(const T *Range, IndexType RangeSize, ProjectionType Proj, PredType Pred)
Definition IsSorted.h:15
Definition ParallelSort.h:13
UE_REWRITE bool IsSorted(const RangeType &Range)
Definition IsSorted.h:66
UE_REWRITE bool IsSortedBy(const RangeType &Range, ProjectionType Projection)
Definition IsSorted.h:93
Definition IsSorted.h:48
FORCEINLINE bool operator()(const T &Lhs, const T &Rhs) const
Definition IsSorted.h:49
Definition IdentityFunctor.h:11
Definition Less.h:19