data.hpp : Fixed wrong signatures for implace sorting and replaced custom swap in favour of std::swap

This commit is contained in:
saundersp
2025-08-24 18:50:16 +02:00
parent cabcfcb330
commit 6fe9243685

110
data.hpp
View File

@@ -106,8 +106,8 @@ namespace asp {
};
template<typename T>
constexpr T& max(const Array<T>& a) noexcept {
T& max_el = a[0];
constexpr T max(const Array<T>& a) noexcept {
T max_el = a[0];
for (size_t i = 1; i < a.length; ++i)
if (a[i] > max_el)
max_el = a[i];
@@ -138,26 +138,19 @@ namespace asp {
inline Array<size_t> range(const size_t& n) noexcept {
Array<size_t> a(n);
return map(a, [](const size_t& i, const size_t&) -> const size_t& {
return map(a, [](const size_t i, const size_t) -> size_t {
return i;
});
}
template<typename T>
constexpr void swap(T* const a, T* const b) noexcept {
const T temp = *a;
*a = *b;
*b = temp;
}
template<typename T>
constexpr void bubble_sort(const Array<T>& a) noexcept {
constexpr void bubble_sort(Array<T>& a) noexcept {
size_t j = 0;
for (size_t i = 0; i < a.length; ++i) {
bool swapped = false;
for (j = i + 1; j < a.length; ++j)
if (a[i] > a[j]) {
swap(&a[i], &a[j]);
std::swap(a[i], a[j]);
swapped = true;
}
if (!swapped)
@@ -166,15 +159,15 @@ namespace asp {
}
template<typename T>
Array<size_t> bubble_sort_arg(const Array<T>& a) noexcept {
inline Array<size_t> bubble_sort_arg(Array<T>& a) noexcept {
Array<size_t> indices = range(a.length);
size_t j;
for (size_t i = 0; i < a.length; ++i) {
bool swapped = false;
for (j = i + 1; j < a.length; ++j)
if (a[i] > a[j]) {
swap(&indices[i], &indices[j]);
swap(&a[i], &a[j]);
std::swap(indices[i], indices[j]);
std::swap(a[i], a[j]);
swapped = true;
}
if (!swapped)
@@ -184,17 +177,17 @@ namespace asp {
}
template<typename T>
constexpr size_t qs_partition(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
constexpr size_t qs_partition(Array<T>& a, const size_t& l, const size_t& h) noexcept {
size_t i = l - 1;
for (size_t j = l; j <= h; ++j)
if (a[j] < a[h])
swap(&a[++i], &a[j]);
swap(&a[++i], &a[h]);
std::swap(a[++i], a[j]);
std::swap(a[++i], a[h]);
return i;
}
template<typename T>
constexpr void quicksort(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
constexpr void quicksort(Array<T>& a, const size_t& l, const size_t& h) noexcept {
if (l >= h)
return;
@@ -205,12 +198,12 @@ namespace asp {
}
template<typename T>
constexpr void quicksort(const Array<T>& a) noexcept {
constexpr void quicksort(Array<T>& a) noexcept {
quicksort(a, 0, a.length - 1);
}
template<typename T>
constexpr void quicksort_iter(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
constexpr void quicksort_iter(Array<T>& a, const size_t& l, const size_t& h) noexcept {
// Create an auxiliary stack
const size_t total = h - l + 1;
@@ -253,25 +246,25 @@ namespace asp {
}
template<typename T>
constexpr void quicksort_iter(const Array<T>& a) noexcept {
constexpr void quicksort_iter(Array<T>& a) noexcept {
quicksort_iter(a, 0, a.length - 1);
}
template<typename T>
constexpr size_t qs_arg_partition(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
constexpr size_t qs_arg_partition(Array<T>& a, Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
size_t i = l - 1;
for (size_t j = l; j <= h; ++j)
if (a[j] < a[h]){
swap(&a[++i], &a[j]);
swap(&indices[i], &indices[j]);
std::swap(a[++i], a[j]);
std::swap(indices[i], indices[j]);
}
swap(&indices[++i], &indices[h]);
swap(&a[i], &a[h]);
std::swap(indices[++i], indices[h]);
std::swap(a[i], a[h]);
return i;
}
template<typename T>
constexpr void quicksort_arg(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
constexpr void quicksort_arg(Array<T>& a, Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
if (l >= h)
return;
@@ -282,19 +275,19 @@ namespace asp {
}
template<typename T>
Array<size_t> quicksort_arg(const Array<T>& other, const size_t& l, const size_t& h) noexcept {
inline Array<size_t> quicksort_arg(Array<T>& other, const size_t& l, const size_t& h) noexcept {
Array<size_t> indices = range(other.length);
quicksort_arg(other, indices, l, h);
return indices;
}
template<typename T>
Array<size_t> quicksort_arg(const Array<T>& a) noexcept {
inline Array<size_t> quicksort_arg(Array<T>& a) noexcept {
return quicksort_arg(a, 0, a.length - 1);
}
template<typename T>
constexpr void quicksort_arg_iter(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
inline void quicksort_arg_iter(Array<T>& a, Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
// Create an auxiliary stack
const size_t total = h - l + 1;
// push initial values of l and h to stack
@@ -334,7 +327,7 @@ namespace asp {
}
template<typename T>
Array<size_t> quicksort_arg_iter(const Array<T>& a) noexcept {
inline Array<size_t> quicksort_arg_iter(Array<T>& a) noexcept {
Array<size_t> indices = range(a.length);
quicksort_arg_iter(a, indices, 0, a.length - 1);
return indices;
@@ -363,7 +356,7 @@ namespace asp {
};
template<typename T>
constexpr void merge(const Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept {
constexpr void merge(Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept {
Array<T> left_arr(m - l + 1);
memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T));
@@ -384,7 +377,7 @@ namespace asp {
}
template<typename T>
constexpr void mergesort(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
constexpr void mergesort(Array<T>& a, const size_t& l, const size_t& r) noexcept {
if (l >= r)
return;
@@ -395,15 +388,15 @@ namespace asp {
}
template<typename T>
constexpr void mergesort(const Array<T>& a) noexcept {
constexpr void mergesort(Array<T>& a) noexcept {
mergesort(a, 0, a.length - 1);
}
template<typename T>
Array<size_t> mergesort_arg(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
inline Array<size_t> mergesort_arg(Array<T>& a) noexcept {
Array<ArgVal<T>> temp_vals(a.length);
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> const ArgVal<T> {
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> ArgVal<T> {
return ArgVal<T>(i, a[i]);
});
@@ -421,7 +414,7 @@ namespace asp {
}
template<typename T>
constexpr void insertion_sort(const Array<T>& a) noexcept {
constexpr void insertion_sort(Array<T>& a) noexcept {
size_t j = 0;
for(size_t i = 1; i < a.length; ++i) {
T key = a[i];
@@ -432,41 +425,18 @@ namespace asp {
}
template<typename T>
Array<size_t> insertion_argsort(const Array<T>& a) noexcept {
Array<size_t> indices = range(a.length);
size_t j = 0;
for(size_t i = 1; i < a.length; ++i){
for(j = i; j > 0 && a[indices[j - 1]] > a[indices[i]]; --j)
indices[j] = indices[j - 1];
indices[j] = i;
}
return indices;
}
template<typename T>
constexpr void counting_sort(const Array<T>& a) noexcept {
const size_t N = a.length;
const T M = max(a);
const T exp = 10;
const T d = 128;
Array<T> countArray(M);
memset(&countArray[0], 0, M * sizeof(T));
foreach(a, [&countArray](const size_t, const T val) -> void {
++countArray[val];
inline Array<size_t> insertion_argsort(Array<T>& a) noexcept {
Array<ArgVal<T>> temp_vals(a.length);
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> ArgVal<T> {
return ArgVal<T>(i, a[i]);
});
for (T i = 1; i <= M; ++i)
countArray[i] += countArray[i - 1];
insertion_sort(temp_vals);
Array<T> output(N);
for (size_t i = N - 1; i > 0; --i){
output[countArray[a[i]] - 1] = a[i];
--countArray[a[i]];
}
memmove(&a[0], &output[0], a.length * sizeof(T));
Array<size_t> indices(a.length);
return map(indices, [&temp_vals](const size_t& i, const size_t&) -> size_t {
return temp_vals[i].indice;
});
}
template<typename T>