105 lines
4.3 KiB
C++
105 lines
4.3 KiB
C++
#include <cassert>
|
|
#include <random>
|
|
#include "toolbox.hpp"
|
|
#include "data.hpp"
|
|
|
|
template<typename T>
|
|
static constexpr bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indices) noexcept {
|
|
for (size_t i = 1; i < a.length; ++i)
|
|
if (indices[i - 1] == indices[i] || a[indices[i - 1]] > a[indices[i]])
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
template<typename T>
|
|
static constexpr bool is_sorted(const asp::Array<T>& a) noexcept {
|
|
for (size_t i = 1; i < a.length; ++i)
|
|
if (a[i - 1] > a[i])
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
template<typename T, size_t N>
|
|
static constexpr void test_sort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* const title) noexcept {
|
|
#ifdef __DEBUG
|
|
printf("xxxxxxxxxxxxxxx IGNORE COPY ");
|
|
#endif
|
|
const asp::Array<T> a(original);
|
|
asp::measure_time_void(gaps, title, fnc, a);
|
|
#ifdef __DEBUG
|
|
const bool result = asp::measure_time<bool>(gaps, "=> Unit test", is_sorted<T>, a);
|
|
// asp::print(original);
|
|
// asp::print(a);
|
|
asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" });
|
|
#else
|
|
assert(is_sorted(a));
|
|
#endif
|
|
}
|
|
|
|
template<typename T, size_t N>
|
|
static void test_argsort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(const asp::Array<T>&), const char* const title) noexcept {
|
|
#ifdef __DEBUG
|
|
printf("xxxxxxxxxxxxxxx IGNORE COPY ");
|
|
#endif
|
|
const asp::Array<T> a(original);
|
|
const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(gaps, title, fnc, a);
|
|
#ifdef __DEBUG
|
|
const bool result = asp::measure_time<bool>(gaps, "=> Unit test", is_arg_sorted<T>, original, indices);
|
|
asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" });
|
|
#else
|
|
assert(is_arg_sorted(original, indices));
|
|
#endif
|
|
}
|
|
|
|
template<typename T>
|
|
static asp::Array<T> create_random_array(const size_t& n) noexcept {
|
|
asp::Array<T> original(n);
|
|
std::random_device rd;
|
|
std::default_random_engine gen(rd());
|
|
std::uniform_int_distribution<T> distribution(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
|
return std::move(asp::map(original, [&distribution, &gen](const size_t&, const T&) -> const T { return distribution(gen); }));
|
|
}
|
|
|
|
int32_t main(int32_t argc, char** argv) {
|
|
asp::toolbox_unit_test();
|
|
|
|
using array_type = uint16_t;
|
|
size_t N = (static_cast<size_t>(1)<<15) - 1;
|
|
// size_t N = std::numeric_limits<array_type>::max();
|
|
if (argc > 2) {
|
|
fprintf(stderr, "Too many arguments\nUsage: ./data <ARRAY_SIZE>\n");
|
|
return EXIT_FAILURE;
|
|
} else if (argc == 2)
|
|
N = std::strtoul(argv[1], argv + strlen(argv[1]) + 1, 10);
|
|
|
|
char title[64];
|
|
sprintf(title, "Sorting %s elements of %s", asp::thousand_sep(N).c_str(), asp::format_byte_size(2 * N * sizeof(array_type)).c_str());
|
|
const std::array<int32_t, 3> gaps = { 48, -17, 25 };
|
|
asp::header(gaps, { title, "Time spent (ns)", "Formatted time spent" });
|
|
|
|
const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>(gaps, "Creating random array", create_random_array<array_type>, N);
|
|
// const asp::Array<array_type> original = { 2, 5, 3, 0, 2, 3, 0, 3 };
|
|
// asp::print(original);
|
|
|
|
test_sort(gaps, original, asp::bubble_sort<array_type>, "Bubble sort");
|
|
test_argsort(gaps, original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)");
|
|
test_sort(gaps, original, asp::quicksort<array_type>, "Quicksort");
|
|
test_argsort(gaps, original, asp::quicksort_arg<array_type>, "Quicksort (arg)");
|
|
test_sort(gaps, original, asp::quicksort_iter<array_type>, "Quicksort (iterative)");
|
|
test_argsort(gaps, original, asp::quicksort_arg_iter<array_type>, "Quicksort (iterative) (arg)");
|
|
test_sort(gaps, original, asp::mergesort<array_type>, "Mergesort");
|
|
test_argsort(gaps, original, asp::mergesort_arg<array_type>, "Mergesort (arg)");
|
|
test_sort(gaps, original, asp::insertion_sort<array_type>, "Insertion sort");
|
|
// test_argsort(gaps, original, asp::insertion_argsort<array_type>, "Insertion argsort");
|
|
|
|
// W.I.P
|
|
// test_sort(gaps, original, asp::counting_sort<array_type>, "Counting sort");
|
|
// test_argsort(gaps, original, asp::counting_sort_arg<array_type>, "Counting sort (arg)");
|
|
// test_sort(gaps, original, asp::radix_sort<array_type>, "Radix sort");
|
|
// test_argsort(gaps, original, asp::radix_sort_arg<array_type>, "Radix sort (arg)");
|
|
|
|
asp::footer(gaps);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|