UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MotionDelayBuffer.inl
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4template<typename ElementType>
6 : Head(0)
7 , bIsFull(false)
8{
9 if (InitialCapacity > 0)
10 {
11 Resize(InitialCapacity);
12 }
13}
14
15template<typename ElementType>
16ElementType& TCircularHistoryBuffer<ElementType>::Add(const ElementType& Element)
17{
18 Elements[Head] = Element;
19 ElementType& EntryRef = Elements[Head];
20
21 Head = GetNextIndex(Head);
22 bIsFull |= (Head == 0);
23
24 return EntryRef;
25}
26
27template<typename ElementType>
29{
31
32 const uint32 CurrentSize = Elements.Num();
33 if (NewCapacity > CurrentSize)
34 {
35 ResizeGrow(NewCapacity - CurrentSize);
36 }
37 else if (NewCapacity < CurrentSize)
38 {
39 ResizeShrink(CurrentSize - NewCapacity);
40 }
41}
42
43template<typename ElementType>
45{
46 RangeCheck(Index);
47 return Elements[AsInternalIndex(Index)];
48}
49
50template<typename ElementType>
52{
53 RangeCheck(Index);
54 return Elements[AsInternalIndex(Index)];
55}
56
57template<typename ElementType>
59{
60 RangeCheck(Index);
61
62 Index = FMath::Min(Index, (uint32)Num());
63 if (!IsFull() || Index < Capacity() - 1)
64 {
65 uint32 NumToShift = FMath::Min(Head, Index + 1);
66 uint32 StartIndex = Head - NumToShift;
67 uint32 TargetIndex = AsInternalIndex(Index);
68
69 Add(Element);
70
72 if (NumToShift > 0)
73 {
74 FMemory::Memmove(&Elements[StartIndex + 1], &Elements[StartIndex], sizeof(ElementType) * NumToShift);
76 }
78 if (ShiftCount < Index + 1)
79 {
80 Elements[0] = Elements.Last();
81 ShiftCount += 1;
82 }
83
84 if (ShiftCount < Index + 1)
85 {
86 NumToShift = Elements.Num() - TargetIndex - 1;
88 FMemory::Memmove(&Elements[TargetIndex + 1], &Elements[TargetIndex], sizeof(ElementType) * NumToShift);
90 }
91 Elements[TargetIndex] = Element;
92 }
93}
95template<typename ElementType>
98 return Elements.Num();
99}
100
101template<typename ElementType>
103{
104 if (IsFull())
105 {
106 return Capacity();
107 }
108 else
109 {
110 return Head;
111 }
112}
113
114template<typename ElementType>
116{
117 Head = 0;
118 bIsFull = false;
119}
120
121template<typename ElementType>
123{
124 return (Head == 0) && !bIsFull;
125}
126
127template<typename ElementType>
129{
130 return bIsFull;
131}
132
133template<typename ElementType>
135{
136 checkSlow(!IsEmpty());
137 checkSlow(Index < (uint32)Elements.Num());
138 checkSlow(!bCheckIfUnderfilled || (!bIsFull && Index >= Head));
139}
140
141template<typename ElementType>
143{
144 // head(3) head(3)
145 // | |
146 // [2][1][0][6][5][4][3], or (if not full): [6/5/4/3/2][1][0][ ][ ][ ][ ]
147 return (Index < Head) ? (Head - Index - 1) : bIsFull ? (Elements.Num() - 1 - Index + Head) : 0;
148}
149
150template<typename ElementType>
152{
153 return ((CurrentIndex + 1) % Elements.Num());
154}
155
156template<typename ElementType>
158{
159 if (bIsFull)
160 {
161 if (Head > 0)
162 {
163 Realign();
164 }
165 Head = Elements.Num();
166 }
167
168 Elements.AddUninitialized(AddedSlack);
169 bIsFull = false;
170}
171
172template<typename ElementType>
174{
175 if (bIsFull)
176 {
177 const uint32 Capacity = Elements.Num();
178
179 TArray<ElementType> TempBuffer;
180 TempBuffer.AddUninitialized(Head);
181
182 ElementType* BufferPtr = Elements.GetData();
183 // head(3)
184 // |
185 // [G][H][I][C][D][E][F] => TempBuffer: [G][H][I]
186 FMemory::Memcpy(TempBuffer.GetData(), BufferPtr, sizeof(ElementType) * Head);
187
188 // [G][H][I][C][D][E][F] => [C][D][E][F][D][E][F]
189 const uint32 MoveCount = Capacity - Head;
190 FMemory::Memcpy(BufferPtr, BufferPtr + Head, sizeof(ElementType) * Head);
191
192 // [C][D][E][F][D][E][F] => [C][D][E][F][G][H][I]
193 FMemory::Memcpy(BufferPtr + MoveCount, TempBuffer.GetData(), sizeof(ElementType) * TempBuffer.Num());
194
195 Head = 0;
196 }
197}
198
199template<typename ElementType>
201{
202 const uint32 NewCapacity = Elements.Num() - ShrinkAmount;
203 //
204 // keep the newest values
205
206 // head(3) head(3)
207 // | |
208 // [H][I][J][D][E][F][G] => [H][I][J][F][G]
209 if (Head < NewCapacity)
210 {
211 Elements.RemoveAtSwap(Head, ShrinkAmount);
212 }
213 // head(3) head(0)
214 // | |
215 // [H][I][J][D][E][F][G] => [I][J]
216 else
217 {
218 // [H][I][J][D][E][F][G] => [H][I][J]
219 Elements.RemoveAt(Head, Elements.Num() - Head);
220 // [H][I][J] => [I][J]
221 Elements.RemoveAtSwap(0, Head - NewCapacity);
222
223 Head = 0;
224 bIsFull = true;
225 }
226}
#define checkSlow(expr)
Definition AssertionMacros.h:332
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
@ Num
Definition MetalRHIPrivate.h:234
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Array.h:670
UE_FORCEINLINE_HINT SizeType AddUninitialized()
Definition Array.h:1664
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
Definition MotionDelayBuffer.h:73
bool IsFull() const
Definition MotionDelayBuffer.inl:128
int32 Num() const
Definition MotionDelayBuffer.inl:102
bool IsEmpty() const
Definition MotionDelayBuffer.inl:122
TCircularHistoryBuffer(uint32 InitialCapacity=0)
Definition MotionDelayBuffer.inl:5
uint32 Capacity() const
Definition MotionDelayBuffer.inl:96
ElementType & operator[](uint32 Index)
Definition MotionDelayBuffer.inl:44
void Resize(uint32 NewCapacity)
Definition MotionDelayBuffer.inl:28
void InsertAt(uint32 Index, const ElementType &Element)
Definition MotionDelayBuffer.inl:58
void Empty()
Definition MotionDelayBuffer.inl:115
ElementType & Add(const ElementType &Element)
Definition MotionDelayBuffer.inl:16
@ false
Definition radaudio_common.h:23
U16 Index
Definition radfft.cpp:71
static UE_FORCEINLINE_HINT void * Memmove(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:109
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160