Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
RemoveIf.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"
6#include "Templates/Invoke.h"
7
8namespace Algo
9{
10 /**
11 * Moves all elements which do not match the predicate to the front of the range, while leaving all
12 * other elements is a constructed but unspecified state. The elements which were not removed are
13 * not guaranteed to be kept in order (unstable).
14 *
15 * @param Range The range of elements to manipulate.
16 * @param Pred A callable which maps elements to truthy values, specifying elements to be removed.
17 *
18 * @return The index of the first element after those which were not removed.
19 */
20 template <typename RangeType, typename Predicate>
21 int32 RemoveIf(RangeType& Range, Predicate Pred)
22 {
23 auto* First = GetData(Range);
24 auto* Last = First + GetNum(Range);
25
26 auto* IterStart = First;
27 auto* IterEnd = Last;
28 for (;;)
29 {
30 // Skip non-removed elements at the start
31 for (;;)
32 {
33 if (IterStart == IterEnd)
34 {
36 }
37
38 if (Invoke(Pred, *IterStart))
39 {
40 break;
41 }
42
43 ++IterStart;
44 }
45
46 // Skip removed elements at the end
47 for (;;)
48 {
49 if (!Invoke(Pred, *(IterEnd - 1)))
50 {
51 break;
52 }
53
54 --IterEnd;
55
56 if (IterStart == IterEnd)
57 {
59 }
60 }
61
62 *IterStart = MoveTemp(*(IterEnd - 1));
63
64 ++IterStart;
65 --IterEnd;
66 }
67 }
68
69 /**
70 * Moves all elements which do not match the predicate to the front of the range, while leaving all
71 * other elements is a constructed but unspecified state. The elements which were not removed are
72 * guaranteed to be kept in order (stable).
73 *
74 * @param Range The range of elements to manipulate.
75 * @param Pred A callable which maps elements to truthy values, specifying elements to be removed.
76 *
77 * @return The index of the first element after those which were not removed.
78 */
79 template <typename RangeType, typename Predicate>
80 int32 StableRemoveIf(RangeType& Range, Predicate Pred)
81 {
82 auto* First = GetData(Range);
83 auto* Last = First + GetNum(Range);
84
85 auto* IterStart = First;
86
87 // Skip non-removed elements at the start
88 for (;;)
89 {
90 if (IterStart == Last)
91 {
93 }
94
95 if (Invoke(Pred, *IterStart))
96 {
97 break;
98 }
99
100 ++IterStart;
101 }
102
103 auto* IterKeep = IterStart;
104 ++IterKeep;
105
106 for (;;)
107 {
108 if (IterKeep == Last)
109 {
111 }
112
113 if (!Invoke(Pred, *IterKeep))
114 {
116 }
117
118 ++IterKeep;
119 }
120 }
121}
#define UE_PTRDIFF_TO_INT32(argument)
Definition Heapify.h:12
int32 StableRemoveIf(RangeType &Range, Predicate Pred)
Definition RemoveIf.h:80
int32 RemoveIf(RangeType &Range, Predicate Pred)
Definition RemoveIf.h:21