Replace various one-liner sorting functor adapters with lambdas.

These tiny adapter classes aren't needed, since all of our clients have
support for lambdas.

Change-Id: Ibf22b1fd0adb3707db570432c50720df9c9329e7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/302581
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
John Stiles 2020-07-14 12:56:15 -04:00 committed by Skia Commit-Bot
parent 9d5c55cedf
commit 955adbe02c
7 changed files with 29 additions and 60 deletions

View File

@ -34,10 +34,10 @@ public:
uint32_t fSpecVersion;
struct Less {
bool operator() (const Info& a, const SkString& b) {
bool operator()(const Info& a, const SkString& b) const {
return strcmp(a.fName.c_str(), b.c_str()) < 0;
}
bool operator() (const SkString& a, const GrVkExtensions::Info& b) {
bool operator()(const SkString& a, const GrVkExtensions::Info& b) const {
return strcmp(a.c_str(), b.fName.c_str()) < 0;
}
};

View File

@ -36,7 +36,7 @@
// bool operator() (const T& t, const K& k)
// bool operator() (const K& t, const T& k)
template <typename T, typename K, typename LESS>
int SkTSearch(const T base[], int count, const K& key, size_t elemSize, LESS& less)
int SkTSearch(const T base[], int count, const K& key, size_t elemSize, const LESS& less)
{
SkASSERT(count >= 0);
if (count <= 0) {
@ -68,41 +68,25 @@ int SkTSearch(const T base[], int count, const K& key, size_t elemSize, LESS& le
return hi;
}
// Adapts a less-than function to a functor.
template <typename T, bool (LESS)(const T&, const T&)> struct SkTLessFunctionToFunctorAdaptor {
bool operator()(const T& a, const T& b) { return LESS(a, b); }
};
// Specialization for case when T==K and the caller wants to use a function rather than functor.
template <typename T, bool (LESS)(const T&, const T&)>
int SkTSearch(const T base[], int count, const T& target, size_t elemSize) {
static SkTLessFunctionToFunctorAdaptor<T, LESS> functor;
return SkTSearch(base, count, target, elemSize, functor);
return SkTSearch(base, count, target, elemSize,
[](const T& a, const T& b) { return LESS(a, b); });
}
// Adapts operator < to a functor.
template <typename T> struct SkTLessFunctor {
bool operator()(const T& a, const T& b) { return a < b; }
};
// Specialization for T==K, compare using op <.
template <typename T>
int SkTSearch(const T base[], int count, const T& target, size_t elemSize) {
static SkTLessFunctor<T> functor;
return SkTSearch(base, count, target, elemSize, functor);
return SkTSearch(base, count, target, elemSize, [](const T& a, const T& b) { return a < b; });
}
// Similar to SkLessFunctionToFunctorAdaptor but makes the functor interface take T* rather than T.
template <typename T, bool (LESS)(const T&, const T&)> struct SkTLessFunctionToPtrFunctorAdaptor {
bool operator() (const T* t, const T* k) { return LESS(*t, *k); }
};
// Specialization for case where domain is an array of T* and the key value is a T*, and you want
// to compare the T objects, not the pointers.
template <typename T, bool (LESS)(const T&, const T&)>
int SkTSearch(T* base[], int count, T* target, size_t elemSize) {
static SkTLessFunctionToPtrFunctorAdaptor<T, LESS> functor;
return SkTSearch(base, count, target, elemSize, functor);
return SkTSearch(base, count, target, elemSize,
[](const T* t, const T* k) { return LESS(*t, *k); });
}
int SkStrSearch(const char*const* base, int count, const char target[],

View File

@ -14,16 +14,6 @@
#include <utility>
/* A comparison functor which performs the comparison 'a < b'. */
template <typename T> struct SkTCompareLT {
bool operator()(const T a, const T b) const { return a < b; }
};
/* A comparison functor which performs the comparison '*a < *b'. */
template <typename T> struct SkTPointerCompareLT {
bool operator()(const T* a, const T* b) const { return *a < *b; }
};
///////////////////////////////////////////////////////////////////////////////
/* Sifts a broken heap. The input array is a heap from root to bottom
@ -41,7 +31,7 @@ template <typename T> struct SkTPointerCompareLT {
* @param bottom the one based index in the array of the last entry in the heap.
*/
template <typename T, typename C>
void SkTHeapSort_SiftUp(T array[], size_t root, size_t bottom, C lessThan) {
void SkTHeapSort_SiftUp(T array[], size_t root, size_t bottom, const C& lessThan) {
T x = array[root-1];
size_t start = root;
size_t j = root << 1;
@ -75,7 +65,7 @@ void SkTHeapSort_SiftUp(T array[], size_t root, size_t bottom, C lessThan) {
* @param bottom the one based index in the array of the last entry in the heap.
*/
template <typename T, typename C>
void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, C lessThan) {
void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, const C& lessThan) {
T x = array[root-1];
size_t child = root << 1;
while (child <= bottom) {
@ -100,7 +90,7 @@ void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, C lessThan) {
* @param count the number of elements in the array.
* @param lessThan a functor with bool operator()(T a, T b) which returns true if a comes before b.
*/
template <typename T, typename C> void SkTHeapSort(T array[], size_t count, C lessThan) {
template <typename T, typename C> void SkTHeapSort(T array[], size_t count, const C& lessThan) {
for (size_t i = count >> 1; i > 0; --i) {
SkTHeapSort_SiftDown(array, i, count, lessThan);
}
@ -114,13 +104,14 @@ template <typename T, typename C> void SkTHeapSort(T array[], size_t count, C le
/** Sorts the array of size count using comparator '<' using a Heap Sort algorithm. */
template <typename T> void SkTHeapSort(T array[], size_t count) {
SkTHeapSort(array, count, SkTCompareLT<T>());
SkTHeapSort(array, count, [](const T& a, const T& b) { return a < b; });
}
///////////////////////////////////////////////////////////////////////////////
/** Sorts the array of size count using comparator lessThan using an Insertion Sort algorithm. */
template <typename T, typename C> static void SkTInsertionSort(T* left, T* right, C lessThan) {
template <typename T, typename C>
static void SkTInsertionSort(T* left, T* right, const C& lessThan) {
for (T* next = left + 1; next <= right; ++next) {
if (!lessThan(*next, *(next - 1))) {
continue;
@ -138,7 +129,7 @@ template <typename T, typename C> static void SkTInsertionSort(T* left, T* right
///////////////////////////////////////////////////////////////////////////////
template <typename T, typename C>
static T* SkTQSort_Partition(T* left, T* right, T* pivot, C lessThan) {
static T* SkTQSort_Partition(T* left, T* right, T* pivot, const C& lessThan) {
using std::swap;
T pivotValue = *pivot;
swap(*pivot, *right);
@ -166,7 +157,8 @@ static T* SkTQSort_Partition(T* left, T* right, T* pivot, C lessThan) {
* @param right the end of the region to be sorted (inclusive).
* @param lessThan a functor with bool operator()(T a, T b) which returns true if a comes before b.
*/
template <typename T, typename C> void SkTIntroSort(int depth, T* left, T* right, C lessThan) {
template <typename T, typename C>
void SkTIntroSort(int depth, T* left, T* right, const C& lessThan) {
while (true) {
if (right - left < 32) {
SkTInsertionSort(left, right, lessThan);
@ -194,7 +186,7 @@ template <typename T, typename C> void SkTIntroSort(int depth, T* left, T* right
* @param right the end of the region to be sorted (inclusive).
* @param lessThan a functor with bool operator()(T a, T b) which returns true if a comes before b.
*/
template <typename T, typename C> void SkTQSort(T* left, T* right, C lessThan) {
template <typename T, typename C> void SkTQSort(T* left, T* right, const C& lessThan) {
if (left >= right) {
return;
}
@ -203,14 +195,14 @@ template <typename T, typename C> void SkTQSort(T* left, T* right, C lessThan) {
SkTIntroSort(depth, left, right, lessThan);
}
/** Sorts the region from left to right using comparator '<' using a Quick Sort algorithm. */
/** Sorts the region from left to right using comparator 'a < b' using a Quick Sort algorithm. */
template <typename T> void SkTQSort(T* left, T* right) {
SkTQSort(left, right, SkTCompareLT<T>());
SkTQSort(left, right, [](const T& a, const T& b) { return a < b; });
}
/** Sorts the region from left to right using comparator '* < *' using a Quick Sort algorithm. */
/** Sorts the region from left to right using comparator '*a < *b' using a Quick Sort algorithm. */
template <typename T> void SkTQSort(T** left, T** right) {
SkTQSort(left, right, SkTPointerCompareLT<T>());
SkTQSort(left, right, [](const T* a, const T* b) { return *a < *b; });
}
#endif

View File

@ -117,8 +117,7 @@ bool GrGLExtensions::init(GrGLStandard standard,
eat_space_sep_strings(&fStrings, extensions);
}
if (!fStrings.empty()) {
SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
SkTQSort(&fStrings.front(), &fStrings.back(), cmp);
SkTQSort(&fStrings.front(), &fStrings.back(), extension_compare);
}
fInitialized = true;
return true;
@ -140,8 +139,7 @@ bool GrGLExtensions::remove(const char ext[]) {
// most a handful of times when our test programs start.
fStrings.removeShuffle(idx);
if (idx != fStrings.count()) {
SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
SkTInsertionSort(&(fStrings.operator[](idx)), &fStrings.back(), cmp);
SkTInsertionSort(&(fStrings.operator[](idx)), &fStrings.back(), extension_compare);
}
return true;
}
@ -152,8 +150,7 @@ void GrGLExtensions::add(const char ext[]) {
// This is not the most effecient approach since we end up looking at all of the
// extensions after the add
fStrings.emplace_back(ext);
SkTLessFunctionToFunctorAdaptor<SkString, extension_compare> cmp;
SkTInsertionSort(&fStrings.front(), &fStrings.back(), cmp);
SkTInsertionSort(&fStrings.front(), &fStrings.back(), extension_compare);
}
}

View File

@ -39,14 +39,12 @@ void GrVkExtensions::init(GrVkGetProc getProc,
const char* const* instanceExtensions,
uint32_t deviceExtensionCount,
const char* const* deviceExtensions) {
SkTLessFunctionToFunctorAdaptor<GrVkExtensions::Info, extension_compare> cmp;
for (uint32_t i = 0; i < instanceExtensionCount; ++i) {
const char* extension = instanceExtensions[i];
// if not already in the list, add it
if (find_info(fExtensions, extension) < 0) {
fExtensions.push_back() = Info(extension);
SkTQSort(&fExtensions.front(), &fExtensions.back(), cmp);
SkTQSort(&fExtensions.front(), &fExtensions.back(), extension_compare);
}
}
for (uint32_t i = 0; i < deviceExtensionCount; ++i) {
@ -54,7 +52,7 @@ void GrVkExtensions::init(GrVkGetProc getProc,
// if not already in the list, add it
if (find_info(fExtensions, extension) < 0) {
fExtensions.push_back() = Info(extension);
SkTQSort(&fExtensions.front(), &fExtensions.back(), cmp);
SkTQSort(&fExtensions.front(), &fExtensions.back(), extension_compare);
}
}
this->getSpecVersions(getProc, instance, physDev);

View File

@ -186,7 +186,7 @@ class DistanceLessThan {
public:
DistanceLessThan(double* distances) : fDistances(distances) { }
double* fDistances;
bool operator()(const int one, const int two) {
bool operator()(const int one, const int two) const {
return fDistances[one] < fDistances[two];
}
};

View File

@ -150,9 +150,7 @@ int SkWGLExtensions::selectFormat(const int formats[],
rankedFormats[i].fSampleCnt = std::max(1, numSamples);
rankedFormats[i].fChoosePixelFormatRank = i;
}
SkTQSort(rankedFormats.begin(),
rankedFormats.begin() + rankedFormats.count() - 1,
SkTLessFunctionToFunctorAdaptor<PixelFormat, pf_less>());
SkTQSort(rankedFormats.begin(), rankedFormats.begin() + rankedFormats.count() - 1, pf_less);
int idx = SkTSearch<PixelFormat, pf_less>(rankedFormats.begin(),
rankedFormats.count(),
desiredFormat,