Modernized code and fancier code printing
This commit is contained in:
362
data.hpp
362
data.hpp
@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
#ifdef __DEBUG
|
||||
#include <stdexcept>
|
||||
#endif
|
||||
@ -11,29 +14,39 @@ namespace asp {
|
||||
std::shared_ptr<T[]> data;
|
||||
size_t length = 0;
|
||||
|
||||
Array(void) noexcept = delete;
|
||||
Array(const size_t& size) noexcept: data(std::shared_ptr<T[]>(new T[size])), length(size) {
|
||||
// #ifdef __DEBUG
|
||||
// printf("Creating array of size %lu\n", size);
|
||||
// #endif
|
||||
constexpr Array(void) noexcept = delete;
|
||||
constexpr Array(const size_t& size) noexcept : data(std::shared_ptr<T[]>(new T[size])), length(size) {
|
||||
#ifdef __DEBUG
|
||||
printf("Creating array of size %lu\n", size);
|
||||
#endif
|
||||
}
|
||||
Array(const Array<T>& other) noexcept: data(std::shared_ptr<T[]>(new T[other.length])), length(other.length) {
|
||||
|
||||
constexpr Array(const std::initializer_list<size_t>& other) noexcept : data(std::shared_ptr<T[]>(new T[other.size()])), length(other.size()) {
|
||||
#ifdef __DEBUG
|
||||
printf("Copying array of size %lu\n", other.size());
|
||||
#endif
|
||||
memcpy(data.get(), other.begin(), length * sizeof(T));
|
||||
}
|
||||
|
||||
|
||||
constexpr Array(const Array<T>& other) noexcept : data(std::shared_ptr<T[]>(new T[other.length])), length(other.length) {
|
||||
#ifdef __DEBUG
|
||||
printf("Copying array of size %lu\n", other.length);
|
||||
#endif
|
||||
memcpy(data.get(), other.data.get(), length * sizeof(T));
|
||||
}
|
||||
Array(Array&& other) noexcept {
|
||||
// #ifdef __DEBUG
|
||||
// printf("Moving array of size %lu\n", other.length);
|
||||
// #endif
|
||||
|
||||
constexpr Array(Array&& other) noexcept {
|
||||
#ifdef __DEBUG
|
||||
printf("Moving array of size %lu\n", other.length);
|
||||
#endif
|
||||
data = other.data;
|
||||
length = other.length;
|
||||
other.data = nullptr;
|
||||
other.length = 0;
|
||||
}
|
||||
|
||||
inline constexpr T& operator[](const size_t& index) const {
|
||||
constexpr T& operator[](const size_t& index) const {
|
||||
#ifdef __DEBUG
|
||||
if (index > length) {
|
||||
fprintf(stderr, "Index %ld out of range in Array of length %ld !\n", index, length);
|
||||
@ -45,10 +58,10 @@ namespace asp {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline constexpr int32_t print(const Array<T>& a, const char* const format) noexcept {
|
||||
constexpr int32_t print(const Array<T>& a, const char* const format) noexcept {
|
||||
int32_t num_written = 0;
|
||||
num_written += printf("[");
|
||||
char formatter[BUFSIZ] = { 0 };
|
||||
char formatter[256] = { 0 };
|
||||
sprintf(formatter, "%s,", format);
|
||||
for (size_t i = 0; i < a.length; ++i)
|
||||
num_written += printf(formatter, a[i]);
|
||||
@ -57,33 +70,36 @@ namespace asp {
|
||||
return num_written;
|
||||
}
|
||||
|
||||
inline constexpr int32_t print(const Array<uint16_t>& a) noexcept {
|
||||
constexpr int32_t print(const Array<uint16_t>& a) noexcept {
|
||||
return print(a, "%b");
|
||||
}
|
||||
|
||||
inline constexpr int32_t print(const Array<int32_t>& a) noexcept {
|
||||
constexpr int32_t print(const Array<int32_t>& a) noexcept {
|
||||
return print(a, "%i");
|
||||
}
|
||||
|
||||
inline constexpr int32_t print(const Array<uint64_t>& a) noexcept {
|
||||
constexpr int32_t print(const Array<uint32_t>& a) noexcept {
|
||||
return print(a, "%u");
|
||||
}
|
||||
|
||||
constexpr int32_t print(const Array<uint64_t>& a) noexcept {
|
||||
return print(a, "%lu");
|
||||
}
|
||||
|
||||
inline constexpr int32_t print(const Array<int16_t>& a) noexcept {
|
||||
//printf("%i\n", a[0]);
|
||||
constexpr int32_t print(const Array<int16_t>& a) noexcept {
|
||||
return print(a, "%i");
|
||||
}
|
||||
|
||||
inline int32_t print(const std::string& s) noexcept {
|
||||
return printf("%s\n", s.c_str());
|
||||
constexpr int32_t print(const std::string_view& s) noexcept {
|
||||
return printf("%s\n", s.data());
|
||||
}
|
||||
|
||||
inline constexpr int32_t print(const char* const s) noexcept {
|
||||
constexpr int32_t print(const char* const s) noexcept {
|
||||
return printf("%s\n", s);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr T& max(const Array<T>& a) noexcept {
|
||||
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)
|
||||
@ -92,7 +108,7 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr T& min(const Array<T>& a) noexcept {
|
||||
constexpr T& min(const Array<T>& a) noexcept {
|
||||
T& max_el = a[0];
|
||||
for (size_t i = 1; i < a.length; ++i)
|
||||
if (a[i] < max_el)
|
||||
@ -101,57 +117,67 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T, typename F>
|
||||
inline constexpr Array<T>& map(Array<T>& a, const F& fnc) noexcept {
|
||||
constexpr Array<T>& map(Array<T>& a, const F& fnc) noexcept {
|
||||
for (size_t i = 0; i < a.length; ++i)
|
||||
a[i] = fnc(i, a[i]);
|
||||
return a;
|
||||
}
|
||||
|
||||
template<typename T, typename F>
|
||||
inline constexpr void foreach(const Array<T>& a, const F& fnc) noexcept {
|
||||
constexpr void foreach(const Array<T>& a, const F& fnc) noexcept {
|
||||
for (size_t i = 0; i < a.length; ++i)
|
||||
fnc(i, a[i]);
|
||||
}
|
||||
|
||||
inline Array<size_t> range(const size_t& n) noexcept {
|
||||
Array<size_t> a(n);
|
||||
return std::move(map(a, [](const size_t& i, const size_t&) -> const size_t& {
|
||||
return map(a, [](const size_t& i, const size_t&) -> const size_t& {
|
||||
return i;
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void swap(T* const a, T* const b) noexcept {
|
||||
constexpr void swap(T* const a, T* const b) noexcept {
|
||||
const T temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void bubble_sort(const Array<T>& a) noexcept {
|
||||
constexpr void bubble_sort(const Array<T>& a) noexcept {
|
||||
size_t j = 0;
|
||||
for (size_t i = 0; i < a.length; ++i)
|
||||
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])
|
||||
if (a[i] > a[j]) {
|
||||
swap(&a[i], &a[j]);
|
||||
swapped = true;
|
||||
}
|
||||
if (!swapped)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Array<size_t> bubble_sort_arg(const Array<T>& a) noexcept {
|
||||
Array<size_t> bubble_sort_arg(const Array<T>& a) noexcept {
|
||||
Array<size_t> indices = range(a.length);
|
||||
size_t j;
|
||||
for (size_t i = 0; i < a.length; ++i)
|
||||
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]){
|
||||
if (a[i] > a[j]) {
|
||||
swap(&indices[i], &indices[j]);
|
||||
swap(&a[i], &a[j]);
|
||||
|
||||
swapped = true;
|
||||
}
|
||||
if (!swapped)
|
||||
break;
|
||||
}
|
||||
return indices;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr size_t qs_partition(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||
constexpr size_t qs_partition(const 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])
|
||||
@ -161,7 +187,7 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void quicksort(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||
constexpr void quicksort(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||
if (l >= h)
|
||||
return;
|
||||
|
||||
@ -172,12 +198,12 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void quicksort(const Array<T>& a) noexcept {
|
||||
constexpr void quicksort(const Array<T>& a) noexcept {
|
||||
quicksort(a, 0, a.length - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void quicksort_iter(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||
constexpr void quicksort_iter(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||
// Create an auxiliary stack
|
||||
|
||||
const size_t total = h - l + 1;
|
||||
@ -220,12 +246,12 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void quicksort_iter(const Array<T>& a) noexcept {
|
||||
constexpr void quicksort_iter(const Array<T>& a) noexcept {
|
||||
quicksort_iter(a, 0, a.length - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline 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(const Array<T>& a, const 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]){
|
||||
@ -238,7 +264,7 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline 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(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
||||
if (l >= h)
|
||||
return;
|
||||
|
||||
@ -249,29 +275,26 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Array<size_t> quicksort_arg(const Array<T>& other, const size_t& l, const size_t& h) noexcept {
|
||||
Array<size_t> quicksort_arg(const 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>
|
||||
inline Array<size_t> quicksort_arg(const Array<T>& a) noexcept {
|
||||
Array<size_t> quicksort_arg(const Array<T>& a) noexcept {
|
||||
return quicksort_arg(a, 0, a.length - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void quicksort_arg_iter(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
||||
constexpr void quicksort_arg_iter(const Array<T>& a, const 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
|
||||
size_t* const stack = new size_t[total]{l,h};
|
||||
|
||||
// initialize top of stack
|
||||
size_t top = 1;
|
||||
|
||||
size_t low = l, high = h;
|
||||
size_t top = 1, low = l, high = h;
|
||||
|
||||
// Keep popping from stack while is not empty
|
||||
while (top <= total) {
|
||||
@ -304,7 +327,7 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Array<size_t> quicksort_arg_iter(const Array<T>& a) noexcept {
|
||||
Array<size_t> quicksort_arg_iter(const Array<T>& a) noexcept {
|
||||
Array<size_t> indices = range(a.length);
|
||||
quicksort_arg_iter(a, indices, 0, a.length - 1);
|
||||
return indices;
|
||||
@ -315,25 +338,25 @@ namespace asp {
|
||||
size_t indice;
|
||||
T val;
|
||||
|
||||
ArgVal(void) noexcept = default;
|
||||
ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {}
|
||||
constexpr ArgVal(void) noexcept = default;
|
||||
constexpr ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {}
|
||||
|
||||
inline constexpr bool operator>(const ArgVal<T>& other) const noexcept {
|
||||
constexpr bool operator>(const ArgVal<T>& other) const noexcept {
|
||||
return std::move(val > other.val);
|
||||
}
|
||||
inline constexpr bool operator<(const ArgVal<T>& other) const noexcept {
|
||||
constexpr bool operator<(const ArgVal<T>& other) const noexcept {
|
||||
return std::move(val < other.val);
|
||||
}
|
||||
inline constexpr bool operator>=(const ArgVal<T>& other) const noexcept {
|
||||
constexpr bool operator>=(const ArgVal<T>& other) const noexcept {
|
||||
return std::move(val >= other.val);
|
||||
}
|
||||
inline constexpr bool operator<=(const ArgVal<T>& other) const noexcept {
|
||||
constexpr bool operator<=(const ArgVal<T>& other) const noexcept {
|
||||
return std::move(val <= other.val);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void merge(const Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept {
|
||||
constexpr void merge(const 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));
|
||||
@ -354,7 +377,7 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void mergesort(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
||||
constexpr void mergesort(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
||||
if (l >= r)
|
||||
return;
|
||||
|
||||
@ -365,12 +388,12 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void mergesort(const Array<T>& a) noexcept {
|
||||
constexpr void mergesort(const Array<T>& a) noexcept {
|
||||
mergesort(a, 0, a.length - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Array<size_t> mergesort_arg(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
||||
Array<size_t> mergesort_arg(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
||||
|
||||
Array<ArgVal<T>> temp_vals(a.length);
|
||||
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> const ArgVal<T> {
|
||||
@ -386,107 +409,162 @@ namespace asp {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Array<size_t> mergesort_arg(const Array<T>& a) noexcept {
|
||||
Array<size_t> mergesort_arg(const Array<T>& a) noexcept {
|
||||
return mergesort_arg(a, 0, a.length - 1);
|
||||
}
|
||||
|
||||
//void count_sort(const Array<int32_t>& a, const int32_t& exp, const int32_t& d) noexcept {
|
||||
// Array<int32_t> output(a.length), count(d);
|
||||
// memset(&count[0], 0, d * sizeof(int32_t));
|
||||
|
||||
// foreach(a, [count, exp, d](const int32_t&, const int32_t& val) -> void {
|
||||
// count[(val / exp) % d]++;
|
||||
// });
|
||||
|
||||
// for (int32_t i = 1; i <= d; ++i)
|
||||
// count[i] += count[i - 1];
|
||||
|
||||
// for (int32_t i = a.length - 1; i >= 0; --i) {
|
||||
// output[count[(a[i] / exp) % d] - 1] = a[i];
|
||||
// count[(a[i] / exp) % d]--;
|
||||
// }
|
||||
|
||||
// memcpy(&a[0], &output[0], a.length * sizeof(int32_t));
|
||||
//}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void counting_sort(const Array<T>& a) noexcept {
|
||||
Array<T> output(a);
|
||||
|
||||
map(a, [output](const size_t& i, const T&) -> const T& {
|
||||
return output[i];
|
||||
});
|
||||
constexpr void insertion_sort(const Array<T>& a) noexcept {
|
||||
size_t j = 0;
|
||||
for(size_t i = 1; i < a.length; ++i) {
|
||||
T key = a[i];
|
||||
for(j = i; j > 0 && a[j - 1] > key; --j)
|
||||
a[j] = a[j - 1];
|
||||
a[j] = key;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Array<size_t> counting_sort_arg(const Array<T>& a) noexcept {
|
||||
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];
|
||||
});
|
||||
|
||||
for (T i = 1; i <= M; ++i)
|
||||
countArray[i] += countArray[i - 1];
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Array<size_t> counting_sort_arg(const Array<T>& a) noexcept {
|
||||
Array<size_t> indices = range(a.length);
|
||||
|
||||
return indices;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void radix_sort_256(T* a, const size_t& n) noexcept {
|
||||
//template<typename T>
|
||||
//void radix_sort(const Array<int32_t>& a) noexcept {
|
||||
if (n <= 1)
|
||||
//if (a.length <= 1)
|
||||
return;
|
||||
|
||||
T* output = new T[n]; // output array
|
||||
size_t* const count = new size_t[256];
|
||||
T* originalArr = a; // So we know which was input
|
||||
|
||||
for (size_t shift = 0, s = 0; shift < 4; shift++, s += 8) {
|
||||
// Zero the counts
|
||||
for (size_t i = 0; i < 256; i++)
|
||||
count[i] = 0;
|
||||
|
||||
// Store count of occurrences in count[]
|
||||
for (size_t i = 0; i < n; i++)
|
||||
count[(a[i] >> s) & 0xff]++;
|
||||
|
||||
// Change count[i] so that count[i] now contains
|
||||
// actual position of this digit in output[]
|
||||
for (size_t i = 1; i < 256; i++)
|
||||
count[i] += count[i - 1];
|
||||
|
||||
// Build the output array
|
||||
for (int32_t i = n - 1; i >= 0; i--) {
|
||||
// precalculate the offset as it's a few instructions
|
||||
const size_t idx = (a[i] >> s) & 0xff;
|
||||
|
||||
// Subtract from the count and store the value
|
||||
output[--count[idx]] = a[i];
|
||||
}
|
||||
|
||||
// Copy the output array to input[], so that input[]
|
||||
// is sorted according to current digit
|
||||
|
||||
// We can just swap the pointers
|
||||
swap(a, output);
|
||||
inline void countsort(int a[], int n, int pos){
|
||||
int* output = new int[n + 1];
|
||||
int max = (a[0] / pos) % 10;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (((a[i] / pos) % 10) > max)
|
||||
max = a[i];
|
||||
}
|
||||
|
||||
// If we switched posize_ters an odd number of times,
|
||||
// make sure we copy before returning
|
||||
if (originalArr == output) {
|
||||
swap(a, output);
|
||||
for (size_t i = 0; i < n; i++)
|
||||
a[i] = output[i];
|
||||
int* count = new int[max + 1];
|
||||
for (int i = 0; i < max; ++i)
|
||||
count[i] = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count[(a[i] / pos) % 10]++;
|
||||
for (int i = 1; i < 10; i++)
|
||||
count[i] += count[i - 1];
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
output[count[(a[i] / pos) % 10] - 1] = a[i];
|
||||
count[(a[i] / pos) % 10]--;
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
a[i] = output[i];
|
||||
|
||||
delete[] output, delete[] count;
|
||||
delete[] output;
|
||||
delete[] count;
|
||||
}
|
||||
|
||||
// template<typename T>
|
||||
// constexpr void radix_sort_256(T* a, const size_t& n) noexcept {
|
||||
// //template<typename T>
|
||||
// //void radix_sort(const Array<int32_t>& a) noexcept {
|
||||
// if (n <= 1)
|
||||
// //if (a.length <= 1)
|
||||
// return;
|
||||
//
|
||||
// T* output = new T[n]; // output array
|
||||
// size_t* const count = new size_t[256];
|
||||
// T* originalArr = a; // So we know which was input
|
||||
//
|
||||
// for (size_t shift = 0, s = 0; shift < 4; shift++, s += 8) {
|
||||
// // Zero the counts
|
||||
// for (size_t i = 0; i < 256; i++)
|
||||
// count[i] = 0;
|
||||
//
|
||||
// // Store count of occurrences in count[]
|
||||
// for (size_t i = 0; i < n; i++)
|
||||
// count[(a[i] >> s) & 0xff]++;
|
||||
//
|
||||
// // Change count[i] so that count[i] now contains
|
||||
// // actual position of this digit in output[]
|
||||
// for (size_t i = 1; i < 256; i++)
|
||||
// count[i] += count[i - 1];
|
||||
//
|
||||
// // Build the output array
|
||||
// for (int32_t i = n - 1; i >= 0; i--) {
|
||||
// // precalculate the offset as it's a few instructions
|
||||
// const size_t idx = (a[i] >> s) & 0xff;
|
||||
//
|
||||
// // Subtract from the count and store the value
|
||||
// output[--count[idx]] = a[i];
|
||||
// }
|
||||
//
|
||||
// // Copy the output array to input[], so that input[]
|
||||
// // is sorted according to current digit
|
||||
//
|
||||
// // We can just swap the pointers
|
||||
// swap(a, output);
|
||||
// }
|
||||
//
|
||||
// // If we switched posize_ters an odd number of times,
|
||||
// // make sure we copy before returning
|
||||
// if (originalArr == output) {
|
||||
// swap(a, output);
|
||||
// for (size_t i = 0; i < n; i++)
|
||||
// a[i] = output[i];
|
||||
// }
|
||||
//
|
||||
// delete[] output, delete[] count;
|
||||
// }
|
||||
|
||||
constexpr void radix_sort_256(int32_t a[], int n){
|
||||
int max = a[0];
|
||||
for (int i = 1; i < n; i++)
|
||||
if (a[i] > max)
|
||||
max = a[i];
|
||||
for (int pos = 1; max / pos > 0; pos *= 10)
|
||||
countsort(a, n, pos);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline constexpr void radix_sort(const Array<T>& a) noexcept {
|
||||
constexpr void radix_sort(const Array<T>& a) noexcept {
|
||||
radix_sort_256(a.data.get(), a.length);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline Array<size_t> radix_sort_arg(const Array<T>& a) noexcept {
|
||||
Array<T> indices = range(a.length);
|
||||
|
||||
return indices;
|
||||
}
|
||||
// template<typename T>
|
||||
// constexpr Array<size_t> radix_sort_arg(const Array<T>& a) noexcept {
|
||||
// Array<T> indices = range(a.length);
|
||||
//
|
||||
// return indices;
|
||||
// }
|
||||
};
|
||||
|
Reference in New Issue
Block a user