UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Compare.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/EqualTo.h"
7#include "Templates/IdentityFunctor.h"
8#include "Templates/Invoke.h"
9#include "Templates/Less.h"
11
13{
14 template <typename InAT, typename InBT, typename ProjectionT, typename PredicateT>
15 constexpr bool Compare(const InAT& InputA, const InBT& InputB, ProjectionT Projection, PredicateT Predicate)
16 {
17 const SIZE_T SizeA = GetNum(InputA);
18 const SIZE_T SizeB = GetNum(InputB);
19
20 if (SizeA != SizeB)
21 {
22 return false;
23 }
24
25 auto* A = GetData(InputA);
26 auto* B = GetData(InputB);
27
28 for (SIZE_T Count = SizeA; Count; --Count)
29 {
30 if (!Invoke(Predicate, Invoke(Projection, *A++), Invoke(Projection, *B++)))
31 {
32 return false;
33 }
34 }
35
36 return true;
37 }
38}
39
40namespace Algo
41{
50 template <typename InAT, typename InBT>
51 [[nodiscard]] constexpr bool Compare(const InAT& InputA, const InBT& InputB)
52 {
54 }
55
65 template <typename InAT, typename InBT, typename PredicateT>
66 [[nodiscard]] constexpr bool Compare(const InAT& InputA, const InBT& InputB, PredicateT Predicate)
67 {
69 }
70
80 template <typename InAT, typename InBT, typename ProjectionT>
81 [[nodiscard]] constexpr bool CompareBy(const InAT& InputA, const InBT& InputB, ProjectionT Projection)
82 {
84 }
85
96 template <typename InAT, typename InBT, typename ProjectionT, typename PredicateT>
97 [[nodiscard]] constexpr bool CompareBy(const InAT& InputA, const InBT& InputB, ProjectionT Projection, PredicateT Predicate)
98 {
100 }
101
124 template <typename MapType, typename KeyLessThanType, typename ValueLessThanType>
126 {
127 using KeyType = typename MapType::KeyType;
128 using ValueType = typename MapType::ValueType;
129 if (A.Num() != B.Num())
130 {
131 return A.Num() < B.Num() ? -1 : 1;
132 }
133 if (A.Num() == 0)
134 {
135 return 0;
136 }
137
138 bool bAllKeysOfAAreInB = true;
139 const KeyType* MinKeyWithDifference = nullptr;
140 bool bMinKeyIsLessInA = false;
141 for (const auto& Pair : A) // TPair<KeyType, ValueType>
142 {
143 const ValueType* BValue = B.Find(Pair.Key);
144 int Compare = 0;
145 if (!BValue)
146 {
147 bAllKeysOfAAreInB = false;
148 Compare = -1;
149 }
150 else if (ValueLessThan(Pair.Value, *BValue))
151 {
152 Compare = -1;
153 }
154 else if (ValueLessThan(*BValue, Pair.Value))
155 {
156 Compare = 1;
157 }
158 if (Compare != 0)
159 {
161 {
162 MinKeyWithDifference = &Pair.Key;
164 }
165 }
166 }
167
168 // The number of keys in A and B is the same (checked above), so if all keys of A are in B, then there are
169 // no additional keys in B that are not in A and we don't need to iterate over B.
171 {
172 // If B has additional keys not in A then we need to check each of those not-in-A keys to see if they
173 // are smaller than the MinKeyWithDifference, and if so they become the MinKeyWithDifference.
174 for (const auto& Pair : B) // TPair<KeyType, ValueType>
175 {
176 if (!A.Contains(Pair.Key))
177 {
179 {
180 MinKeyWithDifference = &Pair.Key;
181 bMinKeyIsLessInA = false;
182 }
183 }
184 }
185 }
187 {
188 return bMinKeyIsLessInA ? -1 : 1;
189 }
190 return 0;
191 }
192
193 template <typename MapType>
194 [[nodiscard]] int CompareMap(const MapType& A, const MapType& B)
195 {
196 return CompareMap(A, B, TLess<>(), TLess<>());
197 }
198
199 template <typename MapType, typename KeyLessThanType>
200 [[nodiscard]] int CompareMap(const MapType& A, const MapType& B, KeyLessThanType KeyLessThan)
201 {
203 }
204
223 template <typename SetType, typename KeyLessThanType>
224 [[nodiscard]] int CompareSet(const SetType& A, const SetType& B, KeyLessThanType KeyLessThan)
225 {
226 using KeyType = typename SetType::ElementType;
227 if (A.Num() != B.Num())
228 {
229 return A.Num() < B.Num() ? -1 : 1;
230 }
231 if (A.Num() == 0)
232 {
233 return 0;
234 }
235
236 const KeyType* MinKeyWithDifference = nullptr;
237 bool bMinKeyIsLessInA = false;
238 for (const KeyType& AKey : A)
239 {
240 if (!B.Contains(AKey))
241 {
243 {
245 bMinKeyIsLessInA = true;
246 }
247 }
248 }
249
250 // The number of keys in A and B is the same (checked above), so if all keys of A are in B, then there are
251 // no additional keys in B that are not in A and we don't need to iterate over B.
253 {
254 // If B has additional keys not in A then we need to check each of those not-in-A keys to see if they
255 // are smaller than the MinKeyWithDifference, and if so they become the MinKeyWithDifference.
256 for (const KeyType& BKey : B)
257 {
258 if (!A.Contains(BKey))
259 {
261 {
263 bMinKeyIsLessInA = false;
264 }
265 }
266 }
267 }
269 {
270 return bMinKeyIsLessInA ? -1 : 1;
271 }
272 return 0;
273 }
274
275 template <typename SetType>
276 [[nodiscard]] int CompareSet(const SetType& A, const SetType& B)
277 {
278 return CompareSet(A, B, TLess<>());
279 }
280}
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
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 > && MoveTempIfPossible(T &&Obj) noexcept
Definition UnrealTemplate.h:538
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition Compare.h:13
constexpr bool Compare(const InAT &InputA, const InBT &InputB, ProjectionT Projection, PredicateT Predicate)
Definition Compare.h:15
Definition ParallelSort.h:13
int CompareMap(const MapType &A, const MapType &B, KeyLessThanType KeyLessThan, ValueLessThanType ValueLessThan)
Definition Compare.h:125
constexpr bool CompareBy(const InAT &InputA, const InBT &InputB, ProjectionT Projection)
Definition Compare.h:81
int CompareSet(const SetType &A, const SetType &B, KeyLessThanType KeyLessThan)
Definition Compare.h:224
constexpr bool Compare(const InAT &InputA, const InBT &InputB)
Definition Compare.h:51
Definition IdentityFunctor.h:11
Definition EqualTo.h:18
Definition Less.h:19