common/array: ++neat code

no logical changes
This commit is contained in:
Hermet Park 2025-05-14 01:29:46 +09:00
parent 7e8743e8fe
commit cd12618529

View file

@ -95,6 +95,13 @@ struct Array
return data[idx]; return data[idx];
} }
void operator=(const Array& rhs)
{
reserve(rhs.count);
if (rhs.count > 0) memcpy(data, rhs.data, sizeof(T) * rhs.count);
count = rhs.count;
}
const T* begin() const const T* begin() const
{ {
return data; return data;
@ -157,17 +164,9 @@ struct Array
return count == 0; return count == 0;
} }
template<class COMPARE> template<class COMPARE> void sort()
void sort()
{ {
qsort<COMPARE>(data, 0, static_cast<int32_t>(count) - 1); qsort<COMPARE>(data, 0, count - 1);
}
void operator=(const Array& rhs)
{
reserve(rhs.count);
if (rhs.count > 0) memcpy(data, rhs.data, sizeof(T) * rhs.count);
count = rhs.count;
} }
~Array() ~Array()
@ -177,23 +176,17 @@ struct Array
private: private:
template<class COMPARE> template<class COMPARE>
void qsort(T* arr, int32_t low, int32_t high) void qsort(T* arr, uint32_t low, uint32_t high)
{ {
if (low < high) { if (low < high) {
int32_t i = low; auto i = low;
int32_t j = high; auto j = high;
T tmp = arr[low]; auto tmp = arr[low];
while (i < j) { while (i < j) {
while (i < j && !COMPARE{}(arr[j], tmp)) --j; while (i < j && !COMPARE{}(arr[j], tmp)) --j;
if (i < j) { if (i < j) arr[i++] = arr[j];
arr[i] = arr[j];
++i;
}
while (i < j && COMPARE{}(arr[i], tmp)) ++i; while (i < j && COMPARE{}(arr[i], tmp)) ++i;
if (i < j) { if (i < j) arr[j--] = arr[i];
arr[j] = arr[i];
--j;
}
} }
arr[i] = tmp; arr[i] = tmp;
qsort<COMPARE>(arr, low, i - 1); qsort<COMPARE>(arr, low, i - 1);