UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DisjointSet.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6
8{
9public:
11 FDisjointSet( const uint32 Size );
12
13 void Init( uint32 Size );
14 void Reset();
15 void AddDefaulted( uint32 Num = 1 );
16
17 void Union( uint32 x, uint32 y );
18 void UnionSequential( uint32 x, uint32 y );
19 uint32 Find( uint32 i );
20
21 uint32 operator[]( uint32 i ) const { return Parents[i]; }
22
23private:
24 TArray< uint32 > Parents;
25};
26
28{
29 Init( Size );
30}
31
33{
35 for( uint32 i = 0; i < Size; i++ )
36 {
37 Parents[i] = i;
38 }
39}
40
42{
43 Parents.Reset();
44}
45
47{
48 uint32 Start = Parents.AddUninitialized( Num );
49
50 for( uint32 i = Start; i < Start + Num; i++ )
51 {
52 Parents[i] = i;
53 }
54}
55
56// Union with splicing
58{
59 uint32 px = Parents[x];
60 uint32 py = Parents[y];
61
62 while( px != py )
63 {
64 // Pick larger
65 if( px < py )
66 {
67 Parents[x] = py;
68 if( x == px )
69 {
70 return;
71 }
72 x = px;
73 px = Parents[x];
74 }
75 else
76 {
77 Parents[y] = px;
78 if( y == py )
79 {
80 return;
81 }
82 y = py;
83 py = Parents[y];
84 }
85 }
86}
87
88// Optimized version of Union when iterating for( x : 0 to N ) unioning x with lower indexes.
89// Neither x nor y can have already been unioned with an index > x.
91{
92 checkSlow( x >= y );
93 checkSlow( x == Parents[x] );
94
95 uint32 px = x;
96 uint32 py = Parents[y];
97 while( px != py )
98 {
99 Parents[y] = px;
100 if( y == py )
101 {
102 return;
103 }
104 y = py;
105 py = Parents[y];
106 }
107}
108
109// Find with path compression
111{
112 // Find root
113 uint32 Start = i;
114 uint32 Root = Parents[i];
115 while( Root != i )
116 {
117 i = Root;
118 Root = Parents[i];
119 }
120
121 // Point all nodes on path to root
122 i = Start;
123 uint32 Parent = Parents[i];
124 while( Parent != Root )
125 {
126 Parents[i] = Root;
127 i = Parent;
128 Parent = Parents[i];
129 }
130
131 return Root;
132}
#define checkSlow(expr)
Definition AssertionMacros.h:332
void Init()
Definition LockFreeList.h:4
@ Num
Definition MetalRHIPrivate.h:234
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition DisjointSet.h:8
void AddDefaulted(uint32 Num=1)
Definition DisjointSet.h:46
FDisjointSet()
Definition DisjointSet.h:10
void Reset()
Definition DisjointSet.h:41
void UnionSequential(uint32 x, uint32 y)
Definition DisjointSet.h:90
void Init(uint32 Size)
Definition DisjointSet.h:32
void Union(uint32 x, uint32 y)
Definition DisjointSet.h:57
uint32 operator[](uint32 i) const
Definition DisjointSet.h:21
uint32 Find(uint32 i)
Definition DisjointSet.h:110
UE_FORCEINLINE_HINT SizeType AddUninitialized()
Definition Array.h:1664
void Reset(SizeType NewSize=0)
Definition Array.h:2246
void SetNumUninitialized(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2369