Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
SkTSort.h File Reference
#include "include/private/base/SkTo.h"
#include "src/base/SkMathPriv.h"
#include <cstddef>
#include <utility>

Go to the source code of this file.

Functions

template<typename T , typename C >
void SkTHeapSort_SiftUp (T array[], size_t root, size_t bottom, const C &lessThan)
 
template<typename T , typename C >
void SkTHeapSort_SiftDown (T array[], size_t root, size_t bottom, const C &lessThan)
 
template<typename T , typename C >
void SkTHeapSort (T array[], size_t count, const C &lessThan)
 
template<typename T >
void SkTHeapSort (T array[], size_t count)
 
template<typename T , typename C >
void SkTInsertionSort (T *left, int count, const C &lessThan)
 
template<typename T , typename C >
TSkTQSort_Partition (T *left, int count, T *pivot, const C &lessThan)
 
template<typename T , typename C >
void SkTIntroSort (int depth, T *left, int count, const C &lessThan)
 
template<typename T , typename C >
void SkTQSort (T *begin, T *end, const C &lessThan)
 
template<typename T >
void SkTQSort (T *begin, T *end)
 
template<typename T >
void SkTQSort (T **begin, T **end)
 

Function Documentation

◆ SkTHeapSort() [1/2]

template<typename T >
void SkTHeapSort ( T  array[],
size_t  count 
)

Sorts the array of size count using comparator '<' using a Heap Sort algorithm.

Definition at line 106 of file SkTSort.h.

106 {
107 SkTHeapSort(array, count, [](const T& a, const T& b) { return a < b; });
108}
int count
void SkTHeapSort(T array[], size_t count, const C &lessThan)
Definition SkTSort.h:93
static bool b
struct MyStruct a[10]
#define T

◆ SkTHeapSort() [2/2]

template<typename T , typename C >
void SkTHeapSort ( T  array[],
size_t  count,
const C lessThan 
)

Sorts the array of size count using comparator lessThan using a Heap Sort algorithm. Be sure to specialize swap if T has an efficient swap operation.

Parameters
arraythe array to be sorted.
countthe number of elements in the array.
lessThana functor with bool operator()(T a, T b) which returns true if a comes before b.

Definition at line 93 of file SkTSort.h.

93 {
94 for (size_t i = count >> 1; i > 0; --i) {
95 SkTHeapSort_SiftDown(array, i, count, lessThan);
96 }
97
98 for (size_t i = count - 1; i > 0; --i) {
99 using std::swap;
100 swap(array[0], array[i]);
101 SkTHeapSort_SiftUp(array, 1, i, lessThan);
102 }
103}
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition SkRefCnt.h:341
void SkTHeapSort_SiftUp(T array[], size_t root, size_t bottom, const C &lessThan)
Definition SkTSort.h:34
void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, const C &lessThan)
Definition SkTSort.h:68

◆ SkTHeapSort_SiftDown()

template<typename T , typename C >
void SkTHeapSort_SiftDown ( T  array[],
size_t  root,
size_t  bottom,
const C lessThan 
)

Definition at line 68 of file SkTSort.h.

68 {
69 T x = array[root-1];
70 size_t child = root << 1;
71 while (child <= bottom) {
72 if (child < bottom && lessThan(array[child-1], array[child])) {
73 ++child;
74 }
75 if (lessThan(x, array[child-1])) {
76 array[root-1] = array[child-1];
77 root = child;
78 child = root << 1;
79 } else {
80 break;
81 }
82 }
83 array[root-1] = x;
84}
double x

◆ SkTHeapSort_SiftUp()

template<typename T , typename C >
void SkTHeapSort_SiftUp ( T  array[],
size_t  root,
size_t  bottom,
const C lessThan 
)

Definition at line 34 of file SkTSort.h.

34 {
35 T x = array[root-1];
36 size_t start = root;
37 size_t j = root << 1;
38 while (j <= bottom) {
39 if (j < bottom && lessThan(array[j-1], array[j])) {
40 ++j;
41 }
42 array[root-1] = array[j-1];
43 root = j;
44 j = root << 1;
45 }
46 j = root >> 1;
47 while (j >= start) {
48 if (lessThan(array[j-1], x)) {
49 array[root-1] = array[j-1];
50 root = j;
51 j = root >> 1;
52 } else {
53 break;
54 }
55 }
56 array[root-1] = x;
57}

◆ SkTInsertionSort()

template<typename T , typename C >
void SkTInsertionSort ( T left,
int  count,
const C lessThan 
)

Sorts the array of size count using comparator lessThan using an Insertion Sort algorithm.

Definition at line 114 of file SkTSort.h.

114 {
115 T* right = left + count - 1;
116 for (T* next = left + 1; next <= right; ++next) {
117 if (!lessThan(*next, *(next - 1))) {
118 continue;
119 }
120 T insert = std::move(*next);
121 T* hole = next;
122 do {
123 *hole = std::move(*(hole - 1));
124 --hole;
125 } while (left < hole && lessThan(insert, *(hole - 1)));
126 *hole = std::move(insert);
127 }
128}
static float next(float f)
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)

◆ SkTIntroSort()

template<typename T , typename C >
void SkTIntroSort ( int  depth,
T left,
int  count,
const C lessThan 
)

Definition at line 163 of file SkTSort.h.

163 {
164 for (;;) {
165 if (count <= 32) {
166 SkTInsertionSort(left, count, lessThan);
167 return;
168 }
169
170 if (depth == 0) {
171 SkTHeapSort<T>(left, count, lessThan);
172 return;
173 }
174 --depth;
175
176 T* middle = left + ((count - 1) >> 1);
177 T* pivot = SkTQSort_Partition(left, count, middle, lessThan);
178 int pivotCount = pivot - left;
179
180 SkTIntroSort(depth, left, pivotCount, lessThan);
181 left += pivotCount + 1;
182 count -= pivotCount + 1;
183 }
184}
void SkTIntroSort(int depth, T *left, int count, const C &lessThan)
Definition SkTSort.h:163
T * SkTQSort_Partition(T *left, int count, T *pivot, const C &lessThan)
Definition SkTSort.h:133
void SkTInsertionSort(T *left, int count, const C &lessThan)
Definition SkTSort.h:114

◆ SkTQSort() [1/3]

template<typename T >
void SkTQSort ( T **  begin,
T **  end 
)

Sorts the region from left to right using comparator '*a < *b' using Introsort.

Definition at line 210 of file SkTSort.h.

210 {
211 SkTQSort(begin, end, [](const T* a, const T* b) { return *a < *b; });
212}
void SkTQSort(T *begin, T *end, const C &lessThan)
Definition SkTSort.h:194
static const char * begin(const StringSlice &s)
Definition editor.cpp:252
glong glong end

◆ SkTQSort() [2/3]

template<typename T >
void SkTQSort ( T begin,
T end 
)

Sorts the region from left to right using comparator 'a < b' using Introsort.

Definition at line 205 of file SkTSort.h.

205 {
206 SkTQSort(begin, end, [](const T& a, const T& b) { return a < b; });
207}

◆ SkTQSort() [3/3]

template<typename T , typename C >
void SkTQSort ( T begin,
T end,
const C lessThan 
)

Sorts the region from left to right using comparator lessThan using Introsort. Be sure to specialize swap if T has an efficient swap operation.

Parameters
beginpoints to the beginning of the region to be sorted
endpoints past the end of the region to be sorted
lessThana functor/lambda which returns true if a comes before b.

Definition at line 194 of file SkTSort.h.

194 {
195 int n = SkToInt(end - begin);
196 if (n <= 1) {
197 return;
198 }
199 // Limit Introsort recursion depth to no more than 2 * ceil(log2(n-1)).
200 int depth = 2 * SkNextLog2(n - 1);
201 SkTIntroSort(depth, begin, n, lessThan);
202}
static int SkNextLog2(uint32_t value)
Definition SkMathPriv.h:238
constexpr int SkToInt(S x)
Definition SkTo.h:29

◆ SkTQSort_Partition()

template<typename T , typename C >
T * SkTQSort_Partition ( T left,
int  count,
T pivot,
const C lessThan 
)

Definition at line 133 of file SkTSort.h.

133 {
134 T* right = left + count - 1;
135 using std::swap;
136 T pivotValue = *pivot;
137 swap(*pivot, *right);
138 T* newPivot = left;
139 while (left < right) {
140 if (lessThan(*left, pivotValue)) {
141 swap(*left, *newPivot);
142 newPivot += 1;
143 }
144 left += 1;
145 }
146 swap(*newPivot, *right);
147 return newPivot;
148}