UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GraphConvert.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Algo/Sort.h"
6#include "Algo/Unique.h"
7#include "Containers/Array.h"
9#include "Containers/Map.h"
10#include "HAL/Platform.h"
12#include "Misc/EnumClassFlags.h"
13#include "Templates/IdentityFunctor.h"
14#include "Templates/Invoke.h"
15#include "Traits/ElementType.h"
16
17#include <utility>
18
19namespace UE::Graph
20{
21
22typedef int32 FVertex;
23// Valid vertices are in the range 0 <= vertex < MAX_int32.
24// This ensures that a edge list that contains an edge to every vertex from 0 to MaxVertex inclusive in a graph can be stored in
25// a TArray or TArrayView.
26inline constexpr FVertex MaxVertex = static_cast<FVertex>(MAX_int32-1);
27inline constexpr FVertex InvalidVertex = static_cast<FVertex>(INDEX_NONE);
28
34struct FGraph
35{
36 // Singular buffer which each element of EdgeLists is a view into.
38 // Each member of EdgeLists is a slice of Buffer.
40
41 FGraph() = default;
42 // Copy construction not implemented as buffers may be very large and expensive to copy
43 FGraph(const FGraph&) = delete;
44 FGraph& operator=(const FGraph&) = delete;
45 FGraph(FGraph&&) = default;
46 FGraph& operator=(FGraph&&) = default;
47
49 {
50 return Buffer.GetAllocatedSize() + EdgeLists.GetAllocatedSize();
51 }
52};
53
61{
64
65 FMappingOneToMany() = default;
66 // Copy construction not implemented as buffers may be very large and expensive to copy
71
73 {
74 return Buffer.GetAllocatedSize() + Mapping.GetAllocatedSize();
75 }
76};
77
99
101{
102 None = 0,
104 Shrink = 1 << 0,
105};
107
128template <typename RangeType, typename GetKeyEdgesType>
130 const RangeType& UniqueKeys,
133{
134 using KeyType = TElementType_T<RangeType>;
135
136 // Map Elements to vertices in VertexOfKey
137 int32 NumVertices = GetNum(UniqueKeys);
139 VertexOfKey.Reserve(NumVertices);
140 FVertex Vertex = 0;
141 for (const KeyType& Key : UniqueKeys)
142 {
146 {
148 }
149 }
150
152 OutGraph.Buffer.Reset();
154 {
155 OutGraph.EdgeLists.Empty(NumVertices);
156 }
157 else
158 {
159 OutGraph.EdgeLists.Reset(NumVertices);
160 }
161
162 // Temporary base pointer for edge lists until the data pointer of OutGraph.Buffer is fixed.
163 FVertex* EdgeListBase = nullptr;
164 // Call GetKeyEdges on each Element, map its returnvalue to Vertices, normalize them, and store them in EdgeOffsets
165 Vertex = 0;
166 for (const KeyType& Element : UniqueKeys)
167 {
168 int64 InitialOffset = OutGraph.Buffer.Num();
169 for (const KeyType& Dependency : Invoke(GetKeyEdges, Element))
170 {
172 if (!TargetVertex)
173 {
174 continue;
175 }
176
177 // Normalize Step 1: remove edges to self
178 if (*TargetVertex == Vertex)
179 {
180 continue;
181 }
182 OutGraph.Buffer.Add(*TargetVertex);
183 }
184
185 // Normalize Step 2: Sort
188
189 // Normalize Step 3: Remove duplicates
190 VertexEdges = VertexEdges.Left(Algo::Unique(VertexEdges));
192
193 // Store the vertex's offset into GraphBuffer, for later fixup
194 // Edge list is guaranteed to fit within 32 bits after removal of duplicates
195 OutGraph.EdgeLists.Emplace(EdgeListBase + InitialOffset, static_cast<int32>(VertexEdges.Num())); //-V769
196
197 ++Vertex;
198 }
200 {
201 OutGraph.Buffer.Shrink();
202 }
203
204 // Correct data pointers in edge lists
205 FVertex* NewBase = OutGraph.Buffer.GetData();
207 {
208 EdgeList = TConstArrayView<FVertex>(NewBase + (EdgeList.GetData() - EdgeListBase), EdgeList.Num());
209 }
210 return OutGraph;
211}
212
227template <typename RangeType, typename ProjectionType>
229 RangeType&& Graph,
232{
234 typedef decltype(Proj(std::declval<const InEdgeRangeType&>())) ProjectedEdgeRangeType;
235
236 int32 NumVertices = Graph.Num();
237 int64 NumEdges = 0;
238 for (const InEdgeRangeType& VertexEdges : Graph)
239 {
240 NumEdges += GetNum(Proj(VertexEdges));
241 }
242
245 {
246 OutGraph.Buffer.Empty(NumEdges);
247 OutGraph.EdgeLists.Empty(NumVertices);
248 }
249 else
250 {
251 OutGraph.Buffer.Reset(NumEdges);
252 OutGraph.EdgeLists.Reset(NumVertices);
253 }
254
255 FVertex* OutBufferData = OutGraph.Buffer.GetData();
256 for (const InEdgeRangeType& InVertexEdges : Graph)
257 {
260 OutGraph.EdgeLists.Emplace(OutBufferData + OutGraph.Buffer.Num(), NumVertexEdges);
262 }
263 check(OutGraph.Buffer.GetData() == OutBufferData); // We have arrayviews into OutBufferData
264 return OutGraph;
265}
266
276template <typename RangeType>
282
294
295
321 FGraph& OutGraph,
322 FMappingOneToMany* OutCondensationVertexToInputVertex,
323 FMappingManyToOne* OutInputVertexToCondensationVertex,
325
342}
#define check(expr)
Definition AssertionMacros.h:314
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
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
typename TElementType< T >::Type TElementType_T
Definition ElementType.h:57
constexpr bool EnumHasAnyFlags(Enum Flags, Enum Contains)
Definition EnumClassFlags.h:35
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
@ Vertex
Definition MetalRHIPrivate.h:223
#define MAX_int32
Definition NumericLimits.h:25
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
Definition ArrayView.h:139
Definition Array.h:670
UE_NODEBUG UE_FORCEINLINE_HINT SIZE_T GetAllocatedSize(void) const
Definition Array.h:1059
Definition UnrealString.h.inl:34
UE_REWRITE void Sort(RangeType &&Range)
Definition Sort.h:16
Definition GraphConvert.cpp:12
FGraph ConvertToSingleBufferGraph(RangeType &&Graph, ProjectionType Proj, EConvertToGraphOptions Options=EConvertToGraphOptions::None)
Definition GraphConvert.h:228
int32 FVertex
Definition GraphConvert.h:22
FGraph ConstructPartialTransposeGraph(TConstArrayView< TConstArrayView< FVertex > > Graph, TArrayView< FVertex > InVertices, int64 MaxOutGraphEdges, TArray< FVertex > &OutInputVerticesPresentInOutputGraph)
Definition GraphConvert.cpp:359
constexpr FVertex MaxVertex
Definition GraphConvert.h:26
bool TryConstructCondensationGraph(TConstArrayView< TConstArrayView< FVertex > > Graph, FGraph &OutGraph, FMappingOneToMany *OutCondensationVertexToInputVertex, FMappingManyToOne *OutInputVertexToCondensedVertex, EConvertToGraphOptions Options)
Definition GraphConvert.cpp:67
FGraph ConstructTransposeGraph(TConstArrayView< TConstArrayView< FVertex > > Graph, EConvertToGraphOptions Options)
Definition GraphConvert.cpp:14
FGraph ConvertToGraph(const RangeType &UniqueKeys, GetKeyEdgesType GetKeyEdges, EConvertToGraphOptions Options=EConvertToGraphOptions::None)
Definition GraphConvert.h:129
constexpr FVertex InvalidVertex
Definition GraphConvert.h:27
EConvertToGraphOptions
Definition GraphConvert.h:101
Definition IdentityFunctor.h:11
Definition BlendSpaceHelpers.h:20
Definition GraphConvert.h:35
FGraph & operator=(FGraph &&)=default
FGraph(FGraph &&)=default
FGraph & operator=(const FGraph &)=delete
FGraph(const FGraph &)=delete
SIZE_T GetAllocatedSize() const
Definition GraphConvert.h:48
TArray< TConstArrayView< FVertex > > EdgeLists
Definition GraphConvert.h:39
TArray64< FVertex > Buffer
Definition GraphConvert.h:37
Definition GraphConvert.h:85
SIZE_T GetAllocatedSize() const
Definition GraphConvert.h:94
FMappingManyToOne & operator=(FMappingManyToOne &&)=default
FMappingManyToOne(const FMappingManyToOne &)=default
FMappingManyToOne & operator=(const FMappingManyToOne &)=default
FMappingManyToOne(FMappingManyToOne &&)=default
TArray< FVertex > Mapping
Definition GraphConvert.h:86
Definition GraphConvert.h:61
FMappingOneToMany & operator=(const FMappingOneToMany &)=delete
FMappingOneToMany & operator=(FMappingOneToMany &&)=default
SIZE_T GetAllocatedSize() const
Definition GraphConvert.h:72
TArray64< FVertex > Buffer
Definition GraphConvert.h:62
FMappingOneToMany(FMappingOneToMany &&)=default
FMappingOneToMany(const FMappingOneToMany &)=delete
TArray< TConstArrayView< FVertex > > Mapping
Definition GraphConvert.h:63