2023-12-16 08:05:21 +01:00

101 lines
3.9 KiB
C++

#include <assert.h>
#include <random>
#include "toolbox.hpp"
#include "data.hpp"
template<typename T>
static 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 (a[indices[i - 1]] > a[indices[i]])
return false;
return true;
}
template<typename T>
static 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>
static void test_sort(const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* title) noexcept {
#ifdef __DEBUG
printf("xxxxxxxxxxxxxxx INGORE COPY ");
#endif
const asp::Array<T> a(original);
asp::measure_time_void(title, fnc, a);
#ifdef __DEBUG
// assert(asp::measure_time<bool>("=> Unit test", is_sorted, a));
const bool result = asp::measure_time<bool>("=> Unit test", is_sorted<T>, a);
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", result ? "Success" : "Failure", "-", "-");
#else
assert(is_sorted(a));
#endif
}
template<typename T>
static void test_argsort(const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(const asp::Array<T>&), const char* title) noexcept {
#ifdef __DEBUG
printf("xxxxxxxxxxxxxxx INGORE COPY ");
#endif
const asp::Array<T> a(original);
const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(title, fnc, a);
#ifdef __DEBUG
// assert(asp::measure_time<bool>("=> Unit test", is_arg_sorted, a, indices));
const bool result = asp::measure_time<bool>("=> Unit test", is_arg_sorted<T>, original, indices);
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", 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> distrib(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
return std::move(asp::map(original, [& distrib, & gen](const size_t&, const T&) -> const T { return distrib(gen); }));
}
int main(int argc, char** argv) {
asp::toolbox_unit_test();
using array_type = uint16_t;
size_t N = static_cast<size_t>(1)<<16;
// 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);
asp::print("Estimating memory footprint at : " + asp::format_byte_size(2 * N * sizeof(array_type)));
char buff[64];
sprintf(buff, "Sorting algorithm for %s elements", asp::thousand_sep(N).c_str());
asp::print_separator(buff);
const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>("Creating random array", create_random_array<array_type>, N);
// std::cout << asp::min(original) << "<=>" << asp::max(original) << std::endl;;
// asp::print(original);
test_sort(original, asp::bubble_sort<array_type>, "Bubble sort");
test_argsort(original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)");
test_sort(original, asp::quicksort<array_type>, "Quicksort");
test_argsort(original, asp::quicksort_arg<array_type>, "Quicksort (arg)");
test_sort(original, asp::quicksort_iter<array_type>, "Quicksort (iterative)");
test_argsort(original, asp::quicksort_arg_iter<array_type>, "Quicksort (iterative) (arg)");
test_sort(original, asp::mergesort<array_type>, "Mergesort");
test_argsort(original, asp::mergesort_arg<array_type>, "Mergesort (arg)");
// W.I.P
// test_sort(original, asp::counting_sort<array_type>, "Counting sort");
// test_argsort(original, asp::counting_sort_arg<array_type>, "Counting sort (arg)");
// test_sort(original, asp::radix_sort<array_type>, "Radix sort");
// test_argsort(original, asp::radix_sort_arg<array_type>, "Radix sort (arg)");
return EXIT_SUCCESS;
}