diff options
author | Vitaly Buka <vitalybuka@google.com> | 2018-05-09 20:42:11 +0000 |
---|---|---|
committer | Vitaly Buka <vitalybuka@google.com> | 2018-05-09 20:42:11 +0000 |
commit | 4d9e83c2e00a1fc8aa7a1e8b367a21f731013c65 (patch) | |
tree | df6e35fdb4d65892218b9bfdb85e8809ccedad9b /lib/sanitizer_common/sanitizer_common.h | |
parent | f2e93289e1b4293f052ca0ef2838d9ff711b2136 (diff) | |
download | compiler-rt-4d9e83c2e00a1fc8aa7a1e8b367a21f731013c65.tar.gz |
[sanitizer] Cleanup sorting functions
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@331915 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/sanitizer_common/sanitizer_common.h')
-rw-r--r-- | lib/sanitizer_common/sanitizer_common.h | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/lib/sanitizer_common/sanitizer_common.h b/lib/sanitizer_common/sanitizer_common.h index 39f9414fa..81336d22f 100644 --- a/lib/sanitizer_common/sanitizer_common.h +++ b/lib/sanitizer_common/sanitizer_common.h @@ -243,8 +243,6 @@ void SleepForMillis(int millis); u64 NanoTime(); u64 MonotonicNanoTime(); int Atexit(void (*function)(void)); -void SortArray(uptr *array, uptr size); -void SortArray(u32 *array, uptr size); bool TemplateMatch(const char *templ, const char *str); // Exit @@ -572,9 +570,14 @@ class InternalScopedString : public InternalMmapVector<char> { uptr length_; }; +template <class T> +struct CompareLess { + bool operator()(const T &a, const T &b) const { return a < b; } +}; + // HeapSort for arrays and InternalMmapVector. -template<class Container, class Compare> -void InternalSort(Container *v, uptr size, Compare comp) { +template <class T, class Compare = CompareLess<T>> +void Sort(T *v, uptr size, Compare comp = {}) { if (size < 2) return; // Stage 1: insert elements to the heap. @@ -582,8 +585,8 @@ void InternalSort(Container *v, uptr size, Compare comp) { uptr j, p; for (j = i; j > 0; j = p) { p = (j - 1) / 2; - if (comp((*v)[p], (*v)[j])) - Swap((*v)[j], (*v)[p]); + if (comp(v[p], v[j])) + Swap(v[j], v[p]); else break; } @@ -591,18 +594,18 @@ void InternalSort(Container *v, uptr size, Compare comp) { // Stage 2: swap largest element with the last one, // and sink the new top. for (uptr i = size - 1; i > 0; i--) { - Swap((*v)[0], (*v)[i]); + Swap(v[0], v[i]); uptr j, max_ind; for (j = 0; j < i; j = max_ind) { uptr left = 2 * j + 1; uptr right = 2 * j + 2; max_ind = j; - if (left < i && comp((*v)[max_ind], (*v)[left])) + if (left < i && comp(v[max_ind], v[left])) max_ind = left; - if (right < i && comp((*v)[max_ind], (*v)[right])) + if (right < i && comp(v[max_ind], v[right])) max_ind = right; if (max_ind != j) - Swap((*v)[j], (*v)[max_ind]); + Swap(v[j], v[max_ind]); else break; } |