UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IndirectArray.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"
7#include "HAL/UnrealMemory.h"
10#include "Containers/Array.h"
11
12/*-----------------------------------------------------------------------------
13 Indirect array.
14 Same as a TArray, but stores pointers to the elements, to allow
15 resizing the array index without relocating the actual elements.
16-----------------------------------------------------------------------------*/
17
18template<typename T,typename Allocator = FDefaultAllocator>
20{
21public:
22 typedef T ElementType;
24
26 [[nodiscard]] TIndirectArray() = default;
28
35 {
36 for (auto& Item : Other)
37 {
38 Add(new T(Item));
39 }
40 }
41
48 {
49 if (&Other != this)
50 {
51 Empty(Other.Num());
52 for (auto& Item : Other)
53 {
54 Add(new T(Item));
55 }
56 }
57
58 return *this;
59 }
61 {
62 if (&Other != this)
63 {
64 Empty();
65 Array = MoveTemp(Other.Array);
66 }
67
68 return *this;
69 }
70
71
74 {
75 Empty();
76 }
77
84 [[nodiscard]] bool IsEmpty() const
85 {
86 return Array.IsEmpty();
87 }
88
95 {
96 return Array.Num();
97 }
98
105 {
106 return (T**)Array.GetData();
107 }
108
115 {
116 return (const T**)Array.GetData();
117 }
118
124 [[nodiscard]] static constexpr uint32 GetTypeSize()
125 {
126 return sizeof(T*);
127 }
128
136 {
137 return *(T*)Array[Index];
138 }
139
149 {
150 return *(T*)Array[Index];
151 }
152
163
173 {
174 return *(T*)Array.Last(IndexFromTheEnd);
175 }
176
181 void Shrink()
182 {
183 Array.Shrink();
184 }
185
193 void Reset(int32 NewSize = 0)
194 {
195 DestructAndFreeItems();
196 Array.Reset(NewSize);
197 }
198
207 {
208 CountBytes(Ar);
209 if (Ar.IsLoading())
210 {
211 // Load array.
212 int32 NewNum = 0;
213 Ar << NewNum;
214 Empty(NewNum);
215 for (int32 Index = 0; Index < NewNum; Index++)
216 {
217 Add(new T);
218 }
219 for (int32 Index = 0; Index < NewNum; Index++)
220 {
221 (*this)[Index].Serialize(Ar, Owner, Index);
222 }
223 }
224 else
225 {
226 // Save array.
227 int32 Num = Array.Num();
228 Ar << Num;
229 for (int32 Index = 0; Index < Num; Index++)
230 {
231 (*this)[Index].Serialize(Ar, Owner, Index);
232 }
233 }
234 }
235
241 void CountBytes(FArchive& Ar) const
242 {
243 Array.CountBytes(Ar);
244 }
245
254 void RemoveAt(int32 Index, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<Allocator>())
255 {
256 check(Index >= 0);
257 check(Index < Array.Num());
258 T** Element = GetData() + Index;
259 delete *Element;
261 }
262
272 void RemoveAt(int32 Index, int32 Count, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<Allocator>())
273 {
274 check(Index >= 0);
275 check(Index <= Array.Num());
276 check(Index + Count <= Array.Num());
277 T** Element = GetData() + Index;
278 for (int32 ElementId = Count; ElementId; --ElementId)
279 {
280 delete *Element;
281 ++Element;
282 }
284 }
286 UE_FORCEINLINE_HINT void RemoveAt(int32 Index, int32 Count, bool bAllowShrinking)
287 {
289 }
290
302 void RemoveAtSwap(int32 Index, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<Allocator>())
303 {
304 check(Index >= 0);
305 check(Index < Array.Num());
306 T** Element = GetData() + Index;
307 delete *Element;
309 }
310
323 void RemoveAtSwap(int32 Index, int32 Count, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<Allocator>())
324 {
325 check(Index >= 0);
326 check(Index <= Array.Num());
327 check(Index + Count <= Array.Num());
328 T** Element = GetData() + Index;
329 for (int32 ElementId = Count; ElementId; --ElementId)
330 {
331 delete *Element;
332 ++Element;
333 }
335 }
338 {
340 }
341
354
361 void Empty(int32 Slack = 0)
362 {
363 DestructAndFreeItems();
364 Array.Empty(Slack);
365 }
366
375 {
376 return Array.Add(Item);
377 }
378
386 {
387 Array.Insert(Item, Index);
388 }
389
397 {
398 Array.Reserve(Number);
399 }
400
409 {
410 return Array.IsValidIndex(Index);
411 }
412
419 {
420 return Array.Max() * sizeof(T*) + Array.Num() * sizeof(T);
421 }
422
423 // Iterators
426
433 {
434 return TIterator(*this);
435 }
436
443 {
444 return TConstIterator(*this);
445 }
446
447private:
448
452 void DestructAndFreeItems()
453 {
454 T** Element = GetData();
455 for (int32 Index = Array.Num(); Index; --Index)
456 {
457 delete *Element;
458 ++Element;
459 }
460 }
461
462public:
471
472private:
473
474 InternalArrayType Array;
475};
476
477
485template<typename T,typename Allocator>
487{
488 A.CountBytes(Ar);
489 if (Ar.IsLoading())
490 {
491 // Load array.
493 Ar << NewNum;
494 A.Empty(NewNum);
495 for (int32 Index = 0; Index < NewNum; Index++)
496 {
497 T* NewElement = new T;
498 Ar << *NewElement;
499 A.Add(NewElement);
500 }
501 }
502 else
503 {
504 // Save array.
505 int32 Num = A.Num();
506 Ar << Num;
507 for (int32 Index = 0; Index < Num; Index++)
508 {
509 Ar << A[Index];
510 }
511 }
512 return Ar;
513}
EAllowShrinking
Definition AllowShrinking.h:10
#define UE_ALLOWSHRINKING_BOOL_DEPRECATED(FunctionName)
Definition AllowShrinking.h:31
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FArchive & operator<<(FArchive &Ar, TIndirectArray< T, Allocator > &A)
Definition IndirectArray.h:486
@ Num
Definition MetalRHIPrivate.h:234
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
virtual void Serialize(void *V, int64 Length)
Definition Archive.h:1689
UE_FORCEINLINE_HINT bool IsLoading() const
Definition Archive.h:236
virtual void CountBytes(SIZE_T InNum, SIZE_T InMax)
Definition Archive.h:125
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType & Last(SizeType IndexFromTheEnd=0) UE_LIFETIMEBOUND
Definition Array.h:1263
void RemoveAt(SizeType Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2083
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
UE_FORCEINLINE_HINT void RemoveAtSwap(SizeType Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2185
UE_REWRITE SizeType Max() const
Definition Array.h:1161
UE_NODEBUG UE_FORCEINLINE_HINT RangedForIteratorType end()
Definition Array.h:3391
UE_REWRITE bool IsEmpty() const
Definition Array.h:1133
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
UE_NODEBUG UE_FORCEINLINE_HINT bool IsValidIndex(SizeType Index) const
Definition Array.h:1122
UE_NODEBUG UE_FORCEINLINE_HINT RangedForIteratorType begin()
Definition Array.h:3389
UE_NODEBUG void CountBytes(FArchive &Ar) const
Definition Array.h:1649
UE_FORCEINLINE_HINT void Swap(SizeType FirstIndexToSwap, SizeType SecondIndexToSwap)
Definition Array.h:3300
UE_FORCEINLINE_HINT void Shrink()
Definition Array.h:1278
SizeType Insert(std::initializer_list< ElementType > InitList, const SizeType InIndex)
Definition Array.h:1875
void Empty(SizeType Slack=0)
Definition Array.h:2273
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition Array.h:64
Definition IndirectArray.h:20
void Serialize(FArchive &Ar, UObject *Owner)
Definition IndirectArray.h:206
void RemoveAt(int32 Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< Allocator >())
Definition IndirectArray.h:254
UE_FORCEINLINE_HINT void Reserve(int32 Number)
Definition IndirectArray.h:396
TIndirectArray & operator=(const TIndirectArray &Other)
Definition IndirectArray.h:47
UE_FORCEINLINE_HINT TDereferencingIterator< const ElementType, typename InternalArrayType::RangedForConstIteratorType > begin() const
Definition IndirectArray.h:468
TIterator CreateIterator()
Definition IndirectArray.h:432
T ElementType
Definition IndirectArray.h:22
TIndirectArray(const TIndirectArray &Other)
Definition IndirectArray.h:34
UE_FORCEINLINE_HINT int32 Num() const
Definition IndirectArray.h:94
UE_FORCEINLINE_HINT const T & operator[](int32 Index) const
Definition IndirectArray.h:148
UE_FORCEINLINE_HINT TDereferencingIterator< const ElementType, typename InternalArrayType::RangedForConstIteratorType > end() const
Definition IndirectArray.h:470
UE_FORCEINLINE_HINT const ElementType & Last(int32 IndexFromTheEnd=0) const
Definition IndirectArray.h:172
bool IsEmpty() const
Definition IndirectArray.h:84
~TIndirectArray()
Definition IndirectArray.h:73
UE_FORCEINLINE_HINT bool IsValidIndex(int32 Index) const
Definition IndirectArray.h:408
void RemoveAtSwap(int32 Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< Allocator >())
Definition IndirectArray.h:302
UE_FORCEINLINE_HINT int32 Add(T *Item)
Definition IndirectArray.h:374
void RemoveAt(int32 Index, int32 Count, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< Allocator >())
Definition IndirectArray.h:272
void Shrink()
Definition IndirectArray.h:181
TIndirectArray & operator=(TIndirectArray &&Other)
Definition IndirectArray.h:60
void Empty(int32 Slack=0)
Definition IndirectArray.h:361
UE_FORCEINLINE_HINT TDereferencingIterator< ElementType, typename InternalArrayType::RangedForIteratorType > begin()
Definition IndirectArray.h:467
TConstIterator CreateConstIterator() const
Definition IndirectArray.h:442
UE_FORCEINLINE_HINT void Insert(T *Item, int32 Index)
Definition IndirectArray.h:385
UE_FORCEINLINE_HINT T & operator[](int32 Index)
Definition IndirectArray.h:135
TIndexedContainerIterator< const TIndirectArray, const ElementType, int32 > TConstIterator
Definition IndirectArray.h:425
void Reset(int32 NewSize=0)
Definition IndirectArray.h:193
UE_FORCEINLINE_HINT TDereferencingIterator< ElementType, typename InternalArrayType::RangedForIteratorType > end()
Definition IndirectArray.h:469
void CountBytes(FArchive &Ar) const
Definition IndirectArray.h:241
static constexpr uint32 GetTypeSize()
Definition IndirectArray.h:124
TArray< void *, Allocator > InternalArrayType
Definition IndirectArray.h:23
void RemoveAtSwap(int32 Index, int32 Count, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< Allocator >())
Definition IndirectArray.h:323
void Swap(int32 FirstIndexToSwap, int32 SecondIndexToSwap)
Definition IndirectArray.h:350
SIZE_T GetAllocatedSize() const
Definition IndirectArray.h:418
UE_FORCEINLINE_HINT const T ** GetData() const
Definition IndirectArray.h:114
TIndirectArray()=default
UE_FORCEINLINE_HINT T ** GetData()
Definition IndirectArray.h:104
TIndirectArray(TIndirectArray &&)=default
TIndexedContainerIterator< TIndirectArray, ElementType, int32 > TIterator
Definition IndirectArray.h:424
UE_FORCEINLINE_HINT ElementType & Last(int32 IndexFromTheEnd=0)
Definition IndirectArray.h:159
Definition Object.h:95
U16 Index
Definition radfft.cpp:71
Definition Array.h:296