#pragma once #include #include #include #include #ifdef __DEBUG #include #endif namespace asp { template 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& 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& other) noexcept : Array(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 constexpr T max(const Array& 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 constexpr T& min(const Array& 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 constexpr Array& map(Array& a, const F& fnc) noexcept { for (size_t i = 0; i < a.length; ++i) a[i] = fnc(i, a[i]); return a; } template constexpr void foreach(const Array& a, const F& fnc) noexcept { for (size_t i = 0; i < a.length; ++i) fnc(i, a[i]); } inline Array range(const size_t& n) noexcept { Array a(n); return map(a, [](const size_t i, const size_t) -> size_t { return i; }); } template constexpr void bubble_sort(Array& 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 inline Array bubble_sort_arg(Array& a) noexcept { Array 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 constexpr size_t qs_partition(Array& 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 constexpr void quicksort(Array& 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 constexpr void quicksort(Array& a) noexcept { quicksort(a, 0, a.length - 1); } template constexpr void quicksort_iter(Array& 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 constexpr void quicksort_iter(Array& a) noexcept { quicksort_iter(a, 0, a.length - 1); } template constexpr size_t qs_arg_partition(Array& a, Array& 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 constexpr void quicksort_arg(Array& a, Array& 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 inline Array quicksort_arg(Array& other, const size_t& l, const size_t& h) noexcept { Array indices = range(other.length); quicksort_arg(other, indices, l, h); return indices; } template inline Array quicksort_arg(Array& a) noexcept { return quicksort_arg(a, 0, a.length - 1); } template inline void quicksort_arg_iter(Array& a, Array& 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 inline Array quicksort_arg_iter(Array& a) noexcept { Array indices = range(a.length); quicksort_arg_iter(a, indices, 0, a.length - 1); return indices; } template 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& other) const noexcept { return val > other.val; } constexpr bool operator<(const ArgVal& other) const noexcept { return val < other.val; } constexpr bool operator>=(const ArgVal& other) const noexcept { return val >= other.val; } constexpr bool operator<=(const ArgVal& other) const noexcept { return val <= other.val; } }; template constexpr void merge(Array& a, const size_t& l, const size_t& m, const size_t& r) noexcept { Array left_arr(m - l + 1); memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T)); Array 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 constexpr void mergesort(Array& 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 constexpr void mergesort(Array& a) noexcept { mergesort(a, 0, a.length - 1); } template inline Array mergesort_arg(Array& a) noexcept { Array> temp_vals(a.length); map(temp_vals, [&a](const size_t& i, const ArgVal&) -> ArgVal { return ArgVal(i, a[i]); }); mergesort(temp_vals, 0, a.length - 1); Array indices(a.length); return map(indices, [&temp_vals](const size_t& i, const size_t&) -> size_t { return temp_vals[i].indice; }); } template constexpr void insertion_sort(Array& 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 inline Array insertion_argsort(Array& a) noexcept { Array> temp_vals(a.length); map(temp_vals, [&a](const size_t& i, const ArgVal&) -> ArgVal { return ArgVal(i, a[i]); }); insertion_sort(temp_vals); Array indices(a.length); return map(indices, [&temp_vals](const size_t& i, const size_t&) -> size_t { return temp_vals[i].indice; }); } template inline void countsort(Array& a, int64_t k, int64_t pos) noexcept { Array 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 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 inline void counting_sort(Array& a) noexcept { countsort(a, max(a) + 1, 1); } template constexpr void radix_sort(Array& 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); } };