UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SizedDisjointSet.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6
7namespace UE::Geometry
8{
9
12{
14
15 FSizedDisjointSet() = default;
20
22 {
25 for (int32 Idx = 0; Idx < NumIDs; Idx++)
26 {
27 Parents[Idx] = Idx;
28 Sizes[Idx] = 1;
29 }
30 }
31
32 // IsElement(Index) returns false if Index is not valid. Find, Union and GetSize should never be called on an invalid index.
33 template<typename IsElementFunction>
35 {
38 for (int32 Idx = 0; Idx < NumIDs; Idx++)
39 {
40 if (IsElement(Idx))
41 {
42 Parents[Idx] = Idx;
43 Sizes[Idx] = 1;
44 }
45 else
46 {
47 Parents[Idx] = -1;
48 Sizes[Idx] = 0;
49 }
50 }
51 }
52
53 // @return the Parent of the Union
55 {
58 if (ParentA == ParentB)
59 {
60 return ParentB;
61 }
62 if (Sizes[ParentA] > Sizes[ParentB])
63 {
65 }
68 return ParentB;
69 }
70
71 // Find the representative cluster element for this Idx
72 // Note: Collapses parent references as it walks them, to make future Find() calls faster --
73 // Use FindWithoutCollapse for a const version without this feature.
75 {
76 int32 Parent = Idx;
77
78 while (Parents[Parent] != Parent)
79 {
82 }
83
84 return Parent;
85 }
86
87 // Find w/out collapsing the upward links
88 // Note this can be less efficient over many calls, if the trees have not been collapsed by previous accesses,
89 // but it is a const access, and easier to use in parallel
91 {
92 int32 Parent = Idx;
93
94 while (Parents[Parent] != Parent)
95 {
97 }
98
99 return Parent;
100 }
101
102 int32 GetSize(int32 Idx) const
103 {
105 return Sizes[Parent];
106 }
107
117 {
118 MinGroupSize = FMath::Max(1, MinGroupSize);
119
120 int32 NumIDs = Sizes.Num();
122 {
123 CompactIdxToGroupID->Reset();
124 }
126 {
127 GroupIDToCompactIdx->Init(-1, NumIDs);
128 }
129 int32 NumGroups = 0;
130 for (int32 ID = 0; ID < NumIDs; ++ID)
131 {
132 if (Parents[ID] == INDEX_NONE) // ignore invalid IDs
133 {
134 continue;
135 }
136
138 if (Parent != ID) // only count groups on the Parent==ID node
139 {
140 continue;
141 }
142
143 if (Sizes[Parent] < MinGroupSize) // ignore too-small groups
144 {
145 continue;
146 }
147
148 // Record the unique GroupID
150 {
152 }
154 {
155 (*GroupIDToCompactIdx)[ID] = NumGroups;
156 }
157 NumGroups++;
158 }
159 return NumGroups;
160 }
161};
162
163
164} // end namespace UE::Geometry
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
void Init()
Definition LockFreeList.h:4
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void SetNumUninitialized(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2369
Definition ParametricSurfaceData.h:18
Disjoint set with additional storage to track the size of each set.
Definition SizedDisjointSet.h:12
int32 Union(int32 A, int32 B)
Definition SizedDisjointSet.h:54
int32 Find(int32 Idx)
Definition SizedDisjointSet.h:74
void Init(int32 NumIDs, IsElementFunction IsElement)
Definition SizedDisjointSet.h:34
void Init(int32 NumIDs)
Definition SizedDisjointSet.h:21
int32 FindWithoutCollapse(int32 Idx) const
Definition SizedDisjointSet.h:90
TArray< int32 > Parents
Definition SizedDisjointSet.h:13
int32 GetSize(int32 Idx) const
Definition SizedDisjointSet.h:102
TArray< int32 > Sizes
Definition SizedDisjointSet.h:13
int32 CompactedGroupIndexToGroupID(TArray< int32 > *CompactIdxToGroupID, TArray< int32 > *GroupIDToCompactIdx, int32 MinGroupSize=1) const
Definition SizedDisjointSet.h:116
FSizedDisjointSet(int32 NumIDs)
Definition SizedDisjointSet.h:16