Files
sorting_algorithms/data.hpp

484 lines
12 KiB
C++

#pragma once
#include <string_view>
#include <cstring>
#include <cstdio>
#include <cstdint>
#ifdef __DEBUG
#include <stdexcept>
#endif
namespace asp {
template<typename T>
struct Array {
T* data = nullptr;
size_t length = 0;
size_t* refcount = nullptr;
constexpr Array(void) noexcept = delete;
constexpr Array(const size_t& size) noexcept : data(new T[size]), length(size), refcount(new size_t(1)) {
#ifdef __DEBUG
printf("Creating array of size %lu\n", size);
#endif
}
constexpr Array(const std::initializer_list<size_t>& init_list) noexcept : data(new T[init_list.size()]), length(init_list.size()), refcount(new size_t(1)) {
#ifdef __DEBUG
printf("Copying initializer_list of size %lu\n", init_list.size());
#endif
memcpy(data, init_list.begin(), length * sizeof(T));
}
constexpr Array(const Array<T>& other) noexcept : Array<T>(other.length) {
#ifdef __DEBUG
printf("Copying array of size %lu\n", other.length);
#endif
memcpy(data, other.data, length * sizeof(T));
}
constexpr Array(Array&& other) noexcept {
#ifdef __DEBUG
printf("Moving array of size %lu\n", other.length);
#endif
if (refcount != nullptr){
delete[] data;
delete refcount;
}
data = other.data;
length = other.length;
refcount = other.refcount;
other.data = nullptr;
other.length = 0;
other.refcount = nullptr;
}
inline ~Array() noexcept {
if (refcount == nullptr)
return;
#ifdef __DEBUG
printf("Destructing array of size %lu and refcount %lu\n", length, *refcount);
#endif
if (--(*refcount) == 0){
#ifdef __DEBUG
printf("Freeing array of size %lu\n", length);
#endif
delete[] data;
data = nullptr;
delete refcount;
refcount = nullptr;
}
}
constexpr Array& operator=(const Array& other) noexcept {
if (this != &other) {
if (--(*refcount) == 0) {
delete[] data;
delete refcount;
}
data = other.data;
length = other.length;
refcount = other.refcount;
++(*refcount);
}
return *this;
}
constexpr T& operator[](const size_t& index) {
#ifdef __DEBUG
if (index > length) {
fprintf(stderr, "Index %ld out of range in Array of length %ld !\n", index, length);
throw std::out_of_range("Index out of range !");
}
#endif
return data[index];
}
constexpr const 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);
throw std::out_of_range("Index out of range !");
}
#endif
return data[index];
}
};
template<typename T>
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];
return max_el;
}
template<typename T>
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)
max_el = a[i];
return max_el;
}
template<typename T, typename F>
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>
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 map(a, [](const size_t i, const size_t) -> size_t {
return i;
});
}
template<typename T>
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]) {
std::swap(a[i], a[j]);
swapped = true;
}
if (!swapped)
break;
}
}
template<typename T>
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]) {
std::swap(indices[i], indices[j]);
std::swap(a[i], a[j]);
swapped = true;
}
if (!swapped)
break;
}
return indices;
}
template<typename T>
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])
std::swap(a[++i], a[j]);
std::swap(a[++i], a[h]);
return i;
}
template<typename T>
constexpr void quicksort(Array<T>& a, const size_t& l, const size_t& h) noexcept {
if (l >= h)
return;
const size_t p = qs_partition(a, l, h);
if (p - 1 <= h)
quicksort(a, l, p - 1);
quicksort(a, p + 1, h);
}
template<typename T>
constexpr void quicksort(Array<T>& a) noexcept {
quicksort(a, 0, a.length - 1);
}
template<typename T>
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;
// 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;
// Keep popping from stack while is not empty
while (top <= total) {
// Pop h and l
high = stack[top--];
low = stack[top--];
if(low >= high)
break;
// Set pivot element at its correct position
// in sorted array
const size_t p = qs_partition(a, low, high);
// If there are elements on left side of pivot,
// then push left side to stack
if (p - 1 > low && p - 1 < total) {
stack[++top] = low;
stack[++top] = p - 1;
}
// If there are elements on right side of pivot,
// then push right side to stack
if (p + 1 < high) {
stack[++top] = p + 1;
stack[++top] = high;
}
}
delete[] stack;
}
template<typename T>
constexpr void quicksort_iter(Array<T>& a) noexcept {
quicksort_iter(a, 0, a.length - 1);
}
template<typename T>
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]){
std::swap(a[++i], a[j]);
std::swap(indices[i], indices[j]);
}
std::swap(indices[++i], indices[h]);
std::swap(a[i], a[h]);
return i;
}
template<typename T>
constexpr void quicksort_arg(Array<T>& a, Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
if (l >= h)
return;
const size_t p = qs_arg_partition(a, indices, l, h);
if (p - 1 <= h)
quicksort_arg(a, indices, l, p - 1);
quicksort_arg(a, indices, p + 1, h);
}
template<typename T>
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>
inline Array<size_t> quicksort_arg(Array<T>& a) noexcept {
return quicksort_arg(a, 0, a.length - 1);
}
template<typename T>
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
size_t* const stack = new size_t[total]{l,h};
// initialize top of stack
size_t top = 1, low = l, high = h;
// Keep popping from stack while is not empty
while (top <= total) {
// Pop h and l
high = stack[top--];
low = stack[top--];
if(low >= high)
break;
// Set pivot element at its correct position
// in sorted array
const size_t p = qs_arg_partition(a, indices, low, high);
// If there are elements on left side of pivot,
// then push left side to stack
if (p - 1 > low && p - 1 < total) {
stack[++top] = low;
stack[++top] = p - 1;
}
// If there are elements on right side of pivot,
// then push right side to stack
if (p + 1 < high) {
stack[++top] = p + 1;
stack[++top] = high;
}
}
delete[] stack;
}
template<typename T>
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;
}
template<typename T>
struct ArgVal {
size_t indice;
T val;
constexpr ArgVal(void) noexcept = default;
constexpr ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {}
constexpr bool operator>(const ArgVal<T>& other) const noexcept {
return val > other.val;
}
constexpr bool operator<(const ArgVal<T>& other) const noexcept {
return val < other.val;
}
constexpr bool operator>=(const ArgVal<T>& other) const noexcept {
return val >= other.val;
}
constexpr bool operator<=(const ArgVal<T>& other) const noexcept {
return val <= other.val;
}
};
template<typename T>
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));
Array<T> right_arr(r - m);
memcpy(&right_arr.data[0], &a[m + 1], right_arr.length * sizeof(T));
size_t i_a0 = 0, i_a1 = 0, i = l;
// Merge the temp arrays back size_to array[left..right]
for (; i_a0 < left_arr.length && i_a1 < right_arr.length; ++i)
a[i] = left_arr[i_a0] <= right_arr[i_a1] ? left_arr[i_a0++] : right_arr[i_a1++];
//
// Copy the remaining elements of left[], if there are any
const size_t leftover = left_arr.length - i_a0;
memcpy(&a[i], &left_arr[i_a0], leftover * sizeof(T));
// Copy the remaining elements of right[], if there are any
memcpy(&a[i + leftover], &right_arr[i_a1], (right_arr.length - i_a1) * sizeof(T));
}
template<typename T>
constexpr void mergesort(Array<T>& a, const size_t& l, const size_t& r) noexcept {
if (l >= r)
return;
const size_t m = l + (r - l) / 2;
mergesort(a, l, m);
mergesort(a, m + 1, r);
merge(a, l, m, r);
}
template<typename T>
constexpr void mergesort(Array<T>& a) noexcept {
mergesort(a, 0, a.length - 1);
}
template<typename T>
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>&) -> ArgVal<T> {
return ArgVal<T>(i, a[i]);
});
mergesort(temp_vals, 0, a.length - 1);
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>
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];
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> 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]);
});
insertion_sort(temp_vals);
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>
inline void countsort(Array<T>& a, int64_t k, int64_t pos) noexcept {
Array<T> output(a.length + 1);
T max = T((a[0] / pos) % k);
for (size_t i = 1; i < a.length; ++i) {
const T val = a[i];
if (((val / pos) % k) > max)
max = val;
}
Array<T> count(max + 1);
memset(count.data, 0, count.length * sizeof(T));
for (size_t i = 0; i < a.length; ++i)
++count[size_t((a[i] / pos) % k)];
// Prefix sum
for (size_t i = 1; i < count.length; i++)
count[i] += count[i - 1];
for (size_t i = a.length - 1; i > 0; --i) {
const T val = a[i];
output[count[size_t((val / pos) % k)] - 1] = val;
--count[size_t((val / pos) % k)];
}
const T val = a[0];
output[count[size_t((val / pos) % k)] - 1] = val;
--count[size_t((val / pos) % k)];
memmove(a.data, output.data, a.length * sizeof(T));
}
template<typename T>
inline void counting_sort(Array<T>& a) noexcept {
countsort<T>(a, max(a) + 1, 1);
}
template<typename T>
constexpr void radix_sort(Array<T>& a) noexcept {
constexpr int64_t k = 10;
T max = a[0];
for (size_t i = 1; i < a.length; i++)
if (a[i] > max)
max = a[i];
for (int64_t pos = 1; max / pos > 0; pos *= k)
countsort(a, k, pos);
}
};