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
5#include "Templates/IdentityFunctor.h"
6#include "Templates/Invoke.h"
7#include "Templates/Less.h"
8
9namespace AlgoImpl
10{
22 template <typename RangeValueType, typename SizeType, typename PredicateValueType, typename ProjectionType, typename SortPredicateType>
24 {
25 // Current start of sequence to check
26 SizeType Start = 0;
27 // Size of sequence to check
28 SizeType Size = Num;
29
30 // 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
31 while (Size > 0)
32 {
33 const SizeType LeftoverSize = Size % 2;
34 Size = Size / 2;
35
36 const SizeType CheckIndex = Start + Size;
37 const SizeType StartIfLess = CheckIndex + LeftoverSize;
38
39 auto&& CheckValue = Invoke(Projection, First[CheckIndex]);
40 Start = SortPredicate(CheckValue, Value) ? StartIfLess : Start;
41 }
42 return Start;
43 }
44
55 template <typename RangeValueType, typename SizeType, typename PredicateValueType, typename ProjectionType, typename SortPredicateType>
57 {
58 // Current start of sequence to check
59 SizeType Start = 0;
60 // Size of sequence to check
61 SizeType Size = Num;
62
63 // 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
64 while (Size > 0)
65 {
66 const SizeType LeftoverSize = Size % 2;
67 Size = Size / 2;
68
69 const SizeType CheckIndex = Start + Size;
70 const SizeType StartIfLess = CheckIndex + LeftoverSize;
71
72 auto&& CheckValue = Invoke(Projection, First[CheckIndex]);
73 Start = !SortPredicate(Value, CheckValue) ? StartIfLess : Start;
74 }
75
76 return Start;
77 }
78}
79
80namespace Algo
81{
91 template <typename RangeType, typename ValueType, typename SortPredicateType>
92 [[nodiscard]] UE_REWRITE auto LowerBound(const RangeType& Range, const ValueType& Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
93 {
95 }
96 template <typename RangeType, typename ValueType>
97 [[nodiscard]] UE_REWRITE auto LowerBound(const RangeType& Range, const ValueType& Value) -> decltype(GetNum(Range))
98 {
100 }
101
112 template <typename RangeType, typename ValueType, typename ProjectionType, typename SortPredicateType>
113 [[nodiscard]] UE_REWRITE auto LowerBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
114 {
116 }
117 template <typename RangeType, typename ValueType, typename ProjectionType>
118 [[nodiscard]] UE_REWRITE auto LowerBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection) -> decltype(GetNum(Range))
119 {
121 }
122
132 template <typename RangeType, typename ValueType, typename SortPredicateType>
133 [[nodiscard]] UE_REWRITE auto UpperBound(const RangeType& Range, const ValueType& Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
134 {
136 }
137 template <typename RangeType, typename ValueType>
138 [[nodiscard]] UE_REWRITE auto UpperBound(const RangeType& Range, const ValueType& Value) -> decltype(GetNum(Range))
139 {
141 }
142
153 template <typename RangeType, typename ValueType, typename ProjectionType, typename SortPredicateType>
154 [[nodiscard]] UE_REWRITE auto UpperBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
155 {
157 }
158 template <typename RangeType, typename ValueType, typename ProjectionType>
159 [[nodiscard]] UE_REWRITE auto UpperBoundBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection) -> decltype(GetNum(Range))
160 {
162 }
163
172 template <typename RangeType, typename ValueType, typename SortPredicateType>
173 [[nodiscard]] auto BinarySearch(const RangeType& Range, const ValueType& Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
174 {
175 auto CheckIndex = LowerBound(Range, Value, SortPredicate);
176 if (CheckIndex < GetNum(Range))
177 {
178 auto&& CheckValue = GetData(Range)[CheckIndex];
179 // Since we returned lower bound we already know Value <= CheckValue. So if Value is not < CheckValue, they must be equal
181 {
182 return CheckIndex;
183 }
184 }
185 return INDEX_NONE;
186 }
187 template <typename RangeType, typename ValueType>
188 [[nodiscard]] UE_REWRITE auto BinarySearch(const RangeType& Range, const ValueType& Value)
189 {
190 return BinarySearch(Range, Value, TLess<>());
191 }
192
202 template <typename RangeType, typename ValueType, typename ProjectionType, typename SortPredicateType>
203 [[nodiscard]] auto BinarySearchBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
204 {
205 auto CheckIndex = LowerBoundBy(Range, Value, Projection, SortPredicate);
206 if (CheckIndex < GetNum(Range))
207 {
208 auto&& CheckValue = Invoke(Projection, GetData(Range)[CheckIndex]);
209 // Since we returned lower bound we already know Value <= CheckValue. So if Value is not < CheckValue, they must be equal
211 {
212 return CheckIndex;
213 }
214 }
215 return INDEX_NONE;
216 }
217 template <typename RangeType, typename ValueType, typename ProjectionType>
218 [[nodiscard]] UE_REWRITE auto BinarySearchBy(const RangeType& Range, const ValueType& Value, ProjectionType Projection)
219 {
220 return BinarySearchBy(Range, Value, Projection, TLess<>());
221 }
222}
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#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
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
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
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
Definition ParallelSort.h:13
UE_REWRITE auto LowerBound(const RangeType &Range, const ValueType &Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:92
auto BinarySearch(const RangeType &Range, const ValueType &Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:173
UE_REWRITE auto LowerBoundBy(const RangeType &Range, const ValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:113
auto BinarySearchBy(const RangeType &Range, const ValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:203
UE_REWRITE auto UpperBound(const RangeType &Range, const ValueType &Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:133
UE_REWRITE auto UpperBoundBy(const RangeType &Range, const ValueType &Value, ProjectionType Projection, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:154
Definition IdentityFunctor.h:11
Definition Less.h:19