UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MeshElementArray.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "MeshTypes.h"
6
14template <typename ElementType>
16{
17public:
18
25 {
26 Array.Container.CountBytes( Ar );
27
28 if( Ar.IsLoading() )
29 {
30 // Load array
32 Ar << AllocatedIndices;
33
34 Array.Container.Empty( AllocatedIndices.Num() );
35 for( auto It = TConstSetBitIterator<>( AllocatedIndices ); It; ++It )
36 {
37 Array.Container.Insert( It.GetIndex(), ElementType() );
38 Ar << Array.Container[ It.GetIndex() ];
39 }
40 }
41 else
42 {
43 // Save array
44 const int32 MaxIndex = Array.Container.GetMaxIndex();
45
46 // We have to build the TBitArray representing allocated indices by hand, as we don't have access to it from outside TSparseArray.
47 // @todo core: consider replacing TSparseArray serialization with this format.
48 TBitArray<> AllocatedIndices( false, MaxIndex );
50
51 for( int32 Index = 0; Index < MaxIndex; ++Index )
52 {
53 if( Array.Container.IsAllocated( Index ) )
54 {
55 AllocatedIndices[ Index ] = true;
57 }
58 }
59
60 // Cut off the trailing unallocated indices so that they are not serialized
61 AllocatedIndices.SetNumUninitialized(MaxAllocatedIndex + 1);
62
63 Ar << AllocatedIndices;
64
65 for( auto It = Array.Container.CreateIterator(); It; ++It )
66 {
67 Ar << *It;
68 }
69 }
70
71 return Ar;
72 }
73
76
78 void Remap( const TSparseArray<int32>& IndexRemap );
79
80
81protected:
82
85};
86
87
92template <typename ElementType, typename ElementIDType>
93class TMeshElementArray final : public TMeshElementArrayBase<ElementType>
94{
95 static_assert( TIsDerivedFrom<ElementIDType, FElementID>::IsDerived, "ElementIDType must be derived from FElementID" );
96
97 using TMeshElementArrayBase<ElementType>::Container;
98
99public:
100
102 inline void Reset( const int32 Elements = 0 )
103 {
105 Container.Reserve( Elements );
106 }
107
109 inline void Reserve( const int32 Elements ) { Container.Reserve( Elements ); }
110
112 inline ElementIDType Add() { return ElementIDType( Container.Add( ElementType() ) ); }
113
115 inline ElementIDType Add( typename TTypeTraits<ElementType>::ConstInitType Element ) { return ElementIDType( Container.Add( Element ) ); }
116
118 inline ElementIDType Add( ElementType&& Element ) { return ElementIDType( Container.Add( Forward<ElementType>( Element ) ) ); }
119
121 inline ElementType& Insert( const ElementIDType ID )
122 {
123 Container.Insert( ID.GetValue(), ElementType() );
124 return Container[ ID.GetValue() ];
125 }
126
128 inline ElementType& Insert( const ElementIDType ID, typename TTypeTraits<ElementType>::ConstInitType Element )
129 {
130 Container.Insert( ID.GetValue(), Element );
131 return Container[ ID.GetValue() ];
132 }
133
135 inline ElementType& Insert( const ElementIDType ID, ElementType&& Element )
136 {
137 Container.Insert( ID.GetValue(), Forward<ElementType>( Element ) );
138 return Container[ ID.GetValue() ];
139 }
140
142 inline void Remove( const ElementIDType ID )
143 {
144 checkSlow( Container.IsAllocated( ID.GetValue() ) );
145 Container.RemoveAt( ID.GetValue() );
146 }
147
149 inline ElementType& operator[]( const ElementIDType ID )
150 {
151 checkSlow( Container.IsAllocated( ID.GetValue() ) );
152 return Container[ ID.GetValue() ];
153 }
154
155 inline const ElementType& operator[]( const ElementIDType ID ) const
156 {
157 checkSlow( Container.IsAllocated( ID.GetValue() ) );
158 return Container[ ID.GetValue() ];
159 }
160
162 inline int32 Num() const { return Container.Num(); }
163
165 inline int32 GetArraySize() const { return Container.GetMaxIndex(); }
166
169 {
170 return Container.Num() > 0 ?
173 }
174
176 inline bool IsValid( const ElementIDType ID ) const
177 {
178 return ID.GetValue() >= 0 && ID.GetValue() < Container.GetMaxIndex() && Container.IsAllocated( ID.GetValue() );
179 }
180
187
199 {
200 public:
201
203 : Array( InArray )
204 {}
205
207 {
208 public:
209
211 : Iterator( MoveTemp( It ) )
212 {}
213
215 {
216 ++Iterator;
217 return *this;
218 }
219
221 {
222 return Iterator ? ElementIDType( Iterator.GetIndex() ) : INDEX_NONE;
223 }
224
225 friend inline bool operator==( const TConstIterator& Lhs, const TConstIterator& Rhs )
226 {
227 return Lhs.Iterator == Rhs.Iterator;
228 }
229
230 friend inline bool operator!=( const TConstIterator& Lhs, const TConstIterator& Rhs )
231 {
232 return Lhs.Iterator != Rhs.Iterator;
233 }
234
235 private:
236
238 };
239
241 {
243 }
244
245 public:
246
247 inline TConstIterator begin() const
248 {
249 return TConstIterator( Array.begin() );
250 }
251
252 inline TConstIterator end() const
253 {
254 return TConstIterator( Array.end() );
255 }
256
257 private:
258
259 const TSparseArray<ElementType>& Array;
260 };
261
263 TElementIDs inline GetElementIDs() const { return TElementIDs( Container ); }
264};
265
266
267template <typename ElementType>
269{
272
273 OutIndexRemap.Empty( Container.GetMaxIndex() );
274
275 // Add valid elements into a new contiguous sparse array. Note non-const iterator so we can move elements.
276 for( typename TSparseArray<ElementType>::TIterator It( Container ); It; ++It )
277 {
278 const int32 OldElementIndex = It.GetIndex();
279
280 // @todo mesheditor: implement TSparseArray::Add( ElementType&& ) to save this obscure approach
281 const int32 NewElementIndex = NewContainer.Add( ElementType() );
283
284 // Provide an O(1) lookup from old index to new index, used when patching up vertex references afterwards
286 }
287
289}
290
291
292template <typename ElementType>
294{
296 NewContainer.Reserve( IndexRemap.GetMaxIndex() );
297
298 // Add valid elements into a new contiguous sparse array. Note non-const iterator so we can move elements.
299 for( typename TSparseArray<ElementType>::TIterator It( Container ); It; ++It )
300 {
301 const int32 OldElementIndex = It.GetIndex();
302
303 check( IndexRemap.IsAllocated( OldElementIndex ) );
304 const int32 NewElementIndex = IndexRemap[ OldElementIndex ];
305
306 // @todo mesheditor: implement TSparseArray::Insert( ElementType&& ) to save this obscure approach
307 NewContainer.Insert( NewElementIndex, ElementType() );
309 }
310
312}
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
@ 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
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition Archive.h:1208
UE_FORCEINLINE_HINT bool IsLoading() const
Definition Archive.h:236
virtual void CountBytes(SIZE_T InNum, SIZE_T InMax)
Definition Archive.h:125
Definition BitArray.h:1944
Definition MeshElementArray.h:16
friend FArchive & operator<<(FArchive &Ar, TMeshElementArrayBase &Array)
Definition MeshElementArray.h:24
void Remap(const TSparseArray< int32 > &IndexRemap)
Definition MeshElementArray.h:293
TSparseArray< ElementType > Container
Definition MeshElementArray.h:84
void Compact(TSparseArray< int32 > &OutIndexRemap)
Definition MeshElementArray.h:268
Definition MeshElementArray.h:207
friend bool operator==(const TConstIterator &Lhs, const TConstIterator &Rhs)
Definition MeshElementArray.h:225
TConstIterator(typename TSparseArray< ElementType >::TConstIterator &&It)
Definition MeshElementArray.h:210
friend bool operator!=(const TConstIterator &Lhs, const TConstIterator &Rhs)
Definition MeshElementArray.h:230
ElementIDType operator*() const
Definition MeshElementArray.h:220
TConstIterator & operator++()
Definition MeshElementArray.h:214
Definition MeshElementArray.h:199
TElementIDs(const TSparseArray< ElementType > &InArray)
Definition MeshElementArray.h:202
TConstIterator end() const
Definition MeshElementArray.h:252
TConstIterator begin() const
Definition MeshElementArray.h:247
TConstIterator CreateConstIterator() const
Definition MeshElementArray.h:240
Definition MeshElementArray.h:94
ElementType & operator[](const ElementIDType ID)
Definition MeshElementArray.h:149
void Reset(const int32 Elements=0)
Definition MeshElementArray.h:102
ElementType & Insert(const ElementIDType ID)
Definition MeshElementArray.h:121
ElementType & Insert(const ElementIDType ID, ElementType &&Element)
Definition MeshElementArray.h:135
void Reserve(const int32 Elements)
Definition MeshElementArray.h:109
ElementType & Insert(const ElementIDType ID, typename TTypeTraits< ElementType >::ConstInitType Element)
Definition MeshElementArray.h:128
int32 GetArraySize() const
Definition MeshElementArray.h:165
TElementIDs GetElementIDs() const
Definition MeshElementArray.h:263
bool IsValid(const ElementIDType ID) const
Definition MeshElementArray.h:176
ElementIDType Add(typename TTypeTraits< ElementType >::ConstInitType Element)
Definition MeshElementArray.h:115
ElementIDType GetFirstValidID() const
Definition MeshElementArray.h:168
int32 Num() const
Definition MeshElementArray.h:162
ElementIDType Add(ElementType &&Element)
Definition MeshElementArray.h:118
friend FArchive & operator<<(FArchive &Ar, TMeshElementArray &Array)
Definition MeshElementArray.h:182
const ElementType & operator[](const ElementIDType ID) const
Definition MeshElementArray.h:155
void Remove(const ElementIDType ID)
Definition MeshElementArray.h:142
ElementIDType Add()
Definition MeshElementArray.h:112
int32 Num() const
Definition SparseArray.h:470
bool IsAllocated(int32 Index) const
Definition SparseArray.h:486
void Reserve(int32 ExpectedNumElements)
Definition SparseArray.h:219
int32 GetMaxIndex() const
Definition SparseArray.h:460
Definition SparseArray.h:1137
Definition SparseArray.h:1116
Definition SparseArray.h:524
void RemoveAt(int32 Index, int32 Count=1)
Definition SparseArray.h:650
int32 Add(const ElementType &Element)
Definition SparseArray.h:564
UE_FORCEINLINE_HINT TRangedForIterator begin()
Definition SparseArray.h:1218
void Reset()
Definition SparseArray.h:682
void Insert(int32 Index, typename TTypeTraits< ElementType >::ConstInitType Element)
Definition SparseArray.h:644
UE_FORCEINLINE_HINT TRangedForIterator end()
Definition SparseArray.h:1220
U16 Index
Definition radfft.cpp:71
Definition UnrealTypeTraits.h:40
TCallTraits< T >::ParamType ConstInitType
Definition UnrealTypeTraits.h:336