UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LinkedListBuilder.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
8template<typename InElementType>
10{
12
14 {
15 return &(Element.Next);
16 }
17};
18
19template<typename InElementType, InElementType* InElementType::* NextLink>
21{
23
25 {
26 return &(Element.*NextLink);
27 }
28};
29
33template<typename InElementType, typename InPointerType, typename InLinkAccessor>
35{
36public:
40
42
43private:
44
45 inline void WriteEndPtr(PointerType NewValue)
46 {
47 // Do not overwrite the same value to avoid dirtying the cache and
48 // also prevent TSAN from thinking we are messing around with existing data.
49 if (*EndPtr != NewValue)
50 {
51 *EndPtr = NewValue;
52 }
53 }
54
55public:
56
58 StartPtr(ListStartPtr),
59 EndPtr(ListStartPtr)
60 {
62 }
63
64 // Move builder back to start and prepare for overwriting
65 // It only changes state of builder, use NullTerminate() to mark list as empty!
67 {
68 EndPtr = StartPtr;
69 }
70
71 UE_DEPRECATED(5.6, "Append is deprecated. Please use AppendTerminated instead.")
73 {
74 AppendTerminated(Element);
75 }
76
77 // Append element, don't touch next link
78 inline void AppendNoTerminate(ElementType& Element)
79 {
80 WriteEndPtr(&Element);
81 EndPtr = LinkAccessor::GetNextPtr(Element);
82 }
83
84 // Append element and mark it as last
85 inline void AppendTerminated(ElementType& Element)
86 {
87 AppendNoTerminate(Element);
89 }
90
91private:
92
93 // Helper method to remove the element pointed to by CurrentLinkPtr.
94 // CurrentLinkPtr is either a pointer to the list start pointer or the
95 // next pointer in the previous element
96 inline void RemoveImpl(PointerType* CurrentLinkPtr)
97 {
98
99 // This gets the address of the next pointer of the element being removed
100 PointerType* NextLinkPtr = LinkAccessor::GetNextPtr(**CurrentLinkPtr);
101
102 // If EndPtr points to the next pointer of the element being removed,
103 // then move the EndPtr back one element so it remains valid.
104 if (EndPtr == NextLinkPtr)
105 {
106 EndPtr = CurrentLinkPtr;
107 }
108
109 // Update previous element link to point to the next element after the element being removed
111
112 // Clear the next pointer in the removed element
113 *NextLinkPtr = nullptr;
114 }
115
116public:
117
118 // Remove all instances that match the predicate. Returns the number of elements removed.
119 template <class PREDICATE_CLASS>
120 inline int32 RemoveAll(const PREDICATE_CLASS& Predicate)
121 {
122 int32 Removed = 0;
123
124 // With the single link list, CurrentLinkPtr will always point to the address of the
125 // variable that points to the current element to be tested, not the element itself.
126 for (PointerType* CurrentLinkPtr = StartPtr; *CurrentLinkPtr;)
127 {
128 if (Predicate(*CurrentLinkPtr))
129 {
130 RemoveImpl(CurrentLinkPtr);
131 ++Removed;
132 }
133 else
134 {
135 CurrentLinkPtr = LinkAccessor::GetNextPtr(**CurrentLinkPtr);
136 }
137 }
138
139 return Removed;
140 }
141
142 inline void Remove(ElementType& Element)
143 {
144 // With the single link list, CurrentLinkPtr will always point to the address of the
145 // variable that points to the current element to be tested, not the element itself.
146 for (PointerType* CurrentLinkPtr = StartPtr; *CurrentLinkPtr; CurrentLinkPtr = LinkAccessor::GetNextPtr(**CurrentLinkPtr))
147 {
148 if (*CurrentLinkPtr == &Element)
149 {
150 RemoveImpl(CurrentLinkPtr);
151 break;
152 }
153 }
154 }
155
156 // Mark end of the list
158 {
159 WriteEndPtr(nullptr);
160 }
161
162 inline void MoveToEnd()
163 {
164 for (PointerType It = *StartPtr; It; It = GetNext(*It))
165 {
166 EndPtr = LinkAccessor::GetNextPtr(*It);
167 }
168 }
169
170 inline bool MoveToNext()
171 {
172 if (*EndPtr)
173 {
174 EndPtr = LinkAccessor::GetNextPtr(**EndPtr);
175 return true;
176 }
177
178 return false;
179 }
180
182 {
183 return GetNextRef(Element);
184 }
185
187 {
188 return *StartPtr;
189 }
190
192 {
193 return *EndPtr;
194 }
195
196private:
197 [[nodiscard]] UE_FORCEINLINE_HINT PointerType& GetNextRef(ElementType& Element) const
198 {
199 return *LinkAccessor::GetNextPtr(Element);
200 }
201
202 PointerType* StartPtr;
203 PointerType* EndPtr;
204};
205
210template<typename InElementType, typename InLinkAccessor = TLinkedListBuilderNextLink<InElementType>>
211struct TLinkedListBuilder : public TLinkedListBuilderBase<InElementType, InElementType*, InLinkAccessor>
212{
214 using Super::Super;
215
217};
#define check(expr)
Definition AssertionMacros.h:314
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
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
Definition LinkedListBuilder.h:35
TLinkedListBuilderBase(PointerType *ListStartPtr)
Definition LinkedListBuilder.h:57
void AppendNoTerminate(ElementType &Element)
Definition LinkedListBuilder.h:78
InLinkAccessor LinkAccessor
Definition LinkedListBuilder.h:39
UE_FORCEINLINE_HINT PointerType GetListStart() const
Definition LinkedListBuilder.h:186
UE_FORCEINLINE_HINT void NullTerminate()
Definition LinkedListBuilder.h:157
int32 RemoveAll(const PREDICATE_CLASS &Predicate)
Definition LinkedListBuilder.h:120
InPointerType PointerType
Definition LinkedListBuilder.h:38
void MoveToEnd()
Definition LinkedListBuilder.h:162
void AppendTerminated(ElementType &Element)
Definition LinkedListBuilder.h:85
UE_NONCOPYABLE(TLinkedListBuilderBase)
UE_FORCEINLINE_HINT void Restart()
Definition LinkedListBuilder.h:66
UE_FORCEINLINE_HINT PointerType GetListEnd() const
Definition LinkedListBuilder.h:191
InElementType ElementType
Definition LinkedListBuilder.h:37
UE_FORCEINLINE_HINT PointerType GetNext(ElementType &Element) const
Definition LinkedListBuilder.h:181
void Remove(ElementType &Element)
Definition LinkedListBuilder.h:142
bool MoveToNext()
Definition LinkedListBuilder.h:170
Definition LinkedListBuilder.h:21
static UE_FORCEINLINE_HINT ElementType ** GetNextPtr(ElementType &Element)
Definition LinkedListBuilder.h:24
InElementType ElementType
Definition LinkedListBuilder.h:22
Definition LinkedListBuilder.h:212
UE_NONCOPYABLE(TLinkedListBuilder)