utils: add sort method in tvgArray

Add a new method in tvg::Array to support sort with custom comparer
This commit is contained in:
RuiwenTang 2023-08-07 22:41:46 +08:00 committed by Hermet Park
parent 1e2051ded3
commit 0d4f45014a

View file

@ -24,6 +24,7 @@
#define _TVG_ARRAY_H_ #define _TVG_ARRAY_H_
#include <memory.h> #include <memory.h>
#include <cstdint>
namespace tvg namespace tvg
{ {
@ -110,6 +111,12 @@ struct Array
return count == 0; return count == 0;
} }
template<class COMPARE>
void sort()
{
qsort<COMPARE>(data, 0, static_cast<int32_t>(count) - 1);
}
void operator=(const Array& rhs) void operator=(const Array& rhs)
{ {
reserve(rhs.count); reserve(rhs.count);
@ -121,6 +128,32 @@ struct Array
{ {
free(data); free(data);
} }
private:
template<class COMPARE>
void qsort(T* arr, int32_t low, int32_t high)
{
if (low < high) {
int32_t i = low;
int32_t j = high;
T tmp = arr[low];
while (i < j) {
while (i < j && !COMPARE{}(arr[j], tmp)) --j;
if (i < j) {
arr[i] = arr[j];
++i;
}
while (i < j && COMPARE{}(arr[i], tmp)) ++i;
if (i < j) {
arr[j] = arr[i];
--j;
}
}
arr[i] = tmp;
qsort<COMPARE>(arr, low, i - 1);
qsort<COMPARE>(arr, i + 1, high);
}
}
}; };
} }