Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
StableSort.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Algo/BinarySearch.h"
6#include "Algo/Rotate.h"
7#include "Templates/IdentityFunctor.h"
8#include "Templates/Invoke.h"
9#include "Templates/Less.h"
10#include "Templates/UnrealTemplate.h" // For GetData, GetNum, Swap
11
12
13namespace AlgoImpl
14{
15 template <typename T, typename ProjectionType, typename PredicateType>
16 void Merge(T* First, int32 Mid, int32 Num, ProjectionType Projection, PredicateType Predicate)
17 {
18 int32 AStart = 0;
20
21 while (AStart < BStart && BStart < Num)
22 {
25
26 if (AStart >= BStart)
27 {
28 return;
29 }
30
34 AStart += NewBOffset + 1;
35 }
36 }
37
38 inline constexpr int32 MinMergeSubgroupSize = 2;
39
40 /**
41 * Sort elements using user defined projection and predicate classes. The sort is stable, meaning that the ordering of equal items is preserved.
42 * This is the internal sorting function used by the Algo::Sort overloads.
43 *
44 * @param First Pointer to the first element to sort.
45 * @param Num The number of items to sort.
46 * @param Projection A projection to apply to each element to get the value to sort by.
47 * @param Predicate A predicate class which compares two projected elements and returns whether one occurs before the other.
48 */
49 template <typename T, typename ProjectionType, typename PredicateType>
50 void StableSortInternal(T* First, int32 Num, ProjectionType Projection, PredicateType Predicate)
51 {
53
55 {
57 {
58 // First pass with simple bubble-sort.
59 do
60 {
62 if (Num < GroupEnd)
63 {
64 GroupEnd = Num;
65 }
66 do
67 {
68 for (int32 It = SubgroupStart; It < GroupEnd - 1; ++It)
69 {
71 {
72 Swap(First[It], First[It + 1]);
73 }
74 }
75 GroupEnd--;
76 }
77 while (GroupEnd - SubgroupStart > 1);
78
80 }
81 while (SubgroupStart < Num);
82 }
83 else
84 {
85 for (int32 Subgroup = 0; Subgroup < Num; Subgroup += 2)
86 {
88 {
90 }
91 }
92 }
93 }
94
96 while (SubgroupSize < Num)
97 {
98 SubgroupStart = 0;
99 do
100 {
102 if (Num - SubgroupStart < MergeNum)
103 {
105 }
106
109 }
110 while (SubgroupStart < Num);
111
112 SubgroupSize <<= 1;
113 }
114 }
115}
116
117namespace Algo
118{
119 /**
120 * Sort a range of elements using its operator<. The sort is stable.
121 *
122 * @param Range The range to sort.
123 */
124 template <typename RangeType>
125 FORCEINLINE void StableSort(RangeType&& Range)
126 {
128 }
129
130 /**
131 * Sort a range of elements using a user-defined predicate class. The sort is stable.
132 *
133 * @param Range The range to sort.
134 * @param Predicate A binary predicate object used to specify if one element should precede another.
135 */
136 template <typename RangeType, typename PredicateType>
137 FORCEINLINE void StableSort(RangeType&& Range, PredicateType Pred)
138 {
140 }
141
142 /**
143 * Sort a range of elements by a projection using the projection's operator<. The sort is stable.
144 *
145 * @param Range The range to sort.
146 * @param Proj The projection to sort by when applied to the element.
147 */
148 template <typename RangeType, typename ProjectionType>
149 FORCEINLINE void StableSortBy(RangeType&& Range, ProjectionType Proj)
150 {
152 }
153
154 /**
155 * Sort a range of elements by a projection using a user-defined predicate class. The sort is stable.
156 *
157 * @param Range The range to sort.
158 * @param Proj The projection to sort by when applied to the element.
159 * @param Predicate A binary predicate object, applied to the projection, used to specify if one element should precede another.
160 */
161 template <typename RangeType, typename ProjectionType, typename PredicateType>
162 FORCEINLINE void StableSortBy(RangeType&& Range, ProjectionType Proj, PredicateType Pred)
163 {
165 }
166}
#define FORCEINLINE
Definition Platform.h:644
Definition Heapify.h:12
FORCEINLINE void StableSortBy(RangeType &&Range, ProjectionType Proj)
Definition StableSort.h:149
FORCEINLINE void StableSortBy(RangeType &&Range, ProjectionType Proj, PredicateType Pred)
Definition StableSort.h:162
FORCEINLINE void StableSort(RangeType &&Range)
Definition StableSort.h:125
FORCEINLINE void StableSort(RangeType &&Range, PredicateType Pred)
Definition StableSort.h:137
void Merge(T *First, int32 Mid, int32 Num, ProjectionType Projection, PredicateType Predicate)
Definition StableSort.h:16
constexpr int32 MinMergeSubgroupSize
Definition StableSort.h:38
void StableSortInternal(T *First, int32 Num, ProjectionType Projection, PredicateType Predicate)
Definition StableSort.h:50