diff --git a/data.hpp b/data.hpp index 8f59234..cdf0e33 100644 --- a/data.hpp +++ b/data.hpp @@ -106,8 +106,8 @@ namespace asp { }; template - constexpr T& max(const Array& a) noexcept { - T& max_el = a[0]; + 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]; @@ -138,26 +138,19 @@ namespace asp { inline Array range(const size_t& n) noexcept { Array 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 - constexpr void swap(T* const a, T* const b) noexcept { - const T temp = *a; - *a = *b; - *b = temp; - } - - template - constexpr void bubble_sort(const Array& a) noexcept { + 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]) { - swap(&a[i], &a[j]); + std::swap(a[i], a[j]); swapped = true; } if (!swapped) @@ -166,15 +159,15 @@ namespace asp { } template - Array bubble_sort_arg(const Array& a) noexcept { + 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]) { - 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 - constexpr size_t qs_partition(const Array& a, const size_t& l, const size_t& h) noexcept { + 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]) - 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 - constexpr void quicksort(const Array& a, const size_t& l, const size_t& h) noexcept { + constexpr void quicksort(Array& a, const size_t& l, const size_t& h) noexcept { if (l >= h) return; @@ -205,12 +198,12 @@ namespace asp { } template - constexpr void quicksort(const Array& a) noexcept { + constexpr void quicksort(Array& a) noexcept { quicksort(a, 0, a.length - 1); } template - constexpr void quicksort_iter(const Array& a, const size_t& l, const size_t& h) noexcept { + 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; @@ -253,25 +246,25 @@ namespace asp { } template - constexpr void quicksort_iter(const Array& a) noexcept { + constexpr void quicksort_iter(Array& a) noexcept { quicksort_iter(a, 0, a.length - 1); } template - constexpr size_t qs_arg_partition(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { + 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]){ - 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 - constexpr void quicksort_arg(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { + constexpr void quicksort_arg(Array& a, Array& indices, const size_t& l, const size_t& h) noexcept { if (l >= h) return; @@ -282,19 +275,19 @@ namespace asp { } template - Array quicksort_arg(const Array& other, const size_t& l, const size_t& h) noexcept { + 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 - Array quicksort_arg(const Array& a) noexcept { + inline Array quicksort_arg(Array& a) noexcept { return quicksort_arg(a, 0, a.length - 1); } template - constexpr void quicksort_arg_iter(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { + 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 @@ -334,7 +327,7 @@ namespace asp { } template - Array quicksort_arg_iter(const Array& a) noexcept { + inline Array quicksort_arg_iter(Array& a) noexcept { Array indices = range(a.length); quicksort_arg_iter(a, indices, 0, a.length - 1); return indices; @@ -363,7 +356,7 @@ namespace asp { }; template - constexpr void merge(const Array& a, const size_t& l, const size_t& m, const size_t& r) noexcept { + 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)); @@ -384,7 +377,7 @@ namespace asp { } template - constexpr void mergesort(const Array& a, const size_t& l, const size_t& r) noexcept { + constexpr void mergesort(Array& a, const size_t& l, const size_t& r) noexcept { if (l >= r) return; @@ -395,15 +388,15 @@ namespace asp { } template - constexpr void mergesort(const Array& a) noexcept { + constexpr void mergesort(Array& a) noexcept { mergesort(a, 0, a.length - 1); } template - Array mergesort_arg(const Array& a, const size_t& l, const size_t& r) noexcept { + inline Array mergesort_arg(Array& a) noexcept { Array> temp_vals(a.length); - map(temp_vals, [&a](const size_t& i, const ArgVal&) -> const ArgVal { + map(temp_vals, [&a](const size_t& i, const ArgVal&) -> ArgVal { return ArgVal(i, a[i]); }); @@ -421,7 +414,7 @@ namespace asp { } template - constexpr void insertion_sort(const Array& a) noexcept { + constexpr void insertion_sort(Array& 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 - Array insertion_argsort(const Array& a) noexcept { - Array 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 - constexpr void counting_sort(const Array& a) noexcept { - const size_t N = a.length; - const T M = max(a); - - const T exp = 10; - const T d = 128; - - Array countArray(M); - memset(&countArray[0], 0, M * sizeof(T)); - - foreach(a, [&countArray](const size_t, const T val) -> void { - ++countArray[val]; + 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]); }); - for (T i = 1; i <= M; ++i) - countArray[i] += countArray[i - 1]; + insertion_sort(temp_vals); - Array 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 indices(a.length); + return map(indices, [&temp_vals](const size_t& i, const size_t&) -> size_t { + return temp_vals[i].indice; + }); } template