Sort GL extension strings and search to find.

Review URL: https://codereview.chromium.org/12316141

git-svn-id: http://skia.googlecode.com/svn/trunk@7889 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bsalomon@google.com 2013-02-27 19:07:32 +00:00
parent f0e92f3814
commit ff436617d8
4 changed files with 39 additions and 10 deletions

View File

@ -244,4 +244,10 @@ private:
/// Creates a new string and writes into it using a printf()-style format.
SkString SkStringPrintf(const char* format, ...);
// Specialized to take advantage of SkString's fast swap path. The unspecialized function is
// declared in SkTypes.h and called by SkTSort.
template <> inline void SkTSwap(SkString& a, SkString& b) {
a.swap(b);
}
#endif

View File

@ -276,6 +276,8 @@ static inline int Sk32ToBool(uint32_t n) {
return (n | (0-n)) >> 31;
}
/** Generic swap function. Classes with efficient swaps should specialize this function to take
their fast path. This function is used by SkTSort. */
template <typename T> inline void SkTSwap(T& a, T& b) {
T c(a);
a = b;

View File

@ -92,7 +92,8 @@ void SkTHeapSort_SiftDown(T array[], size_t root, size_t bottom, C lessThan) {
array[root-1] = x;
}
/** Sorts the array of size count using comparator lessThan using a Heap Sort algorithm
/** Sorts the array of size count using comparator lessThan using a Heap Sort algorithm. Be sure to
* specialize SkTSwap if T has an efficient swap operation.
*
* @param array the array to be sorted.
* @param count the number of elements in the array.
@ -180,7 +181,8 @@ template <typename T, typename C> void SkTIntroSort(int depth, T* left, T* right
}
}
/** Sorts the region from left to right using comparator lessThan using a Quick Sort algorithm.
/** Sorts the region from left to right using comparator lessThan using a Quick Sort algorithm. Be
* sure to specialize SkTSwap if T has an efficient swap operation.
*
* @param left the beginning of the region to be sorted.
* @param right the end of the region to be sorted (inclusive).
@ -205,4 +207,14 @@ template <typename T> void SkTQSort(T** left, T** right) {
SkTQSort(left, right, SkTPointerCompareLT<T>());
}
/** Adapts a tri-state SkTSearch comparison function to a bool less-than SkTSort functor */
template <typename T, int (COMPARE)(const T*, const T*)>
class SkTSearchCompareLTFunctor {
public:
bool operator()(const T& a, const T& b) {
return COMPARE(&a, &b) < 0;
}
};
#endif

View File

@ -9,6 +9,15 @@
#include "gl/GrGLDefines.h"
#include "gl/GrGLUtil.h"
#include "SkTSearch.h"
#include "SkTSort.h"
namespace {
inline int extension_compare(const SkString* a, const SkString* b) {
return strcmp(a->c_str(), b->c_str());
}
}
bool GrGLExtensions::init(GrGLBinding binding,
GrGLGetStringProc getString,
GrGLGetStringiProc getStringi,
@ -64,16 +73,16 @@ bool GrGLExtensions::init(GrGLBinding binding,
}
GrAssert(i == extensionCnt);
}
SkTSearchCompareLTFunctor<SkString, extension_compare> cmp;
SkTQSort(&fStrings.front(), &fStrings.back(), cmp);
return true;
}
bool GrGLExtensions::has(const char* ext) const {
// TODO: Sort the extensions and binary search.
int count = fStrings.count();
for (int i = 0; i < count; ++i) {
if (fStrings[i].equals(ext)) {
return true;
}
}
return false;
SkString extensionStr(ext);
int idx = SkTSearch<SkString, extension_compare>(&fStrings.front(),
fStrings.count(),
extensionStr,
sizeof(SkString));
return idx >= 0;
}