UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
BinarySearch.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
8
9// nb: this was copied over from Core until uLang can properly depend on Core.
10
11namespace uLang
12{
13namespace AlgoImpl
14{
26 template <typename RangeValueType, typename SizeType, typename PredicateValueType, typename ProjectionType, typename SortPredicateType>
28 {
29 // Current start of sequence to check
30 SizeType Start = 0;
31 // Size of sequence to check
32 SizeType Size = Num;
33
34 // With this method, if Size is even it will do one more comparison than necessary, but because Size can be predicted by the CPU it is faster in practice
35 while (Size > 0)
36 {
37 const SizeType LeftoverSize = Size % 2;
38 Size = Size / 2;
39
40 const SizeType CheckIndex = Start + Size;
41 const SizeType StartIfLess = CheckIndex + LeftoverSize;
42
43 auto&& CheckValue = Invoke(Projection, First[CheckIndex]);
44 Start = SortPredicate(CheckValue, Value) ? StartIfLess : Start;
45 }
46 return Start;
47 }
48
59 template <typename RangeValueType, typename SizeType, typename PredicateValueType, typename ProjectionType, typename SortPredicateType>
61 {
62 // Current start of sequence to check
63 SizeType Start = 0;
64 // Size of sequence to check
65 SizeType Size = Num;
66
67 // With this method, if Size is even it will do one more comparison than necessary, but because Size can be predicted by the CPU it is faster in practice
68 while (Size > 0)
69 {
70 const SizeType LeftoverSize = Size % 2;
71 Size = Size / 2;
72
73 const SizeType CheckIndex = Start + Size;
74 const SizeType StartIfLess = CheckIndex + LeftoverSize;
75
76 auto&& CheckValue = Invoke(Projection, First[CheckIndex]);
77 Start = !SortPredicate(Value, CheckValue) ? StartIfLess : Start;
78 }
79
80 return Start;
81 }
82}
83
84namespace Algo
85{
95 template <typename RangeType, typename ValueType, typename SortPredicateType>
96 [[nodiscard]] ULANG_FORCEINLINE auto LowerBound(const RangeType& Range, const ValueType& Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
97 {
99 }
100 template <typename RangeType, typename ValueType>
101 [[nodiscard]] ULANG_FORCEINLINE auto LowerBound(const RangeType& Range, const ValueType& Value) -> decltype(GetNum(Range))
102 {
104 }
105
116 template <typename RangeType, typename ValueType, typename ProjectionType, typename SortPredicateType>
117 [[nodiscard]] ULANG_FORCEINLINE auto LowerBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
118 {
120 }
121 template <typename RangeType, typename ValueType, typename ProjectionType>
122 [[nodiscard]] ULANG_FORCEINLINE auto LowerBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection) -> decltype(GetNum(Range))
123 {
125 }
126
136 template <typename RangeType, typename ValueType, typename SortPredicateType>
137 [[nodiscard]] ULANG_FORCEINLINE auto UpperBound(const RangeType& Range, const ValueType& Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
138 {
140 }
141 template <typename RangeType, typename ValueType>
142 [[nodiscard]] ULANG_FORCEINLINE auto UpperBound(const RangeType& Range, const ValueType& Value) -> decltype(GetNum(Range))
143 {
145 }
146
157 template <typename RangeType, typename ValueType, typename ProjectionType, typename SortPredicateType>
158 [[nodiscard]] ULANG_FORCEINLINE auto UpperBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
159 {
161 }
162 template <typename RangeType, typename ValueType, typename ProjectionType>
163 [[nodiscard]] ULANG_FORCEINLINE auto UpperBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection) -> decltype(GetNum(Range))
164 {
166 }
167
176 template <typename RangeType, typename ValueType, typename SortPredicateType>
177 [[nodiscard]] ULANG_FORCEINLINE auto BinarySearch(const RangeType& Range, const ValueType& Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
178 {
179 auto CheckIndex = LowerBound(Range, Value, SortPredicate);
180 if (CheckIndex < GetNum(Range))
181 {
182 auto&& CheckValue = GetData(Range)[CheckIndex];
183 // Since we returned lower bound we already know Value <= CheckValue. So if Value is not < CheckValue, they must be equal
185 {
186 return CheckIndex;
187 }
188 }
189 return IndexNone;
190 }
191 template <typename RangeType, typename ValueType>
192 [[nodiscard]] ULANG_FORCEINLINE auto BinarySearch(const RangeType& Range, const ValueType& Value)
193 {
194 return BinarySearch(Range, Value, TLess<>());
195 }
196
206 template <typename RangeType, typename ValueType, typename ProjectionType, typename SortPredicateType>
207 [[nodiscard]] ULANG_FORCEINLINE auto BinarySearchBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
208 {
209 auto CheckIndex = LowerBoundBy(Range, Value, Projection, SortPredicate);
210 if (CheckIndex < GetNum(Range))
211 {
212 auto&& CheckValue = Invoke(Projection, GetData(Range)[CheckIndex]);
213 // Since we returned lower bound we already know Value <= CheckValue. So if Value is not < CheckValue, they must be equal
215 {
216 return CheckIndex;
217 }
218 }
219 return IndexNone;
220 }
221 template <typename RangeType, typename ValueType, typename ProjectionType>
222 [[nodiscard]] ULANG_FORCEINLINE auto BinarySearchBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection)
223 {
224 return BinarySearchBy(Range, Value, Projection, TLess<>());
225 }
226}
227}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Num
Definition MetalRHIPrivate.h:234
bool SortPredicate(const FCompileOnTheFlyData &A, const FCompileOnTheFlyData &B)
Definition MovieSceneCompiledDataManager.cpp:364
AUTORTFM_INFER constexpr auto Projection(Invocable0Type &&Invocable0, InvocableTypes &&... Invocables)
Definition Projection.h:108
#define ULANG_FORCEINLINE
Definition Common.h:188
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
uint32 Size
Definition VulkanMemory.cpp:4034
Definition BinarySearch.h:10
Definition ParallelSort.h:13
ULANG_FORCEINLINE SizeType UpperBoundInternal(RangeValueType *First, const SizeType Num, const PredicateValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate)
Definition BinarySearch.h:60
ULANG_FORCEINLINE SizeType LowerBoundInternal(RangeValueType *First, const SizeType Num, const PredicateValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate)
Definition BinarySearch.h:27
ULANG_FORCEINLINE auto UpperBoundBy(const RangeType &Range, const ValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:158
ULANG_FORCEINLINE auto BinarySearch(const RangeType &Range, const ValueType &Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:177
ULANG_FORCEINLINE auto BinarySearchBy(const RangeType &Range, const ValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:207
ULANG_FORCEINLINE auto LowerBound(const RangeType &Range, const ValueType &Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:96
ULANG_FORCEINLINE auto LowerBoundBy(const RangeType &Range, const ValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:117
ULANG_FORCEINLINE auto UpperBound(const RangeType &Range, const ValueType &Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:137
Definition VVMEngineEnvironment.h:23
@ IndexNone
Definition Common.h:381
ULANG_FORCEINLINE auto Invoke(FuncType &&Func, ArgTypes &&... Args) -> decltype(uLang::ForwardArg< FuncType >(Func)(uLang::ForwardArg< ArgTypes >(Args)...))
Definition Invoke.h:47
Definition IdentityFunctor.h:15
Definition Sorting.h:36