UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MRUArray.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"
8#include "Containers/Array.h"
9
10/*-----------------------------------------------------------------------------
11 MRU array.
12-----------------------------------------------------------------------------*/
13
19template<typename T, typename InAllocator = FDefaultAllocator>
21 : public TArray<T, InAllocator>
22{
23public:
25
28
33 : Super()
34 {
35 MaxItems = 0;
36 }
37
38 [[nodiscard]] TMRUArray(TMRUArray&&) = default;
39 [[nodiscard]] TMRUArray(const TMRUArray&) = default;
41 TMRUArray& operator=(const TMRUArray&) = default;
42
50 int32 Add(const T& Item)
51 {
52 const int32 idx = Super::Add(Item);
53 this->Swap(idx, 0);
54 CullArray();
55 return 0;
56 }
57
66 {
67 const int32 idx = Super::AddZeroed(Count);
68 this->Swap(idx, 0);
69 CullArray();
70 return 0;
71 }
72
80 int32 AddUnique(const T& Item)
81 {
82 // Remove any existing copies of the item.
83 this->Remove(Item);
84 this->Insert(Item, 0);
85
86 CullArray();
87
88 return 0;
89 }
90
94 void CullArray()
95 {
96 // 0 = no limit
97 if (!MaxItems)
98 {
99 return;
100 }
101
102 while (this->Num() > MaxItems)
103 {
104 this->RemoveAt(this->Num() - 1, 1);
105 }
106 }
107};
108
109
110
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
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void RemoveAt(SizeType Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2083
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
SizeType AddZeroed()
Definition Array.h:2755
SizeType Insert(std::initializer_list< ElementType > InitList, const SizeType InIndex)
Definition Array.h:1875
Definition MRUArray.h:22
void CullArray()
Definition MRUArray.h:94
TMRUArray & operator=(const TMRUArray &)=default
TMRUArray(TMRUArray &&)=default
int32 AddUnique(const T &Item)
Definition MRUArray.h:80
TMRUArray(const TMRUArray &)=default
TMRUArray & operator=(TMRUArray &&)=default
TMRUArray()
Definition MRUArray.h:32
int32 MaxItems
Definition MRUArray.h:27
int32 AddZeroed(int32 Count=1)
Definition MRUArray.h:65
TArray< T, InAllocator > Super
Definition MRUArray.h:24
int32 Add(const T &Item)
Definition MRUArray.h:50