Modernized code and fancier code printing
This commit is contained in:
74
data.cpp
74
data.cpp
@ -1,51 +1,51 @@
|
||||
#include <assert.h>
|
||||
#include <cassert>
|
||||
#include <random>
|
||||
#include "toolbox.hpp"
|
||||
#include "data.hpp"
|
||||
|
||||
template<typename T>
|
||||
constexpr static bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indices) noexcept {
|
||||
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 (a[indices[i - 1]] > a[indices[i]])
|
||||
if (indices[i - 1] == indices[i] || a[indices[i - 1]] > a[indices[i]])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr static bool is_sorted(const asp::Array<T>& a) noexcept {
|
||||
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>
|
||||
constexpr static void test_sort(const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* const title) noexcept {
|
||||
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(title, fnc, a);
|
||||
asp::measure_time_void(gaps, 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", "-", "-");
|
||||
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>
|
||||
constexpr static void test_argsort(const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(const asp::Array<T>&), const char* const title) noexcept {
|
||||
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>>(title, fnc, a);
|
||||
const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(gaps, 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", "-", "-");
|
||||
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
|
||||
@ -64,7 +64,7 @@ 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)<<16;
|
||||
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");
|
||||
@ -72,29 +72,33 @@ int32_t main(int32_t argc, char** argv) {
|
||||
} 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 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" });
|
||||
|
||||
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;;
|
||||
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(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)");
|
||||
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(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)");
|
||||
// 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user